Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace date format RegExp with simple/naive parser #141

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions src/fmtDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,35 @@ const subs = {

export function fmtDate(tpl) {
let parts = [];

let R = /\{([a-z]+)\}|[^{]+/yi, m;

while (m = R.exec(tpl))
parts.push(m[0][0] == '{' ? subs[m[1]] : m[0]);
let index = 0;

while(index < tpl.length) {
let literal = '';
while(tpl[index] !== '{' && index < tpl.length) {
literal += tpl[index];
index += 1;
}
parts.push(literal);

if(index < tpl.length && tpl[index] === '{') {
let template = '';
index += 1;
while(index < tpl.length) {
const char = tpl[index].toLowerCase();
if(char < 'a' || 'z' < char) break;
template += tpl[index];
index += 1;
}
if(tpl[index] !== '}') {
break;
}
if(template.length === 0) {
break;
}
parts.push(subs[template]);
index += 1;
}
}

return d => {
let out = '';
Expand All @@ -128,4 +152,4 @@ export function tzDate(date, tz) {
let date2 = new Date(date.toLocaleString('en-US', {timeZone: tz}));
date2.setMilliseconds(date[getMilliseconds]());
return date2;
}
}