PreParsePostFormat
Pre-parse / Post-format consente di elaborare l'input prima del parsing e di formattare l'output della stringa dopo la formattazione. Basato su un comportamento simile per le impostazioni locali in moment.js.
NOTA: Questo plugin richiede che il plugin localeData
sia importato precedentemente (poiché ne dipende).
NOTA: Questo plugin influisce anche sul plugin relativeTime
, intenzionalmente (replica il comportamento di implementazione di moment.js).
Esempio di utilizzo
Ad esempio, nella localizzazione AR viene utilizzato per supportare i numeri arabi.
javascript
// 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, '،');
},
// ...
};
// ...
I test dovrebbero fornire un'idea chiara su come utilizzare il plugin.