PreParsePostFormat
预解析/后格式化允许你在解析器之前处理输入并在格式化器之后处理字符串输出。基于 moment.js 中区域设置的类似行为。
¥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.
注意:该插件需要在它之前导入 localeData 插件(因为它取决于它的功能)。
¥NOTE: this plugin requires the localeData plugin to be imported before it (as it depends on it's functionality).
注意:该插件还会影响相对时间插件,也是设计使然(模仿 moment.js 实现行为)。
¥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.
// Arabic [ar]
import dayjs from 'dayjs'
import preParsePostFormat from 'dayjs/plugin/preParsePostFormat'
dayjs.extend(preParsePostFormat)
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 should also give you a good idea on how to use the plugin if this isn't clear enough ;).