Skip to content

Commit

Permalink
fix(repeat): 0 or any negative number returns empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Oct 10, 2018
1 parent 426e8dd commit 947b0c8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
3 changes: 1 addition & 2 deletions src/repeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
* @returns 结果字符串
*/
export default function repeat(str: string | number, n: number = 1) {
n = Math.round(n <= 0 ? 1 : n)
let result = ''
while (n--) {
while (n-- > 0) {
result += str
}
return result
Expand Down
8 changes: 4 additions & 4 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ describe('repeat', () => {
expect(vtils.repeat(1, 2)).toBe('11')
})
test('负数、0、1', () => {
expect(vtils.repeat(1, -1)).toBe('1')
expect(vtils.repeat('我们', -2)).toBe('我们')
expect(vtils.repeat(1, 0)).toBe('1')
expect(vtils.repeat('我们', 0)).toBe('我们')
expect(vtils.repeat(1, -1)).toBe('')
expect(vtils.repeat('我们', -2)).toBe('')
expect(vtils.repeat(1, 0)).toBe('')
expect(vtils.repeat('我们', 0)).toBe('')
expect(vtils.repeat(1, 1)).toBe('1')
expect(vtils.repeat('我们', 1)).toBe('我们')
})
Expand Down

0 comments on commit 947b0c8

Please sign in to comment.