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-error This proposal introduces a global method Error.isError to accurately determine whether an object is an Error type.
The need arises because Symbol.toStringTag can make Object.prototype.toString() return unreliable values.
By creating a fake error class that returns 'Error' from its [Symbol.toStringTag], the Object.prototype.toString.call output becomes [object Error]:
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()))The widely used is-error package (30 w weekly downloads) implements this check with Object.prototype.toString() === '[object Error]'.
RegExp.escape
Proposal address: proposal-regex-escaping Developers 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.
let text = "Hello.";
// this would match . against any character rather than matching it against a dot
ourLongText.replace(new RegExp(text, "g"))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.
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));This proposal adds a RegExp.escape function that escapes all characters with special regex meaning.
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 \(\?:\)"Uint8Array to/from base64 and hex
Proposal address: proposal-arraybuffer-base64 The proposal aims to add support for converting binary data in Uint8Array to and from Base64 (and hex) directly.
JavaScript currently provides btoa and atob for string‑Base64 conversion, which can be used with binary data but are limited to the Latin‑1 range (0x00‑0xFF).
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 ]Because atob and btoa are limited to Latin‑1, a more general binary conversion capability is needed. The proposal introduces Uint8Array.prototype.toBase64 and Uint8Array.fromBase64 for direct conversion:
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])The proposal also adds Uint8Array.prototype.toHex and Uint8Array.fromHex for hex conversion.
Time Zone Canonicalization
Proposal address: proposal-canonical-tz ECMAScript 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.equals to check if two zones refer to the same location.
Temporal.TimeZone.from('Asia/Calcutta').equals('Asia/Kolkata');
// => trueSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
