(
date: DateArg<Date> & {},
formatStr: string,
)
| 85 | * //=> '2014-02-11' |
| 86 | */ |
| 87 | export function lightFormat( |
| 88 | date: DateArg<Date> & {}, |
| 89 | formatStr: string, |
| 90 | ): string { |
| 91 | const date_ = toDate(date); |
| 92 | |
| 93 | if (!isValid(date_)) { |
| 94 | throw new RangeError("Invalid time value"); |
| 95 | } |
| 96 | |
| 97 | const tokens = formatStr.match(formattingTokensRegExp); |
| 98 | |
| 99 | // The only case when formattingTokensRegExp doesn't match the string is when it's empty |
| 100 | if (!tokens) return ""; |
| 101 | |
| 102 | const result = tokens |
| 103 | .map((substring) => { |
| 104 | // Replace two single quote characters with one single quote character |
| 105 | if (substring === "''") { |
| 106 | return "'"; |
| 107 | } |
| 108 | |
| 109 | const firstCharacter = substring[0]; |
| 110 | if (firstCharacter === "'") { |
| 111 | return cleanEscapedString(substring); |
| 112 | } |
| 113 | |
| 114 | const formatter = lightFormatters[firstCharacter as Token]; |
| 115 | if (formatter) { |
| 116 | return formatter(date_, substring); |
| 117 | } |
| 118 | |
| 119 | if (firstCharacter.match(unescapedLatinCharacterRegExp)) { |
| 120 | throw new RangeError( |
| 121 | "Format string contains an unescaped latin alphabet character `" + |
| 122 | firstCharacter + |
| 123 | "`", |
| 124 | ); |
| 125 | } |
| 126 | |
| 127 | return substring; |
| 128 | }) |
| 129 | .join(""); |
| 130 | |
| 131 | return result; |
| 132 | } |
| 133 | |
| 134 | function cleanEscapedString(input: string) { |
| 135 | const matches = input.match(escapedStringRegExp); |
no test coverage detected