# PreParsePostFormat

URL: https://caijiao.org/dayjs/plugin/preparse-postformat
Source: docs/dayjs/plugin/preparse-postformat.md
Description: PreParsePostFormat 预解析/后格式化让您在解析前处理输入，并在格式化要输出的字符串。 参考类似moment.js 国际化里的用法。

预解析/后格式化让您在解析前处理输入，并在格式化要输出的字符串。 参考类似[moment.js](https://momentjs.com/docs/#/i18n/locale-data/) 国际化里的用法。

注意：此插件需要在 localeData 插件之前导入(因为有依赖关系)。

注意：此插件也会改变 relativeTime 插件的相关行为

## 基础用法

例如： [在阿拉伯语言中，它被用于支持阿拉伯数字的特殊显示](https://github.com/iamkun/dayjs/pull/1255/commits/e26e802d767eec89aae02c8cecf87f517600a698)。

```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, "،");
  },
  // ...
};
// ...
```

[单元测试](https://github.com/iamkun/dayjs/blob/dev/test/plugin/preParsePostFormat.test.js) 也应该让您很好地了解如何使用插件。
