From b72fc3b066e762c75eb28595bff6b75198625487 Mon Sep 17 00:00:00 2001 From: Julien Deniau Date: Mon, 26 May 2025 22:31:31 +0000 Subject: [PATCH] Require to import --- README.md | 44 +++++++++++++++---------------- type-definitions/immutable.d.ts | 28 ++++++++++---------- website/docs/Collection.Keyed.mdx | 20 +++++++------- website/docs/List.mdx | 13 +++------ website/docs/Seq.Keyed.mdx | 20 +++++++------- website/docs/isImmutable().mdx | 2 +- website/docs/isKeyed().mdx | 2 +- 7 files changed, 62 insertions(+), 67 deletions(-) diff --git a/README.md b/README.md index e823a4213..76cda05a5 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ bun add immutable Then require it into any module. ```js -const { Map } = require('immutable'); +import { Map } from 'immutable'; const map1 = Map({ a: 1, b: 2, c: 3 }); const map2 = map1.set('b', 50); map1.get('b') + ' vs. ' + map2.get('b'); // 2 vs. 50 @@ -135,7 +135,7 @@ lib. Include either `"target": "es2015"` or `"lib": "es2015"` in your `tsc` command. ```js -const { Map } = require('immutable'); +import { Map } from 'immutable'; const map1 = Map({ a: 1, b: 2, c: 3 }); const map2 = map1.set('b', 50); map1.get('b') + ' vs. ' + map2.get('b'); // 2 vs. 50 @@ -184,7 +184,7 @@ treat Immutable.js collections as values, it's important to use the instead of the `===` operator which determines object _reference identity_. ```js -const { Map } = require('immutable'); +import { Map } from 'immutable'; const map1 = Map({ a: 1, b: 2, c: 3 }); const map2 = Map({ a: 1, b: 2, c: 3 }); map1.equals(map2); // true @@ -200,7 +200,7 @@ potentially be more costly. The `===` equality check is also used internally by `Immutable.is` and `.equals()` as a performance optimization. ```js -const { Map } = require('immutable'); +import { Map } from 'immutable'; const map1 = Map({ a: 1, b: 2, c: 3 }); const map2 = map1.set('b', 2); // Set to same value map1 === map2; // true @@ -212,7 +212,7 @@ than the object itself, this results in memory savings and a potential boost in execution speed for programs which rely on copies (such as an undo-stack). ```js -const { Map } = require('immutable'); +import { Map } from 'immutable'; const map = Map({ a: 1, b: 2, c: 3 }); const mapCopy = map; // Look, "copies" are free! ``` @@ -238,7 +238,7 @@ immutable collection. Methods which return new arrays, like `slice` or `concat`, instead return new immutable collections. ```js -const { List } = require('immutable'); +import { List } from 'immutable'; const list1 = List([1, 2]); const list2 = list1.push(3, 4, 5); const list3 = list2.unshift(0); @@ -256,7 +256,7 @@ found on `Immutable.Set`, including collection operations like `forEach()` and `map()`. ```js -const { Map } = require('immutable'); +import { Map } from 'immutable'; const alpha = Map({ a: 1, b: 2, c: 3, d: 4 }); alpha.map((v, k) => k.toUpperCase()).join(); // 'A,B,C,D' @@ -269,7 +269,7 @@ accepts plain JavaScript Arrays and Objects anywhere a method expects a `Collection`. ```js -const { Map, List } = require('immutable'); +import { Map, List } from 'immutable'; const map1 = Map({ a: 1, b: 2, c: 3, d: 4 }); const map2 = Map({ c: 10, a: 20, t: 30 }); const obj = { d: 100, o: 200, g: 300 }; @@ -289,7 +289,7 @@ native API. Because Seq evaluates lazily and does not cache intermediate results, these operations can be extremely efficient. ```js -const { Seq } = require('immutable'); +import { Seq } from 'immutable'; const myObject = { a: 1, b: 2, c: 3 }; Seq(myObject) .map((x) => x * x) @@ -302,7 +302,7 @@ JavaScript Object properties are always strings, even if written in a quote-less shorthand, while Immutable Maps accept keys of any type. ```js -const { fromJS } = require('immutable'); +import { fromJS } from 'immutable'; const obj = { 1: 'one' }; console.log(Object.keys(obj)); // [ "1" ] @@ -325,7 +325,7 @@ to `JSON.stringify` directly. They also respect the custom `toJSON()` methods of nested objects. ```js -const { Map, List } = require('immutable'); +import { Map, List } from 'immutable'; const deep = Map({ a: 1, b: 2, c: List([3, 4, 5]) }); console.log(deep.toObject()); // { a: 1, b: 2, c: List [ 3, 4, 5 ] } console.log(deep.toArray()); // [ 1, 2, List [ 3, 4, 5 ] ] @@ -357,7 +357,7 @@ All Immutable.js collections are [Iterable][iterators], which allows them to be used anywhere an Iterable is expected, such as when spreading into an Array. ```js -const { List } = require('immutable'); +import { List } from 'immutable'; const aList = List([1, 2, 3]); const anArray = [0, ...aList, 4, 5]; // [ 0, 1, 2, 3, 4, 5 ] ``` @@ -376,7 +376,7 @@ The collections in Immutable.js are intended to be nested, allowing for deep trees of data, similar to JSON. ```js -const { fromJS } = require('immutable'); +import { fromJS } from 'immutable'; const nested = fromJS({ a: { b: { c: [3, 4, 5] } } }); // Map { a: Map { b: Map { c: List [ 3, 4, 5 ] } } } ``` @@ -386,7 +386,7 @@ most useful are `mergeDeep`, `getIn`, `setIn`, and `updateIn`, found on `List`, `Map` and `OrderedMap`. ```js -const { fromJS } = require('immutable'); +import { fromJS } from 'immutable'; const nested = fromJS({ a: { b: { c: [3, 4, 5] } } }); const nested2 = nested.mergeDeep({ a: { b: { d: 6 } } }); @@ -419,7 +419,7 @@ const obj1 = { a: 1, b: 2, c: 3 }; const obj2 = { a: 1, b: 2, c: 3 }; obj1 !== obj2; // two different instances are always not equal with === -const { Map, is } = require('immutable'); +import { Map, is } from 'immutable'; const map1 = Map({ a: 1, b: 2, c: 3 }); const map2 = Map({ a: 1, b: 2, c: 3 }); map1 !== map2; // two different instances are not reference-equal @@ -431,7 +431,7 @@ Value equality allows Immutable.js collections to be used as keys in Maps or values in Sets, and retrieved with different but equivalent collections: ```js -const { Map, Set } = require('immutable'); +import { Map, Set } from 'immutable'; const map1 = Map({ a: 1, b: 2, c: 3 }); const map2 = Map({ a: 1, b: 2, c: 3 }); const set = Set().add(map1); @@ -469,7 +469,7 @@ change in _value_ occurred, to allow for efficient _reference equality_ checking to quickly determine if no change occurred. ```js -const { Map } = require('immutable'); +import { Map } from 'immutable'; const originalMap = Map({ a: 1, b: 2, c: 3 }); const updatedMap = originalMap.set('b', 2); updatedMap === originalMap; // No-op .set() returned the original reference. @@ -480,7 +480,7 @@ of these operations occur independently, so two similar updates will not return the same reference: ```js -const { Map } = require('immutable'); +import { Map } from 'immutable'; const originalMap = Map({ a: 1, b: 2, c: 3 }); const updatedMap = originalMap.set('b', 1000); // New instance, leaving the original immutable. @@ -512,7 +512,7 @@ As an example, building `list2` results in the creation of 1, not 3, new immutable Lists. ```js -const { List } = require('immutable'); +import { List } from 'immutable'; const list1 = List([1, 2, 3]); const list2 = list1.withMutations(function (list) { list.push(4).push(5).push(6); @@ -550,7 +550,7 @@ For example, the following performs no work, because the resulting `Seq`'s values are never iterated: ```js -const { Seq } = require('immutable'); +import { Seq } from 'immutable'; const oddSquares = Seq([1, 2, 3, 4, 5, 6, 7, 8]) .filter((x) => x % 2 !== 0) .map((x) => x * x); @@ -567,7 +567,7 @@ oddSquares.get(1); // 9 Any collection can be converted to a lazy Seq with `Seq()`. ```js -const { Map, Seq } = require('immutable'); +import { Map, Seq } from 'immutable'; const map = Map({ a: 1, b: 2, c: 3 }); const lazySeq = Seq(map); ``` @@ -587,7 +587,7 @@ As well as expressing logic that would otherwise seem memory or time limited, for example `Range` is a special kind of Lazy sequence. ```js -const { Range } = require('immutable'); +import { Range } from 'immutable'; Range(1, Infinity) .skip(1000) .map((n) => -n) diff --git a/type-definitions/immutable.d.ts b/type-definitions/immutable.d.ts index b545b80f9..43119ccab 100644 --- a/type-definitions/immutable.d.ts +++ b/type-definitions/immutable.d.ts @@ -1372,7 +1372,7 @@ declare namespace Immutable { * a collection of other sets. * * ```js - * const { Set } = require('immutable') + * import { Set } from 'immutable' * const intersected = Set.intersect([ * Set([ 'a', 'b', 'c' ]) * Set([ 'c', 'a', 't' ]) @@ -1387,7 +1387,7 @@ declare namespace Immutable { * collection of other sets. * * ```js - * const { Set } = require('immutable') + * import { Set } from 'immutable' * const unioned = Set.union([ * Set([ 'a', 'b', 'c' ]) * Set([ 'c', 'a', 't' ]) @@ -2029,7 +2029,7 @@ declare namespace Immutable { * `new` keyword during construction. * * ```js - * const { Range } = require('immutable') + * import { Range } from 'immutable' * Range(10, 15) // [ 10, 11, 12, 13, 14 ] * Range(10, 30, 5) // [ 10, 15, 20, 25 ] * Range(30, 10, 5) // [ 30, 25, 20, 15 ] @@ -2050,7 +2050,7 @@ declare namespace Immutable { * `new` keyword during construction. * * ```js - * const { Repeat } = require('immutable') + * import { Repeat } from 'immutable' * Repeat('foo') // [ 'foo', 'foo', 'foo', ... ] * Repeat('bar', 4) // [ 'bar', 'bar', 'bar', 'bar' ] * ``` @@ -2065,7 +2065,7 @@ declare namespace Immutable { * create Record instances. * * ```js - * const { Record } = require('immutable') + * import { Record } from 'immutable' * const ABRecord = Record({ a: 1, b: 2 }) * const myRecord = ABRecord({ b: 3 }) * ``` @@ -2229,7 +2229,7 @@ declare namespace Immutable { * method. If one was not provided, the string "Record" is returned. * * ```js - * const { Record } = require('immutable') + * import { Record } from 'immutable' * const Person = Record({ * name: null * }, 'Person') @@ -2449,7 +2449,7 @@ declare namespace Immutable { * `Seq`'s values are never iterated: * * ```js - * const { Seq } = require('immutable') + * import { Seq } from 'immutable' * const oddSquares = Seq([ 1, 2, 3, 4, 5, 6, 7, 8 ]) * .filter(x => x % 2 !== 0) * .map(x => x * x) @@ -2557,7 +2557,7 @@ declare namespace Immutable { * `mapper` function. * * ```js - * const { Seq } = require('immutable') + * import { Seq } from 'immutable' * Seq.Keyed({ a: 1, b: 2 }).map(x => 10 * x) * // Seq { "a": 10, "b": 20 } * ``` @@ -2691,7 +2691,7 @@ declare namespace Immutable { * `mapper` function. * * ```js - * const { Seq } = require('immutable') + * import { Seq } from 'immutable' * Seq.Indexed([ 1, 2 ]).map(x => 10 * x) * // Seq [ 10, 20 ] * ``` @@ -2994,7 +2994,7 @@ declare namespace Immutable { * `mapper` function. * * ```js - * const { Seq } = require('immutable') + * import { Seq } from 'immutable' * Seq([ 1, 2 ]).map(x => 10 * x) * // Seq [ 10, 20 ] * ``` @@ -3012,7 +3012,7 @@ declare namespace Immutable { * `mapper` function. * * ```js - * const { Seq } = require('immutable') + * import { Seq } from 'immutable' * Seq([ 1, 2 ]).map(x => 10 * x) * // Seq [ 10, 20 ] * ``` @@ -3171,7 +3171,7 @@ declare namespace Immutable { * `mapper` function. * * ```js - * const { Collection } = require('immutable') + * import { Collection } from 'immutable' * Collection.Keyed({ a: 1, b: 2 }).map(x => 10 * x) * // Seq { "a": 10, "b": 20 } * ``` @@ -3465,7 +3465,7 @@ declare namespace Immutable { * `mapper` function. * * ```js - * const { Collection } = require('immutable') + * import { Collection } from 'immutable' * Collection.Indexed([1,2]).map(x => 10 * x) * // Seq [ 1, 2 ] * ``` @@ -3530,7 +3530,7 @@ declare namespace Immutable { * the value as both the first and second arguments to the provided function. * * ```js - * const { Collection } = require('immutable') + * import { Collection } from 'immutable' * const seq = Collection.Set([ 'A', 'B', 'C' ]) * // Seq { "A", "B", "C" } * seq.forEach((v, k) => diff --git a/website/docs/Collection.Keyed.mdx b/website/docs/Collection.Keyed.mdx index d932babb1..bfcf38d39 100644 --- a/website/docs/Collection.Keyed.mdx +++ b/website/docs/Collection.Keyed.mdx @@ -98,7 +98,7 @@ Returns a new Collection.Keyed of the same type where the keys and values have b ```js -const { Map } = require('immutable'); +import { Map } from 'immutable'; Map({ a: 'z', b: 'y' }).flip(); // Map { "z": "a", "y": "b" } ``` @@ -123,7 +123,7 @@ Returns a new Collection.Keyed with values passed through a `mapper` function. /> ```js -const { Collection } = require('immutable'); +import { Collection } from 'immutable'; Collection.Keyed({ a: 1, b: 2 }).map((x) => 10 * x); // Seq { "a": 10, "b": 20 } ``` @@ -139,7 +139,7 @@ Returns a new Collection.Keyed of the same type with keys passed through a `mapp /> ```js -const { Map } = require('immutable'); +import { Map } from 'immutable'; Map({ a: 1, b: 2 }).mapKeys((x) => x.toUpperCase()); // Map { "A": 1, "B": 2 } ``` @@ -155,7 +155,7 @@ Returns a new Collection.Keyed of the same type with entries ([key, value] tuple /> ```js -const { Map } = require('immutable'); +import { Map } from 'immutable'; Map({ a: 1, b: 2 }).mapEntries(([k, v]) => [k.toUpperCase(), v * 2]); // Map { "A": 2, "B": 4 } ``` @@ -436,7 +436,7 @@ Returns a new Collection of the same type with only the entries for which the `p /> ```js -const { Map } = require('immutable'); +import { Map } from 'immutable'; Map({ a: 1, b: 2, c: 3, d: 4 }).filterNot((x) => x % 2 === 0); // Map { "a": 1, "c": 3 } ``` @@ -466,7 +466,7 @@ If a `comparator` is not provided, a default comparator uses `<` and `>`. - Is pure, i.e. it must always return the same value for the same pair of values. ```js -const { Map } = require('immutable'); +import { Map } from 'immutable'; Map({ c: 3, a: 1, b: 2 }).sort((a, b) => { if (a < b) { return -1; @@ -629,7 +629,7 @@ Returns a new Collection of the same type which includes entries from this Colle /> ```js -const { List } = require('immutable'); +import { List } from 'immutable'; List(['dog', 'frog', 'cat', 'hat', 'god']).takeWhile((x) => x.match(/o/)); // List [ "dog", "frog" ] ``` @@ -643,7 +643,7 @@ Returns a new Collection of the same type which includes entries from this Colle /> ```js -const { List } = require('immutable'); +import { List } from 'immutable'; List(['dog', 'frog', 'cat', 'hat', 'god']).takeUntil((x) => x.match(/at/)); // List [ "dog", "frog" ] ``` @@ -829,7 +829,7 @@ Like `max`, but also accepts a `comparatorValueMapper` which allows for comparin /> ```js -const { List } = require('immutable'); +import { List } from 'immutable'; const l = List([ { name: 'Bob', avgHit: 1 }, { name: 'Max', avgHit: 3 }, @@ -859,7 +859,7 @@ Like `min`, but also accepts a `comparatorValueMapper` which allows for comparin /> ```js -const { List } = require('immutable'); +import { List } from 'immutable'; const l = List([ { name: 'Bob', avgHit: 1 }, { name: 'Max', avgHit: 3 }, diff --git a/website/docs/List.mdx b/website/docs/List.mdx index 2f2424700..35c71ea48 100644 --- a/website/docs/List.mdx +++ b/website/docs/List.mdx @@ -629,9 +629,7 @@ The returned Seq will have identical iteration order as this List. `} /> v === 'B') // Seq [ "B" ] @@ -949,16 +947,14 @@ Returns the value found by following a path of keys or indices through nested Co /> Plain JavaScript Object or Arrays may be nested within an Immutable.js Collection, and getIn() can access those values as well: @@ -1011,8 +1007,7 @@ This is similar to `List(collection)`, but provided to allow for chained express `} /> diff --git a/website/docs/Seq.Keyed.mdx b/website/docs/Seq.Keyed.mdx index c81a37632..951b7f3bd 100644 --- a/website/docs/Seq.Keyed.mdx +++ b/website/docs/Seq.Keyed.mdx @@ -99,7 +99,7 @@ Returns a new Collection.Keyed of the same type where the keys and values have b ```js -const { Map } = require('immutable'); +import { Map } from 'immutable'; Map({ a: 'z', b: 'y' }).flip(); // Map { "z": "a", "y": "b" } ``` @@ -129,7 +129,7 @@ Returns a new Seq.Keyed with values passed through a `mapper` function. /> ```js -const { Seq } = require('immutable'); +import { Seq } from 'immutable'; Seq.Keyed({ a: 1, b: 2 }).map((x) => 10 * x); // Seq { "a": 10, "b": 20 } ``` @@ -145,7 +145,7 @@ Returns a new Collection.Keyed of the same type with keys passed through a `mapp /> ```js -const { Map } = require('immutable'); +import { Map } from 'immutable'; Map({ a: 1, b: 2 }).mapKeys((x) => x.toUpperCase()); // Map { "A": 1, "B": 2 } ``` @@ -161,7 +161,7 @@ Returns a new Collection.Keyed of the same type with entries ([key, value] tuple /> ```js -const { Map } = require('immutable'); +import { Map } from 'immutable'; Map({ a: 1, b: 2 }).mapEntries(([k, v]) => [k.toUpperCase(), v * 2]); // Map { "A": 2, "B": 4 } ``` @@ -442,7 +442,7 @@ Returns a new Collection of the same type with only the entries for which the `p /> ```js -const { Map } = require('immutable'); +import { Map } from 'immutable'; Map({ a: 1, b: 2, c: 3, d: 4 }).filterNot((x) => x % 2 === 0); // Map { "a": 1, "c": 3 } ``` @@ -472,7 +472,7 @@ If a `comparator` is not provided, a default comparator uses `<` and `>`. - Is pure, i.e. it must always return the same value for the same pair of values. ```js -const { Map } = require('immutable'); +import { Map } from 'immutable'; Map({ c: 3, a: 1, b: 2 }).sort((a, b) => { if (a < b) { return -1; @@ -635,7 +635,7 @@ Returns a new Collection of the same type which includes entries from this Colle /> ```js -const { List } = require('immutable'); +import { List } from 'immutable'; List(['dog', 'frog', 'cat', 'hat', 'god']).takeWhile((x) => x.match(/o/)); // List [ "dog", "frog" ] ``` @@ -649,7 +649,7 @@ Returns a new Collection of the same type which includes entries from this Colle /> ```js -const { List } = require('immutable'); +import { List } from 'immutable'; List(['dog', 'frog', 'cat', 'hat', 'god']).takeUntil((x) => x.match(/at/)); // List [ "dog", "frog" ] ``` @@ -835,7 +835,7 @@ Like `max`, but also accepts a `comparatorValueMapper` which allows for comparin /> ```js -const { List } = require('immutable'); +import { List } from 'immutable'; const l = List([ { name: 'Bob', avgHit: 1 }, { name: 'Max', avgHit: 3 }, @@ -865,7 +865,7 @@ Like `min`, but also accepts a `comparatorValueMapper` which allows for comparin /> ```js -const { List } = require('immutable'); +import { List } from 'immutable'; const l = List([ { name: 'Bob', avgHit: 1 }, { name: 'Max', avgHit: 3 }, diff --git a/website/docs/isImmutable().mdx b/website/docs/isImmutable().mdx index 993ecaeac..0e67a46c0 100644 --- a/website/docs/isImmutable().mdx +++ b/website/docs/isImmutable().mdx @@ -10,7 +10,7 @@ True if `maybeImmutable` is an Immutable Collection or Record. Note: Still returns true even if the collections is within a `withMutations()`. ```js -const { isImmutable, Map, List, Stack } = require('immutable'); +import { isImmutable, Map, List, Stack } from 'immutable'; isImmutable([]); // false isImmutable({}); // false isImmutable(Map()); // true diff --git a/website/docs/isKeyed().mdx b/website/docs/isKeyed().mdx index 574f4a4d8..1d73cb53b 100644 --- a/website/docs/isKeyed().mdx +++ b/website/docs/isKeyed().mdx @@ -8,7 +8,7 @@ True if `maybeKeyed` is a Collection.Keyed, or any of its subclasses. ```js -const { isKeyed, Map, List, Stack } = require('immutable'); +import { isKeyed, Map, List, Stack } from 'immutable'; isKeyed([]); // false isKeyed({}); // false isKeyed(Map()); // true