ancient-cursor
Advanced tools
Comparing version 0.1.3 to 0.2.0
interface IBundle { | ||
type: string; | ||
path: string; | ||
path: TBundlePaths; | ||
[key: string]: any; | ||
@@ -24,16 +24,9 @@ } | ||
} | ||
interface IBundleUnset extends IBundle { | ||
} | ||
interface IBundleArraySplice extends IBundle { | ||
start: number; | ||
deleteCount: number; | ||
values: any[]; | ||
} | ||
interface IBundleArrayRemove extends IBundle { | ||
selector: object; | ||
} | ||
interface IBundleArrayExtend extends IBundleValue { | ||
selector: object; | ||
} | ||
declare function get(data: any, path: any): any; | ||
declare type TBundlePath = string; | ||
declare type TBundleSelector = any; | ||
declare type TBundlePathsStep = TBundlePath | TBundleSelector; | ||
declare type TBundlePaths = string | TBundlePathsStep[]; | ||
declare function toPath(data: any, paths: TBundlePaths): TBundlePath; | ||
declare function get(data: any, paths: TBundlePaths): any; | ||
declare function getByPath(data: any, path: any): any; | ||
declare function prepare(container: any, bundle: any): { | ||
@@ -44,2 +37,2 @@ bundlePath: string[]; | ||
declare const bundleParsers: IBundleParsers; | ||
export { IBundleParser, IBundleParsers, IBundleChanges, IBundle, IBundleValue, IBundleUnset, IBundleArraySplice, IBundleArrayRemove, IBundleArrayExtend, bundleParsers, get, prepare }; | ||
export { IBundleParser, IBundleParsers, IBundleChanges, IBundle, IBundleValue, TBundlePath, TBundleSelector, TBundlePathsStep, TBundlePaths, bundleParsers, get, getByPath, toPath, prepare }; |
"use strict"; | ||
exports.__esModule = true; | ||
var _ = require("lodash"); | ||
function get(data, path) { | ||
var arrayPath = _.toPath(path); | ||
function toPath(data, paths) { | ||
if (typeof (paths) === 'string') | ||
return paths; | ||
if (!_.isArray(paths)) | ||
throw new Error('Path must be array TBundlePaths.'); | ||
var pointer = data; | ||
var result = ''; | ||
_.each(paths, function (p) { | ||
var path; | ||
if (_.isString(p)) { | ||
path = p; | ||
} | ||
else { | ||
path = '' + _.findIndex(pointer, p); | ||
if (path === '-1') | ||
throw new Error("Not founded by selector in path " + paths + "."); | ||
} | ||
pointer = getByPath(pointer, path); | ||
result += path ? (result ? '.' + path : path) : ''; | ||
}); | ||
return result; | ||
} | ||
exports.toPath = toPath; | ||
function get(data, paths) { | ||
return getByPath(data, toPath(data, paths)); | ||
} | ||
exports.get = get; | ||
function getByPath(data, path) { | ||
return path.length ? _.get(data, path) : data; | ||
} | ||
exports.get = get; | ||
exports.getByPath = getByPath; | ||
function prepare(container, bundle) { | ||
var bundlePath = _.toPath(bundle.path); | ||
var bundlePath = _.toPath(toPath(container.data, bundle.path)); | ||
var oldValue = _.clone(get(container.data, bundlePath)); | ||
@@ -50,3 +76,3 @@ return { oldValue: oldValue, bundlePath: bundlePath }; | ||
}, | ||
arraySplice: function (container, bundle) { | ||
splice: function (container, bundle) { | ||
var _a = prepare(container, bundle), oldValue = _a.oldValue, bundlePath = _a.bundlePath; | ||
@@ -61,3 +87,3 @@ var value = get(container.data, bundlePath); | ||
}, | ||
arrayRemove: function (container, bundle) { | ||
remove: function (container, bundle) { | ||
var _a = prepare(container, bundle), oldValue = _a.oldValue, bundlePath = _a.bundlePath; | ||
@@ -71,22 +97,2 @@ var value = get(container.data, bundlePath); | ||
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 }; | ||
} | ||
@@ -93,0 +99,0 @@ }; |
import { TClass, IInstance } from 'ancient-mixins/lib/mixins'; | ||
import { INode, INodeEventsList } from 'ancient-mixins/lib/node'; | ||
import { IBundle, IBundleChanges } from './bundle'; | ||
import { IBundle, IBundleChanges, TBundlePaths } from './bundle'; | ||
declare type TCursor = ICursor<ICursorEventsList>; | ||
@@ -48,5 +48,5 @@ interface ICursorEventChangedData { | ||
parse(bundle: IBundle): IBundleChanges; | ||
get(path: string): any; | ||
get(paths: TBundlePaths): any; | ||
} | ||
declare function watch({oldValue, newValue, bundlePath, data, bundle}: IBundleChanges, path: string, listener: (data: ICursorWatchData) => void): void; | ||
declare function watch({oldValue, newValue, bundlePath, data, bundle}: IBundleChanges, paths: TBundlePaths, listener: (data: ICursorWatchData) => void): void; | ||
declare function mixin<T extends TClass<IInstance>>(superClass: T): any; | ||
@@ -53,0 +53,0 @@ declare const MixedCursor: TClass<ICursor<ICursorEventsList>>; |
@@ -16,9 +16,9 @@ "use strict"; | ||
var bundle_1 = require("./bundle"); | ||
function watch(_a, path, listener) { | ||
function watch(_a, paths, listener) { | ||
var oldValue = _a.oldValue, newValue = _a.newValue, bundlePath = _a.bundlePath, data = _a.data, bundle = _a.bundle; | ||
var watchPath = _.toPath(path); | ||
var watchPath = _.toPath(bundle_1.toPath(data, paths)); | ||
var localWatchPath = watchPath.slice(bundlePath.length); | ||
var localBundlePath = bundlePath.slice(watchPath.length); | ||
var leastPathLength = Math.min(watchPath.length, bundlePath.length); | ||
var value = bundle_1.get(data, path); | ||
var value = bundle_1.get(data, paths); | ||
var localOldValue; | ||
@@ -38,3 +38,3 @@ var localNewValue; | ||
else { | ||
localOldValue = localNewValue = bundle_1.get(data, path); | ||
localOldValue = localNewValue = bundle_1.get(data, paths); | ||
isClone = false; | ||
@@ -93,4 +93,4 @@ } | ||
}; | ||
Cursor.prototype.get = function (path) { | ||
return bundle_1.get(this.data, path); | ||
Cursor.prototype.get = function (paths) { | ||
return bundle_1.get(this.data, paths); | ||
}; | ||
@@ -97,0 +97,0 @@ return Cursor; |
{ | ||
"name": "ancient-cursor", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"description": "Abstract container of data synchronization.", | ||
@@ -5,0 +5,0 @@ "bugs": "https://github.com/AncientSouls/Cursor/issues", |
@@ -7,83 +7,82 @@ "use strict"; | ||
describe('Bundle:', function () { | ||
it('set', function () { | ||
var container = { data: { a: [{ b: { c: 123 } }] } }; | ||
bundle_1.bundleParsers.set(container, { | ||
type: 'set', | ||
path: 'a.0.b.c', | ||
value: 234 | ||
describe('paths', function () { | ||
it('path', function () { | ||
chai_1.assert.equal(bundle_1.get({ a: [{ b: [{ c: 'd' }] }] }, 'a.0.b.0.c'), 'd'); | ||
}); | ||
chai_1.assert.deepEqual(container.data, { a: [{ b: { c: 234 } }] }); | ||
}); | ||
it('extend', function () { | ||
var container = { data: { a: [{ b: { c: 123 } }] } }; | ||
bundle_1.bundleParsers.extend(container, { | ||
type: 'extend', | ||
path: 'a.0.b', | ||
value: { d: 234 } | ||
it('(path[]|find)[]', function () { | ||
chai_1.assert.equal(bundle_1.get({ | ||
a: [ | ||
{ x: 5, b: [ | ||
{ y: 3, c: 9 }, | ||
{ y: 2, c: 4 }, | ||
] }, | ||
{ x: 7, b: [ | ||
{ y: 6, c: 3 }, | ||
{ y: 8, c: 1 }, | ||
] }, | ||
] | ||
}, ['a', { x: 7 }, 'b', ['y', 6], 'c']), 3); | ||
}); | ||
chai_1.assert.deepEqual(container.data, { a: [{ b: { c: 123, d: 234 } }] }); | ||
}); | ||
it('unset', function () { | ||
var container = { data: { a: [{ b: { c: 123 } }] } }; | ||
bundle_1.bundleParsers.unset(container, { | ||
type: 'unset', | ||
path: 'a.0.b.c' | ||
describe('types', function () { | ||
it('set', function () { | ||
var container = { data: { a: [{ b: { c: 123 } }] } }; | ||
bundle_1.bundleParsers.set(container, { | ||
type: 'set', | ||
path: 'a.0.b.c', | ||
value: 234 | ||
}); | ||
chai_1.assert.deepEqual(container.data, { a: [{ b: { c: 234 } }] }); | ||
}); | ||
chai_1.assert.deepEqual(container.data, { a: [{ b: {} }] }); | ||
}); | ||
it('arraySplice', function () { | ||
var container = { data: { a: [{ b: { c: 123 } }, { b: { c: 456 } }] } }; | ||
bundle_1.bundleParsers.arraySplice(container, { | ||
type: 'arraySplice', | ||
path: 'a', | ||
start: 1, | ||
deleteCount: 0, | ||
values: [ | ||
{ b: { c: 234 } }, | ||
{ b: { c: 345 } }, | ||
] | ||
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 } }] }); | ||
}); | ||
chai_1.assert.deepEqual(container.data, { a: [ | ||
{ b: { c: 123 } }, | ||
{ b: { c: 234 } }, | ||
{ b: { c: 345 } }, | ||
{ b: { c: 456 } }, | ||
] }); | ||
}); | ||
it('arrayRemove', function () { | ||
var container = { data: { a: [{ b: { c: 123 } }, { b: { c: 456 } }] } }; | ||
bundle_1.bundleParsers.arrayRemove(container, { | ||
type: 'arrayRemove', | ||
path: 'a', | ||
selector: { b: { c: 123 } } | ||
it('unset', function () { | ||
var container = { data: { a: [{ b: { c: 123 } }] } }; | ||
bundle_1.bundleParsers.unset(container, { | ||
type: 'unset', | ||
path: 'a.0.b.c' | ||
}); | ||
chai_1.assert.deepEqual(container.data, { a: [{ b: {} }] }); | ||
}); | ||
chai_1.assert.deepEqual(container.data, { a: [ | ||
{ b: { c: 456 } }, | ||
] }); | ||
}); | ||
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 } | ||
it('splice', function () { | ||
var container = { data: { a: [{ b: { c: 123 } }, { b: { c: 456 } }] } }; | ||
bundle_1.bundleParsers.splice(container, { | ||
type: 'splice', | ||
path: 'a', | ||
start: 1, | ||
deleteCount: 0, | ||
values: [ | ||
{ b: { c: 234 } }, | ||
{ b: { c: 345 } }, | ||
] | ||
}); | ||
chai_1.assert.deepEqual(container.data, { a: [ | ||
{ b: { c: 123 } }, | ||
{ b: { c: 234 } }, | ||
{ b: { c: 345 } }, | ||
{ b: { c: 456 } }, | ||
] }); | ||
}); | ||
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 } | ||
it('remove', function () { | ||
var container = { data: { x: [ | ||
{ y: 1, a: [{ b: { c: 123 } }, { b: { c: 456 } }] }, | ||
{ y: 2, a: [{ b: { c: 123 } }, { b: { c: 456 } }] }, | ||
] } }; | ||
bundle_1.bundleParsers.remove(container, { | ||
type: 'remove', | ||
path: ['x', ['y', 2], 'a'], | ||
selector: { b: { c: 123 } } | ||
}); | ||
chai_1.assert.deepEqual(container.data, { x: [ | ||
{ y: 1, a: [{ b: { c: 123 } }, { b: { c: 456 } }] }, | ||
{ y: 2, a: [{ b: { c: 456 } }] }, | ||
] }); | ||
}); | ||
chai_1.assert.deepEqual(container.data, { a: [ | ||
{ b: { c: 123 }, d: 234 }, | ||
{ b: { c: 456 } }, | ||
] }); | ||
}); | ||
@@ -90,0 +89,0 @@ }); |
@@ -65,2 +65,9 @@ "use strict"; | ||
}); | ||
watch(['a', {}, 'b'], function (_a) { | ||
var isClone = _a.isClone, oldValue = _a.oldValue, newValue = _a.newValue; | ||
chai_1.assert.isTrue(isClone); | ||
chai_1.assert.deepEqual(oldValue, { c: 'd' }); | ||
chai_1.assert.deepEqual(newValue, undefined); | ||
counter++; | ||
}); | ||
watch('a.0.b', function (_a) { | ||
@@ -106,3 +113,3 @@ var isClone = _a.isClone, oldValue = _a.oldValue, newValue = _a.newValue; | ||
}); | ||
chai_1.assert.equal(counter, 7); | ||
chai_1.assert.equal(counter, 8); | ||
done(); | ||
@@ -109,0 +116,0 @@ }); |
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
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
59873
796