diff --git a/src/index.js b/src/index.js index af24e37..cb919ed 100644 --- a/src/index.js +++ b/src/index.js @@ -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 { + 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 + } }