Skip to content

Commit

Permalink
refactor: simplify date formatting logic and enhance dateFormatter fu…
Browse files Browse the repository at this point in the history
…nction
  • Loading branch information
vctrtvfrrr authored and webketje committed Nov 6, 2024
1 parent de5c645 commit dffe27b
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions src/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,26 @@ const dateTokens = {

const dateTokenKeys = Object.keys(dateTokens)

export function dateFormatter(format, locale) {
return function formatDate(date) {
const result = []
while (format.length) {
let token
for (const tokenKey of dateTokenKeys) {
const match = format.match(tokenKey)
if (match && match.index === 0) {
token = match[0]
break
}
function formatDate(date, format, locale) {
const result = []
while (format.length) {
let token
for (const tokenKey of dateTokenKeys) {
const match = format.match(tokenKey)
if (match && match.index === 0) {
token = match[0]
break
}
const mappable = !!token
if (!token) token = format.slice(0, 1)
format = format.slice(token.length)
if (mappable) token = dateTokens[token](date, locale)
result.push(token)
}
return result.join('')
const mappable = !!token
if (!token) token = format.slice(0, 1)
format = format.slice(token.length)
if (mappable) token = dateTokens[token](date, locale)
result.push(token)
}
return result.join('')
}

export function dateFormatter(format, locale) {
return (date) => formatDate(date, format, locale)
}

0 comments on commit dffe27b

Please sign in to comment.