Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'compression-plugin' of https://github.com/w33ble/store.js…
… into gh-204-compression
- Loading branch information
Showing
5 changed files
with
566 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
module.exports = [ | ||
require('./compression'), | ||
require('./defaults'), | ||
require('./dump'), | ||
require('./events'), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const LZString = require('./lib/lz-string') | ||
|
||
module.exports = compressionPlugin | ||
|
||
function compressionPlugin() { | ||
return { | ||
get: get, | ||
set: set, | ||
} | ||
|
||
function get(super_fn, key, raw) { | ||
var val = super_fn(key) | ||
if (!val || raw) return val | ||
var decompressed = LZString.decompress(val) | ||
// fallback to existing values that are not compressed | ||
return (decompressed == null) ? val : JSON.parse(decompressed) | ||
} | ||
|
||
function set(super_fn, key, val, raw) { | ||
if (raw) return super_fn(key, val) | ||
var compressed = LZString.compress(JSON.stringify(val)) | ||
super_fn(key, compressed) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require('./compression') | ||
var util = require('../tests/util') | ||
|
||
module.exports = { | ||
setup: setup, | ||
} | ||
|
||
function setup(store) { | ||
var raw = true | ||
|
||
test('string compression', function() { | ||
store.set('foo', 'baz') | ||
assert(store.get('foo', raw) == 'ᄂゆ䀀', 'string should be lz compressed') | ||
assert(store.get('foo') == 'baz', 'value should be baz') | ||
}) | ||
|
||
test('object compression', function () { | ||
var obj = { one: { two: 3 }} | ||
store.set('foo', obj) | ||
assert(store.get('foo', raw) == '㞂ݠ꘠岠ᜃ릎٠⾆耀', 'object should be lz compressed') | ||
assert(util.deepEqual(store.get('foo'), obj), 'should deep equal original object') | ||
store.remove('foo') | ||
}) | ||
|
||
test('decompress uncopmressed data', function () { | ||
// fake uncompressed data by storing raw value | ||
store.set('foo', 'baz', raw) | ||
assert(store.get('foo') == 'baz', 'value should be baz') | ||
store.remove('foo') | ||
}) | ||
|
||
test('decompress non-existing data', function () { | ||
assert(store.get('bar') == undefined, 'value should be undefined') | ||
store.remove('bar') | ||
}) | ||
|
||
} |
Oops, something went wrong.