Skip to content

Commit 4d83ff9

Browse files
committed
feat: [array] 增加listFill方法用于数组填充
1 parent 3ac4f6e commit 4d83ff9

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

play/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
},
99
"dependencies": {
1010
"@iconify-json/carbon": "^1.1.25",
11-
"@vueuse/core": "^10.7.0",
11+
"@vueuse/core": "^10.11.1",
1212
"vue": "^3.3.11",
1313
"vue-toastification": "2.0.0-rc.5"
1414
},

src/array/base.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export function flat<T>(lists: readonly T[][]): T[] {
3939
* @param startOrLength 开始值或者长度
4040
* @param end 结束值
4141
* @param valueOrMapper 指定值或方法
42+
* @param step 步长
4243
* @returns Array 生成好的值
4344
* @example
4445
* list(3) // 0, 1, 2, 3
@@ -151,3 +152,53 @@ export function arrayFirst(list: any, defaultVal?: any) {
151152
export function arrayLast(list: any, defaultVal?: any) {
152153
return list?.[list.length - 1] ?? defaultVal
153154
}
155+
156+
export interface DataInfoOptions {
157+
base?: any[]
158+
fillData?: any | Function
159+
isIncludeBase?: boolean
160+
num?: number
161+
}
162+
/**
163+
* @description 数组填充
164+
* @param dataInfo 填充数据对象
165+
* @returns Array 处理好的数组
166+
* ```
167+
* const fillData = (i: number) => {
168+
* return {
169+
* add: i,
170+
* };
171+
*};
172+
* const newList = listFill({
173+
* base: [{ base: 1 }, { base: 2 }],
174+
* fillData,
175+
* isIncludeBase: true,
176+
* num: 4,
177+
* });
178+
* // [
179+
* // {
180+
* // "base": 1,
181+
* // "add": 0
182+
* // },
183+
* // {
184+
* // "base": 2,
185+
* // "add": 1
186+
* // },
187+
* // {
188+
* // "add": 2
189+
* // },
190+
* // {
191+
* // "add": 3
192+
* // }
193+
* // ]
194+
* ```
195+
*/
196+
export function listFill(dataInfo: DataInfoOptions): any[] {
197+
const { base = [], fillData = {}, isIncludeBase = false, num = 0 } = dataInfo
198+
const mapper = isFunction(fillData) ? fillData : () => fillData
199+
const baseNum = base.length >= num ? base.length : num
200+
const createNum = baseNum - 1 >= 0 ? baseNum - 1 : 0
201+
return list(createNum).map((_, index) => {
202+
return index < base.length ? (isIncludeBase ? { ...base[index], ...mapper(index) } : base[index]) : mapper(index)
203+
})
204+
}

0 commit comments

Comments
 (0)