Comparing version 1.1.0 to 1.1.1
@@ -0,1 +1,8 @@ | ||
## v1.1.1 (2017-04-14) | ||
### Minor changes | ||
* Change collection helper callback arguments [#2](https://github.com/tsuyoshiwada/dot-wild/issues/2) | ||
## v1.1.0 (2017-04-10) | ||
@@ -2,0 +9,0 @@ |
@@ -1,4 +0,1 @@ | ||
/** | ||
* Getter | ||
*/ | ||
export declare const get: (data: any, path: string | number, value?: any) => any; | ||
@@ -22,8 +19,11 @@ /** | ||
export declare const expand: (data: any) => any; | ||
export declare const forEach: (data: any, path: string | number, iteratee: (value: any, key: string | number, array: any) => void) => void; | ||
/** | ||
* Executes a provided function once for each element. | ||
*/ | ||
export declare const forEach: (data: any, path: string | number, iteratee: (value: any, key: string | number, path: string, data: any) => boolean | void) => void; | ||
/** | ||
* Create a new element | ||
* with the results of calling a provided function on every element. | ||
*/ | ||
export declare const map: (data: any, path: string | number, iteratee: (value: any, key: string | number, array: any) => any) => any[]; | ||
export declare const map: (data: any, path: string | number, iteratee: (value: any, key: string | number, path: string, data: any) => any) => any[]; | ||
/** | ||
@@ -30,0 +30,0 @@ * Match key |
@@ -99,9 +99,9 @@ "use strict"; | ||
}; | ||
/** | ||
* Getter | ||
*/ | ||
exports.get = function (data, path, value) { | ||
if (value === void 0) { value = null; } | ||
var internalGet = function (data, path, value) { | ||
if (!path || !isString(path)) { | ||
return value; | ||
return { | ||
exist: false, | ||
wildcard: false, | ||
values: [[value, []]], | ||
}; | ||
} | ||
@@ -111,29 +111,49 @@ var key = '__get_item__'; | ||
var length = tokens.length; | ||
var useWildcard = false; | ||
var index = 0; | ||
var context = (_a = {}, _a[key] = [data], _a); | ||
var state = { | ||
index: 0, | ||
context: (_a = {}, _a[key] = [[data, []]], _a), | ||
wildcard: false, | ||
}; | ||
tokens.forEach(function (token) { | ||
var next = []; | ||
each(context[key], function (item) { | ||
each(state.context[key], function (_a) { | ||
var item = _a[0], p = _a[1]; | ||
each(item, function (v, k) { | ||
if (matchToken(k, token)) { | ||
if (token === '*') { | ||
useWildcard = true; | ||
} | ||
next.push(v); | ||
if (!matchToken(k, token)) | ||
return; | ||
if (token === '*') { | ||
state.wildcard = true; | ||
} | ||
next.push([v, p.concat([k])]); | ||
}); | ||
}); | ||
if (next.length > 0) { | ||
context = (_a = {}, _a[key] = next, _a); | ||
index++; | ||
state.context = (_a = {}, _a[key] = next, _a); | ||
state.index++; | ||
} | ||
var _a; | ||
}); | ||
if (index !== length) | ||
return value; | ||
var v = context[key]; | ||
return useWildcard ? v : v.shift(); | ||
if (state.index !== length) { | ||
return { | ||
exist: false, | ||
wildcard: state.wildcard, | ||
values: [[value, []]], | ||
}; | ||
} | ||
return { | ||
exist: true, | ||
wildcard: state.wildcard, | ||
values: state.context[key], | ||
}; | ||
var _a; | ||
}; | ||
exports.get = function (data, path, value) { | ||
if (value === void 0) { value = null; } | ||
var _a = internalGet(data, path, value), exist = _a.exist, wildcard = _a.wildcard, values = _a.values; | ||
if (!exist) | ||
return values[0][0]; | ||
if (wildcard) | ||
return values.map(function (v) { return v[0]; }); | ||
return values[0][0]; | ||
}; | ||
/** | ||
@@ -333,9 +353,10 @@ * Setter | ||
*/ | ||
var toIterable = function (value) { return !isObj(value) && !isArray(value) ? [value] : value; }; | ||
exports.forEach = function (data, path, iteratee) { | ||
var result = exports.get(data, path); | ||
if (result === null) | ||
var _a = internalGet(data, path, null), exist = _a.exist, values = _a.values; | ||
if (!exist) | ||
return; | ||
var obj = toIterable(result); | ||
each(obj, iteratee); | ||
each(values, function (_a) { | ||
var v = _a[0], p = _a[1]; | ||
return iteratee(v, p[p.length - 1], p.join('.'), data); | ||
}); | ||
}; | ||
@@ -347,11 +368,9 @@ /** | ||
exports.map = function (data, path, iteratee) { | ||
var result = exports.get(data, path); | ||
if (result === null) | ||
var _a = internalGet(data, path, null), exist = _a.exist, values = _a.values; | ||
if (!exist) | ||
return []; | ||
var obj = toIterable(result); | ||
var values = []; | ||
each(obj, function (value, key, array) { | ||
values[key] = iteratee(value, key, array); | ||
return values.map(function (_a) { | ||
var v = _a[0], p = _a[1]; | ||
return iteratee(v, p[p.length - 1], p.join('.'), data); | ||
}); | ||
return values; | ||
}; | ||
@@ -358,0 +377,0 @@ /** |
{ | ||
"name": "dot-wild", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Use powerful dot notation (dot path + wildcard) to manipulate properties of JSON", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -162,11 +162,13 @@ dot-wild | ||
*/ | ||
dot.forEach(postData, 'data.posts.*.id', (value, key) => { | ||
dot.forEach(postData, 'data.posts.*.id', (value, key, path, data) => { | ||
// value => 1, 2 | ||
// key => 0, 1 | ||
// key => 'id', 'id | ||
// path => 'data.posts.0.id', 'data.posts.1.id' | ||
// data => postData... | ||
}); | ||
dot.map(postData, 'data.tags.*.name', (value, key) => { | ||
return `${key}.${value}`; | ||
dot.map(postData, 'data.tags.*.name', (value, key, path, data) => { | ||
return `${dot.get(data, path)} === ${value} (${key})`; | ||
}); | ||
// => ['0.tag 1', '1.tag 2'] | ||
// => ['tag 1 === tag 1 (name)', 'tag 2 === tag 2 (name)'] | ||
@@ -173,0 +175,0 @@ |
@@ -375,34 +375,45 @@ import * as assert from 'power-assert'; | ||
it('forEach()', () => { | ||
let cnt = 0; | ||
let result: { values: any[]; keys: any[] } = { | ||
values: [], | ||
keys: [], | ||
}; | ||
let results: any[] = []; | ||
// Not found | ||
dot.forEach(sampleData, 'hoge.fuga', () => { | ||
throw new Error('error'); | ||
}); | ||
// Normal path | ||
dot.forEach(sampleData, 'nested', (value: any, key: string, data: any) => { | ||
cnt += 1; | ||
result.values.push(value); | ||
result.keys.push(key); | ||
assert.deepStrictEqual(sampleData.nested, data); | ||
dot.forEach(sampleData, 'nested', (value: any, key: any, path: string, data: any) => { | ||
assert(dot.get(data, path) === value); | ||
results.push([value, key, path]); | ||
}); | ||
assert(cnt === 1); | ||
assert.deepStrictEqual(result.values[0], sampleData.nested.deep); | ||
assert(results.length === 1); | ||
assert.deepStrictEqual(results[0][0], sampleData.nested); | ||
assert(results[0][1] === 'nested'); | ||
assert(results[0][2] === 'nested'); | ||
// Use wildcard | ||
cnt = 0; | ||
result.values = []; | ||
result.keys = []; | ||
results = []; | ||
dot.forEach(sampleData, '*', (value: any, key: number) => { | ||
cnt += 1; | ||
result.values.push(value); | ||
result.keys.push(key); | ||
assert(key === cnt - 1); | ||
dot.forEach(sampleData, 'tags.*.*', (value: any, key: any, path: string, data: any) => { | ||
assert.deepStrictEqual(dot.get(data, path), value); | ||
results.push([value, key, path]); | ||
}); | ||
assert(cnt === 2); | ||
assert.deepStrictEqual(result.values[0], sampleData.tags); | ||
assert.deepStrictEqual(result.values[1], sampleData.nested); | ||
assert(results.length === 4); | ||
assert(results[0][0] === 1); | ||
assert(results[0][1] === 'id'); | ||
assert(results[0][2] === 'tags.0.id'); | ||
assert(results[1][0] === 'tag1'); | ||
assert(results[1][1] === 'tag'); | ||
assert(results[1][2] === 'tags.0.tag'); | ||
assert(results[2][0] === 2); | ||
assert(results[2][1] === 'id'); | ||
assert(results[2][2] === 'tags.1.id'); | ||
assert(results[3][0] === 'tag2'); | ||
assert(results[3][1] === 'tag'); | ||
assert(results[3][2] === 'tags.1.tag'); | ||
}); | ||
@@ -412,18 +423,51 @@ | ||
it('map()', () => { | ||
let result: any[] = []; | ||
let results: any[] = []; | ||
// Not found | ||
results = dot.map(sampleData, 'foo.bar', () => { | ||
throw new Error('error'); | ||
}); | ||
// Normal path | ||
result = dot.map(sampleData, 'tags', (value: any, key: any, array: any) => { | ||
assert.deepStrictEqual(sampleData.tags, array); | ||
return value.id + key; | ||
results = dot.map(sampleData, 'tags', (value: any, key: any, path: string, data: any) => { | ||
assert.deepStrictEqual(dot.get(data, path), value); | ||
return [value, key, path]; | ||
}); | ||
assert.deepStrictEqual(result, [1, 3]); | ||
assert(results.length === 1); | ||
assert.deepStrictEqual(results[0][0], sampleData.tags); | ||
assert(results[0][1] === 'tags'); | ||
assert(results[0][2] === 'tags'); | ||
// Use wildcard | ||
result = dot.map(sampleData, 'nested.deep.*.members.*.profile.age', (value: any, key: any) => { | ||
return value + key; | ||
results = dot.map(sampleData, 'nested.deep.*.members.*.profile.age', (value: any, key: any, path: string, data: any) => { | ||
assert.deepStrictEqual(dot.get(data, path), value); | ||
return [value, key, path]; | ||
}); | ||
assert.deepStrictEqual(result, [24, 31, 35, 22, 37, 45]); | ||
assert(results.length === 6); | ||
assert(results[0][0] === 24); | ||
assert(results[0][1] === 'age'); | ||
assert(results[0][2] === 'nested.deep.0.members.0.profile.age'); | ||
assert(results[1][0] === 30); | ||
assert(results[1][1] === 'age'); | ||
assert(results[1][2] === 'nested.deep.0.members.1.profile.age'); | ||
assert(results[2][0] === 33); | ||
assert(results[2][1] === 'age'); | ||
assert(results[2][2] === 'nested.deep.0.members.2.profile.age'); | ||
assert(results[3][0] === 19); | ||
assert(results[3][1] === 'age'); | ||
assert(results[3][2] === 'nested.deep.1.members.0.profile.age'); | ||
assert(results[4][0] === 33); | ||
assert(results[4][1] === 'age'); | ||
assert(results[4][2] === 'nested.deep.1.members.1.profile.age'); | ||
assert(results[5][0] === 40); | ||
assert(results[5][1] === 'age'); | ||
assert(results[5][2] === 'nested.deep.1.members.2.profile.age'); | ||
}); | ||
@@ -430,0 +474,0 @@ |
36948
844
253