deepwalker
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -24,2 +24,89 @@ "use strict"; | ||
exports.deepwalker = void 0; | ||
var InstanceResult = /** @class */ (function () { | ||
function InstanceResult(result) { | ||
this.toValue = function (path) { | ||
var _a, _b; | ||
return path ? (_a = this === null || this === void 0 ? void 0 : this._result[0]) === null || _a === void 0 ? void 0 : _a.value[path] : (_b = this === null || this === void 0 ? void 0 : this.result[0]) === null || _b === void 0 ? void 0 : _b.value; | ||
}; | ||
this.filter = function (filterFn) { | ||
return this.setResult(this.result.filter(filterFn)); | ||
}; | ||
this.sort = function (sortFn) { | ||
this.result.sort(sortFn); | ||
return this; | ||
}; | ||
this.pick = function (array) { | ||
this.result = this.result.map(function (el) { | ||
return __assign(__assign({}, el), { value: Object.fromEntries(Object.entries(el.value) | ||
.filter(function (_a) { | ||
var key = _a[0]; | ||
return array.includes(key); | ||
})) }); | ||
}); | ||
return this; | ||
}; | ||
this.flatten = function (array) { | ||
if (array === void 0) { array = []; } | ||
this.result = this.result.reduce(function (result, el) { | ||
var arrayOfValues = Object.keys(el.value).filter(function (key) { return array.includes(key); }).map(function (key) { | ||
return { | ||
dimensions: el.dimensions.concat(key), value: __assign(__assign({}, Object.fromEntries(Object.entries(el.value) | ||
.filter(function (_a) { | ||
var key = _a[0]; | ||
return !array.includes(key); | ||
}))), { value: el.value[key] }) | ||
}; | ||
}); | ||
return result.concat(arrayOfValues); | ||
}, []); | ||
return this; | ||
}; | ||
this.concat = function (instance) { | ||
this.result = this.result.concat(instance.result); | ||
return this; | ||
}; | ||
this.slice = function (number) { | ||
this.result = this.result.slice(0, number); | ||
return this; | ||
}; | ||
this.toValues = function () { | ||
return this.result.map(function (r) { return r.value; }); | ||
}; | ||
this.toString = function (transformer) { | ||
if (!this.result || this.result.length == 0) | ||
return ''; | ||
return transformer(createResultsObject(this.result)); | ||
}; | ||
this.convertEach = function (converter) { | ||
if (!this.result || this.result.length == 0) | ||
return []; | ||
return this.result.map(converter); | ||
}; | ||
this.haveResults = function () { | ||
if (!this.result || this.result.length == 0) | ||
return false; | ||
return true; | ||
}; | ||
this.toMap = function (path) { | ||
return this.result.reduce(function (res, r, i) { | ||
var deepResult = res; | ||
r.dimensions.forEach(function (dim, i) { | ||
if (!deepResult[dim] && i !== (r.dimensions.length - 1)) { | ||
deepResult[dim] = {}; | ||
} | ||
if (i === (r.dimensions.length - 1)) | ||
deepResult[dim] = path ? r.value[path] : r.value; | ||
deepResult = deepResult[dim]; | ||
}); | ||
return res; | ||
}, {}); | ||
}; | ||
this.setResult = function (result) { | ||
this.result = result; | ||
return this; | ||
}; | ||
this.result = result; | ||
} | ||
return InstanceResult; | ||
}()); | ||
function deepwalker(object) { | ||
@@ -30,72 +117,6 @@ var instance = { | ||
get: function (queryPath) { | ||
var instanceResult = __assign(__assign({ _result: null }, instance), { toValue: function (path) { | ||
var _a, _b; | ||
return path ? (_a = instanceResult === null || instanceResult === void 0 ? void 0 : instanceResult._result[0]) === null || _a === void 0 ? void 0 : _a.value[path] : (_b = instanceResult === null || instanceResult === void 0 ? void 0 : instanceResult._result[0]) === null || _b === void 0 ? void 0 : _b.value; | ||
}, filter: function (filterFn) { | ||
return instanceResult.setResult(instanceResult._result.filter(filterFn)); | ||
}, sort: function (sortFn) { | ||
instanceResult._result.sort(sortFn); | ||
return instanceResult; | ||
}, pick: function (array) { | ||
instanceResult._result = instanceResult._result.map(function (el) { | ||
return __assign(__assign({}, el), { value: Object.fromEntries(Object.entries(el.value) | ||
.filter(function (_a) { | ||
var key = _a[0]; | ||
return array.includes(key); | ||
})) }); | ||
}); | ||
return instanceResult; | ||
}, flatten: function (array) { | ||
if (array === void 0) { array = []; } | ||
instanceResult._result = instanceResult._result.reduce(function (result, el) { | ||
var arrayOfValues = Object.keys(el.value).filter(function (key) { return array.includes(key); }).map(function (key) { | ||
return { | ||
dimensions: el.dimensions.concat(key), value: __assign(__assign({}, Object.fromEntries(Object.entries(el.value) | ||
.filter(function (_a) { | ||
var key = _a[0]; | ||
return !array.includes(key); | ||
}))), { value: el.value[key] }) | ||
}; | ||
}); | ||
return result.concat(arrayOfValues); | ||
}, []); | ||
return instanceResult; | ||
}, concat: function (instance) { | ||
instanceResult._result = instanceResult._result.concat(instance._result); | ||
return instanceResult; | ||
}, slice: function (number) { | ||
instanceResult._result = instanceResult._result.slice(0, number); | ||
return instanceResult; | ||
}, toValues: function () { | ||
return instanceResult._result.map(function (r) { return r.value; }); | ||
}, toString: function (transformer) { | ||
if (!instanceResult._result || instanceResult._result.length == 0) | ||
return ''; | ||
return transformer(createResultsObject(instanceResult._result)); | ||
}, convertEach: function (converter) { | ||
if (!instanceResult._result || instanceResult._result.length == 0) | ||
return []; | ||
return instanceResult._result.map(converter); | ||
}, haveResults: function () { | ||
if (!instanceResult._result || instanceResult._result.length == 0) | ||
return false; | ||
return true; | ||
}, toMap: function (path) { | ||
return instanceResult._result.reduce(function (res, r, i) { | ||
var deepResult = res; | ||
r.dimensions.forEach(function (dim, i) { | ||
if (!deepResult[dim] && i !== (r.dimensions.length - 1)) { | ||
deepResult[dim] = {}; | ||
} | ||
if (i === (r.dimensions.length - 1)) | ||
deepResult[dim] = path ? r.value[path] : r.value; | ||
deepResult = deepResult[dim]; | ||
}); | ||
return res; | ||
}, {}); | ||
}, setResult: function (result) { | ||
instanceResult._result = result; | ||
return instanceResult; | ||
} }); | ||
return instanceResult.setResult(walker(queryPath.split("."), object)); | ||
return new InstanceResult(walker(queryPath.split("."), object)); | ||
}, | ||
setResult: function (result) { | ||
return new InstanceResult(result); | ||
} | ||
@@ -115,3 +136,3 @@ }; | ||
if (i === void 0) { i = 0; } | ||
if (pathArray.length == 0) { | ||
if (result && pathArray.length == 0) { | ||
result[i].value = object; | ||
@@ -131,4 +152,7 @@ return result; | ||
newResults.dimensions.push(key); | ||
var resultIndex = result.length; | ||
result[resultIndex] = newResults; | ||
var resultIndex; | ||
if (result) { | ||
resultIndex = result.length; | ||
result[resultIndex] = newResults; | ||
} | ||
return walker(__spreadArray([], pathArray, true), object[key], result, resultIndex); | ||
@@ -138,3 +162,3 @@ }); | ||
} | ||
return walker(pathArray, object[step], result, i).filter(filterUndefineds); | ||
return walker(pathArray, object[step || ''], result, i).filter(filterUndefineds); | ||
; | ||
@@ -141,0 +165,0 @@ } |
@@ -5,6 +5,97 @@ | ||
} | ||
type DeepwalkerResult = Deepwalker & { | ||
type DeepwalkerResult = InstanceResult & { | ||
toValues: Function | ||
} | ||
class InstanceResult { | ||
result: object; | ||
constructor(result: object) { | ||
this.result = result; | ||
} | ||
toValue = function (this: any, path: any) { | ||
return path ? this?._result[0]?.value[path] : this?.result[0]?.value; | ||
} | ||
filter = function (this: any, filterFn: Function) { | ||
return this.setResult(this.result.filter(filterFn)) | ||
} | ||
sort = function (this: any, sortFn: Function) { | ||
this.result.sort(sortFn) | ||
return this; | ||
} | ||
pick = function (this: any, array: Array<any>) { | ||
this.result = this.result.map((el) => { | ||
return { | ||
...el, | ||
value: Object.fromEntries( | ||
Object.entries(el.value) | ||
.filter(([key]) => array.includes(key)) | ||
) | ||
} | ||
}) | ||
return this; | ||
} | ||
flatten = function (this: any, array: any = []) { | ||
this.result = this.result.reduce((result, el) => { | ||
const arrayOfValues = Object.keys(el.value).filter(key => array.includes(key)).map((key) => { | ||
return { | ||
dimensions: el.dimensions.concat(key), value: { | ||
...Object.fromEntries( | ||
Object.entries(el.value) | ||
.filter(([key]) => !array.includes(key)) | ||
), | ||
value: el.value[key] | ||
} | ||
} | ||
}) | ||
return result.concat(arrayOfValues); | ||
}, []); | ||
return this; | ||
} | ||
concat = function (this: any, instance) { | ||
this.result = this.result.concat(instance.result); | ||
return this; | ||
} | ||
slice = function (this: any, number) { | ||
this.result = this.result.slice(0, number) | ||
return this; | ||
} | ||
toValues = function (this: any) { | ||
return this.result.map(r => r.value); | ||
} | ||
toString = function (this: any, transformer) { | ||
if (!this.result || this.result.length == 0) return ''; | ||
return transformer(createResultsObject(this.result)) | ||
} | ||
convertEach = function (this: any, converter) { | ||
if (!this.result || this.result.length == 0) return []; | ||
return this.result.map(converter); | ||
} | ||
haveResults = function (this: any) { | ||
if (!this.result || this.result.length == 0) return false; | ||
return true; | ||
} | ||
toMap = function (this: any, path) { | ||
return this.result.reduce((res, r, i) => { | ||
let deepResult = res; | ||
r.dimensions.forEach((dim, i) => { | ||
if (!deepResult[dim] && i !== (r.dimensions.length - 1)) { | ||
deepResult[dim] = {}; | ||
} | ||
if (i === (r.dimensions.length - 1)) deepResult[dim] = path ? r.value[path] : r.value; | ||
deepResult = deepResult[dim]; | ||
}) | ||
return res; | ||
}, {}); | ||
} | ||
setResult = function (this: any, result) { | ||
this.result = result; | ||
return this; | ||
} | ||
} | ||
export function deepwalker(object: any): Deepwalker { | ||
@@ -15,90 +106,6 @@ const instance = { | ||
get: function (queryPath: string): DeepwalkerResult { | ||
const instanceResult = { | ||
_result: null, | ||
...instance, | ||
toValue: function (path) { | ||
return path ? instanceResult?._result[0]?.value[path] : instanceResult?._result[0]?.value; | ||
}, | ||
filter: function (filterFn) { | ||
return instanceResult.setResult(instanceResult._result.filter(filterFn)) | ||
}, | ||
sort: function (sortFn) { | ||
instanceResult._result.sort(sortFn) | ||
return instanceResult; | ||
}, | ||
pick: function (array) { | ||
instanceResult._result = instanceResult._result.map((el) => { | ||
return { | ||
...el, | ||
value: Object.fromEntries( | ||
Object.entries(el.value) | ||
.filter(([key]) => array.includes(key)) | ||
) | ||
} | ||
}) | ||
return instanceResult; | ||
}, | ||
flatten: function (array = []) { | ||
instanceResult._result = instanceResult._result.reduce((result, el) => { | ||
const arrayOfValues = Object.keys(el.value).filter(key => array.includes(key)).map((key) => { | ||
return { | ||
dimensions: el.dimensions.concat(key), value: { | ||
...Object.fromEntries( | ||
Object.entries(el.value) | ||
.filter(([key]) => !array.includes(key)) | ||
), | ||
value: el.value[key] | ||
} | ||
} | ||
}) | ||
return result.concat(arrayOfValues); | ||
}, []); | ||
return instanceResult; | ||
}, | ||
concat: function (instance) { | ||
instanceResult._result = instanceResult._result.concat(instance._result); | ||
return instanceResult; | ||
}, | ||
slice: function (number) { | ||
instanceResult._result = instanceResult._result.slice(0, number) | ||
return instanceResult; | ||
}, | ||
toValues: function () { | ||
return instanceResult._result.map(r => r.value); | ||
}, | ||
toString: function (transformer) { | ||
if (!instanceResult._result || instanceResult._result.length == 0) return ''; | ||
return transformer(createResultsObject(instanceResult._result)) | ||
}, | ||
convertEach: function (converter) { | ||
if (!instanceResult._result || instanceResult._result.length == 0) return []; | ||
return instanceResult._result.map(converter); | ||
}, | ||
haveResults: function () { | ||
if (!instanceResult._result || instanceResult._result.length == 0) return false; | ||
return true; | ||
}, | ||
toMap: function (path) { | ||
return instanceResult._result.reduce((res, r, i) => { | ||
let deepResult = res; | ||
r.dimensions.forEach((dim, i) => { | ||
if (!deepResult[dim] && i !== (r.dimensions.length - 1)) { | ||
deepResult[dim] = {}; | ||
} | ||
if (i === (r.dimensions.length - 1)) deepResult[dim] = path ? r.value[path] : r.value; | ||
deepResult = deepResult[dim]; | ||
}) | ||
return res; | ||
}, {}); | ||
}, | ||
setResult: function (result) { | ||
instanceResult._result = result; | ||
return instanceResult; | ||
} | ||
} | ||
return instanceResult.setResult(walker(queryPath.split("."), object)); | ||
return new InstanceResult(walker(queryPath.split("."), object)); | ||
}, | ||
setResult: function (result: object) { | ||
return new InstanceResult(result); | ||
} | ||
@@ -127,3 +134,3 @@ | ||
function walker(pathArray: Array<string>, object: any, result?: Array<DeepResult>, i: number = 0): Array<DeepResult> { | ||
if (pathArray.length == 0) { | ||
if (result && pathArray.length == 0) { | ||
result[i].value = object; | ||
@@ -141,4 +148,7 @@ return result; | ||
newResults.dimensions.push(key); | ||
const resultIndex = result.length; | ||
result[resultIndex] = newResults; | ||
let resultIndex; | ||
if (result) { | ||
resultIndex = result.length; | ||
result[resultIndex] = newResults; | ||
} | ||
return walker([...pathArray], object[key], result, resultIndex); | ||
@@ -149,3 +159,3 @@ }) | ||
return walker(pathArray, object[step], result, i).filter(filterUndefineds);; | ||
return walker(pathArray, object[step || ''], result, i).filter(filterUndefineds);; | ||
} | ||
@@ -152,0 +162,0 @@ |
{ | ||
"name": "deepwalker", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "deepwalker.js", |
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
7
416
14276