@babel/plugin-transform-numeric-separator
示例
¥Example
十进制字面量
¥Decimal Literals
let budget = 1_000_000_000_000;
// What is the value of `budget`? It's 1 trillion!
//
// Let's confirm:
console.log(budget === 10 ** 12); // true
二进制字面量
¥Binary Literals
let nibbles = 0b1010_0001_1000_0101;
// Is bit 7 on? It sure is!
// 0b1010_0001_1000_0101
// ^
//
// We can double check:
console.log(!!(nibbles & (1 << 7))); // true
十六进制字面量
¥Hex Literal
// Messages are sent as 24 bit values, but should be
// treated as 3 distinct bytes:
let message = 0xa0_b0_c0;
// What's the value of the upper most byte? It's A0, or 160.
// We can confirm that:
let a = (message >> 16) & 0xff;
console.log(a.toString(16), a); // a0, 160
// What's the value of the middle byte? It's B0, or 176.
// Let's just make sure...
let b = (message >> 8) & 0xff;
console.log(b.toString(16), b); // b0, 176
// What's the value of the lower most byte? It's C0, or 192.
// Again, let's prove that:
let c = message & 0xff;
console.log(c.toString(16), b); // c0, 192
八进制字面量
¥Octal Literal
挥手的表情
¥hand wave emoji
八进制非常适合权限,但以 0o0000
形式表示时也更好看。这里的分隔符没有真正的好处。
¥Octals are great for permissions, but also look better when represented in 0o0000
form. No real benefit with separators here.
安装
¥Installation
- npm
- Yarn
- pnpm
npm install --save-dev @babel/plugin-transform-numeric-separator
yarn add --dev @babel/plugin-transform-numeric-separator
pnpm add --save-dev @babel/plugin-transform-numeric-separator
用法
¥Usage
使用配置文件(推荐)
¥With a configuration file (Recommended)
{
"plugins": ["@babel/plugin-transform-numeric-separator"]
}
通过 CLI
¥Via CLI
babel --plugins @babel/plugin-transform-numeric-separator script.js
通过 Node API
¥Via Node API
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-transform-numeric-separator"],
});
附加信息
¥Additional Information
如果你需要进一步将 ES2015 十进制、二进制、十六进制和八进制数字表示编译为 ES2015 之前的数字字面量形式,请添加 "@babel/plugin-transform-literals"
插件:
¥If you need to further compile ES2015 Decimal, Binary, Hex and Octal number representations to their pre-ES2015 numeric literal form, add the "@babel/plugin-transform-literals"
plugin:
@babel/plugin-transform-literals
已包含在 @babel/preset-env 中。
¥@babel/plugin-transform-literals
is already included in @babel/preset-env.
使用配置文件(推荐)
¥With a configuration file (Recommended)
{
"presets": ["@babel/preset-env"],
"plugins": ["@babel/plugin-transform-numeric-separator"]
}
{
"plugins": ["@babel/plugin-transform-numeric-separator", "@babel/plugin-transform-literals"]
}
参考
¥References