-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
35 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters