tutuca 0.9.39 → 0.9.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/tutuca-cli.js +92 -39
- package/dist/tutuca-dev.js +18 -0
- package/dist/tutuca-dev.min.js +1 -1
- package/package.json +6 -7
- package/skill/immutable-js/SKILL.md +79 -0
- package/skill/immutable-js/references/collection.md +346 -0
- package/skill/immutable-js/references/conversions.md +99 -0
- package/skill/immutable-js/references/deep-updates.md +172 -0
- package/skill/immutable-js/references/equality.md +95 -0
- package/skill/immutable-js/references/list.md +266 -0
- package/skill/immutable-js/references/map.md +300 -0
- package/skill/immutable-js/references/predicates.md +93 -0
- package/skill/immutable-js/references/range-repeat.md +55 -0
- package/skill/immutable-js/references/record.md +196 -0
- package/skill/immutable-js/references/seq.md +248 -0
- package/skill/immutable-js/references/set.md +270 -0
- package/skill/immutable-js/references/shallow-functional.md +99 -0
- package/skill/immutable-js/references/stack.md +210 -0
- package/skill/margaui/SKILL.md +101 -0
- package/skill/margaui/components/accordion.md +127 -0
- package/skill/margaui/components/alert.md +174 -0
- package/skill/margaui/components/avatar.md +220 -0
- package/skill/margaui/components/badge.md +193 -0
- package/skill/margaui/components/breadcrumbs.md +103 -0
- package/skill/margaui/components/button.md +322 -0
- package/skill/margaui/components/calendar.md +67 -0
- package/skill/margaui/components/card.md +373 -0
- package/skill/margaui/components/carousel.md +387 -0
- package/skill/margaui/components/chat.md +171 -0
- package/skill/margaui/components/checkbox.md +101 -0
- package/skill/margaui/components/collapse.md +172 -0
- package/skill/margaui/components/countdown.md +165 -0
- package/skill/margaui/components/diff.md +53 -0
- package/skill/margaui/components/divider.md +107 -0
- package/skill/margaui/components/dock.md +173 -0
- package/skill/margaui/components/drawer.md +184 -0
- package/skill/margaui/components/dropdown.md +388 -0
- package/skill/margaui/components/fab.md +346 -0
- package/skill/margaui/components/fieldset.md +88 -0
- package/skill/margaui/components/file-input.md +84 -0
- package/skill/margaui/components/filter.md +52 -0
- package/skill/margaui/components/footer.md +583 -0
- package/skill/margaui/components/hero.md +135 -0
- package/skill/margaui/components/hover-3d.md +129 -0
- package/skill/margaui/components/hover-gallery.md +49 -0
- package/skill/margaui/components/indicator.md +265 -0
- package/skill/margaui/components/input.md +389 -0
- package/skill/margaui/components/join.md +100 -0
- package/skill/margaui/components/kbd.md +127 -0
- package/skill/margaui/components/label.md +102 -0
- package/skill/margaui/components/link.md +96 -0
- package/skill/margaui/components/list.md +182 -0
- package/skill/margaui/components/loading.md +105 -0
- package/skill/margaui/components/mask.md +168 -0
- package/skill/margaui/components/menu.md +856 -0
- package/skill/margaui/components/mockup-browser.md +39 -0
- package/skill/margaui/components/mockup-code.md +81 -0
- package/skill/margaui/components/mockup-phone.md +39 -0
- package/skill/margaui/components/mockup-window.md +33 -0
- package/skill/margaui/components/modal.md +178 -0
- package/skill/margaui/components/navbar.md +282 -0
- package/skill/margaui/components/pagination.md +122 -0
- package/skill/margaui/components/progress.md +135 -0
- package/skill/margaui/components/radial-progress.md +67 -0
- package/skill/margaui/components/radio.md +133 -0
- package/skill/margaui/components/range.md +134 -0
- package/skill/margaui/components/rating.md +170 -0
- package/skill/margaui/components/select.md +225 -0
- package/skill/margaui/components/skeleton.md +64 -0
- package/skill/margaui/components/stack.md +142 -0
- package/skill/margaui/components/stat.md +254 -0
- package/skill/margaui/components/status.md +73 -0
- package/skill/margaui/components/steps.md +138 -0
- package/skill/margaui/components/swap.md +152 -0
- package/skill/margaui/components/tab.md +248 -0
- package/skill/margaui/components/table.md +1018 -0
- package/skill/margaui/components/text-rotate.md +91 -0
- package/skill/margaui/components/textarea.md +85 -0
- package/skill/margaui/components/theme-controller.md +266 -0
- package/skill/margaui/components/timeline.md +1356 -0
- package/skill/margaui/components/toast.md +165 -0
- package/skill/margaui/components/toggle.md +135 -0
- package/skill/margaui/components/tooltip.md +181 -0
- package/skill/margaui/components/validator.md +163 -0
- package/skill/{advanced.md → tutuca/advanced.md} +5 -0
- package/skill/{cli.md → tutuca/cli.md} +17 -0
- package/skill/{core.md → tutuca/core.md} +5 -0
- /package/skill/{SKILL.md → tutuca/SKILL.md} +0 -0
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
`Seq` is a lazy, immutable sequence. Operations such as `map`, `filter`, `take`, and `flatMap` build up a description of computation that is not executed until values are pulled out of the sequence. Use `Seq` for chaining transforms without allocating intermediate concrete collections, for representing infinite or potentially-infinite sequences, and for cheap lazy views over existing collections (arrays, objects, other `Collection`s). Once a `Seq` is consumed via a strict (terminating) operation, the chain may cache its result internally if `cacheResult()` was called; otherwise re-iteration re-runs the upstream chain.
|
|
2
|
+
|
|
3
|
+
`Seq` is a conversion function, not a class — never use `new`. The `Seq` constructor inspects its argument and returns `Seq.Keyed`, `Seq.Indexed`, or (when given a `Collection.Set`) `Seq.Set`.
|
|
4
|
+
|
|
5
|
+
## Laziness — what to expect
|
|
6
|
+
|
|
7
|
+
- `seq.map(...).filter(...).take(10)` performs zero work until something pulls values
|
|
8
|
+
- Terminating ops that materialize / iterate: `toArray`, `toJS`, `toJSON`, `toList`, `toMap`, `toOrderedMap`, `toSet`, `toOrderedSet`, `toStack`, `toObject`, `forEach`, `reduce`, `reduceRight`, `every`, `some`, `find`, `findEntry`, `findLast`, `count`, `countBy`, `join`, `get`, `first`, `last`, and `for..of` iteration
|
|
9
|
+
- `toSeq()` is a no-op on a `Seq` and stays lazy
|
|
10
|
+
- Infinite sequences (e.g., from `Range(1, Infinity)`, `Repeat('x')`) are fine as long as a downstream operation bounds them via `take`, `slice`, `takeWhile`, `takeUntil`, or a short-circuiting op like `find`/`some`
|
|
11
|
+
- Side effects placed inside `map`/`filter` callbacks do NOT run at chain-build time — they run when (and as many times as) values are pulled. Each terminating call re-runs the chain unless `cacheResult()` was inserted
|
|
12
|
+
- Short-circuit consumers (`find`, `some`, `every`, `get(i)`) only iterate as far as needed: `Seq([1,2,3,4]).filter(odd).map(square).get(1)` calls the filter three times and the map exactly once
|
|
13
|
+
|
|
14
|
+
## The three Seq variants
|
|
15
|
+
|
|
16
|
+
- `Seq.Keyed<K, V>` — key/value pairs; iteration yields `[K, V]` entries. Adds `flip`, `mapKeys`, `mapEntries`. Constructed from objects, `[K, V]` iterables, or other keyed collections.
|
|
17
|
+
- `Seq.Indexed<T>` — ordered values with numeric indices; iteration yields `T`. Adds `interpose`, `interleave`, `splice`, `zip`, `zipAll`, `zipWith`. Constructed from arrays, array-likes, or non-keyed iterables.
|
|
18
|
+
- `Seq.Set<T>` — values where keys equal values; iteration yields `T`. Inherits `Collection.Set` ops (`union`, `intersect`, `subtract`, `isSubset`, `isSuperset`). Because `Seq` is lazy, `Seq.Set` does NOT enforce uniqueness eagerly — duplicates may flow through until a strict `Set`/`OrderedSet` materialization deduplicates.
|
|
19
|
+
|
|
20
|
+
## Construction
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { Seq, Range, Repeat } from 'immutable';
|
|
24
|
+
|
|
25
|
+
Seq(); // empty, untyped
|
|
26
|
+
Seq(iterable); // dispatched: Indexed/Keyed/Set
|
|
27
|
+
Seq.Keyed<K, V>(entries?: Iterable<[K,V]>);
|
|
28
|
+
Seq.Keyed<V>(obj: { [k: string]: V });
|
|
29
|
+
Seq.Indexed<T>(collection?: Iterable<T> | ArrayLike<T>);
|
|
30
|
+
Seq.Indexed.of<T>(...values: T[]);
|
|
31
|
+
Seq.Set<T>(collection?: Iterable<T> | ArrayLike<T>);
|
|
32
|
+
Seq.Set.of<T>(...values: T[]);
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
`Range(start?, end?, step?)` and `Repeat(value, times?)` produce `Seq.Indexed` instances and are the canonical way to create infinite indexed sequences (see range-repeat.md).
|
|
36
|
+
|
|
37
|
+
## Construction examples
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { Seq, Range, Map } from 'immutable';
|
|
41
|
+
|
|
42
|
+
// Array → Seq.Indexed
|
|
43
|
+
Seq([1, 2, 3]); // Seq.Indexed [ 1, 2, 3 ]
|
|
44
|
+
|
|
45
|
+
// Object → Seq.Keyed
|
|
46
|
+
Seq({ a: 1, b: 2 }); // Seq.Keyed { "a": 1, "b": 2 }
|
|
47
|
+
|
|
48
|
+
// Wrapping an existing concrete collection preserves variant
|
|
49
|
+
Seq(Map({ a: 1, b: 2 })); // Seq.Keyed
|
|
50
|
+
Seq.Indexed.of(1, 2, 3); // Seq.Indexed [ 1, 2, 3 ]
|
|
51
|
+
Seq.Set.of('a', 'b', 'a'); // Seq.Set (duplicates not yet collapsed)
|
|
52
|
+
|
|
53
|
+
// Lazy chain — map/filter run only on toArray()
|
|
54
|
+
Seq([1, 2, 3, 4])
|
|
55
|
+
.map(x => x * x)
|
|
56
|
+
.filter(x => x > 4)
|
|
57
|
+
.toArray(); // [ 9, 16 ]
|
|
58
|
+
|
|
59
|
+
// Infinite sequence, bounded by take
|
|
60
|
+
Range(1, Infinity) // 1, 2, 3, ...
|
|
61
|
+
.map(x => x * x)
|
|
62
|
+
.take(5)
|
|
63
|
+
.toList(); // List [ 1, 4, 9, 16, 25 ]
|
|
64
|
+
|
|
65
|
+
// Short-circuit: only iterates until first match
|
|
66
|
+
Range(1, Infinity).find(n => n * n > 100); // 11
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Methods unique-or-emphasized for Seq
|
|
70
|
+
|
|
71
|
+
Most behavior is inherited from `Collection`, `Collection.Keyed`, `Collection.Indexed`, and `Collection.Set` (see collection.md). The methods below are either unique to `Seq` or behave differently because of laziness.
|
|
72
|
+
|
|
73
|
+
### cacheResult
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
cacheResult(): this
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Forces the current chain to evaluate once, caches results in memory, and returns a new `Seq` whose downstream ops iterate the cache. After `cacheResult()` the `Seq` always has a defined `size`. Use when a `Seq` will be iterated multiple times and the upstream chain is expensive — without it, each terminating op re-runs the whole chain.
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
const squares = Seq([1, 2, 3]).map(x => x * x).cacheResult();
|
|
83
|
+
squares.join() + squares.join(); // map runs 3 times total, not 6
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### toSeq
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
toSeq(): this
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Returns the same `Seq` (no-op). Each variant declares this so that chains stay typed at the same variant.
|
|
93
|
+
|
|
94
|
+
### map / filter / flatMap / partition / concat
|
|
95
|
+
|
|
96
|
+
Lazy on `Seq` — these do not allocate any concrete collection. The variant-typed overloads (`Seq.Keyed`, `Seq.Indexed`, `Seq.Set`) preserve the variant in the return type. Behavior matches the inherited `Collection` versions; see collection.md for full signatures.
|
|
97
|
+
|
|
98
|
+
### size
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
readonly size: number | undefined;
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
May be `undefined` when the size cannot be determined without iterating. Operations that change cardinality unpredictably — notably `filter` — yield a `Seq` with `size === undefined`. `Seq`s built from arrays, objects, `Range`, `Repeat`, and `cacheResult()` always have a numeric `size` (which is `Infinity` for unbounded ranges/repeats).
|
|
105
|
+
|
|
106
|
+
### Materialization helpers
|
|
107
|
+
|
|
108
|
+
`toList`, `toMap`, `toOrderedMap`, `toSet`, `toOrderedSet`, `toStack`, `toArray`, `toObject`, `toJS`, `toJSON` all force evaluation. On infinite sequences they will hang — bound the sequence with `take`/`slice` first. The variant of the resulting concrete collection follows the destination, not the source: a `Seq.Indexed` consumed via `toMap()` becomes a `Map<number, T>` keyed by index.
|
|
109
|
+
|
|
110
|
+
### Seq.isSeq
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
Seq.isSeq(maybeSeq: unknown): maybeSeq is Seq.Indexed<unknown> | Seq.Keyed<unknown, unknown> | Seq.Set<unknown>
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
True only for actual `Seq` instances (not for backing concrete collections like `List`/`Map`/`Set`). Prefer the broader `isIndexed`, `isKeyed`, `isAssociative` predicates from predicates.md when the only thing you care about is shape.
|
|
117
|
+
|
|
118
|
+
## Variant-specific highlights
|
|
119
|
+
|
|
120
|
+
Each variant inherits everything from the matching `Collection.X` (see collection.md). Below are the methods you will reach for on a `Seq` that are specific to that shape.
|
|
121
|
+
|
|
122
|
+
### Seq.Keyed
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
flip(): Seq.Keyed<V, K>
|
|
126
|
+
mapKeys<M>(mapper: (key: K, value: V, iter: this) => M, context?: unknown): Seq.Keyed<M, V>
|
|
127
|
+
mapEntries<KM, VM>(
|
|
128
|
+
mapper: (entry: [K, V], index: number, iter: this) => [KM, VM] | undefined,
|
|
129
|
+
context?: unknown
|
|
130
|
+
): Seq.Keyed<KM, VM>
|
|
131
|
+
concat<KC, VC>(...colls: Array<Iterable<[KC, VC]>>): Seq.Keyed<K | KC, V | VC>
|
|
132
|
+
toArray(): Array<[K, V]>
|
|
133
|
+
toJS(): { [key in PropertyKey]: DeepCopy<V> }
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
`flip` swaps keys and values lazily. `mapKeys` rewrites only the keys. `mapEntries` rewrites both as a tuple, and returning `undefined` from the mapper drops the entry (a lazy filter+map combined).
|
|
137
|
+
|
|
138
|
+
### Seq.Indexed
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
zip<U>(other: Collection<unknown, U>): Seq.Indexed<[T, U]>
|
|
142
|
+
zipAll<U>(other: Collection<unknown, U>): Seq.Indexed<[T, U]> // pads with undefined
|
|
143
|
+
zipWith<U, Z>(zipper: (a: T, b: U) => Z, other: Collection<unknown, U>): Seq.Indexed<Z>
|
|
144
|
+
concat<C>(...valuesOrCollections: Array<Iterable<C> | C>): Seq.Indexed<T | C>
|
|
145
|
+
toArray(): Array<T>
|
|
146
|
+
toJS(): Array<DeepCopy<T>>
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
Inherits `interpose`, `interleave`, `splice`, `flatten`, `findIndex`, `indexOf`, etc. from `Collection.Indexed` (see collection.md). All remain lazy.
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
Seq([1, 2, 3]).zip(Seq(['a', 'b', 'c'])); // Seq [ [1,'a'], [2,'b'], [3,'c'] ]
|
|
153
|
+
Seq([1, 2, 3]).interpose(0).toArray(); // [ 1, 0, 2, 0, 3 ]
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Seq.Set
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
concat<U>(...collections: Array<Iterable<U>>): Seq.Set<T | U>
|
|
160
|
+
toArray(): Array<T>
|
|
161
|
+
toJS(): Array<DeepCopy<T>>
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Inherits `union`, `intersect`, `subtract`, `isSubset`, `isSuperset` from `Collection.Set` (see collection.md). Note: because evaluation is deferred, duplicate values produced by `union`/`concat` only get deduplicated when materialized into a real `Set` or `OrderedSet`.
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
Seq.Set.of(1, 2, 3)
|
|
168
|
+
.union([3, 4, 5])
|
|
169
|
+
.toSet(); // Set { 1, 2, 3, 4, 5 } — dedup at toSet
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Gotchas
|
|
173
|
+
|
|
174
|
+
- Side effects in `map`/`filter`/`flatMap` callbacks fire on consumption, not at chain-build time, and re-fire on every iteration unless `cacheResult()` was called
|
|
175
|
+
- Re-iterating a `Seq` re-runs the entire upstream chain — use `cacheResult()` if you will iterate twice or more, but be aware it must fully evaluate (so do not call it on an infinite `Seq`)
|
|
176
|
+
- `.size` may be `undefined` after `filter` (and similar shape-changing ops) and `Infinity` for unbounded `Range`/`Repeat`; never assume `.size` returns a finite number
|
|
177
|
+
- Strict materializers (`toList`, `toArray`, `toJS`, `toMap`, `toSet`, `forEach`, `reduce`, `every`, `count`, `join`) will hang on an unbounded `Seq` — bound it with `take`/`slice`/`takeWhile` or use a short-circuiting op like `find`/`some`
|
|
178
|
+
- An iterator object (one without `Symbol.iterator` returning `this`) passed to `Seq()` is treated as a plain object and becomes a `Seq.Keyed` over its enumerable keys, which is rarely what you want — wrap it in an iterable first or use `Seq.Indexed(Array.from(iter))`
|
|
179
|
+
- `Seq.Set` does not enforce value uniqueness; only the eager `Set`/`OrderedSet` collections do
|
|
180
|
+
- `Seq.Keyed` allows duplicate keys to flow through the chain — they are only collapsed when converted to a `Map`/`OrderedMap` (last write wins)
|
|
181
|
+
- `Seq` is NOT `Collection` — `isCollection(seq)` is true, but `isList(seq)`/`isMap(seq)`/`isSet(seq)` are all false. Use `isSeq` plus `isIndexed`/`isKeyed` for runtime checks
|
|
182
|
+
- `Seq` does not have a single canonical "empty" instance — `Seq()` returns a fresh empty seq with `K`/`V` defaulted to `unknown`; provide type parameters when needed
|
|
183
|
+
|
|
184
|
+
## Common patterns
|
|
185
|
+
|
|
186
|
+
### Lazy view over a concrete collection
|
|
187
|
+
|
|
188
|
+
Calling `.toSeq()` on a `List`, `Map`, `Set`, etc. returns the matching `Seq` variant, letting you chain transforms without allocating an intermediate `List`/`Map`/`Set` per step:
|
|
189
|
+
|
|
190
|
+
```ts
|
|
191
|
+
import { List } from 'immutable';
|
|
192
|
+
|
|
193
|
+
const list = List([1, 2, 3, 4, 5]);
|
|
194
|
+
const result = list
|
|
195
|
+
.toSeq() // Seq.Indexed view
|
|
196
|
+
.map(x => x * 2)
|
|
197
|
+
.filter(x => x > 4)
|
|
198
|
+
.reduce((sum, x) => sum + x, 0); // 24
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
### Caching a chain used twice
|
|
202
|
+
|
|
203
|
+
```ts
|
|
204
|
+
const expensive = Seq(rows)
|
|
205
|
+
.map(parseRow)
|
|
206
|
+
.filter(isValid)
|
|
207
|
+
.cacheResult(); // run parseRow/isValid exactly once
|
|
208
|
+
|
|
209
|
+
const count = expensive.count();
|
|
210
|
+
const first = expensive.first(); // does NOT re-parse
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### Bounded infinite sequence
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
import { Range } from 'immutable';
|
|
217
|
+
|
|
218
|
+
// First 10 primes — Range is infinite, take bounds it
|
|
219
|
+
Range(2, Infinity)
|
|
220
|
+
.filter(isPrime)
|
|
221
|
+
.take(10)
|
|
222
|
+
.toArray();
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
### Object → keyed transform → object
|
|
226
|
+
|
|
227
|
+
```ts
|
|
228
|
+
Seq({ a: 1, b: 2, c: 3 })
|
|
229
|
+
.filter(v => v > 1)
|
|
230
|
+
.mapKeys(k => k.toUpperCase())
|
|
231
|
+
.toObject(); // { B: 2, C: 3 }
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
### Building from generators
|
|
235
|
+
|
|
236
|
+
A generator function returns an iterator. Wrap it in `Seq.Indexed` (don't pass the iterator directly to `Seq()`, which would treat it as a keyed object):
|
|
237
|
+
|
|
238
|
+
```ts
|
|
239
|
+
function* nats() { let n = 0; while (true) yield n++; }
|
|
240
|
+
Seq.Indexed(nats()).take(5).toArray(); // [ 0, 1, 2, 3, 4 ]
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## See also
|
|
244
|
+
|
|
245
|
+
- collection.md — parent classes (`Collection`, `Collection.Keyed`, `Collection.Indexed`, `Collection.Set`) supply the bulk of the inherited API: `take`, `skip`, `slice`, `reverse`, `sort`, `sortBy`, `groupBy`, `count`, `find`, `every`, `some`, `reduce`, `flatten`, `interpose`, `interleave`, `splice`, `union`, `intersect`, `subtract`, etc.
|
|
246
|
+
- range-repeat.md — `Range()` and `Repeat()` produce `Seq.Indexed` and are the idiomatic way to build infinite indexed sequences
|
|
247
|
+
- list.md, map.md, set.md — eager equivalents; convert with `toList()`, `toMap()`, `toSet()` once a chain should be materialized
|
|
248
|
+
- predicates.md — `isSeq`, `isIndexed`, `isKeyed`, `isAssociative` for runtime type checks
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
`Set` is an unordered collection of unique values with `O(log32 N)` `add` and `has`. Uniqueness is determined by `Immutable.is` value-equality, not reference identity, so `Set([Map({a: 1}), Map({a: 1})]).size` is `1` (a native `Set` would yield `2`). Iteration order is undefined but stable across iterations of the same instance. `OrderedSet` is a variant that preserves insertion order at the cost of more memory and slower (amortized, non-stable) `add`. Both are factory functions — call them without `new`.
|
|
2
|
+
|
|
3
|
+
```js
|
|
4
|
+
import { Set, OrderedSet } from 'immutable';
|
|
5
|
+
```
|
|
6
|
+
|
|
7
|
+
## Construction
|
|
8
|
+
|
|
9
|
+
### Set
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
Set<T>(collection?: Iterable<T> | ArrayLike<T>): Set<T>
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Empty when called with no argument; otherwise dedupes the input.
|
|
16
|
+
|
|
17
|
+
### Set.of
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
Set.of<T>(...values: Array<T>): Set<T>
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Variadic equivalent of `Set([...])`.
|
|
24
|
+
|
|
25
|
+
### Set.fromKeys
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
Set.fromKeys<T>(iter: Collection.Keyed<T, unknown>): Set<T>
|
|
29
|
+
Set.fromKeys<T>(iter: Collection<T, unknown>): Set<T>
|
|
30
|
+
Set.fromKeys(obj: { [key: string]: unknown }): Set<string>
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Builds a Set from the keys of a Keyed collection or plain object.
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
Set.fromKeys({ a: 1, b: 2 }); // => Set { "a", "b" }
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Set.intersect / Set.union (static)
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
Set.intersect<T>(sets: Iterable<Iterable<T>>): Set<T>
|
|
43
|
+
Set.union<T>(sets: Iterable<Iterable<T>>): Set<T>
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Static set-algebra over an iterable of iterables (note: a single argument, unlike the instance methods).
|
|
47
|
+
|
|
48
|
+
## Reading
|
|
49
|
+
|
|
50
|
+
### has / includes
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
has(value: T): boolean
|
|
54
|
+
includes(value: T): boolean // alias: contains
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Membership test using `Immutable.is`.
|
|
58
|
+
|
|
59
|
+
### size
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
readonly size: number
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### first / last
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
first<NSV>(notSetValue: NSV): T | NSV
|
|
69
|
+
first(): T | undefined
|
|
70
|
+
last<NSV>(notSetValue: NSV): T | NSV
|
|
71
|
+
last(): T | undefined
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
For a plain `Set`, "first"/"last" follow the stable but undefined iteration order; for `OrderedSet`, they follow insertion order.
|
|
75
|
+
|
|
76
|
+
### find
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
find(predicate: (value: T, key: T, iter: this) => boolean, context?: unknown, notSetValue?: T): T | undefined
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
First value for which `predicate` returns truthy. Companions: `findLast`, `findEntry`, `findKey`, `keyOf` (note: in a Set, key === value).
|
|
83
|
+
|
|
84
|
+
## Persistent changes
|
|
85
|
+
|
|
86
|
+
### add
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
add(value: T): this
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Returns a Set including `value`. No-op (returns same instance) if already present. Safe in `withMutations`.
|
|
93
|
+
|
|
94
|
+
### delete / remove
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
delete(value: T): this
|
|
98
|
+
remove(value: T): this // alias of delete
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Returns a Set without `value`. Safe in `withMutations`. Prefer `remove` if targeting IE8.
|
|
102
|
+
|
|
103
|
+
### clear
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
clear(): this
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Returns an empty Set of the same type.
|
|
110
|
+
|
|
111
|
+
### union / merge / concat
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
union<C>(...collections: Array<Iterable<C>>): Set<T | C>
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Adds every value from each provided iterable. Variadic — accepts multiple iterables in one call. `merge` and `concat` are aliases.
|
|
118
|
+
|
|
119
|
+
### intersect
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
intersect(...collections: Array<Iterable<T>>): this
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Keeps only values present in this Set AND every provided iterable. Variadic.
|
|
126
|
+
|
|
127
|
+
### subtract
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
subtract(...collections: Array<Iterable<T>>): this
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Removes every value contained in any of the provided iterables. Variadic.
|
|
134
|
+
|
|
135
|
+
```js
|
|
136
|
+
Set([1, 2, 3]).subtract([1, 3]); // => Set { 2 }
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
All four set-algebra instance methods are safe in `withMutations`.
|
|
140
|
+
|
|
141
|
+
## Sequence algorithms
|
|
142
|
+
|
|
143
|
+
### map
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
map<M>(mapper: (value: T, key: T, iter: this) => M, context?: unknown): Set<M>
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
In a Set, `key === value` for every callback.
|
|
150
|
+
|
|
151
|
+
### flatMap
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
flatMap<M>(mapper: (value: T, key: T, iter: this) => Iterable<M>, context?: unknown): Set<M>
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### filter / filterNot
|
|
158
|
+
|
|
159
|
+
```ts
|
|
160
|
+
filter<F extends T>(predicate: (value: T, key: T, iter: this) => value is F, context?: unknown): Set<F>
|
|
161
|
+
filter(predicate: (value: T, key: T, iter: this) => unknown, context?: unknown): this
|
|
162
|
+
filterNot(predicate: (value: T, key: T, iter: this) => boolean, context?: unknown): this
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
Always return a new instance. The type-guard overload of `filter` narrows the element type.
|
|
166
|
+
|
|
167
|
+
### sort / sortBy
|
|
168
|
+
|
|
169
|
+
```ts
|
|
170
|
+
sort(comparator?: Comparator<T>): this & OrderedSet<T>
|
|
171
|
+
sortBy<C>(comparatorValueMapper: (value: T, key: T, iter: this) => C, comparator?: (a: C, b: C) => number): this & OrderedSet<T>
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
Eager. Returns an `OrderedSet`. Default comparator uses `<`/`>`. The comparator may return a `PairSorting` enum value.
|
|
175
|
+
|
|
176
|
+
### groupBy
|
|
177
|
+
|
|
178
|
+
```ts
|
|
179
|
+
groupBy<G>(grouper: (value: T, key: T, iter: this) => G, context?: unknown): Map<G, Set<T>>
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
Eager. Buckets values into a `Map` of `Set`s.
|
|
183
|
+
|
|
184
|
+
### partition
|
|
185
|
+
|
|
186
|
+
```ts
|
|
187
|
+
partition(predicate: (value: T, key: T, iter: this) => boolean, context?: unknown): [this, this]
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
Returns `[falseValues, trueValues]`.
|
|
191
|
+
|
|
192
|
+
## Conversion
|
|
193
|
+
|
|
194
|
+
### toArray / toJS / toJSON
|
|
195
|
+
|
|
196
|
+
```ts
|
|
197
|
+
toArray(): Array<T>
|
|
198
|
+
toJS(): Array<DeepCopy<T>>
|
|
199
|
+
toJSON(): Array<T>
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
`toJS` recurses into nested Immutable collections; `toJSON`/`toArray` are shallow.
|
|
203
|
+
|
|
204
|
+
### toSeq / toIndexedSeq / toSetSeq / toKeyedSeq
|
|
205
|
+
|
|
206
|
+
```ts
|
|
207
|
+
toSeq(): Seq.Set<T>
|
|
208
|
+
toIndexedSeq(): Seq.Indexed<T>
|
|
209
|
+
toSetSeq(): Seq.Set<T>
|
|
210
|
+
toKeyedSeq(): Seq.Keyed<T, T>
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### toMap / toOrderedMap / toOrderedSet / toList / toStack
|
|
214
|
+
|
|
215
|
+
Convert to other Immutable collections; in a Set, `toMap()` produces `Map<T, T>` of `[value, value]` pairs.
|
|
216
|
+
|
|
217
|
+
### asImmutable / asMutable
|
|
218
|
+
|
|
219
|
+
```ts
|
|
220
|
+
asMutable(): this
|
|
221
|
+
asImmutable(): this
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
See conversions.md for the full conversion matrix and `toJS` deep-conversion rules.
|
|
225
|
+
|
|
226
|
+
## Mutation batching
|
|
227
|
+
|
|
228
|
+
### withMutations
|
|
229
|
+
|
|
230
|
+
```ts
|
|
231
|
+
withMutations(mutator: (mutable: this) => unknown): this
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
Apply a batch of mutations to a transient mutable copy and get back an immutable result. Only `add`, `delete`/`remove`, `clear`, `union`, `intersect`, `subtract` are safe inside.
|
|
235
|
+
|
|
236
|
+
```js
|
|
237
|
+
Set([1, 2, 3]).withMutations(s => {
|
|
238
|
+
s.add(4).add(5).delete(1);
|
|
239
|
+
}); // => Set { 2, 3, 4, 5 }
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
### wasAltered
|
|
243
|
+
|
|
244
|
+
```ts
|
|
245
|
+
wasAltered(): boolean
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
True only on a mutable copy (from `asMutable`) that has been modified.
|
|
249
|
+
|
|
250
|
+
## OrderedSet
|
|
251
|
+
|
|
252
|
+
Same factory shape and full API as `Set`, plus `OrderedSet.isOrderedSet`. Iteration follows insertion order, matching native ES6 `Set`.
|
|
253
|
+
|
|
254
|
+
```ts
|
|
255
|
+
OrderedSet<T>(collection?: Iterable<T> | ArrayLike<T>): OrderedSet<T>
|
|
256
|
+
OrderedSet.of<T>(...values: Array<T>): OrderedSet<T>
|
|
257
|
+
OrderedSet.fromKeys<T>(iter: Collection<T, unknown>): OrderedSet<T>
|
|
258
|
+
OrderedSet.isOrderedSet(maybeOrderedSet: unknown): boolean
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
Adding a value that is already present does NOT move it to the end — original position is retained. `OrderedSet#add` is amortized `O(log32 N)` but not stable, and uses more memory than `Set`. Use it only when iteration order matters; otherwise prefer `Set`.
|
|
262
|
+
|
|
263
|
+
`union`, `merge`, `concat` on an `OrderedSet` return `OrderedSet<T | C>` (preserving the ordered type).
|
|
264
|
+
|
|
265
|
+
## See also
|
|
266
|
+
|
|
267
|
+
- `collection.md` — `Collection.Set` shared interface (all the inherited iteration/search/reduce methods: `forEach`, `reduce`, `every`, `some`, `count`, `countBy`, `max`/`maxBy`, `min`/`minBy`, `isSubset`, `isSuperset`, `slice`, `take`/`skip` family).
|
|
268
|
+
- `equality.md` — `Immutable.is`, which defines Set uniqueness and `has`/`includes` behavior, plus `hashCode` semantics.
|
|
269
|
+
- `predicates.md` — `Set.isSet`, `OrderedSet.isOrderedSet`.
|
|
270
|
+
- `conversions.md` — `toJS` deep conversion and round-tripping with native types.
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
These are JS-friendly top-level helpers exported from `immutable`: `get`, `set`, `has`, `update`, `remove`. They act on a single key/index (no path traversal). Use when interop with plain JS objects/arrays is needed, or when writing functions that accept either Immutable or plain inputs. For Immutable inputs, the instance methods (`map.set`, `list.get`) are equivalent and return the same result.
|
|
2
|
+
|
|
3
|
+
## Common signature shape
|
|
4
|
+
|
|
5
|
+
- First arg is the collection — an Immutable `Collection`, a plain object, or an `Array`.
|
|
6
|
+
- Second arg is the key (string/symbol for objects/Maps, number index for arrays/Lists).
|
|
7
|
+
- `set` / `update` / `remove` return a NEW collection of the same kind; the input is never mutated.
|
|
8
|
+
- `get` returns the value (or `notSetValue` / `undefined`); `has` returns a boolean.
|
|
9
|
+
- Plain objects are treated as keyed; arrays are treated as indexed.
|
|
10
|
+
- For Immutable inputs, structural sharing applies; for plain JS inputs, a shallow clone is produced.
|
|
11
|
+
|
|
12
|
+
## get
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
get<K, V>(collection, key, notSetValue?): V | undefined
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Returns the value at `key`, or `notSetValue` (or `undefined`) if absent. Functional alternative to `collection.get(key)` / `collection[key]`.
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
get(['dog', 'frog', 'cat'], 2); // => 'cat'
|
|
22
|
+
get({ x: 123, y: 456 }, 'x'); // => 123
|
|
23
|
+
get({ x: 123, y: 456 }, 'z', 'fallback'); // => 'fallback'
|
|
24
|
+
get(List([1, 2, 3]), 0); // => 1
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## has
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
has(collection: object, key: unknown): boolean
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Returns `true` if `key` is defined in the collection. For plain objects this is equivalent to `hasOwnProperty`.
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
has(['dog', 'frog', 'cat'], 2); // => true
|
|
37
|
+
has(['dog', 'frog', 'cat'], 5); // => false
|
|
38
|
+
has({ x: 123, y: 456 }, 'x'); // => true
|
|
39
|
+
has({ x: 123, y: 456 }, 'z'); // => false
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## set
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
set<C>(collection: C, key, value): C
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Returns a copy of the collection with `key` set to `value`. For plain JS, returns a new shallow-cloned object/array; the original is not mutated.
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
set(List(['dog', 'frog', 'cat']), 1, 'cow'); // => List ['dog', 'cow', 'cat']
|
|
52
|
+
set({ x: 123, y: 456 }, 'x', 789); // => { x: 789, y: 456 }
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## update
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
update<C>(collection: C, key, updater): C
|
|
59
|
+
update<C>(collection: C, key, notSetValue, updater): C
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Returns a copy with `key` set to `updater(currentValue)`. If `notSetValue` is supplied and the key is absent, the updater receives `notSetValue` instead of `undefined`.
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
update(List(['dog', 'frog', 'cat']), 1, v => v.toUpperCase());
|
|
66
|
+
// => List ['dog', 'FROG', 'cat']
|
|
67
|
+
|
|
68
|
+
update({ x: 123, y: 456 }, 'x', v => v * 6);
|
|
69
|
+
// => { x: 738, y: 456 }
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## remove
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
remove<C>(collection: C, key): C
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Alias: `delete`. Returns a copy of the collection with `key` removed. For plain objects, returns a new shallow-cloned object without that key. For arrays and `List`, removes the element at that index and shifts following elements down by one.
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
remove(List(['dog', 'frog', 'cat']), 1); // => List ['dog', 'cat']
|
|
82
|
+
remove({ x: 123, y: 456 }, 'x'); // => { y: 456 }
|
|
83
|
+
remove([10, 20, 30], 1); // => [10, 30]
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Gotchas
|
|
87
|
+
|
|
88
|
+
- These are NOT path-based — for nested paths, use the `*In` variants (`getIn`, `setIn`, `updateIn`, `removeIn`, `hasIn`); see `deep-updates.md`.
|
|
89
|
+
- On plain JS arrays and on `List`, `remove(arr, i)` shifts later elements down (like `Array.prototype.splice`); it does not produce a sparse/hole result.
|
|
90
|
+
- For Immutable inputs, returns the same identity (`===`) when no change occurs; this short-circuit does not apply to plain JS inputs (which always shallow-clone on write).
|
|
91
|
+
- For plain JS, the prototype chain is preserved by spreading; class instances are shallow-copied as plain objects of the same shape.
|
|
92
|
+
- `update` with no `notSetValue` passes `undefined` to the updater for missing keys — guard against this in your updater.
|
|
93
|
+
|
|
94
|
+
## See also
|
|
95
|
+
|
|
96
|
+
- `deep-updates.md` — path-based variants (`getIn`, `setIn`, `updateIn`, `removeIn`, `hasIn`, `mergeIn`, `mergeDeepIn`).
|
|
97
|
+
- `list.md` — indexed Immutable collection.
|
|
98
|
+
- `map.md` — keyed Immutable collection.
|
|
99
|
+
- `conversions.md` — moving between plain JS and Immutable.
|