Skip to content

Commit

Permalink
feat: add range
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Nov 8, 2018
1 parent dda5200 commit a0d1a8a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export { default as parseCssValue } from './parseCssValue'
export { default as pick } from './pick'
export { default as preventEventDefault } from './preventEventDefault'
export { default as randomString } from './randomString'
export { default as range } from './range'
export { default as reduce } from './reduce'
export { default as repeat } from './repeat'
export { default as request } from './request'
Expand Down
27 changes: 27 additions & 0 deletions src/range.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 创建一个包含从 start 到 end,但不包含 end 本身范围数字的数组。
*
* @param start 开始数字
* @param [end] 结束数字
* @param [step] 步进值
* @returns 开始到结束范围内数字组成的数组
*/
export default function range(start: number, end?: number, step?: number): number[] {
if (arguments.length === 1) {
if (start > 0) {
end = start
start = 0
} else {
end = 0
}
step = 1
} else {
step = step == null ? 1 : step
}
const result: number[] = []
while (start < end) {
result.push(start)
start += step
}
return result
}
9 changes: 9 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1271,3 +1271,12 @@ describe('set', () => {
})
})
})

describe('range', () => {
test('ok', () => {
expect(vtils.range(2)).toEqual([0, 1])
expect(vtils.range(2, 5)).toEqual([2, 3, 4])
expect(vtils.range(5, 10, 2)).toEqual([5, 7, 9])
expect(vtils.range(-3)).toEqual([-3, -2, -1])
})
})

0 comments on commit a0d1a8a

Please sign in to comment.