这是indexloc提供的服务,不要输入任何密码
Skip to content
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
45 changes: 24 additions & 21 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
var RGX = /([^{]*?)\w(?=\})/g;
var RGX = /([^{]*?)\w(?=\})/g

var dict = {
YYYY: 'getFullYear',
YY: 'getYear',
MM: function (d) {
return d.getMonth() + 1;
},
MMMM: d => d.toLocaleString('default', { month: 'long' }),
MMM: d => d.toLocaleString('default', { month: 'short' }),
MM: d => d.getMonth() + 1,
DD: 'getDate',
HH: 'getHours',
mm: 'getMinutes',
ss: 'getSeconds',
fff: 'getMilliseconds'
};
}

export default function (str) {
var parts=[], offset=0;
str.replace(RGX, function (key, _, idx) {
export default str => {
var parts = [],
offset = 0
str.replace(RGX, (key, _, idx) => {
// save preceding string
parts.push(str.substring(offset, idx - 1));
offset = idx += key.length + 1;
parts.push(str.substring(offset, idx - 1))
offset = idx += key.length + 1
// save function
parts.push(function (d) {
return ('00' + (typeof dict[key]==='string' ? d[dict[key]]() : dict[key](d))).slice(-key.length);
});
});
parts.push(d =>
('00' + (typeof dict[key] === 'string' ? d[dict[key]]() : dict[key](d))).slice(-key.length)
)
})

if (offset !== str.length) {
parts.push(str.substring(offset));
parts.push(str.substring(offset))
}

return function (arg) {
var out='', i=0, d=arg||new Date();
for (; i<parts.length; i++) {
out += (typeof parts[i]==='string') ? parts[i] : parts[i](d);
return arg => {
var out = '',
i = 0,
d = arg || new Date()
for (; i < parts.length; i++) {
out += typeof parts[i] === 'string' ? parts[i] : parts[i](d)
}
return out;
};
return out
}
}