mirror of https://github.com/ghostfolio/ghostfolio
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.3 KiB
60 lines
1.3 KiB
import { isArray } from './_internals/isArray.js'
|
|
import { mapArray, mapObject } from './map.js'
|
|
import { type } from './type.js'
|
|
|
|
export function evolveArray(rules, list){
|
|
return mapArray(
|
|
(x, i) => {
|
|
if (type(rules[ i ]) === 'Function'){
|
|
return rules[ i ](x)
|
|
}
|
|
|
|
return x
|
|
},
|
|
list,
|
|
true
|
|
)
|
|
}
|
|
|
|
export function evolveObject(rules, iterable){
|
|
return mapObject((x, prop) => {
|
|
if (type(x) === 'Object'){
|
|
const typeRule = type(rules[ prop ])
|
|
if (typeRule === 'Function'){
|
|
return rules[ prop ](x)
|
|
}
|
|
if (typeRule === 'Object'){
|
|
return evolve(rules[ prop ], x)
|
|
}
|
|
|
|
return x
|
|
}
|
|
if (type(rules[ prop ]) === 'Function'){
|
|
return rules[ prop ](x)
|
|
}
|
|
|
|
return x
|
|
}, iterable)
|
|
}
|
|
|
|
export function evolve(rules, iterable){
|
|
if (arguments.length === 1){
|
|
return _iterable => evolve(rules, _iterable)
|
|
}
|
|
const rulesType = type(rules)
|
|
const iterableType = type(iterable)
|
|
|
|
if (iterableType !== rulesType){
|
|
throw new Error('iterableType !== rulesType')
|
|
}
|
|
|
|
if (![ 'Object', 'Array' ].includes(rulesType)){
|
|
throw new Error(`'iterable' and 'rules' are from wrong type ${ rulesType }`)
|
|
}
|
|
|
|
if (iterableType === 'Object'){
|
|
return evolveObject(rules, iterable)
|
|
}
|
|
|
|
return evolveArray(rules, iterable)
|
|
}
|
|
|