New JavaScript Proposals Transform Error Handling, RegExp, and Binary Data
Several Stage‑3 TC39 proposals—including Error.isError for reliable error type checks, RegExp.escape for safe regex construction, Uint8Array methods for base64/hex conversion, and Temporal.TimeZone.prototype.equals for canonical time‑zone comparison—aim to enhance JavaScript’s core capabilities across browsers and Node.js.
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: proposal-is-error . It introduces a global method Error.isError to accurately determine whether a value is an Error object. The need arises because Symbol.toStringTag can make Object.prototype.toString() return unreliable strings.
Using a fabricated FakeError class that returns 'Error' from its [Symbol.toStringTag] getter, the method can correctly identify both real Error instances and faked ones:
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 popular is-error package implements the same check with Object.prototype.toString() === '[object Error]'.
RegExp.escape
Proposal: proposal-regex-escaping . Developers often need to build regular expressions from strings without treating special characters as regex tokens, but no built‑in method exists.
Example of the problem:
let text = "Hello.";
// this would match . against any character rather than matching it against a dot
ourLongText.replace(new RegExp(text, "g"));Escaping user input is also required for security, yet existing implementations may miss some characters or mishandle multi‑code‑unit UTF‑8 symbols (e.g., '😂'.length === 2).
The proposal adds RegExp.escape to escape all regex‑special characters:
const str = prompt("Please enter a string");
const escaped = RegExp.escape(str);
const re = new RegExp(escaped, 'g'); // handles reg exp special tokens with the replacement.
console.log(ourLongText.replace(re));Examples:
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: proposal-arraybuffer-base64 . It adds binary‑data conversion methods to Uint8Array, enabling direct base64 and hexadecimal transformations.
Current JavaScript provides btoa and atob for string‑based base64, but they are limited to Latin‑1 encoded data. The proposal introduces Uint8Array.prototype.toBase64, Uint8Array.fromBase64, Uint8Array.prototype.toHex, and Uint8Array.fromHex for robust handling.
Base64 example:
const binaryData = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]);
// binary data → Base64 encoding
const base64Data = btoa(String.fromCharCode.apply(null, binaryData));
console.log(base64Data); // "SGVsbG8sIFdvcmxkIQ=="
// Base64 encoding → 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 ]Using the new methods:
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])Time Zone Canonicalization
Proposal: proposal-canonical-tz . Time‑zone names in ECMAScript come from the IANA Time Zone Database, which is periodically updated. The proposal standardizes handling of updated zones, simplifies identifiers, and adds Temporal.TimeZone.prototype.equals to compare two zones for equivalence.
Temporal.TimeZone.from('Asia/Calcutta').equals('Asia/Kolkata'); // => trueReferences
proposal-is-error: https://github.com/tc39/proposal-is-error
proposal-regex-escaping: https://github.com/tc39/proposal-regex-escaping
proposal-arraybuffer-base64: https://github.com/tc39/proposal-arraybuffer-base64#uint8array-tofrom-base64-and-hex
proposal-canonical-tz: https://github.com/tc39/proposal-canonical-tz
Time Zone Database: https://www.iana.org/time-zones
Signed-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.
