Comparing version 0.0.2 to 0.0.3
/*! | ||
* MapQL v0.0.2 - A MongoDB inspired ES6 Map() query langauge. - Copyright (c) 2017 Louis T. (https://lou.ist/) | ||
* MapQL v0.0.3 - A MongoDB inspired ES6 Map() query langauge. - Copyright (c) 2017 Louis T. (https://lou.ist/) | ||
* Licensed under the MIT license - https://raw.githubusercontent.com/LouisT/MapQL/master/LICENSE | ||
* Updated on 18-06-2017 at 07:06:49 | ||
* Updated on 19-06-2017 at 23:06:09 | ||
*/ | ||
@@ -888,3 +888,3 @@ | ||
} | ||
compile (obj = {}, update = false) { | ||
compile (queries = {}, update = false) { | ||
let results = { | ||
@@ -894,10 +894,10 @@ operator: false, | ||
}; | ||
for (let key of Object.keys(obj)) { | ||
for (let key of Object.keys(queries)) { | ||
let isLO = this.isLogicalOperator(key); | ||
if (Helpers.is(obj[key], 'object')) { | ||
for (let mode of Object.keys(obj[key])) { | ||
results.list.push([key, mode, obj[key][mode]]); | ||
if (Helpers.is(queries[key], 'object')) { | ||
for (let mode of Object.keys(queries[key])) { | ||
results.list.push([key, mode, queries[key][mode]]); | ||
} | ||
} else if (isLO && Array.isArray(obj[key])) { | ||
for (let subobj of obj[key]) { | ||
} else if (isLO && Array.isArray(queries[key])) { | ||
for (let subobj of queries[key]) { | ||
results.list.push(this.compile(subobj)); | ||
@@ -911,3 +911,3 @@ } | ||
(isUQ || update) ? key : '$eq', | ||
obj[key] | ||
queries[key] | ||
]); | ||
@@ -1010,5 +1010,8 @@ } | ||
} | ||
update (query, modifiers, options = {}) { | ||
let opts = Object.assign({ multi: false }, options), | ||
cursor = this[opts.multi ? 'find' : 'findOne'](query); | ||
update (queries, modifiers, options = {}) { | ||
let opts = Object.assign({ | ||
multi: false, | ||
projections: {} | ||
}, options), | ||
cursor = this[Helpers.is(queries, 'string') ? 'findByKey' : 'find'](queries, opts.projections, !opts.multi); | ||
if (!cursor.empty()) { | ||
@@ -1026,2 +1029,28 @@ let update = this.compile(modifiers, true); | ||
} | ||
remove (queries, multi = false) { | ||
let removed = []; | ||
if (Helpers.is(queries, '!object')) { | ||
for (let key of (Array.isArray(queries) ? queries : [queries])) { | ||
if (this.has(key) && this.delete(key)) { | ||
removed.push(key); | ||
} | ||
} | ||
} else { | ||
let _queries = this.compile(queries); | ||
if (!!_queries.list.length) { | ||
for (let entry of this.entries()) { | ||
if (this._validate(entry, _queries)) { | ||
if (this.delete(entry[0])) { | ||
if (!multi) { | ||
return [entry[0]]; | ||
} else { | ||
removed.push(entry[0]); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return removed; | ||
} | ||
export (options = {}) { | ||
@@ -1028,0 +1057,0 @@ let opts = Object.assign({ |
/*! | ||
* MapQL v0.0.2 - A MongoDB inspired ES6 Map() query langauge. - Copyright (c) 2017 Louis T. (https://lou.ist/) | ||
* MapQL v0.0.3 - A MongoDB inspired ES6 Map() query langauge. - Copyright (c) 2017 Louis T. (https://lou.ist/) | ||
* Licensed under the MIT license - https://raw.githubusercontent.com/LouisT/MapQL/master/LICENSE | ||
* Updated on 18-06-2017 at 07:06:49 | ||
* Updated on 19-06-2017 at 23:06:09 | ||
*/ | ||
@@ -826,3 +826,3 @@ | ||
} | ||
compile (obj = {}, update = false) { | ||
compile (queries = {}, update = false) { | ||
let results = { | ||
@@ -832,10 +832,10 @@ operator: false, | ||
}; | ||
for (let key of Object.keys(obj)) { | ||
for (let key of Object.keys(queries)) { | ||
let isLO = this.isLogicalOperator(key); | ||
if (Helpers.is(obj[key], 'object')) { | ||
for (let mode of Object.keys(obj[key])) { | ||
results.list.push([key, mode, obj[key][mode]]); | ||
if (Helpers.is(queries[key], 'object')) { | ||
for (let mode of Object.keys(queries[key])) { | ||
results.list.push([key, mode, queries[key][mode]]); | ||
} | ||
} else if (isLO && Array.isArray(obj[key])) { | ||
for (let subobj of obj[key]) { | ||
} else if (isLO && Array.isArray(queries[key])) { | ||
for (let subobj of queries[key]) { | ||
results.list.push(this.compile(subobj)); | ||
@@ -849,3 +849,3 @@ } | ||
(isUQ || update) ? key : '$eq', | ||
obj[key] | ||
queries[key] | ||
]); | ||
@@ -948,5 +948,8 @@ } | ||
} | ||
update (query, modifiers, options = {}) { | ||
let opts = Object.assign({ multi: false }, options), | ||
cursor = this[opts.multi ? 'find' : 'findOne'](query); | ||
update (queries, modifiers, options = {}) { | ||
let opts = Object.assign({ | ||
multi: false, | ||
projections: {} | ||
}, options), | ||
cursor = this[Helpers.is(queries, 'string') ? 'findByKey' : 'find'](queries, opts.projections, !opts.multi); | ||
if (!cursor.empty()) { | ||
@@ -964,2 +967,28 @@ let update = this.compile(modifiers, true); | ||
} | ||
remove (queries, multi = false) { | ||
let removed = []; | ||
if (Helpers.is(queries, '!object')) { | ||
for (let key of (Array.isArray(queries) ? queries : [queries])) { | ||
if (this.has(key) && this.delete(key)) { | ||
removed.push(key); | ||
} | ||
} | ||
} else { | ||
let _queries = this.compile(queries); | ||
if (!!_queries.list.length) { | ||
for (let entry of this.entries()) { | ||
if (this._validate(entry, _queries)) { | ||
if (this.delete(entry[0])) { | ||
if (!multi) { | ||
return [entry[0]]; | ||
} else { | ||
removed.push(entry[0]); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return removed; | ||
} | ||
export (options = {}) { | ||
@@ -966,0 +995,0 @@ let opts = Object.assign({ |
{ | ||
"name": "mapql", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "A MongoDB inspired ES6 Map() query langauge.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -20,2 +20,4 @@ MapQL (WIP) | ||
=== | ||
Used with `{Instance}.find(<Query>)` and `{Instance}.remove(<Query>[, <Multi (Boolean)>])`. | ||
* Comparison | ||
@@ -34,2 +36,4 @@ * $eq, $gt, $gte, $lt, $lte, $ne, $in, $nin | ||
=== | ||
Used with `{Instance}.update(<Query>, <Update>)`. | ||
* Fields | ||
@@ -36,0 +40,0 @@ * $set, $inc, $mul, $unset |
@@ -34,3 +34,3 @@ /*! | ||
*/ | ||
compile (obj = {}, update = false) { | ||
compile (queries = {}, update = false) { | ||
let results = { | ||
@@ -40,11 +40,11 @@ operator: false, | ||
}; | ||
for (let key of Object.keys(obj)) { | ||
for (let key of Object.keys(queries)) { | ||
let isLO = this.isLogicalOperator(key); | ||
if (Helpers.is(obj[key], 'object')) { | ||
for (let mode of Object.keys(obj[key])) { | ||
results.list.push([key, mode, obj[key][mode]]); | ||
if (Helpers.is(queries[key], 'object')) { | ||
for (let mode of Object.keys(queries[key])) { | ||
results.list.push([key, mode, queries[key][mode]]); | ||
} | ||
// If the query is an array, treat it as a logical operator. | ||
} else if (isLO && Array.isArray(obj[key])) { | ||
for (let subobj of obj[key]) { | ||
} else if (isLO && Array.isArray(queries[key])) { | ||
for (let subobj of queries[key]) { | ||
// Recursively compile sub-queries for logical operators. | ||
@@ -60,3 +60,3 @@ results.list.push(this.compile(subobj)); | ||
(isUQ || update) ? key : '$eq', | ||
obj[key] | ||
queries[key] | ||
]); | ||
@@ -219,5 +219,8 @@ } | ||
*/ | ||
update (query, modifiers, options = {}) { | ||
let opts = Object.assign({ multi: false }, options), | ||
cursor = this[opts.multi ? 'find' : 'findOne'](query); | ||
update (queries, modifiers, options = {}) { | ||
let opts = Object.assign({ | ||
multi: false, | ||
projections: {} | ||
}, options), | ||
cursor = this[Helpers.is(queries, 'string') ? 'findByKey' : 'find'](queries, opts.projections, !opts.multi); | ||
if (!cursor.empty()) { | ||
@@ -237,2 +240,35 @@ let update = this.compile(modifiers, true); | ||
/* | ||
* Delete entries if they match the provided query operators. | ||
* If queries is an Array or String of key(s), treat as array | ||
* and remove each key. Returns an Array of deleted IDs. If | ||
* `multi` is true remove all matches. | ||
*/ | ||
remove (queries, multi = false) { | ||
let removed = []; | ||
if (Helpers.is(queries, '!object')) { | ||
for (let key of (Array.isArray(queries) ? queries : [queries])) { | ||
if (this.has(key) && this.delete(key)) { | ||
removed.push(key); | ||
} | ||
} | ||
} else { | ||
let _queries = this.compile(queries); | ||
if (!!_queries.list.length) { | ||
for (let entry of this.entries()) { | ||
if (this._validate(entry, _queries)) { | ||
if (this.delete(entry[0])) { | ||
if (!multi) { | ||
return [entry[0]]; | ||
} else { | ||
removed.push(entry[0]); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return removed; | ||
} | ||
/* | ||
* Export current Document's to JSON key/value. | ||
@@ -239,0 +275,0 @@ * |
@@ -51,4 +51,4 @@ /*! | ||
}); | ||
it('it should set test1 to { foo: \'bar\' }', () => { | ||
assert.equal(MapQL.set('test1', { foo: 'bar' }), MapQL); | ||
it('it should set test1 to { foo: \'bar\', foobar: true }', () => { | ||
assert.equal(MapQL.set('test1', { foo: 'bar', foobar: true }), MapQL); | ||
}); | ||
@@ -93,12 +93,30 @@ it('it should set test2 to { bar: 5, baz: 6 }', () => { | ||
}); | ||
it('it should find test0 with: { \'$gte\': 9 }', () => { | ||
assert.equal(MapQL.find({ '$gte': 9 })[0]._id, 'test0'); | ||
}); | ||
it('it should find test0 with: { \'$gte\': 10 }', () => { | ||
assert.equal(MapQL.find({ '$gte': 10 })[0]._id, 'test0'); | ||
}); | ||
it('it should find test0 with: { \'$lte\': 11 }', () => { | ||
assert.equal(MapQL.find({ '$gte': 9 })[0]._id, 'test0'); | ||
}); | ||
it('it should find test0 with: { \'$lte\': 10 }', () => { | ||
assert.equal(MapQL.find({ '$gte': 10 })[0]._id, 'test0'); | ||
}); | ||
it('it should find test1 with: { foo: { \'$eq\': \'bar\' } }', () => { | ||
assert.equal(MapQL.find({ foo: { '$eq': 'bar' } })[0]._id, 'test1'); | ||
}); | ||
it('it should find test1 with: { foo: \'bar\', foobar: { \'$exists\': true } }', () => { | ||
assert.equal(MapQL.find({ foo: 'bar', foobar: { '$exists': true } })[0]._id, 'test1'); | ||
}); | ||
it('it should find test2 with: { bar: { \'$lt\': 10 }, baz: { \'$gt\': 3 } }', () => { | ||
assert.equal(MapQL.find({ bar: { '$lt': 10 }, baz: { '$gt': 3 } })[0]._id, 'test2'); | ||
}); | ||
it('it should find test2 with: { bar: 5, foobar: { \'$exists\': false } }', () => { | ||
assert.equal(MapQL.find({ bar: 5, foobar: { '$exists': false } })[0]._id, 'test2'); | ||
}); | ||
it('it should find test3 with: { \'$regex\': /^Str/i }', () => { | ||
assert.equal(MapQL.find({ '$regex': /^Str/i })[0]._id, 'test3'); | ||
}); | ||
it('it should find test4 with: { bar: { \'$type\': \'number\' } }', () => { | ||
it('it should find test4 with: { qux: { \'$type\': \'number\' } }', () => { | ||
assert.equal(MapQL.find({ qux: { '$type': 'number' } })[0]._id, 'test4'); | ||
@@ -115,11 +133,17 @@ }); | ||
}); | ||
it('it should find test8 with: { \'array\': { \'$in\': [\'_A\'] } }', () => { | ||
it('it should find test8 with: { \'array\': { \'$in\': [\'A2\'] } }', () => { | ||
assert.equal(MapQL.find({ 'array': { '$in': ['A2'] } })[0]._id, 'test8'); | ||
}); | ||
it('it should find test9 with: { foo: 1, \'array\': { \'$nin\': [\'_B\'] } }', () => { | ||
it('it should find test9 with: { foo: 1, \'array\': { \'$nin\': [\'B3\'] } }', () => { | ||
assert.equal(MapQL.find({ foo: 1, 'array': { '$nin': ['B3'] } })[0]._id, 'test9'); | ||
}); | ||
it('it should find test9 with: { foo: 1, \'array.2\': { \'$ne\': \'B3\' } }', () => { | ||
assert.equal(MapQL.find({ foo: 1, 'array.2': { '$ne': 'B3' } })[0]._id, 'test9'); | ||
}); | ||
it('it should find test10 with: { foo: \'baz\', \'$where\': function () { return Array.isArray(this.array); } }', () => { | ||
assert.equal(MapQL.find({ foo: 'baz', '$where': function () { return Array.isArray(this.array); } })[0]._id, 'test10'); | ||
}); | ||
it('it should find test10 with: { foo: \'baz\', array: { \'$type\': \'array\' } }', () => { | ||
assert.equal(MapQL.find({ foo: 'baz', array: { '$type': 'array' } })[0]._id, 'test10'); | ||
}); | ||
it('it should find test11 with: { \'array\': { \'$size\': 4 } }', () => { | ||
@@ -158,2 +182,37 @@ assert.equal(MapQL.find({ 'array': { '$size': 4 } })[0]._id, 'test11'); | ||
}); | ||
describe('Modify', () => { | ||
// Run all updates at the end so find()/findByKey()/chain() have the correct values. | ||
describe('#update()', () => { | ||
it('it should set "test0" to "100" for update({ \'$eq\': 10 }, { \'$set\': 100 })', () => { | ||
let $update = MapQL.update({ '$eq': 10 }, { '$set': 100 }); | ||
assert.deepEqual($update[0].value, 100); | ||
assert.deepEqual($update[0]._id, 'test0'); | ||
}); | ||
it('it should increment "test0" to "200" for update(\'test0\', { \'$inc\': 100 })', () => { | ||
let $update = MapQL.update('test0', { '$inc': 100 }); | ||
assert.deepEqual($update[0].value, 200); | ||
assert.deepEqual($update[0]._id, 'test0'); | ||
}); | ||
it('it should multiply "test0" to "400" for update(\'test0\', { \'$mul\': 2 })', () => { | ||
let $update = MapQL.update('test0', { '$mul': 2 }); | ||
assert.deepEqual($update[0].value, 400); | ||
assert.deepEqual($update[0]._id, 'test0'); | ||
}); | ||
it('it should remove "foobar" from "test1" for update(\'test1\', { \'$unset\': { foobar: 1 } })', () => { | ||
let $update = MapQL.update('test1', { '$unset': { foobar: 1 } }); | ||
assert.deepEqual($update[0].value.foobar, undefined); | ||
assert.deepEqual($update[0]._id, 'test1'); | ||
}); | ||
it('it should remove "A" from "test7" array for update(\'test7\', { \'$pop\': { \'array\': 1 } })', () => { | ||
let $update = MapQL.update('test7', { '$pop': { 'array': 1 } }); | ||
assert.deepEqual($update[0].value.array, ['B', 'C']); | ||
assert.deepEqual($update[0]._id, 'test7'); | ||
}); | ||
it('it should remove "C" from "test7" array for update(\'test7\', { \'$pop\': { \'array\': -1 } })', () => { | ||
let $update = MapQL.update('test7', { '$pop': { 'array': -1 } }); | ||
assert.deepEqual($update[0].value.array, ['B']); | ||
assert.deepEqual($update[0]._id, 'test7'); | ||
}); | ||
}); | ||
}); | ||
describe('Delete', () => { | ||
@@ -165,2 +224,16 @@ describe('#delete()', () => { | ||
}); | ||
describe('#remove()', () => { | ||
it('it should remove test1 for remove({ foo: \'bar\' })', () => { | ||
assert.equal(MapQL.remove({ foo: 'bar' })[0], 'test1'); | ||
}); | ||
it('it should remove test2 for remove({ bar: { \'$lt\': 10 }, baz: { \'$gt\': 3 } })', () => { | ||
assert.equal(MapQL.remove({ bar: { '$lt': 10 }, baz: { '$gt': 3 } })[0], 'test2'); | ||
}); | ||
it('it should remove test3 for remove({ \'$regex\': /^Str/i })', () => { | ||
assert.equal(MapQL.remove({ '$regex': /^Str/i })[0], 'test3'); | ||
}); | ||
it('it should remove test4 for remove({ qux: { \'$type\': \'number\' } })', () => { | ||
assert.equal(MapQL.remove({ qux: { '$type': 'number' } })[0], 'test4'); | ||
}); | ||
}); | ||
describe('#clear()', () => { | ||
@@ -167,0 +240,0 @@ it('it should remove all entries', () => { |
@@ -0,1 +1,4 @@ | ||
IMPORTANT: Need to document ALL the things! | ||
=== | ||
TODO | ||
@@ -21,2 +24,4 @@ === | ||
* Improve the `Field` update modifiers. | ||
* ~~Add `MapQL.remove(<Query>[, <multi>])` to remove entries via query.~~ | ||
* Improve type checking of queries passed. Must be Array, String or Object. | ||
* Implement babel with grunt; add polyfills to work with ES5 browsers. | ||
@@ -23,0 +28,0 @@ * Add checks to make sure the lib is supported in the user browser. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
142877
3470
189
4