跳至內容

unicorn/prefer-code-point 吹毛求疵

🛠️ 此規則有自動修正可用。

作用

偏好使用 String.prototype.codePointAt 而非 String.prototype.charCodeAt。偏好使用 String.fromCodePoint 而非 String.fromCharCode

為何這是不好的?

String#codePointAt()String.fromCodePoint() 對 Unicode 的支援更好。

String.fromCodePoint()String.fromCharCode() 之間的差異

範例

此規則的不正確程式碼範例

javascript
"🦄".charCodeAt(0);
String.fromCharCode(0x1f984);

此規則的正確程式碼範例

javascript
"🦄".codePointAt(0);
String.fromCodePoint(0x1f984);

參考

以 MIT 許可證發布。