Skip to content

Commit

Permalink
feat: add formatTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Apr 10, 2019
1 parent 6dd070c commit bcfe631
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 18 deletions.
21 changes: 3 additions & 18 deletions src/formatDate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { reduce } from './reduce'
import { repeat } from './repeat'
import { formatTemplate, FormatTemplatePatternToValue } from './formatTemplate'
import { toDate } from './toDate'

export type FormatDateValue = string | number | Date
Expand All @@ -13,7 +12,7 @@ export type FormatDateValue = string | number | Date
*/
export function formatDate(value: FormatDateValue, template: string): string {
const date = toDate(value)
const patterns: { [key: string]: number } = {
const patternToValue: FormatTemplatePatternToValue = {
y: date.getFullYear(), // 年
m: date.getMonth() + 1, // 月
d: date.getDate(), // 日
Expand All @@ -22,19 +21,5 @@ export function formatDate(value: FormatDateValue, template: string): string {
s: date.getSeconds(), // 秒
l: date.getMilliseconds(), // 毫秒
}
return reduce(patterns, (result, patternValue, patternKey) => {
const patternValueStr = String(patternValue)
const len = patternValueStr.length
return result.replace(
new RegExp(`(${patternKey}+)`, 'g'),
$0 => {
const n = $0.length
return (
n === 1 || n === len ? patternValueStr // n=1或n=len: 直接返回
: n < len ? patternValueStr.substr(len - n) // n<len: 截取后n位
: repeat(0, n - len) + patternValueStr // n>len: 前填充0到n位
)
},
)
}, template)
return formatTemplate(template, patternToValue)
}
33 changes: 33 additions & 0 deletions src/formatTemplate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { reduce } from './reduce'
import { repeat } from './repeat'

export type FormatTemplatePatternToValue = Record<string | number, string | number>

/**
* 根据 `patternToValue` 格式化模板字符串。
*
* @param template 模板字符串
* @param patternToValue 单字符 --> 值
* @returns 格式化后的字符串
*/
export function formatTemplate(template: string, patternToValue: FormatTemplatePatternToValue): string {
return reduce(
patternToValue,
(result, value, pattern) => {
const strValue = String(value)
const strLen = strValue.length
return result.replace(
new RegExp(`(${pattern}+)`, 'g'),
$0 => {
const n = $0.length
return (
n === 1 || n === strLen ? strValue // n=1或n=len: 直接返回
: n < strLen ? strValue.substr(strLen - n) // n<len: 截取后n位
: repeat(0, n - strLen) + strValue // n>len: 前填充0到n位
)
},
)
},
template,
)
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export * from './flexible'
export * from './formatCurrency'
export * from './formatDate'
export * from './formatDateDiff'
export * from './formatTemplate'
export * from './forOwn'
export * from './get'
export * from './getDaysInMonth'
Expand Down
7 changes: 7 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1725,3 +1725,10 @@ describe('getDaysInMonth', () => {
}
})
})

describe('formatTemplate', () => {
test('ok', () => {
expect(vtils.formatTemplate('hello', { h: 78, l: '=-', o: '0' })).toBe('78e=-0')
expect(vtils.formatTemplate('hello', { e: 'pyyy' })).toBe('hpyyyllo')
})
})

0 comments on commit bcfe631

Please sign in to comment.