jsonschema-key-compression
Advanced tools
Comparing version 0.0.1 to 1.0.0
@@ -31,4 +31,4 @@ "use strict"; | ||
* e.g: | ||
* - input: 'name.firstName' | ||
* - ouput: '|a.|b' | ||
* - input: 'names[1].firstName' | ||
* - ouput: '|a[1].|b' | ||
*/ | ||
@@ -53,2 +53,8 @@ function compressedPath(table, path) { | ||
throwErrorIfCompressionFlagUsed(table, key); | ||
/** | ||
* keys could be array-accessors like myArray[4] | ||
* we have to split and readd the squared brackets value | ||
*/ | ||
var splitSquaredBrackets = key.split('['); | ||
key = splitSquaredBrackets.shift(); | ||
var compressedKey = table.compressedToUncompressed.get(key); | ||
@@ -59,6 +65,70 @@ if (!compressedKey) { | ||
else { | ||
return table.compressionFlag + compressedKey; | ||
var readdSquared = splitSquaredBrackets.length ? '[' + splitSquaredBrackets.join('[') : ''; | ||
return table.compressionFlag + compressedKey + readdSquared; | ||
} | ||
} | ||
exports.compressedAndFlaggedKey = compressedAndFlaggedKey; | ||
/** | ||
* compress a mango-query | ||
* so that it can be used to find documents | ||
* in a database where all documents are compressed | ||
*/ | ||
function compressQuery(table, query) { | ||
var ret = { | ||
selector: compressQuerySelector(table, query.selector) | ||
}; | ||
if (query.skip) | ||
ret.skip = query.skip; | ||
if (query.limit) | ||
ret.limit = query.limit; | ||
if (query.fields) { | ||
ret.fields = query.fields | ||
.map(function (field) { return compressedPath(table, field); }); | ||
} | ||
if (query.sort) { | ||
ret.sort = query.sort.map(function (item) { | ||
if (typeof item === 'string') { | ||
var hasMinus = item.startsWith('-'); | ||
if (hasMinus) { | ||
item = item.substr(1); | ||
} | ||
var compressedField = compressedPath(table, item); | ||
if (hasMinus) { | ||
compressedField = '-' + compressedField; | ||
} | ||
return compressedField; | ||
} | ||
else { | ||
return compressQuerySelector(table, item); | ||
} | ||
}); | ||
} | ||
return ret; | ||
} | ||
exports.compressQuery = compressQuery; | ||
function compressQuerySelector(table, selector) { | ||
if (Array.isArray(selector)) { | ||
return selector.map(function (item) { return compressQuerySelector(table, item); }); | ||
} | ||
else if (typeof selector === 'object' && selector !== null) { | ||
var ret_2 = {}; | ||
Object.keys(selector).forEach(function (key) { | ||
var useKey; | ||
if (key.startsWith('$')) { | ||
// operator | ||
useKey = key; | ||
} | ||
else { | ||
// property path | ||
useKey = compressedPath(table, key); | ||
} | ||
ret_2[useKey] = compressQuerySelector(table, selector[key]); | ||
}); | ||
return ret_2; | ||
} | ||
else { | ||
return selector; | ||
} | ||
} | ||
exports.compressQuerySelector = compressQuerySelector; | ||
//# sourceMappingURL=compress.js.map |
@@ -24,4 +24,4 @@ "use strict"; | ||
var ret = new Set(); | ||
function addSchema(schema) { | ||
var keys = getPropertiesOfSchema(schema); | ||
function addSchema(innerSchema) { | ||
var keys = getPropertiesOfSchema(innerSchema); | ||
Array.from(keys).forEach(function (k) { return ret.add(k); }); | ||
@@ -28,0 +28,0 @@ } |
@@ -39,8 +39,15 @@ "use strict"; | ||
function decompressedKey(table, key) { | ||
var decompressedKey = table.uncompressedToCompressed.get(key); | ||
if (!decompressedKey) { | ||
/** | ||
* keys could be array-accessors like myArray[4] | ||
* we have to split and readd the squared brackets value | ||
*/ | ||
var splitSquaredBrackets = key.split('['); | ||
key = splitSquaredBrackets.shift(); | ||
var decompressed = table.uncompressedToCompressed.get(key); | ||
if (!decompressed) { | ||
return key; | ||
} | ||
else { | ||
return decompressedKey; | ||
var readdSquared = splitSquaredBrackets.length ? '[' + splitSquaredBrackets.join('[') : ''; | ||
return decompressed + readdSquared; | ||
} | ||
@@ -47,0 +54,0 @@ } |
@@ -9,2 +9,4 @@ "use strict"; | ||
exports.compressedPath = compress_1.compressedPath; | ||
exports.compressQuerySelector = compress_1.compressQuerySelector; | ||
exports.compressQuery = compress_1.compressQuery; | ||
var decompress_1 = require("./decompress"); | ||
@@ -11,0 +13,0 @@ exports.decompressObject = decompress_1.decompressObject; |
@@ -12,4 +12,2 @@ "use strict"; | ||
* @link https://github.com/matthewmueller/number-to-letter/blob/master/index.js | ||
* @param {number} nr | 10000000 | ||
* @return {string} the string-representation of the number | '2oMX' | ||
*/ | ||
@@ -29,3 +27,3 @@ function numberToLetter(nr) { | ||
exports.numberToLetter = numberToLetter; | ||
function alphabeticCompare(a, b) { | ||
exports.alphabeticCompare = function (a, b) { | ||
if (a < b) { | ||
@@ -38,4 +36,3 @@ return -1; | ||
return 0; | ||
} | ||
exports.alphabeticCompare = alphabeticCompare; | ||
}; | ||
//# sourceMappingURL=util.js.map |
@@ -101,6 +101,8 @@ "use strict"; | ||
.map(function (obj) { return index_1.compressObject(table, obj); }); | ||
var x = []; | ||
for (var i = 0; i < compressedObjects.length; i++) { | ||
// console.dir(compressedObjects[i]); | ||
console.dir(compressedObjects[i]); | ||
var decompressed = index_1.decompressObject(table, compressedObjects[i]); | ||
// console.dir(decompressed); | ||
x.push(decompressed); | ||
console.dir(decompressed); | ||
} | ||
@@ -107,0 +109,0 @@ var elapsed = async_test_util_1.performanceNow() - startTime; |
@@ -58,4 +58,116 @@ "use strict"; | ||
}); | ||
it('should be able to compress array-paths', function () { | ||
var table = test_util_1.getDefaultCompressionTable(); | ||
var compressed = index_1.compressedPath(table, 'objectArray[1].deepNested'); | ||
assert.ok(compressed.includes('[1]')); | ||
assert.ok(compressed.startsWith(table.compressionFlag)); | ||
}); | ||
}); | ||
describe('.compressQuerySelector()', function () { | ||
it('should compress the selector', function () { | ||
var selector = { | ||
'active': { | ||
$eq: true | ||
} | ||
}; | ||
var table = test_util_1.getDefaultCompressionTable(); | ||
var compressed = index_1.compressQuerySelector(table, selector); | ||
assert.deepEqual(compressed, { '|a': { $eq: true } }); | ||
}); | ||
it('deeper nested', function () { | ||
var selector = { | ||
$and: [ | ||
{ | ||
active: true | ||
}, | ||
{ | ||
'nestedObject.nestedAttribute': { | ||
$ne: 'foobar' | ||
} | ||
} | ||
] | ||
}; | ||
var table = test_util_1.getDefaultCompressionTable(); | ||
var compressed = index_1.compressQuerySelector(table, selector); | ||
assert.deepEqual(compressed, { | ||
$and: [ | ||
{ | ||
'|a': true | ||
}, | ||
{ | ||
'|g.|f': { | ||
$ne: 'foobar' | ||
} | ||
} | ||
] | ||
}); | ||
}); | ||
}); | ||
describe('.compressQuery()', function () { | ||
it('should compress all attributes', function () { | ||
var query = { | ||
selector: { | ||
$and: [ | ||
{ | ||
active: true | ||
}, | ||
{ | ||
'nestedObject.nestedAttribute': { | ||
$ne: 'foobar' | ||
} | ||
} | ||
] | ||
}, | ||
skip: 1, | ||
limit: 1, | ||
fields: [ | ||
'id', | ||
'name' | ||
], | ||
sort: [ | ||
'name', | ||
'-active' | ||
] | ||
}; | ||
var table = test_util_1.getDefaultCompressionTable(); | ||
var compressed = index_1.compressQuery(table, query); | ||
assert.deepEqual(compressed, { | ||
selector: { | ||
$and: [ | ||
{ | ||
'|a': true | ||
}, | ||
{ | ||
'|g.|f': { | ||
$ne: 'foobar' | ||
} | ||
} | ||
] | ||
}, | ||
skip: 1, | ||
limit: 1, | ||
fields: [ | ||
'id', | ||
'|e' | ||
], | ||
sort: [ | ||
'|e', | ||
'-|a' | ||
] | ||
}); | ||
}); | ||
it('should work with sort-objects', function () { | ||
var query = { | ||
selector: {}, | ||
sort: [ | ||
{ 'name': 1 }, | ||
{ '-active': -1 } | ||
] | ||
}; | ||
var table = test_util_1.getDefaultCompressionTable(); | ||
var compressed = index_1.compressQuery(table, query); | ||
assert.deepEqual(compressed.sort, [{ '|e': 1 }, { '-active': -1 }]); | ||
}); | ||
}); | ||
}); | ||
//# sourceMappingURL=compress.test.js.map |
@@ -25,4 +25,25 @@ "use strict"; | ||
}); | ||
it('should compress all parts', function () { | ||
var table = test_util_1.getDefaultCompressionTable(); | ||
var path = 'nestedObject.nestedAttribute'; | ||
var compressed = index_1.compressedPath(table, path); | ||
var split = compressed.split('.'); | ||
split.forEach(function (p) { return assert.ok(p.startsWith(table.compressionFlag)); }); | ||
}); | ||
it('turnarround should output an equal path', function () { | ||
var table = test_util_1.getDefaultCompressionTable(); | ||
var path = 'nestedObject.nestedAttribute'; | ||
var compressed = index_1.compressedPath(table, path); | ||
var decompressed = index_1.decompressedPath(table, compressed); | ||
assert.equal(path, decompressed); | ||
}); | ||
it('should be able to compress array-paths', function () { | ||
var table = test_util_1.getDefaultCompressionTable(); | ||
var path = 'objectArray[1].deepNested'; | ||
var compressed = index_1.compressedPath(table, path); | ||
var decompressed = index_1.decompressedPath(table, compressed); | ||
assert.equal(path, decompressed); | ||
}); | ||
}); | ||
}); | ||
//# sourceMappingURL=decompress.test.js.map |
{ | ||
"name": "jsonschema-key-compression", | ||
"version": "0.0.1", | ||
"version": "1.0.0", | ||
"description": "Compress json-data based on it's json-schema", | ||
@@ -5,0 +5,0 @@ "author": "pubkey", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
83450
1283
0