From b00e012979c02c845abde8916398ee238ed9d6dc Mon Sep 17 00:00:00 2001 From: Moshe Jonathan Gordon Radian Date: Sun, 17 Oct 2021 08:24:23 +0300 Subject: [PATCH 1/2] Create preparse-postformat.md Added documentation for the preparse / postformat plugin. --- docs/plugin/preparse-postformat.md | 66 ++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 docs/plugin/preparse-postformat.md diff --git a/docs/plugin/preparse-postformat.md b/docs/plugin/preparse-postformat.md new file mode 100644 index 000000000..03801629d --- /dev/null +++ b/docs/plugin/preparse-postformat.md @@ -0,0 +1,66 @@ +--- +id: preparse-postformat +title: Pre-parse / Post-format Plugin +--- +Pre-parse / Post-format lets you process the input before the parser and process the string output after the formatter. [Based on similar behavior for locales in moment.js](https://momentjs.com/docs/#/i18n/locale-data/). + +NOTE: this plugin requires the localeData plugin to be imported before it (as it depends on it's functionality). This is done [by design](https://github.com/iamkun/dayjs/pull/1255#issuecomment-753325420). + +NOTE: this plugin also affects the relative time plugin, also by design (mimics the moment.js implementaiton behavior). + +## Sample usage +e.g. [In the AR locale specifically, it is used to support Arabic numerals](https://github.com/iamkun/dayjs/pull/1255/commits/e26e802d767eec89aae02c8cecf87f517600a698). + +```javascript +// Arabic [ar] +import dayjs from 'dayjs' + +const months = 'يناير_فبراير_مارس_أبريل_مايو_يونيو_يوليو_أغسطس_سبتمبر_أكتوبر_نوفمبر_ديسمبر'.split('_') +const symbolMap = { + 1: '١', + 2: '٢', + 3: '٣', + 4: '٤', + 5: '٥', + 6: '٦', + 7: '٧', + 8: '٨', + 9: '٩', + 0: '٠' +} + +const numberMap = { + '١': '1', + '٢': '2', + '٣': '3', + '٤': '4', + '٥': '5', + '٦': '6', + '٧': '7', + '٨': '8', + '٩': '9', + '٠': '0' +} + +const locale = { + name: 'ar', + // ... + preparse(string) { + return string + .replace( + /[١٢٣٤٥٦٧٨٩٠]/g, + match => numberMap[match] + ) + .replace(/،/g, ',') + }, + postformat(string) { + return string + .replace(/\d/g, match => symbolMap[match]) + .replace(/,/g, '،') + }, + // ... +} +// ... +``` + +[The tests](https://github.com/VehpuS/dayjs/blob/e565267d925b40af8c7e0663c303f9a397372d03/test/plugin/preParsePostFormat.test.js) should also give you a good idea on how to use the plugin if this isn't clear enough ;). From 6924ec2166464fbe570d3f797bd523fc40304f7e Mon Sep 17 00:00:00 2001 From: Moshe Jonathan Gordon Radian Date: Tue, 2 Nov 2021 02:33:09 +0200 Subject: [PATCH 2/2] doc: add preparse-postformat plugin doc to sidebar --- website/sidebars.json | 1 + 1 file changed, 1 insertion(+) diff --git a/website/sidebars.json b/website/sidebars.json index f00bac617..f1cbe407c 100644 --- a/website/sidebars.json +++ b/website/sidebars.json @@ -123,6 +123,7 @@ "plugin/min-max", "plugin/object-support", "plugin/plural-get-set", + "plugin/preparse-postformat", "plugin/quarter-of-year", "plugin/relative-time", "plugin/timezone",