Skip to content

Commit cfdbdcb

Browse files
committed
Added ES2025 features
1 parent bfdfd13 commit cfdbdcb

File tree

5 files changed

+933
-0
lines changed

5 files changed

+933
-0
lines changed

README.md

Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
3434
| ES2022 Or ES13 | June 2022 |
3535
| ES2023 Or ES14 | June 2023 |
3636
| ES2024 Or ES15 | June 2024 |
37+
| ES2025 Or ES16 | June 2025 |
3738

3839
### Table of Contents
3940

@@ -127,6 +128,11 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
127128
|7 | [Pipeline Operator](#pipeline-operator) |
128129
|8 | [Records and Tuples](#records-and-tuples) |
129130
|9 | [Decorators](#decorators) |
131+
| | **ES2025 Or ES16**|
132+
|1 | [Set Methods](#set-methods) |
133+
|2 | [Iterator Helpers](#iterator-helpers) |
134+
|3 | [Temporal API](#temporal-api-es2025) |
135+
|4 | [Decorator Metadata](#decorator-metadata) |
130136

131137
## ES2015 Or ES6
132138

@@ -2751,3 +2757,268 @@ Most of these features already supported by some browsers and try out with babel
27512757
```
27522758
27532759
**[⬆ Back to Top](#table-of-contents)**
2760+
2761+
## ES2025 or ES16
2762+
2763+
ES2025 is planned to be released in June 2025 with several new features and enhancements for JavaScript developers. These features aim to improve developer productivity and code readability.
2764+
2765+
1. ### Set Methods
2766+
2767+
ES2025 introduces several new methods to the Set prototype that make working with sets more convenient and expressive. These methods provide operations commonly found in set theory, such as union, intersection, and difference.
2768+
2769+
```javascript
2770+
// Create some sample sets
2771+
const set1 = new Set([1, 2, 3, 4, 5]);
2772+
const set2 = new Set([3, 4, 5, 6, 7]);
2773+
2774+
// Union - combines elements from both sets
2775+
const union = set1.union(set2);
2776+
console.log([...union]); // [1, 2, 3, 4, 5, 6, 7]
2777+
2778+
// Intersection - elements present in both sets
2779+
const intersection = set1.intersection(set2);
2780+
console.log([...intersection]); // [3, 4, 5]
2781+
2782+
// Difference - elements in first set but not in second
2783+
const difference = set1.difference(set2);
2784+
console.log([...difference]); // [1, 2]
2785+
2786+
// Symmetric Difference - elements in either set but not in both
2787+
const symDifference = set1.symmetricDifference(set2);
2788+
console.log([...symDifference]); // [1, 2, 6, 7]
2789+
```
2790+
2791+
The new Set methods include:
2792+
2793+
1. **Set.prototype.union()**: Returns a new Set containing all elements from both sets.
2794+
2. **Set.prototype.intersection()**: Returns a new Set containing elements present in all sets.
2795+
3. **Set.prototype.difference()**: Returns a new Set containing elements present in the first set but not in the second.
2796+
4. **Set.prototype.symmetricDifference()**: Returns a new Set containing elements present in either set but not in both.
2797+
5. **Set.prototype.isSubsetOf()**: Returns a boolean indicating if the set is a subset of the given set.
2798+
6. **Set.prototype.isSupersetOf()**: Returns a boolean indicating if the set is a superset of the given set.
2799+
7. **Set.prototype.isDisjointFrom()**: Returns a boolean indicating if the set has no elements in common with the given set.
2800+
2801+
These methods can be particularly useful for data processing, filtering, and comparison operations.
2802+
2803+
```javascript
2804+
// Practical example: Finding common interests between users
2805+
const user1Interests = new Set(["coding", "reading", "music", "hiking"]);
2806+
const user2Interests = new Set(["gaming", "music", "movies", "hiking"]);
2807+
2808+
// Find common interests
2809+
const commonInterests = user1Interests.intersection(user2Interests);
2810+
console.log([...commonInterests]); // ["music", "hiking"]
2811+
2812+
// Find unique interests of user1
2813+
const uniqueInterests = user1Interests.difference(user2Interests);
2814+
console.log([...uniqueInterests]); // ["coding", "reading"]
2815+
```
2816+
2817+
**[⬆ Back to Top](#table-of-contents)**
2818+
2819+
2. ### Iterator Helpers
2820+
2821+
ES2025 introduces Iterator Helpers, which provide a set of utility methods that can be chained together to perform operations on iterators in a more readable and functional way. This feature makes working with iterators more convenient and expressive.
2822+
2823+
```javascript
2824+
// Create a generator function
2825+
function* generateNumbers() {
2826+
yield 1;
2827+
yield 2;
2828+
yield 3;
2829+
yield 4;
2830+
yield 5;
2831+
}
2832+
2833+
// Use iterator helpers to transform the values
2834+
const result = generateNumbers()[Symbol.iterator]()
2835+
.filter(x => x > 1) // Keep only values greater than 1
2836+
.map(x => x * 10) // Multiply each value by 10
2837+
.take(3) // Take only the first 3 values
2838+
.toArray(); // Convert to an array
2839+
2840+
console.log(result); // [20, 30, 40]
2841+
```
2842+
2843+
The new Iterator Helper methods include:
2844+
2845+
1. **Iterator.prototype.map()**: Maps each value in the iterator to a new value.
2846+
2. **Iterator.prototype.filter()**: Filters values in the iterator based on a predicate.
2847+
3. **Iterator.prototype.take()**: Takes a specified number of values from the iterator.
2848+
4. **Iterator.prototype.drop()**: Skips a specified number of values from the iterator.
2849+
5. **Iterator.prototype.flatMap()**: Maps each value and flattens the result.
2850+
6. **Iterator.prototype.reduce()**: Reduces the iterator to a single value.
2851+
7. **Iterator.prototype.toArray()**: Converts the iterator to an array.
2852+
8. **Iterator.prototype.forEach()**: Executes a function for each value in the iterator.
2853+
9. **Iterator.prototype.some()**: Checks if some values satisfy a condition.
2854+
10. **Iterator.prototype.every()**: Checks if all values satisfy a condition.
2855+
2856+
Iterator Helpers are particularly useful for processing streams of data in a memory-efficient way:
2857+
2858+
```javascript
2859+
function* generateUserData() {
2860+
yield { id: 1, name: "Alice", age: 25 };
2861+
yield { id: 2, name: "Bob", age: 30 };
2862+
yield { id: 3, name: "Charlie", age: 35 };
2863+
yield { id: 4, name: "Dave", age: 40 };
2864+
}
2865+
2866+
// Find users over 30, extract their names, and take the first 2
2867+
const olderUserNames = generateUserData()[Symbol.iterator]()
2868+
.filter(user => user.age > 30)
2869+
.map(user => user.name)
2870+
.take(2)
2871+
.toArray();
2872+
2873+
console.log(olderUserNames); // ["Charlie", "Dave"]
2874+
```
2875+
2876+
**[⬆ Back to Top](#table-of-contents)**
2877+
2878+
3. ### Temporal API {#temporal-api-es2025}
2879+
2880+
The Temporal API is a new date and time API for JavaScript that addresses many of the limitations of the existing Date object. It provides a modern, immutable, and more intuitive way to work with dates and times.
2881+
2882+
```javascript
2883+
// Get the current date
2884+
const today = Temporal.Now.plainDate();
2885+
console.log(today.toString()); // e.g., "2025-06-01"
2886+
2887+
// Create a specific date
2888+
const date = Temporal.PlainDate.from("2025-06-01");
2889+
console.log("Year:", date.year); // 2025
2890+
console.log("Month:", date.month); // 6
2891+
console.log("Day:", date.day); // 1
2892+
console.log("Day of week:", date.dayOfWeek); // e.g., 7 (Sunday)
2893+
2894+
// Date arithmetic
2895+
const futureDate = date.add({ months: 1, days: 15 });
2896+
console.log(futureDate.toString()); // "2025-07-16"
2897+
2898+
// Calculate duration between dates
2899+
const duration = date.until(futureDate);
2900+
console.log(duration.toString()); // "P1M15D" (1 month, 15 days)
2901+
```
2902+
2903+
The Temporal API includes several objects for different date/time representations:
2904+
2905+
1. **Temporal.Now**: Provides methods to get the current date and time in various formats.
2906+
2. **Temporal.Instant**: Represents a fixed point in time (like a timestamp).
2907+
3. **Temporal.PlainDate**: Represents a calendar date without time or timezone.
2908+
4. **Temporal.PlainTime**: Represents wall-clock time without date or timezone.
2909+
5. **Temporal.PlainDateTime**: Combines date and time without timezone.
2910+
6. **Temporal.ZonedDateTime**: Full date, time, and timezone information.
2911+
7. **Temporal.Duration**: Represents a length of time.
2912+
8. **Temporal.TimeZone**: Represents a timezone.
2913+
9. **Temporal.Calendar**: Represents a calendar system.
2914+
2915+
The Temporal API is particularly useful for applications that need to handle dates and times across different timezones:
2916+
2917+
```javascript
2918+
// Calculate flight duration between timezones
2919+
function calculateFlightDuration(departure, arrival) {
2920+
const departureTZ = Temporal.ZonedDateTime.from(departure);
2921+
const arrivalTZ = Temporal.ZonedDateTime.from(arrival);
2922+
return departureTZ.until(arrivalTZ);
2923+
}
2924+
2925+
const flightDeparture = "2025-06-01T08:30:00-04:00[America/New_York]";
2926+
const flightArrival = "2025-06-01T22:15:00+02:00[Europe/Paris]";
2927+
console.log("Flight duration:", calculateFlightDuration(flightDeparture, flightArrival).toString());
2928+
// e.g., "PT9H45M" (9 hours, 45 minutes)
2929+
```
2930+
2931+
**[⬆ Back to Top](#table-of-contents)**
2932+
2933+
4. ### Decorator Metadata
2934+
2935+
ES2025 enhances JavaScript decorators by allowing them to associate metadata with decorated elements. This provides a standard way to attach and retrieve metadata for classes, methods, and properties.
2936+
2937+
```javascript
2938+
// Define a decorator that attaches metadata
2939+
function apiEndpoint(path, method = "GET") {
2940+
return function(target, context) {
2941+
context.metadata = {
2942+
...context.metadata,
2943+
api: { path, method }
2944+
};
2945+
return target;
2946+
};
2947+
}
2948+
2949+
// Define a class with decorated methods
2950+
class UserService {
2951+
@apiEndpoint("/users", "GET")
2952+
getAllUsers() {
2953+
// Implementation...
2954+
}
2955+
2956+
@apiEndpoint("/users/:id", "GET")
2957+
getUserById(id) {
2958+
// Implementation...
2959+
}
2960+
2961+
@apiEndpoint("/users", "POST")
2962+
createUser(name, age) {
2963+
// Implementation...
2964+
}
2965+
}
2966+
2967+
// Access the metadata
2968+
function generateApiDocs(serviceClass) {
2969+
const methodNames = Object.getOwnPropertyNames(serviceClass.prototype)
2970+
.filter(name => name !== 'constructor');
2971+
2972+
for (const methodName of methodNames) {
2973+
const method = serviceClass.prototype[methodName];
2974+
const metadata = method.context?.metadata;
2975+
2976+
if (metadata && metadata.api) {
2977+
console.log(`${metadata.api.method} ${metadata.api.path} - ${methodName}`);
2978+
}
2979+
}
2980+
}
2981+
2982+
generateApiDocs(UserService);
2983+
// Output:
2984+
// GET /users - getAllUsers
2985+
// GET /users/:id - getUserById
2986+
// POST /users - createUser
2987+
```
2988+
2989+
Decorator Metadata is particularly useful for:
2990+
2991+
1. **API Documentation**: Automatically generating documentation from code.
2992+
2. **Validation**: Defining validation rules for method parameters.
2993+
3. **Dependency Injection**: Specifying dependencies for classes and methods.
2994+
4. **Serialization**: Defining how objects should be serialized/deserialized.
2995+
5. **Framework Integration**: Providing metadata for frameworks to use.
2996+
2997+
```javascript
2998+
// Example of parameter validation with decorator metadata
2999+
function validateParams(target, context) {
3000+
const originalMethod = target.value;
3001+
const methodMetadata = context.metadata;
3002+
3003+
target.value = function(...args) {
3004+
if (methodMetadata && methodMetadata.params) {
3005+
const validations = methodMetadata.params;
3006+
3007+
for (let i = 0; i < validations.length; i++) {
3008+
const validation = validations[i];
3009+
const arg = args[i];
3010+
3011+
if (validation.required && (arg === undefined || arg === null)) {
3012+
throw new Error(`Parameter ${i} is required for ${context.name}`);
3013+
}
3014+
}
3015+
}
3016+
3017+
return originalMethod.apply(this, args);
3018+
};
3019+
3020+
return target;
3021+
}
3022+
```
3023+
3024+
**[⬆ Back to Top](#table-of-contents)**

es2025/1.set-methods.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Set Methods - New in ES2025
2+
// ES2025 introduces several new methods to the Set prototype that make working with sets more convenient
3+
4+
// Create some sample sets
5+
const set1 = new Set([1, 2, 3, 4, 5]);
6+
const set2 = new Set([3, 4, 5, 6, 7]);
7+
const set3 = new Set([5, 6, 7, 8, 9]);
8+
9+
console.log("Original sets:");
10+
console.log("set1:", [...set1]); // [1, 2, 3, 4, 5]
11+
console.log("set2:", [...set2]); // [3, 4, 5, 6, 7]
12+
console.log("set3:", [...set3]); // [5, 6, 7, 8, 9]
13+
14+
// 1. Set.prototype.union()
15+
// Returns a new Set containing all elements from both sets
16+
console.log("\n--- Set.prototype.union() ---");
17+
const union = set1.union(set2);
18+
console.log("set1.union(set2):", [...union]); // [1, 2, 3, 4, 5, 6, 7]
19+
20+
// Union with multiple sets
21+
const multiUnion = set1.union(set2, set3);
22+
console.log("set1.union(set2, set3):", [...multiUnion]); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
23+
24+
// 2. Set.prototype.intersection()
25+
// Returns a new Set containing elements present in all sets
26+
console.log("\n--- Set.prototype.intersection() ---");
27+
const intersection = set1.intersection(set2);
28+
console.log("set1.intersection(set2):", [...intersection]); // [3, 4, 5]
29+
30+
// Intersection with multiple sets
31+
const multiIntersection = set1.intersection(set2, set3);
32+
console.log("set1.intersection(set2, set3):", [...multiIntersection]); // [5]
33+
34+
// 3. Set.prototype.difference()
35+
// Returns a new Set containing elements present in the first set but not in the second
36+
console.log("\n--- Set.prototype.difference() ---");
37+
const difference = set1.difference(set2);
38+
console.log("set1.difference(set2):", [...difference]); // [1, 2]
39+
console.log("set2.difference(set1):", [...set2.difference(set1)]); // [6, 7]
40+
41+
// 4. Set.prototype.symmetricDifference()
42+
// Returns a new Set containing elements present in either set but not in both
43+
console.log("\n--- Set.prototype.symmetricDifference() ---");
44+
const symDifference = set1.symmetricDifference(set2);
45+
console.log("set1.symmetricDifference(set2):", [...symDifference]); // [1, 2, 6, 7]
46+
47+
// 5. Set.prototype.isSubsetOf()
48+
// Returns a boolean indicating if the set is a subset of the given set
49+
console.log("\n--- Set.prototype.isSubsetOf() ---");
50+
const subset = new Set([3, 4]);
51+
console.log("subset:", [...subset]); // [3, 4]
52+
console.log("subset.isSubsetOf(set1):", subset.isSubsetOf(set1)); // true
53+
console.log("set1.isSubsetOf(subset):", set1.isSubsetOf(subset)); // false
54+
55+
// 6. Set.prototype.isSupersetOf()
56+
// Returns a boolean indicating if the set is a superset of the given set
57+
console.log("\n--- Set.prototype.isSupersetOf() ---");
58+
console.log("set1.isSupersetOf(subset):", set1.isSupersetOf(subset)); // true
59+
console.log("subset.isSupersetOf(set1):", subset.isSupersetOf(set1)); // false
60+
61+
// 7. Set.prototype.isDisjointFrom()
62+
// Returns a boolean indicating if the set has no elements in common with the given set
63+
console.log("\n--- Set.prototype.isDisjointFrom() ---");
64+
const disjointSet = new Set([10, 11, 12]);
65+
console.log("disjointSet:", [...disjointSet]); // [10, 11, 12]
66+
console.log("set1.isDisjointFrom(disjointSet):", set1.isDisjointFrom(disjointSet)); // true
67+
console.log("set1.isDisjointFrom(set2):", set1.isDisjointFrom(set2)); // false
68+
69+
// Practical example: Finding common interests between users
70+
console.log("\n--- Practical Example: User Interests ---");
71+
const user1Interests = new Set(["coding", "reading", "music", "hiking"]);
72+
const user2Interests = new Set(["gaming", "music", "movies", "hiking"]);
73+
const user3Interests = new Set(["travel", "photography", "hiking", "cooking"]);
74+
75+
// Find common interests between all users
76+
const commonInterests = user1Interests.intersection(user2Interests, user3Interests);
77+
console.log("Common interests among all users:", [...commonInterests]); // ["hiking"]
78+
79+
// Find unique interests of user1
80+
const uniqueInterests = user1Interests.difference(user2Interests, user3Interests);
81+
console.log("Unique interests of user1:", [...uniqueInterests]); // ["coding", "reading"]
82+
83+
// Find all interests across users
84+
const allInterests = user1Interests.union(user2Interests, user3Interests);
85+
console.log("All interests:", [...allInterests]);
86+
// ["coding", "reading", "music", "hiking", "gaming", "movies", "travel", "photography", "cooking"]

0 commit comments

Comments
 (0)