Comparing version 2.2.0 to 2.2.1
@@ -0,1 +1,6 @@ | ||
## v2.2.1 (2017-06-09) | ||
* Fix a bug that remove() method left empty element of array | ||
## v2.2.0 (2017-05-23) | ||
@@ -2,0 +7,0 @@ |
@@ -10,5 +10,2 @@ /** | ||
export declare const set: (data: any, path: string | number, value: any) => any; | ||
/** | ||
* Deleter | ||
*/ | ||
export declare const remove: (data: any, path: string | number) => any; | ||
@@ -15,0 +12,0 @@ /** |
@@ -206,56 +206,60 @@ "use strict"; | ||
*/ | ||
var arrayRemove = function (array, index) { return (array.slice(0, index).concat(array.slice(index + 1))); }; | ||
var simpleRemove = function (data, path) { | ||
if (isArray(data) && isArrayKey(path)) { | ||
data = arrayRemove(data, parseInt(path, 10)); | ||
} | ||
else { | ||
delete data[path]; | ||
} | ||
return data; | ||
}; | ||
exports.remove = function (data, path) { | ||
if (!path || !isString(path)) | ||
if (!path || !isString(path)) { | ||
return data; | ||
} | ||
var _data = clone(data); | ||
if (!hasToken(path) && path !== '*') { | ||
if (isArrayKey(path) && isArray(_data)) { | ||
_data.splice(parseInt(path, 10), 1); | ||
} | ||
else { | ||
delete _data[path]; | ||
} | ||
return _data; | ||
return simpleRemove(_data, path); | ||
} | ||
var tokens = exports.tokenize(path); | ||
if (tokens.indexOf('*') < 0) { | ||
var res = _data; | ||
var result = _data; | ||
each(tokens, function (token, i) { | ||
if (i === tokens.length - 1) { | ||
delete _data[token]; | ||
_data = simpleRemove(_data, token); | ||
return false; | ||
} | ||
_data = _data[token]; | ||
if (!isObj(_data) && !isArray(_data)) { | ||
return false; | ||
else { | ||
_data = _data[token]; | ||
return isObj(_data) || isArray(_data); | ||
} | ||
return true; | ||
}); | ||
return res; | ||
return result; | ||
} | ||
else { | ||
var token_2 = tokens.shift(); | ||
var nextPath_2 = tokens.join('.'); | ||
if (token_2 === undefined) | ||
return _data; | ||
each(_data, function (v, k) { | ||
if (!matchToken(k, token_2)) | ||
return; | ||
if (isObj(v) || isArray(v)) { | ||
if (nextPath_2) { | ||
_data[k] = exports.remove(v, nextPath_2); | ||
var first = tokens.shift(); | ||
var later = tokens.join('.'); | ||
var isDataArray = isArray(_data); | ||
var count = 0; | ||
if (first === undefined) { | ||
return _data; | ||
} | ||
each(_data, function (v, k) { | ||
if (!matchToken(k, first)) { | ||
return; | ||
} | ||
if ((!isObj(v) && !isArray(v)) || !later) { | ||
if (!later) { | ||
if (isDataArray) { | ||
_data = arrayRemove(_data, parseInt(k, 10) - count); | ||
count += isDataArray ? 1 : 0; | ||
} | ||
else { | ||
if (isArray(_data[k])) { | ||
_data[k].splice(parseInt(k), 1); | ||
return; | ||
} | ||
delete _data[k]; | ||
} | ||
} | ||
else if (!nextPath_2) { | ||
delete _data[k]; | ||
} | ||
}); | ||
} | ||
return; | ||
} | ||
_data[k] = exports.remove(v, later); | ||
}); | ||
return _data; | ||
@@ -262,0 +266,0 @@ }; |
{ | ||
"name": "dot-wild", | ||
"version": "2.2.0", | ||
"version": "2.2.1", | ||
"description": "Use powerful dot notation (dot path + wildcard) to manipulate properties of JSON", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -245,2 +245,4 @@ import * as assert from 'power-assert'; | ||
let result: any = null; | ||
const t2 = [ | ||
@@ -259,9 +261,17 @@ { nest: [{ deep: { name: 'foo' } }] }, | ||
assert.deepStrictEqual(dot.remove(t2, '0'), [ | ||
result = dot.remove(t2, '*'); | ||
assert.deepStrictEqual(result, []); | ||
assert(result.length === 0); | ||
assertT2(); | ||
result = dot.remove(t2, '0'); | ||
assert.deepStrictEqual(result, [ | ||
{ nest: [{ deep: { name: 'bar' } }] }, | ||
{ nest: [{ deep: { name: 'baz' } }] }, | ||
]); | ||
assert(result.length === 2); | ||
assertT2(); | ||
assert.deepStrictEqual(dot.remove(t2, '0.nest'), [ | ||
result = dot.remove(t2, '0.nest'); | ||
assert.deepStrictEqual(result, [ | ||
{}, | ||
@@ -271,5 +281,7 @@ { nest: [{ deep: { name: 'bar' } }] }, | ||
]); | ||
assert(result.length === 3); | ||
assertT2(); | ||
assert.deepStrictEqual(dot.remove(t2, '2.nest.0.deep.name'), [ | ||
result = dot.remove(t2, '2.nest.0.deep.name'); | ||
assert.deepStrictEqual(result, [ | ||
{ nest: [{ deep: { name: 'foo' } }] }, | ||
@@ -279,8 +291,12 @@ { nest: [{ deep: { name: 'bar' } }] }, | ||
]); | ||
assert(result.length === 3); | ||
assertT2(); | ||
assert.deepStrictEqual(dot.remove(t2, '*'), []); | ||
result = dot.remove(t2, '*'); | ||
assert.deepStrictEqual(result, []); | ||
assert(result.length === 0); | ||
assertT2(); | ||
assert.deepStrictEqual(dot.remove(t2, '*.nest.*.deep'), [ | ||
result = dot.remove(t2, '*.nest.*.deep'); | ||
assert.deepStrictEqual(result, [ | ||
{ nest: [{}] }, | ||
@@ -292,3 +308,4 @@ { nest: [{}] }, | ||
assert.deepStrictEqual(dot.remove(t2, '*.nest.*.deep.name'), [ | ||
result = dot.remove(t2, '*.nest.*.deep.name'); | ||
assert.deepStrictEqual(result, [ | ||
{ nest: [{ deep: {} }] }, | ||
@@ -298,5 +315,7 @@ { nest: [{ deep: {} }] }, | ||
]); | ||
assert(result.length === 3); | ||
assertT2(); | ||
assert.deepStrictEqual(dot.remove(t2, '*.nest.*.deep.name.hoge'), [ | ||
result = dot.remove(t2, '*.nest.*.deep.name.hoge'); | ||
assert.deepStrictEqual(result, [ | ||
{ nest: [{ deep: { name: 'foo' } }] }, | ||
@@ -306,3 +325,16 @@ { nest: [{ deep: { name: 'bar' } }] }, | ||
]); | ||
assert(result.length === 3); | ||
assertT2(); | ||
const values = [ | ||
1, | ||
2, | ||
3, | ||
4, | ||
5, | ||
]; | ||
result = dot.remove(values, '*'); | ||
assert.deepStrictEqual(result, []); | ||
assert(result.length === 0); | ||
}); | ||
@@ -309,0 +341,0 @@ |
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
42211
934