Skip to content

Commit

Permalink
feat: add pad
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jan 19, 2019
1 parent 1cadd37 commit 4feaa1b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export { default as mapValues } from './mapValues'
export { default as noop } from './noop'
export { default as objectToQueryString } from './objectToQueryString'
export { default as omit } from './omit'
export { default as pad } from './pad'
export { default as padEnd } from './padEnd'
export { default as padStart } from './padStart'
export { default as parseCSSValue } from './parseCSSValue'
Expand Down
22 changes: 22 additions & 0 deletions src/pad.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import repeat from './repeat'

/**
* 在 str 两侧填充字符。
*
* @param str 要填充的字符串
* @param [length=0] 目标长度
* @param [chars=' '] 填充字符
* @returns 填充后的字符串
*/
export default function pad(str: string, length: number = 0, chars: string = ' '): string {
let suffix = ''
let prefix = ''
if (length > str.length) {
const affixLength = length - str.length
const halfAffixLength = Math.floor(affixLength / 2)
const affix = repeat(chars, affixLength).slice(0, affixLength)
prefix = affix.substring(0, halfAffixLength)
suffix = affix.substring(halfAffixLength, affixLength)
}
return prefix + str + suffix
}
2 changes: 1 addition & 1 deletion src/padEnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function padEnd(str: string, length: number = 0, chars: string =
let suffix = ''
if (length > str.length) {
const suffixLength = length - str.length
suffix = repeat(chars, length - str.length).slice(0, suffixLength)
suffix = repeat(chars, suffixLength).slice(0, suffixLength)
}
return str + suffix
}
2 changes: 1 addition & 1 deletion src/padStart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function padStart(str: string, length: number = 0, chars: string
let prefix = ''
if (length > str.length) {
const prefixLength = length - str.length
prefix = repeat(chars, length - str.length).slice(0, prefixLength)
prefix = repeat(chars, prefixLength).slice(0, prefixLength)
}
return prefix + str
}
10 changes: 10 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1464,3 +1464,13 @@ describe('padEnd', () => {
expect(vtils.padEnd('abc', 3)).toBe('abc')
})
})

describe('pad', () => {
test('ok', () => {
expect(vtils.pad('1', 3, '0')).toBe('010')
expect(vtils.pad('1', 4, '0')).toBe('0100')
expect(vtils.pad('abc', 8)).toBe(' abc ')
expect(vtils.pad('abc', 8, '_-')).toBe('_-abc_-_')
expect(vtils.pad('abc', 3)).toBe('abc')
})
})

0 comments on commit 4feaa1b

Please sign in to comment.