Skip to content

Commit

Permalink
feat: add fill
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Oct 16, 2018
1 parent e6886ca commit 3880400
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/fill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* 使用 value 来填充(替换) arr,从 start 位置开始, 到 end 位置结束(但不包括 end 位置)。
*
* @param arr 要填充改变的数组
* @param [value] 填充(替换)值
* @param [start=0] 开始位置
* @param [end=arr.length] 结束位置
* @returns 填充改变后的数组
*/
export default function fill(arr: any[], value?: any, start: number = 0, end: number = arr.length): any[] {
const newArr = arr.slice()
const arrLength = newArr.length
start = Math.max(0, start < 0 ? arrLength + start : start)
end = Math.min(arrLength, end < 0 ? arrLength + end : end)
while (start < end) {
newArr[start++] = value
}
return newArr
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export { default as castArray } from './castArray'
export { default as clamp } from './clamp'
export { default as cssTransform } from './cssTransform'
export { default as Disposer } from './Disposer'
export { default as fill } from './fill'
export { default as forOwn } from './forOwn'
export { default as getType } from './getType'
export { default as inBrowser } from './inBrowser'
Expand Down
48 changes: 48 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,3 +563,51 @@ describe('shuffle', () => {
expect(vtils.shuffle(arr2).sort()).toEqual(arr2.sort())
})
})

describe('fill', () => {
const emptyArr = Array(3)
const arr4 = [1, 2, 3, 4]
test('默认 start 为 0,end 为 arr.length', () => {
expect(vtils.fill(emptyArr, '*')).toEqual(['*', '*', '*'])
expect(vtils.fill(arr4, '*')).toEqual(['*', '*', '*', '*'])
})
test('value 未指定则为 undefined', () => {
expect(vtils.fill(emptyArr)).toEqual([undefined, undefined, undefined])
expect(vtils.fill(arr4)).toEqual([undefined, undefined, undefined, undefined])
})
test('start 为正数', () => {
expect(vtils.fill(emptyArr, '*', 1)).toEqual([undefined, '*', '*'])
expect(vtils.fill(arr4, '*', 1)).toEqual([1, '*', '*', '*'])
})
test('start 为正数且大于或等于 arr.length', () => {
expect(vtils.fill(arr4, '*', 4)).toEqual(arr4)
expect(vtils.fill(arr4, '*', 5)).toEqual(arr4)
expect(vtils.fill(arr4, '*', 60)).toEqual(arr4)
})
test('start 为负数', () => {
expect(vtils.fill(emptyArr, '*', -1)).toEqual([undefined, undefined, '*'])
expect(vtils.fill(arr4, '*', -2)).toEqual([1, 2, '*', '*'])
})
test('start 为负数且小于或等于 -arr.length', () => {
expect(vtils.fill(emptyArr, '*', -3)).toEqual(['*', '*', '*'])
expect(vtils.fill(arr4, '*', -4)).toEqual(['*', '*', '*', '*'])
expect(vtils.fill(arr4, '*', -5)).toEqual(['*', '*', '*', '*'])
})
test('end 为正数', () => {
expect(vtils.fill(emptyArr, '*', 1, 2)).toEqual([undefined, '*', undefined])
expect(vtils.fill(arr4, '*', 1, 2)).toEqual([1, '*', 3, 4])
})
test('end 为正数且大于或等于 arr.length', () => {
expect(vtils.fill(arr4, '*', 1, 4)).toEqual([1, '*', '*', '*'])
expect(vtils.fill(arr4, '*', 1, 7)).toEqual([1, '*', '*', '*'])
expect(vtils.fill(arr4, '*', 1, Infinity)).toEqual([1, '*', '*', '*'])
})
test('end 为负数', () => {
expect(vtils.fill(emptyArr, '*', -2, -1)).toEqual([undefined, '*', undefined])
expect(vtils.fill(arr4, '*', 1, -1)).toEqual([1, '*', '*', 4])
})
test('end 小于或等于 start', () => {
expect(vtils.fill(emptyArr, '*', 3, 2)).toEqual([undefined, undefined, undefined])
expect(vtils.fill(emptyArr, '*', -2, -3)).toEqual([undefined, undefined, undefined])
})
})

0 comments on commit 3880400

Please sign in to comment.