Comparing version 1.0.12 to 1.0.13
{ | ||
"name": "js-awe", | ||
"version": "1.0.12", | ||
"version": "1.0.13", | ||
"homepage": "https://github.com/josuamanuel/js-awe", | ||
"author": "josuamanuel@hotmail.com", | ||
"description": "", | ||
"repository": "github:josuamanuel/jsTrading", | ||
"bugs":{ | ||
"url" : "https://github.com/josuamanuel/jsTrading/issues", | ||
"email" : "josuamanuel@hotmail.com" | ||
}, | ||
"description": "Awesome js utils and constructs", | ||
"type": "module", | ||
@@ -8,0 +13,0 @@ "main": "src/index.js", |
@@ -404,3 +404,5 @@ 'use strict' | ||
// reviver is called for each node as: reviver(nodeRef, currentPath, parent). For example: currentPath=['root', 'parent', 'son', '0', 'jose'] | ||
// reviver is called for each node as: reviver(nodeRef, currentPath, parent, key). | ||
// For example: being objIni={a:{b:{c:3}},d:4} the reviver to call node a.b will be | ||
// reviver({c:3}, ['a','b'], {b:{c:3}}, 'b') currentPath=['root', 'parent', 'son', '0', 'jose'] | ||
// reviver return value will impact traverse: | ||
@@ -490,2 +492,33 @@ // undefined: do nothing. | ||
function traverseVertically(functionToRun, verFields, toTraverse) | ||
{ | ||
if(Array.isArray(toTraverse) === false) return | ||
let runIndex = 0 | ||
let maxLength = 0 | ||
let firstTime = true | ||
for(let i = 0; firstTime || i < maxLength; i++) | ||
{ | ||
let toReturn = [] | ||
for(let j = 0; j< toTraverse.length; j++) | ||
{ | ||
toReturn[j] = {...toTraverse[j]} | ||
for(const field of verFields) | ||
{ | ||
if( firstTime) { | ||
maxLength = toTraverse[j]?.[field]?.length > maxLength | ||
? toTraverse[j][field].length | ||
: maxLength | ||
} | ||
toReturn[j][field] = toTraverse[j]?.[field]?.[i] | ||
} | ||
} | ||
if(maxLength !== 0) { | ||
functionToRun(toReturn, runIndex) | ||
runIndex++ | ||
} | ||
firstTime = false | ||
} | ||
} | ||
function copyPropsWithValueUsingRules(objDest, copyRules, shouldUpdateOnlyEmptyFields = false) { | ||
@@ -1597,2 +1630,3 @@ | ||
traverse, | ||
traverseVertically, | ||
copyPropsWithValue, | ||
@@ -1658,2 +1692,3 @@ copyPropsWithValueUsingRules, | ||
traverse, | ||
traverseVertically, | ||
copyPropsWithValue, | ||
@@ -1660,0 +1695,0 @@ copyPropsWithValueUsingRules, |
import { strict as assert } from 'assert' | ||
import { EnumMap, Enum, formatDate, CustomError, findDeepKey, traverse } from '../src/jsUtils.js' | ||
import { EnumMap, Enum, formatDate, CustomError, findDeepKey, traverse, traverseVertically } from '../src/jsUtils.js' | ||
import clone from 'just-clone' | ||
@@ -635,2 +635,169 @@ | ||
//traverseVertically() | ||
const subjectTV = [ | ||
{ | ||
name:'apple', | ||
avge:[ | ||
{ | ||
date:"2022-01-01", | ||
close: 22.1 | ||
}, | ||
{ | ||
date:"2022-01-02", | ||
} | ||
], | ||
hist:[ | ||
{ | ||
date:"2022-01-01", | ||
close: 22.1 | ||
}, | ||
{ | ||
date:"2022-01-02", | ||
} | ||
] | ||
}, | ||
{ | ||
name:'microsoft', | ||
avge:[ | ||
{ | ||
date:"2022-01-01", | ||
close: 22.1 | ||
}, | ||
{ | ||
date:"2022-01-02", | ||
} | ||
], | ||
hist:[ | ||
{ | ||
date:"2022-01-01", | ||
}, | ||
{ | ||
date:"2022-01-02", | ||
}, | ||
{ | ||
date:"2022-01-03", | ||
close:99 | ||
} | ||
] | ||
} | ||
] | ||
it('traverseVertically a complex array', () => { | ||
let calledWithIndex = [] | ||
traverseVertically( | ||
(verticalSlice, runIndex)=> { | ||
calledWithIndex[runIndex] = true | ||
if(runIndex === 0) { | ||
assert.deepEqual( | ||
verticalSlice, | ||
[ | ||
{ | ||
name:'apple', | ||
avge:{ date:"2022-01-01", close: 22.1}, | ||
hist:{ date:"2022-01-01", close: 22.1}, | ||
}, | ||
{ | ||
name:'microsoft', | ||
avge: { date:"2022-01-01", close: 22.1 }, | ||
hist: { date:"2022-01-01"}, | ||
} | ||
] | ||
) | ||
} | ||
if(runIndex === 1) { | ||
assert.deepEqual( | ||
verticalSlice, | ||
[ | ||
{ | ||
name:'apple', | ||
avge: { date:"2022-01-02" }, | ||
hist: { date:"2022-01-02" } | ||
}, | ||
{ | ||
name:'microsoft', | ||
avge: { date:"2022-01-02" }, | ||
hist: { date:"2022-01-02" } | ||
} | ||
] | ||
) | ||
} | ||
if(runIndex === 2) { | ||
assert.deepEqual( | ||
verticalSlice, | ||
[ | ||
{ | ||
name:'apple', | ||
avge:undefined, | ||
hist:undefined, | ||
}, | ||
{ | ||
name:'microsoft', | ||
avge: undefined, | ||
hist: { date:"2022-01-03", close:99}, | ||
} | ||
] | ||
) | ||
} | ||
}, | ||
['avge','hist'], | ||
subjectTV | ||
) | ||
assert.deepEqual(calledWithIndex, [true,true,true], 'Index for false values indicates that functionToRun was omitted and over run') | ||
}) | ||
it('traverseVertically an undefined object', () => | ||
traverseVertically( | ||
()=> {assert.fail('It should not have called functionToRun')}, | ||
['result'], | ||
undefined | ||
) | ||
) | ||
it('traverseVertically an empty array', () => | ||
traverseVertically( | ||
()=> {assert.fail('It should not have called functionToRun')}, | ||
['result'], | ||
[] | ||
) | ||
) | ||
it('traverseVertically an array with all vertical fields not being arrays', () => | ||
traverseVertically( | ||
()=> {assert.fail('It should not have called functionToRun')}, | ||
['result'], | ||
[{result:12}] | ||
) | ||
) | ||
it('traverseVertically an array with null, empty objects, and one that has a vertical field match', () => { | ||
let wasCalled = false | ||
traverseVertically( | ||
(verticalSlice, runIndex) => { | ||
if(runIndex === 0) { | ||
wasCalled = true | ||
assert.deepEqual( | ||
verticalSlice, | ||
[ | ||
{ hist: undefined, avge: undefined }, | ||
{ hist: undefined, avge: undefined }, | ||
{ avge: undefined, hist: undefined }, | ||
{ hist: undefined, avge: undefined }, | ||
{ name: 'apple', hist: 12, avge: undefined } | ||
] | ||
) | ||
} | ||
}, | ||
['hist', 'avge'], | ||
[{},{hist:undefined},{avge:[]}, null,{name:'apple',hist:[12]}] | ||
) | ||
if(wasCalled === false) assert.fail('It should have called functionToRun passed as argument') | ||
}) | ||
}) |
@@ -128,3 +128,17 @@ export default jsUtils; | ||
export function removeDuplicates(arr: any): any[]; | ||
export function traverse(objIni: any, reviver: any, pureFunction?: boolean): any; | ||
/** | ||
* Access each node of the object calling the reviver function. Reviver can return: | ||
* undefined: no change | ||
* traverse.skip: stop accessing subtrees | ||
* traverse.stop: stop completly the object traverse | ||
* traverse.delete: delete the current node | ||
* | ||
* @param objIni The object to traverse. | ||
* @param reviver The function to be called for each node | ||
* @pureFunction true: work with a deepClone of objIni, false: work with objIni passed as a parameter. | ||
* @returns Return the object after applying reviver actions | ||
*/ | ||
export function traverse(objIni: any, reviver: (value:any, path:string[], parent:any, prop:string) => any, pureFunction?: boolean): any | ||
export namespace traverse { | ||
@@ -136,2 +150,25 @@ export const skip: symbol; | ||
} | ||
/** | ||
* Takes vertifical slices of a two nested array and calls functionToRun with each slice | ||
* @example | ||
* ``` | ||
* traverseVertically ( | ||
* functionToRun | ||
* ['marathonResults'], // List of fields for the second level nested array | ||
* [{name:'Jose',marathonResults:[12,35]},{name:'Ana',marathonResults:[1]}] | ||
* ) | ||
* ``` | ||
* This will produce folllowing calls | ||
* ``` | ||
* functionToRun([{name:'Jose', marathonResults:12}, {name:'Ana',marathonResults:1}]) | ||
* functionToRun([{name:'Jose', marathonResults:35}, {name:'Ana',marathonResults:undefined}]) | ||
* ``` | ||
* | ||
* @param functionToRun This function will be called for each vertical slice. | ||
* @param verFields Fields inside each item of the traverse array that contains the second nested arrays to iterate vertically. | ||
* @param toTraverse: Array to be traverse | ||
* @returns void | ||
*/ | ||
export function traverseVertically(functionToRun: (verticalSlice: object, runIndex:number) => any, verFields:string[], toTraverse:any[]):void | ||
export function copyPropsWithValue(objDest: any, shouldUpdateOnlyEmptyFields?: boolean): (input: any) => any; | ||
@@ -138,0 +175,0 @@ export function copyPropsWithValueUsingRules(objDest: any, copyRules: any, shouldUpdateOnlyEmptyFields?: boolean): (inputObj: any) => any; |
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
219996
6633