New JavaScript Proposals: Error.isError, RegExp.escape, Uint8Array Base64
These recent ECMAScript proposals—Error.isError for reliable error type checking, RegExp.escape to safely escape user‑provided strings, Uint8Array methods for seamless Base64 and hex conversions, and Temporal.TimeZone.prototype.equals for robust time‑zone comparison—aim to simplify development across browsers and Node.js by addressing long‑standing inconsistencies.
When a proposal reaches Stage 3, browsers and Node.js begin implementing its features, and changes are rare unless major issues arise.
Error.isError
Proposal address:
proposal-is-errorThis proposal introduces a global method
Error.isErrorto accurately determine whether an object is an
Errortype.
The need arises because
Symbol.toStringTagcan make
Object.prototype.toString()return unreliable values.
By creating a fake error class that returns
'Error'from its
[Symbol.toStringTag], the
Object.prototype.toString.calloutput becomes
[object Error]:
<code>class FakeError {
get [Symbol.toStringTag]() {
return 'Error';
}
}
// Output: [object Error]
console.log(Object.prototype.toString.call(new FakeError()));
// Output: [object Error]
console.log(Object.prototype.toString.call(new Error()));
// Output: true
console.log(Object.prototype.toString.call(new Error()) === Object.prototype.toString.call(new FakeError()))
</code>The widely used
is-errorpackage (30 w weekly downloads) implements this check with
Object.prototype.toString() === '[object Error]'.
RegExp.escape
Proposal address:
proposal-regex-escapingDevelopers often need to build regular expressions from strings without treating special characters as regex tokens, but JavaScript lacks a built‑in way to escape them safely.
<code>let text = "Hello.";
// this would match . against any character rather than matching it against a dot
ourLongText.replace(new RegExp(text, "g"))
</code>When user input is used to construct a regex, it must be escaped first to avoid security issues. Existing escape implementations may miss some special characters or mishandle multi‑code‑unit UTF‑8 characters (e.g.,
'😂'.length === 2), creating maintenance burdens.
<code>const str = prompt("Please enter a string");
const escaped = RegExp.escape(str);
const re = new RegExp(escaped, 'g'); // handles regex special tokens with the replacement.
console.log(ourLongText.replace(re));
</code>This proposal adds a
RegExp.escapefunction that escapes all characters with special regex meaning.
<code>RegExp.escape("The Quick Brown Fox"); // "The Quick Brown Fox"
RegExp.escape("Buy it. use it. break it. fix it."); // "Buy it\. use it\. break it\. fix it\."
RegExp.escape("(*.*)"); // "\(\*\.\*\)"
RegExp.escape("。^・ェ・^。"); // "。\^・ェ・\^。"
RegExp.escape("😊 *_* +_+ ... 👍"); // "😊 \*_* \+_+ \.\.\. 👍"
RegExp.escape("\d \D (?:)"); // "\\d \\D \(\?:\)"
</code>Uint8Array to/from base64 and hex
Proposal address:
proposal-arraybuffer-base64The proposal aims to add support for converting binary data in
Uint8Arrayto and from Base64 (and hex) directly.
JavaScript currently provides
btoaand
atobfor string‑Base64 conversion, which can be used with binary data but are limited to the Latin‑1 range (0x00‑0xFF).
<code>const binaryData = new Uint8Array([72,101,108,108,111,44,32,87,111,114,108,100,33]);
// binary data → Base64
const base64Data = btoa(String.fromCharCode.apply(null, binaryData));
console.log(base64Data); // "SGVsbG8sIFdvcmxkIQ=="
// Base64 → binary data
const decodedBinaryData = new Uint8Array(atob(base64Data).split('').map(function(c) {
return c.charCodeAt(0);
}));
console.log(decodedBinaryData); // Uint8Array[ 72,101,108,108,111,44,32,87,111,114,108,100,33 ]
</code>Because
atoband
btoaare limited to Latin‑1, a more general binary conversion capability is needed. The proposal introduces
Uint8Array.prototype.toBase64and
Uint8Array.fromBase64for direct conversion:
<code>const raw = new Uint8Array([72,101,108,108,111,32,87,111,114,108,100]);
const str = raw.toBase64(); // 'SGVsbG8gV29ybGQ='
const arr = Uint8Array.fromBase64(str); // Uint8Array([72,101,108,108,111,32,87,111,114,108,100])
</code>The proposal also adds
Uint8Array.prototype.toHexand
Uint8Array.fromHexfor hex conversion.
Time Zone Canonicalization
Proposal address:
proposal-canonical-tzECMAScript time‑zone names come from the IANA Time Zone Database and are periodically updated, requiring developers to keep their zone identifiers current. This proposal standardizes handling of updated zones, simplifies identifiers, and introduces
Temporal.TimeZone.prototype.equalsto check if two zones refer to the same location.
<code>Temporal.TimeZone.from('Asia/Calcutta').equals('Asia/Kolkata');
// => true
</code>Taobao Frontend Technology
The frontend landscape is constantly evolving, with rapid innovations across familiar languages. Like us, your understanding of the frontend is continually refreshed. Join us on Taobao, a vibrant, all‑encompassing platform, to uncover limitless potential.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.