-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utils): 新增 makeConditionalArray
- Loading branch information
Showing
3 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import { makeConditionalArray } from './makeConditionalArray' | ||
|
||
describe('makeConditionalArray', () => { | ||
test('ok', () => { | ||
type Item = { | ||
id: number | ||
} | ||
expect( | ||
makeConditionalArray([ | ||
{ | ||
id: 1, | ||
}, | ||
false, | ||
{ | ||
id: 4, | ||
}, | ||
] as Item[]), | ||
).toEqual([ | ||
{ | ||
id: 1, | ||
}, | ||
{ | ||
id: 4, | ||
}, | ||
]) | ||
|
||
const makeConditionalArrayFork = makeConditionalArray.fork<Item>() | ||
expect( | ||
makeConditionalArrayFork([ | ||
{ | ||
id: 1, | ||
}, | ||
false, | ||
{ | ||
id: 4, | ||
}, | ||
{ | ||
id: 6, | ||
}, | ||
false, | ||
false, | ||
{ | ||
id: 0, | ||
}, | ||
]), | ||
).toEqual([ | ||
{ | ||
id: 1, | ||
}, | ||
{ | ||
id: 4, | ||
}, | ||
{ | ||
id: 6, | ||
}, | ||
{ | ||
id: 0, | ||
}, | ||
]) | ||
}) | ||
|
||
test('children', () => { | ||
type Item = { | ||
id: number | ||
sub: Item[] | ||
} | ||
const makeConditionalArrayFork = makeConditionalArray.fork<Item, 'sub'>( | ||
'sub', | ||
) | ||
expect( | ||
makeConditionalArrayFork([ | ||
{ | ||
id: 1, | ||
sub: [], | ||
}, | ||
false, | ||
{ | ||
id: 4, | ||
sub: [], | ||
}, | ||
{ | ||
id: 6, | ||
sub: [], | ||
}, | ||
false, | ||
false, | ||
{ | ||
id: 0, | ||
sub: [ | ||
{ | ||
id: 100, | ||
sub: [], | ||
}, | ||
false, | ||
{ | ||
id: 101, | ||
sub: [], | ||
}, | ||
], | ||
}, | ||
]), | ||
).toEqual([ | ||
{ | ||
id: 1, | ||
sub: [], | ||
}, | ||
{ | ||
id: 4, | ||
sub: [], | ||
}, | ||
{ | ||
id: 6, | ||
sub: [], | ||
}, | ||
{ | ||
id: 0, | ||
sub: [ | ||
{ | ||
id: 100, | ||
sub: [], | ||
}, | ||
{ | ||
id: 101, | ||
sub: [], | ||
}, | ||
], | ||
}, | ||
]) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { IsNever, Merge } from '../types' | ||
|
||
export type ConditionalArrayItem<T, K extends keyof T = never> = | ||
| (IsNever<K> extends true | ||
? T | ||
: Merge< | ||
T, | ||
{ | ||
[k in K]: Array<ConditionalArrayItem<T, K>> | ||
} | ||
>) | ||
| false | ||
|
||
/** | ||
* 构造条件数组,即允许一个数组的项出现 false 值,且最终 false 值的项目会被排除。 | ||
* | ||
* @param array 数组 | ||
* @param childrenKey 子数组键 | ||
*/ | ||
export function makeConditionalArray<T extends any, K extends keyof T>( | ||
array: Array<ConditionalArrayItem<T, K>>, | ||
childrenKey: K, | ||
): T[] | ||
/** | ||
* 构造条件数组,即允许一个数组的项出现 false 值,且最终 false 值的项目会被排除。 | ||
* | ||
* @param array 数组 | ||
*/ | ||
export function makeConditionalArray<T extends any>( | ||
array: Array<ConditionalArrayItem<T>>, | ||
): T[] | ||
export function makeConditionalArray(array: any[], childrenKey?: any): any[] { | ||
return array.filter(function filter(item) { | ||
if (item && childrenKey && Array.isArray(item[childrenKey])) { | ||
item[childrenKey] = (item[childrenKey] as any).filter(filter) | ||
} | ||
return !!item | ||
}) as any | ||
} | ||
|
||
function makeConditionalArrayFork<T extends any>(): ( | ||
array: Array<ConditionalArrayItem<T>>, | ||
) => T[] | ||
function makeConditionalArrayFork<T extends any, K extends keyof T>( | ||
childrenKey: K, | ||
): (array: Array<ConditionalArrayItem<T, K>>) => T[] | ||
function makeConditionalArrayFork(childrenKey?: any) { | ||
// @ts-ignore | ||
return (array: any) => makeConditionalArray(array, childrenKey) | ||
} | ||
makeConditionalArray.fork = makeConditionalArrayFork |