ancient-cursor
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -21,5 +21,7 @@ interface IBundle { | ||
} | ||
interface IBundleSet extends IBundle { | ||
interface IBundleValue extends IBundle { | ||
value: any; | ||
} | ||
interface IBundleUnset extends IBundle { | ||
} | ||
interface IBundleArraySplice extends IBundle { | ||
@@ -30,2 +32,8 @@ start: number; | ||
} | ||
interface IBundleArrayRemove extends IBundle { | ||
selector: object; | ||
} | ||
interface IBundleArrayExtend extends IBundleValue { | ||
selector: object; | ||
} | ||
declare function get(data: any, path: any): any; | ||
@@ -37,2 +45,2 @@ declare function prepare(container: any, bundle: any): { | ||
declare const bundleParsers: IBundleParsers; | ||
export { IBundleParser, IBundleParsers, IBundleChanges, IBundle, IBundleSet, IBundleArraySplice, bundleParsers, get, prepare }; | ||
export { IBundleParser, IBundleParsers, IBundleChanges, IBundle, IBundleValue, IBundleUnset, IBundleArraySplice, IBundleArrayRemove, IBundleArrayExtend, bundleParsers, get, prepare }; |
@@ -11,3 +11,3 @@ "use strict"; | ||
var bundlePath = _.toPath(bundle.path); | ||
var oldValue = get(container.data, bundlePath); | ||
var oldValue = _.clone(get(container.data, bundlePath)); | ||
return { oldValue: oldValue, bundlePath: bundlePath }; | ||
@@ -28,2 +28,13 @@ } | ||
}, | ||
extend: function (container, bundle) { | ||
var _a = prepare(container, bundle), oldValue = _a.oldValue, bundlePath = _a.bundlePath; | ||
if (!bundlePath.length) { | ||
_.extend(container.data, bundle.value); | ||
} | ||
else { | ||
_.extend(get(container.data, bundlePath), bundle.value); | ||
} | ||
var newValue = get(container.data, bundlePath); | ||
return { oldValue: oldValue, newValue: newValue, bundlePath: bundlePath, bundle: bundle, data: container.data }; | ||
}, | ||
unset: function (container, bundle) { | ||
@@ -52,5 +63,4 @@ var _a = prepare(container, bundle), oldValue = _a.oldValue, bundlePath = _a.bundlePath; | ||
arrayRemove: function (container, bundle) { | ||
var bundlePath = prepare(container, bundle).bundlePath; | ||
var _a = prepare(container, bundle), oldValue = _a.oldValue, bundlePath = _a.bundlePath; | ||
var value = get(container.data, bundlePath); | ||
var oldValue = _.clone(value); | ||
if (!_.isArray(value)) { | ||
@@ -62,2 +72,22 @@ throw new Error("Data by path \"" + bundle.path + "\" is not an array but " + typeof (value) + "."); | ||
return { oldValue: oldValue, newValue: newValue, bundlePath: bundlePath, bundle: bundle, data: container.data }; | ||
}, | ||
arrayFilterAndExtend: function (container, bundle) { | ||
var _a = prepare(container, bundle), oldValue = _a.oldValue, bundlePath = _a.bundlePath; | ||
var value = get(container.data, bundlePath); | ||
if (!_.isArray(value)) { | ||
throw new Error("Data by path \"" + bundle.path + "\" is not an array but " + typeof (value) + "."); | ||
} | ||
_.each(value, function (v) { return _.matches(bundle.selector) ? _.extend(v, bundle.value) : null; }); | ||
var newValue = value; | ||
return { oldValue: oldValue, newValue: newValue, bundlePath: bundlePath, bundle: bundle, data: container.data }; | ||
}, | ||
arrayFindAndExtend: function (container, bundle) { | ||
var _a = prepare(container, bundle), oldValue = _a.oldValue, bundlePath = _a.bundlePath; | ||
var value = get(container.data, bundlePath); | ||
if (!_.isArray(value)) { | ||
throw new Error("Data by path \"" + bundle.path + "\" is not an array but " + typeof (value) + "."); | ||
} | ||
_.extend(_.find(value, bundle.selector), bundle.value); | ||
var newValue = value; | ||
return { oldValue: oldValue, newValue: newValue, bundlePath: bundlePath, bundle: bundle, data: container.data }; | ||
} | ||
@@ -64,0 +94,0 @@ }; |
{ | ||
"name": "ancient-cursor", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Abstract container of data synchronization.", | ||
@@ -21,5 +21,5 @@ "bugs": "https://github.com/AncientSouls/Cursor/issues", | ||
"@types/mocha": "^2.2.48", | ||
"@types/node": "^9.4.6", | ||
"@types/node": "^9.4.7", | ||
"chai": "^4.1.2", | ||
"mocha": "^5.0.1", | ||
"mocha": "^5.0.4", | ||
"source-map-support": "^0.5.3", | ||
@@ -26,0 +26,0 @@ "tslint": "^5.9.1", |
@@ -16,2 +16,11 @@ "use strict"; | ||
}); | ||
it('extend', function () { | ||
var container = { data: { a: [{ b: { c: 123 } }] } }; | ||
bundle_1.bundleParsers.extend(container, { | ||
type: 'extend', | ||
path: 'a.0.b', | ||
value: { d: 234 } | ||
}); | ||
chai_1.assert.deepEqual(container.data, { a: [{ b: { c: 123, d: 234 } }] }); | ||
}); | ||
it('unset', function () { | ||
@@ -55,2 +64,28 @@ var container = { data: { a: [{ b: { c: 123 } }] } }; | ||
}); | ||
it('arrayFilterAndExtend', function () { | ||
var container = { data: { a: [{ b: { c: 123 } }, { b: { c: 456 } }] } }; | ||
bundle_1.bundleParsers.arrayFilterAndExtend(container, { | ||
type: 'arrayFilterAndExtend', | ||
path: 'a', | ||
selector: { b: {} }, | ||
value: { d: 234 } | ||
}); | ||
chai_1.assert.deepEqual(container.data, { a: [ | ||
{ b: { c: 123 }, d: 234 }, | ||
{ b: { c: 456 }, d: 234 }, | ||
] }); | ||
}); | ||
it('arrayFindAndExtend', function () { | ||
var container = { data: { a: [{ b: { c: 123 } }, { b: { c: 456 } }] } }; | ||
bundle_1.bundleParsers.arrayFindAndExtend(container, { | ||
type: 'arrayFindAndExtend', | ||
path: 'a', | ||
selector: { b: {} }, | ||
value: { d: 234 } | ||
}); | ||
chai_1.assert.deepEqual(container.data, { a: [ | ||
{ b: { c: 123 }, d: 234 }, | ||
{ b: { c: 456 } }, | ||
] }); | ||
}); | ||
}); | ||
@@ -57,0 +92,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
59510
791