From 6680ff8578ed478aec944770b038742a52f521dc Mon Sep 17 00:00:00 2001 From: Huanyi Xie <74809554+xiehuanyi@users.noreply.github.com> Date: Thu, 8 Oct 2026 14:33:45 +0300 Subject: [PATCH] fix: pad full years to four digits --- src/index.js | 2 +- test/full-year-padding.js | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 test/full-year-padding.js diff --git a/src/index.js b/src/index.js index 3ea001b..e3ec49a 100644 --- a/src/index.js +++ b/src/index.js @@ -22,7 +22,7 @@ export default function (str, custom) { offset = idx += key.length + 1; // save function parts.push(custom && custom[key] || function (d) { - return ('00' + (typeof MAP[key] === 'string' ? d[MAP[key]]() : MAP[key](d))).slice(-key.length); + return ('000' + (typeof MAP[key] === 'string' ? d[MAP[key]]() : MAP[key](d))).slice(-key.length); }); }); diff --git a/test/full-year-padding.js b/test/full-year-padding.js new file mode 100644 index 0000000..664883b --- /dev/null +++ b/test/full-year-padding.js @@ -0,0 +1,12 @@ +const test = require('tape'); +const fn = require('../dist/tinydate'); + +test('full year has four digits', t => { + const stamp = fn('{YYYY}'); + for (const year of [0, 1, 9, 10, 99, 100, 999, 1000, 2017]) { + const date = new Date(0); + date.setFullYear(year); + t.is(stamp(date), ('000' + year).slice(-4), 'full year for ' + year); + } + t.end(); +});