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);