Skip to content

Commit

Permalink
feat(utils): 新增 makeConditionalArray
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed May 30, 2023
1 parent 88240c5 commit 6f87b8f
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export * from './loadCss'
export * from './loadResource'
export * from './LocalStoragePlus'
export * from './loopUntil'
export * from './makeConditionalArray'
export * from './makeEnum'
export * from './md5'
export * from './MiniProgramUrl'
Expand Down
130 changes: 130 additions & 0 deletions src/utils/makeConditionalArray.test.ts
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: [],
},
],
},
])
})
})
51 changes: 51 additions & 0 deletions src/utils/makeConditionalArray.ts
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

0 comments on commit 6f87b8f

Please sign in to comment.