@@ -39,6 +39,7 @@ export function flat<T>(lists: readonly T[][]): T[] {
39
39
* @param startOrLength 开始值或者长度
40
40
* @param end 结束值
41
41
* @param valueOrMapper 指定值或方法
42
+ * @param step 步长
42
43
* @returns Array 生成好的值
43
44
* @example
44
45
* list(3) // 0, 1, 2, 3
@@ -151,3 +152,53 @@ export function arrayFirst(list: any, defaultVal?: any) {
151
152
export function arrayLast ( list : any , defaultVal ?: any ) {
152
153
return list ?. [ list . length - 1 ] ?? defaultVal
153
154
}
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