Skip to content

Commit 6b1192d

Browse files
committed
Add ES2024 features
1 parent 14293df commit 6b1192d

4 files changed

+166
-1
lines changed

README.md

+89-1
Original file line numberDiff line numberDiff line change
@@ -2619,4 +2619,92 @@ Most of these features already supported by some browsers and try out with babel
26192619
for (let x of result.get("senior")) {
26202620
console.log(x.name + " " + x.age);
26212621
}
2622-
```
2622+
```
2623+
2624+
2. ### Temporal API
2625+
2626+
The Temporal API is a modern API for working with dates and times, used to supersede the original Date API. It provides a more comprehensive and user-friendly way to handle date and time manipulation.
2627+
2628+
3. ### Well formed unicode strings
2629+
Unicode strings are mainly used for representing a wide range of characters from different languages and symbols. In UTF-16, strings which contain lone surrogates(16-bit Code Unit) are considered as "malformed" or "not well formatted". These lone surrogates can be of two types,
2630+
2631+
1. **Leading surrogates:** Range between `0XD800` to `0XDBFF`
2632+
2. **Trailing Surrogate:** Range between `0XDC00` to `0XDFFF`
2633+
2634+
Well-Formed Unicode Strings feature introduced below two string methods to check and convert into wellformed strings.
2635+
2636+
1. **String.prototype.isWellFormed:**
2637+
This method is used to check if the string contains lone surrogates or not. Returns `true`, if unicode string is not present. The following stings can be verified either as well-formed or not well-formed strigns,
2638+
2639+
```javascript
2640+
const str1 = "Hello World \uD815";
2641+
const str2 = "Welcome to ES2024";
2642+
const str3 = "Welcome to ES2024 😀";
2643+
2644+
console.log(str1.isWellFormed()); // false
2645+
console.log(str2.isWellFormed()); // true
2646+
console.log(str2.isWellFormed()); // true
2647+
```
2648+
2649+
**Note:** Emojis are considered as well-formed unicode strings.
2650+
2651+
2. **String.prototype.toWellFormed:**
2652+
This method is used to return a string by converting unpaired surrogate(i.e, leading and trailing surrogates) code points with `U+FFFD` Replacement characters.
2653+
2654+
```javascript
2655+
const str1 = "Hello World \uD815";
2656+
const str2 = "Welcome to ES2024";
2657+
2658+
console.log(str1.toWellFormed()); // Hello World �
2659+
console.log(str2.toWellFormed()); // Welcome to ES2024
2660+
```
2661+
2662+
These two methods are mainly helpful for developers to work with string encoding without any errors. For example, the below encoding process throws an error due to lone surrogates,
2663+
2664+
```javascript
2665+
const url = "https://somedomain.com/query=\uD423";
2666+
2667+
try {
2668+
console.log(encodeURI(url));
2669+
} catch (e) {
2670+
console.log('Error:', e.message); // Expected: URIError: URI malformed
2671+
}
2672+
```
2673+
2674+
After applying `toWellFormed()` method, the lone surrogate is replaced with the Unicode replacement character (U+FFFD). It make sure `encodeURI()` is processed without errors.
2675+
2676+
```javascript
2677+
console.log(encodeURI(url.toWellFormed())); // https://somedomain.com/query=%ED%90%A3
2678+
```
2679+
4. ### Atomic waitSync
2680+
The `Atomics.waitAsync()` is a static method that waits asynchronously on a shared memory location and returns a Promise. It is non-blocking as compared to `Atomics.wait()` and can be used on the main thread. The syntax looks like below,
2681+
2682+
```javascript
2683+
Atomics.waitAsync(typedArray, ind, val, timeOut);
2684+
```
2685+
If the promise is not fulfilled then it will lead to a 'time-out' status otherwise the status will always be 'ok' once the promise has been fulfilled.
2686+
2687+
Let's take a shared Int32Array. Here it waits asynchrously for position 0 and expects a result 0 waiting for 500ms.
2688+
2689+
```javascript
2690+
const arrayBuffer = new SharedArrayBuffer(1024);
2691+
const arr = new Int32Array(arrayBuffer);
2692+
2693+
Atomics.waitAsync(arr, 0 , 0 , 500); // { async: true, value: Promise {<pending>}}
2694+
2695+
Atomics.notify(arr, 0); // { async: true, value: Promise {<fulfilled>: 'ok'} }
2696+
```
2697+
After that, the notify method awakes the waiting agent(i.e, array) that are sleeping in waiting queue and the promise is fulfilled.
2698+
2699+
Remember that, SharedArrayBuffer have been disabled on most browsers unless you specify `Cross-Origin-Opener-Policy` and `Cross-Origin-Embedder-Policy` headers. For example,
2700+
2701+
`
2702+
Cross-Origin-Opener-Policy: same-origin
2703+
Cross-Origin-Embedder-Policy: require-corp
2704+
`
2705+
2706+
**Note:** There will be a TypeError if typedArray is not an **Int32Array** or **BigInt64Array** that views a SharedArrayBuffer.
2707+
2708+
5. ### RegEx v Flag and string properties
2709+
2710+
**[⬆ Back to Top](#table-of-contents)**

es2024/1.groupby-objects-maps.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const persons = [
2+
{name:"John", age:70},
3+
{name:"Kane", age:5},
4+
{name:"Jack", age:50},
5+
{name:"Rambo", age:15}
6+
];
7+
8+
// Callback function to categorize people based on age
9+
function callbackFunc({ age }) {
10+
if(age >= 60) {
11+
return "senior";
12+
} else if(age > 17 && age < 60) {
13+
return "adult";
14+
}
15+
else {
16+
return "kid";
17+
}
18+
}
19+
20+
//Object groupBy
21+
const result = Object.groupBy(persons, callbackFunc);
22+
23+
console.log("Kids: ");
24+
for (let [x,y] of result.kid.entries()) {
25+
console.log(y.name + " " + y.age);
26+
}
27+
28+
console.log("Adults: ");
29+
for (let [x,y] of result.adult.entries()) {
30+
console.log(y.name + " " + y.age);
31+
}
32+
33+
console.log("Seniors: ");
34+
for (let [x,y] of result.senior.entries()) {
35+
console.log(y.name + " " + y.age);
36+
}
37+
38+
//Map groupBy
39+
const result1 = Map.groupBy(persons, callbackFunc);
40+
41+
console.log("Kids: ");
42+
for (let x of result1.get("kid")) {
43+
console.log(x.name + " " + x.age);
44+
}
45+
46+
console.log("Adults: ");
47+
for (let x of result1.get("adult")) {
48+
console.log(x.name + " " + x.age);
49+
}
50+
51+
console.log("Seniors: ");
52+
for (let x of result1.get("senior")) {
53+
console.log(x.name + " " + x.age);
54+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const str1 = "Hello World \uD815";
2+
const str2 = "Welcome to ES2024";
3+
const str3 = "Welcome to ES2024 😀";
4+
5+
console.log(str1.isWellFormed()); // false
6+
console.log(str2.isWellFormed()); // true
7+
console.log(str2.isWellFormed()); // true
8+
9+
console.log(str1.toWellFormed()); // Hello World �
10+
console.log(str2.toWellFormed()); // Welcome to ES2024
11+
12+
const url = "https://somedomain.com/query=\uD423";
13+
try {
14+
console.log(encodeURI(url.toWellFormed())); // https://somedomain.com/query=%ED%90%A3
15+
} catch (e) {
16+
console.log('Error:', e.message);
17+
}

es2024/4.atomics-waitasync.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const arrayBuffer = new SharedArrayBuffer(1024);
2+
const arr = new Int32Array(arrayBuffer);
3+
4+
Atomics.waitAsync(arr, 0 , 0 , 500); // { async: true, value: Promise {<pending>}}
5+
6+
Atomics.notify(arr, 0); // { async: true, value: Promise {<fulfilled>: 'ok'} }

0 commit comments

Comments
 (0)