Comparing version 2.2.4 to 2.2.5-alpha.1
@@ -5,4 +5,11 @@ /** | ||
export declare const tokenize: (str: string) => string[]; | ||
export declare const get: (data: any, path: string | number, value?: any) => any; | ||
/** | ||
* Getter | ||
*/ | ||
export interface DotGetOptions { | ||
iterateObject?: boolean; | ||
iterateArray?: boolean; | ||
} | ||
export declare const get: (data: any, path: string | number, value?: any, options?: DotGetOptions | undefined) => any; | ||
/** | ||
* Setter | ||
@@ -24,3 +31,3 @@ */ | ||
*/ | ||
export declare const forEach: (data: any, path: string | number, iteratee: (value: any, key: string | number, context: any, path: string, data: any) => boolean | void) => void; | ||
export declare const forEach: (data: any, path: string | number, iteratee: (value: any, key: string | number, context: any, path: string, data: any) => boolean | void, options?: DotGetOptions | undefined) => void; | ||
/** | ||
@@ -30,3 +37,3 @@ * Create a new element | ||
*/ | ||
export declare const map: (data: any, path: string | number, iteratee: (value: any, key: string | number, context: any, path: string, data: any) => any) => any[]; | ||
export declare const map: (data: any, path: string | number, iteratee: (value: any, key: string | number, context: any, path: string, data: any) => any, options?: DotGetOptions | undefined) => any[]; | ||
/** | ||
@@ -33,0 +40,0 @@ * Match key |
"use strict"; | ||
var __assign = (this && this.__assign) || Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -94,3 +102,8 @@ /** | ||
}; | ||
var internalGet = function (data, path, value) { | ||
var defaultGetOptions = { | ||
iterateObject: true, | ||
iterateArray: true, | ||
}; | ||
var internalGet = function (data, path, value, options) { | ||
var opts = __assign({}, defaultGetOptions, (options || {})); | ||
if (!path || !isString(path)) { | ||
@@ -118,6 +131,13 @@ return { | ||
return; | ||
if (token === '*') { | ||
if (token !== '*') { | ||
next.push([v, item, p.concat([k])]); | ||
} | ||
else { | ||
if (!opts.iterateObject && isObj(item)) | ||
return; | ||
if (!opts.iterateArray && isArray(item)) | ||
return; | ||
state.wildcard = true; | ||
next.push([v, item, p.concat([k])]); | ||
} | ||
next.push([v, item, p.concat([k])]); | ||
}); | ||
@@ -145,5 +165,5 @@ }); | ||
}; | ||
exports.get = function (data, path, value) { | ||
exports.get = function (data, path, value, options) { | ||
if (value === void 0) { value = null; } | ||
var _a = internalGet(data, path, value), exist = _a.exist, wildcard = _a.wildcard, values = _a.values; | ||
var _a = internalGet(data, path, value, options), exist = _a.exist, wildcard = _a.wildcard, values = _a.values; | ||
if (!exist) | ||
@@ -358,4 +378,4 @@ return values[0][0]; | ||
*/ | ||
exports.forEach = function (data, path, iteratee) { | ||
var _a = internalGet(data, path, null), exist = _a.exist, values = _a.values; | ||
exports.forEach = function (data, path, iteratee, options) { | ||
var _a = internalGet(data, path, null, options), exist = _a.exist, values = _a.values; | ||
if (!exist) | ||
@@ -372,4 +392,4 @@ return; | ||
*/ | ||
exports.map = function (data, path, iteratee) { | ||
var _a = internalGet(data, path, null), exist = _a.exist, values = _a.values; | ||
exports.map = function (data, path, iteratee, options) { | ||
var _a = internalGet(data, path, null, options), exist = _a.exist, values = _a.values; | ||
if (!exist) | ||
@@ -376,0 +396,0 @@ return []; |
{ | ||
"name": "dot-wild", | ||
"version": "2.2.4", | ||
"version": "2.2.5-alpha.1", | ||
"description": "Use powerful dot notation (dot path + wildcard) to manipulate properties of JSON", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -236,3 +236,3 @@ # dot-wild | ||
* `get(data, path, [value]): Object | any[]` | ||
* `get(data, path, [value, options]): Object | any[]` | ||
* `set(data, path, value): Object | any[]` | ||
@@ -243,4 +243,4 @@ * `remove(data, path): Object | any[]` | ||
* `expand(data): Object | any[]` | ||
* `forEach(data, path, iteratee): void` | ||
* `map(data, path, iteratee): any[]` | ||
* `forEach(data, path, iteratee, options): void` | ||
* `map(data, path, iteratee, options): any[]` | ||
* `tokenize(path): string[]` | ||
@@ -284,4 +284,16 @@ * `matchPath(pathA, pathB): boolean` | ||
#### options | ||
This is an option for Getter method. (`get`, `forEach`. and `map`) | ||
```javascript | ||
{ | ||
iterateObject: true; // If it is `true`, it will enumerate the values of the object when using wildcards | ||
iterateArray: true; // If it is `true`, it will enumerate the values of the array when using wildcards | ||
} | ||
``` | ||
## Contribute | ||
@@ -288,0 +300,0 @@ |
@@ -116,2 +116,68 @@ import * as assert from 'power-assert'; | ||
it('get() with options', () => { | ||
const value = { | ||
obj: { | ||
kfoo: 'foo', | ||
kbar: 'bar', | ||
kbaz: 'baz', | ||
}, | ||
arr: [ | ||
'foo', | ||
'bar', | ||
'baz', | ||
], | ||
}; | ||
const obj = { | ||
kfoo: 'foo', | ||
kbar: 'bar', | ||
kbaz: 'baz', | ||
}; | ||
const arr = [ | ||
'foo', | ||
'bar', | ||
'baz', | ||
]; | ||
// obj | ||
assert.deepStrictEqual(dot.get(value, 'obj.*'), [ | ||
'foo', | ||
'bar', | ||
'baz', | ||
]); | ||
assert.deepStrictEqual(dot.get(value, 'obj.*', null, { iterateObject: false }), null); | ||
assert.deepStrictEqual(dot.get(value, 'obj.*', 'default', { iterateObject: false }), 'default'); | ||
assert.deepStrictEqual(dot.get(obj, '*'), [ | ||
'foo', | ||
'bar', | ||
'baz', | ||
]); | ||
assert.deepStrictEqual(dot.get(obj, '*', null, { iterateObject: false }), null); | ||
assert.deepStrictEqual(dot.get(obj, '*', 'default', { iterateObject: false }), 'default'); | ||
// arr | ||
assert.deepStrictEqual(dot.get(value, 'arr.*'), [ | ||
'foo', | ||
'bar', | ||
'baz', | ||
]); | ||
assert.deepStrictEqual(dot.get(value, 'arr.*', null, { iterateArray: false }), null); | ||
assert.deepStrictEqual(dot.get(value, 'arr.*', 'default', { iterateArray: false }), 'default'); | ||
assert.deepStrictEqual(dot.get(arr, '*'), [ | ||
'foo', | ||
'bar', | ||
'baz', | ||
]); | ||
assert.deepStrictEqual(dot.get(arr, '*', null, { iterateArray: false }), null); | ||
assert.deepStrictEqual(dot.get(arr, '*', 'default', { iterateArray: false }), 'default'); | ||
}); | ||
it('set()', () => { | ||
@@ -582,2 +648,56 @@ const t1 = { foo: { bar: 'baz' } }; | ||
it('forEach() with options', () => { | ||
const sampleData = { | ||
obj: { | ||
kfoo: 'foo', | ||
kbar: 'bar', | ||
kbaz: 'baz', | ||
}, | ||
arr: [ | ||
'foo', | ||
'bar', | ||
'baz', | ||
], | ||
}; | ||
let results: any[] = []; | ||
dot.forEach(sampleData, 'obj.*', (value: any, key: any, _: any, path: string) => { | ||
results.push([value, key, path]); | ||
}); | ||
assert.deepStrictEqual(results, [ | ||
['foo', 'kfoo', 'obj.kfoo'], | ||
['bar', 'kbar', 'obj.kbar'], | ||
['baz', 'kbaz', 'obj.kbaz'], | ||
]); | ||
results = []; | ||
dot.forEach(sampleData, 'obj.*', (value: any, key: any, _: any, path: string) => { | ||
results.push([value, key, path]); | ||
}, { iterateObject: false }); | ||
assert.deepStrictEqual(results, []); | ||
dot.forEach(sampleData, 'arr.*', (value: any, key: any, _: any, path: string) => { | ||
results.push([value, key, path]); | ||
}); | ||
assert.deepStrictEqual(results, [ | ||
['foo', 0, 'arr.0'], | ||
['bar', 1, 'arr.1'], | ||
['baz', 2, 'arr.2'], | ||
]); | ||
results = []; | ||
dot.forEach(sampleData, 'arr.*', (value: any, key: any, _: any, path: string) => { | ||
results.push([value, key, path]); | ||
}, { iterateArray: false }); | ||
assert.deepStrictEqual(results, []); | ||
}); | ||
it('map()', () => { | ||
@@ -638,2 +758,44 @@ let results: any[] = []; | ||
it('map() with options', () => { | ||
const sampleData = { | ||
obj: { | ||
kfoo: 'foo', | ||
kbar: 'bar', | ||
kbaz: 'baz', | ||
}, | ||
arr: [ | ||
'foo', | ||
'bar', | ||
'baz', | ||
], | ||
}; | ||
let results: any[] = []; | ||
results = dot.map(sampleData, 'obj.*', (value: any, key: any, _: any, path: string) => [value, key, path]); | ||
assert.deepStrictEqual(results, [ | ||
['foo', 'kfoo', 'obj.kfoo'], | ||
['bar', 'kbar', 'obj.kbar'], | ||
['baz', 'kbaz', 'obj.kbaz'], | ||
]); | ||
results = dot.map(sampleData, 'obj.*', (value: any, key: any, _: any, path: string) => [value, key, path], { iterateObject: false }); | ||
assert.deepStrictEqual(results, []); | ||
results = dot.map(sampleData, 'arr.*', (value: any, key: any, _: any, path: string) => [value, key, path]); | ||
assert.deepStrictEqual(results, [ | ||
['foo', 0, 'arr.0'], | ||
['bar', 1, 'arr.1'], | ||
['baz', 2, 'arr.2'], | ||
]); | ||
results = dot.map(sampleData, 'arr.*', (value: any, key: any, _: any, path: string) => [value, key, path], { iterateArray: false }); | ||
assert.deepStrictEqual(results, []); | ||
}); | ||
it('matchPath()', () => { | ||
@@ -640,0 +802,0 @@ assert(dot.matchPath('', '') === true); |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
49780
1185
312
1