-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fixing flatMap fields mapping for one field flatting (#62)
- Loading branch information
1 parent
a118469
commit e50818c
Showing
4 changed files
with
160 additions
and
142 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,130 +1,64 @@ | ||
import { tail, head } from './../types/base'; | ||
import { isIterable } from '../is-iterable'; | ||
import { CombineIngredients, Ingredient } from './ingredients'; | ||
import { AnyIterable } from 'augmentative-iterable'; | ||
import { CombineIngredients } from './ingredients'; | ||
|
||
function flattable(value: unknown) { | ||
return typeof value !== 'string' && isIterable(value); | ||
} | ||
|
||
function getNextIterable( | ||
current: any, | ||
path: string[], | ||
nextIndex: number, | ||
): [any, number] { | ||
let item = current; | ||
if (flattable(item)) { | ||
return [item, nextIndex]; | ||
function fillField(result: any, field: string, subItem: any) { | ||
if (!result.hasOwnProperty(field)) { | ||
result[field] = subItem; | ||
} | ||
|
||
while (nextIndex < path.length) { | ||
item = item[path[nextIndex]]; | ||
nextIndex++; | ||
if (flattable(item)) { | ||
break; | ||
} | ||
} | ||
return [item, nextIndex]; | ||
} | ||
|
||
function fillMiddleFieldsFactory( | ||
oldItem: any, | ||
start: number, | ||
nextIndex: number, | ||
path: string[], | ||
) { | ||
return (x: any) => { | ||
let current = oldItem; | ||
for (let i = start; i < nextIndex; i++) { | ||
current = current[path[i]]; | ||
if (!flattable(current)) { | ||
x[path[i]] = current; | ||
} | ||
} | ||
|
||
return x; | ||
}; | ||
} | ||
|
||
function fillMiddleToHeadFieldsFactory( | ||
oldItem: any, | ||
start: number, | ||
nextIndex: number, | ||
path: string[], | ||
) { | ||
const fillMiddleFields = fillMiddleFieldsFactory( | ||
oldItem, | ||
start, | ||
nextIndex, | ||
path, | ||
); | ||
return (sub: any) => { | ||
const newItem = fillMiddleFields({}); | ||
newItem[head] = sub; | ||
return newItem; | ||
function getNewResult(result: any, field: string, subItem: any): any { | ||
result = { | ||
...result, | ||
}; | ||
fillField(result, field, subItem); | ||
return result; | ||
} | ||
|
||
function mapRoot( | ||
iterable: AnyIterable<any>, | ||
map: Ingredient, | ||
key: string | number | symbol, | ||
value: unknown, | ||
) { | ||
return map.call(iterable, (x) => { | ||
x[key] = value; | ||
return x; | ||
}); | ||
} | ||
|
||
function flatMap( | ||
item: any, | ||
ingredients: CombineIngredients, | ||
path: string[], | ||
nextIndex: number, | ||
): any { | ||
const oldItem = item; | ||
const { flatten, map } = ingredients; | ||
const start = nextIndex; | ||
const result = getNextIterable(item, path, nextIndex); | ||
item = result[0]; | ||
nextIndex = result[1]; | ||
function flatMapFactory(ingredients: CombineIngredients, deepFlat: Function) { | ||
return function flatMap( | ||
result: any, | ||
item: any, | ||
path: string[], | ||
nextIndex: number, | ||
): any { | ||
const { flatten } = ingredients; | ||
const start = nextIndex; | ||
|
||
if (nextIndex < path.length) { | ||
return flatten.call(item, (sub: any) => { | ||
const mapped = map.call( | ||
flatMap(sub, ingredients, path, nextIndex), | ||
fillMiddleFieldsFactory(oldItem, start, nextIndex, path), | ||
); | ||
return mapRoot( | ||
mapped, | ||
map, | ||
nextIndex > 0 ? path[nextIndex - 1] : tail, | ||
sub, | ||
); | ||
}); | ||
} | ||
|
||
const fillMiddleToHeadItems = fillMiddleToHeadFieldsFactory( | ||
oldItem, | ||
start, | ||
nextIndex, | ||
path, | ||
); | ||
let current: any = item; | ||
for (let i = start; i < path.length; i++) { | ||
const field = path[i]; | ||
current = current[field]; | ||
if (flattable(current)) { | ||
return flatten.call(deepFlat(current), (subItem: any) => | ||
flatMap(getNewResult(result, field, subItem), subItem, path, i + 1), | ||
); | ||
} | ||
fillField(result, field, current); | ||
} | ||
result[head] = current; | ||
|
||
return flattable(item) | ||
? map.call(item, fillMiddleToHeadItems) | ||
: [fillMiddleToHeadItems(item)]; | ||
return [result]; | ||
}; | ||
} | ||
|
||
export function flatJoinRecipe(ingredients: CombineIngredients) { | ||
const { map, iterateAll } = ingredients; | ||
const { flatten } = ingredients; | ||
const deepFlat = (x: Iterable<any>): any => | ||
flatten.call(x, (y: any) => (flattable(y) ? deepFlat(y) : [y])); | ||
const flatMap = flatMapFactory(ingredients, deepFlat); | ||
return function <T, R>(this: Iterable<T>, ...path: string[]): Iterable<R> { | ||
return iterateAll( | ||
map.call(this, (x) => { | ||
const flatted = flatMap(x, ingredients, path, 0); | ||
return flattable(x) ? flatted : mapRoot(flatted, map, tail, x); | ||
}), | ||
); | ||
function process(y: any): any { | ||
return flatMap({ [tail]: y }, y, path, 0); | ||
} | ||
|
||
return flatten.call(this, (x) => | ||
flattable(x) ? flatten.call(deepFlat(x), process) : process(x), | ||
) as any; | ||
}; | ||
} |
Oops, something went wrong.