@sheetxl/primitives 0.7.27 → 0.7.28-beta.2
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/browser/cjs/index.cjs +1 -6
- package/browser/esm/index.mjs +683 -307
- package/node/esm/index.mjs +684 -307
- package/package.json +1 -1
- package/types/index.d.ts +2 -1622
- package/types/src/ErrorPolicy.d.ts +78 -0
- package/types/src/Flatten.d.ts +34 -0
- package/types/src/FormulaContext.d.ts +227 -0
- package/types/src/FormulaError.d.ts +328 -0
- package/types/src/IFormulaContext.d.ts +533 -0
- package/types/src/IFunction.d.ts +608 -0
- package/types/src/IRange.d.ts +1091 -0
- package/types/src/IRangeStats.d.ts +478 -0
- package/types/src/IReferenceRange.d.ts +22 -0
- package/types/src/IRichData.d.ts +37 -0
- package/types/src/IRuntime.d.ts +65 -0
- package/types/src/Lambda.d.ts +17 -0
- package/types/src/LocaleProfile.d.ts +79 -0
- package/types/src/Observable.d.ts +46 -0
- package/types/src/Omittable.d.ts +50 -0
- package/types/src/Repeating.d.ts +66 -0
- package/types/src/Scalar.d.ts +67 -0
- package/types/src/ScalarType.d.ts +109 -0
- package/types/src/WildCard.d.ts +26 -0
- package/types/src/index.d.ts +20 -0
|
@@ -0,0 +1,1091 @@
|
|
|
1
|
+
import { ScalarType } from './ScalarType';
|
|
2
|
+
import { Scalar } from './Scalar';
|
|
3
|
+
import { FormulaError, MaybeError } from './FormulaError';
|
|
4
|
+
import { IRangeStats } from './IRangeStats';
|
|
5
|
+
/**
|
|
6
|
+
* An immutable two-dimensional, mixed-type sparse array with functional vector semantics.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* IRange is a persistent data structure — all operations return new ranges
|
|
10
|
+
* via structural sharing. This allows for zero-copy cache-friendly writes, SIMD optimized
|
|
11
|
+
* reads, and lockless concurrent evaluation.
|
|
12
|
+
*
|
|
13
|
+
* Designed to ensure familiarity for NumPy and JAX-ML users.
|
|
14
|
+
*
|
|
15
|
+
* 5 primitives are the core methods that have direct access to internal state.
|
|
16
|
+
*
|
|
17
|
+
* - entries() — iterator with sparse/dense, axis, direction, and coercible options.
|
|
18
|
+
* - sparseBuilder() — streaming writer for sparse output construction.
|
|
19
|
+
* - getShape() — returns { rows, columns }.
|
|
20
|
+
* - resize() — returns a new IRange at a different shape with a fill value.
|
|
21
|
+
* - getType() — the scalar type used for default coercion if set.
|
|
22
|
+
*
|
|
23
|
+
* 6 additional methods have been highly optimized for performance.
|
|
24
|
+
*
|
|
25
|
+
* - getValueAt(row, column) — fast random access to individual cells.
|
|
26
|
+
* - fill() — smart vector compression, zero copy.
|
|
27
|
+
* - fillEmpty() — virtual update for empty.
|
|
28
|
+
* - clone() — creates a deep copy of the range.
|
|
29
|
+
* - getStats() — returns range statistics.
|
|
30
|
+
* - hasError() — exact error-member existence, raw untyped read.
|
|
31
|
+
*
|
|
32
|
+
* `errors()` is composed from the primitives but may be accelerated by implementations.
|
|
33
|
+
*
|
|
34
|
+
* All other methods — including `mask`, `choose`, `select`, `tile`, `repeat`, and the rest of the
|
|
35
|
+
* utility surface are composed entirely in terms of those primitives with no special internal access.
|
|
36
|
+
*/
|
|
37
|
+
export interface IRange<T extends Scalar = Scalar> {
|
|
38
|
+
/**
|
|
39
|
+
* Returns the permutation indices that would sort the range.
|
|
40
|
+
* Analogous to `np.argsort`. Use with `take()` to sort a parallel range (SORTBY pattern).
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* // single key
|
|
44
|
+
* const perm = keyRange.argsort(1);
|
|
45
|
+
* return dataRange.take(perm);
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* const perm = array.argsort([key1, key2], [1, -1]);
|
|
49
|
+
* return dataRange.take(perm);
|
|
50
|
+
*
|
|
51
|
+
* @param keyRanges Single key range or array of key ranges.
|
|
52
|
+
* @param orders `1` for ascending (default), `-1` for descending, per key.
|
|
53
|
+
* @param axis Sort by rows (default) or columns.
|
|
54
|
+
*/
|
|
55
|
+
argsort(keyRanges: IRange | IRange[], orders?: number | number[], axis?: IRange.Axis): number[];
|
|
56
|
+
/**
|
|
57
|
+
* Returns the indices of the distinct rows (or columns) in this range, keeping the FIRST
|
|
58
|
+
* occurrence of each. Analogous to `np.unique(arr, axis=0, return_index=True)`, but returning
|
|
59
|
+
* only the kept indices (numpy's sorted, unique-value form isn't what `UNIQUE()` wants).
|
|
60
|
+
*
|
|
61
|
+
* @param axis Dedupe rows (default) or columns.
|
|
62
|
+
* @param exactlyOnce If true, keep only rows/columns that occur EXACTLY once — drops every
|
|
63
|
+
* row that has a duplicate, rather than keeping one copy of it. Default false.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* const kept = range.uniqueIndices();
|
|
67
|
+
* return range.take(kept); // the SORTBY-style pattern argsort's own docs use
|
|
68
|
+
*/
|
|
69
|
+
uniqueIndices(axis?: IRange.Axis, exactlyOnce?: boolean): number[];
|
|
70
|
+
/**
|
|
71
|
+
* Applies a callback function to elements from this range and another range or a scalar,
|
|
72
|
+
* using NumPy-style broadcasting.
|
|
73
|
+
*
|
|
74
|
+
* **Broadcasting Rules:**
|
|
75
|
+
* - If `other` is a scalar, it is broadcasted to match the shape of this range.
|
|
76
|
+
* - If `other` is a range, both ranges must have the same length.
|
|
77
|
+
*
|
|
78
|
+
* @param other The other range or scalar to broadcast with.
|
|
79
|
+
* @param callbackFn The function to apply to corresponding elements.
|
|
80
|
+
* @param options {@link IRange.BroadcastOptions}
|
|
81
|
+
*
|
|
82
|
+
* @returns A new range with the broadcasted results.
|
|
83
|
+
*
|
|
84
|
+
* @remarks
|
|
85
|
+
* Broadcasting rules (NumPy-style):
|
|
86
|
+
* - Scalar broadcasts to any shape: (1×1) + (3×4) → (3×4)
|
|
87
|
+
* - Same shapes work: (3×4) + (3×4) → (3×4)
|
|
88
|
+
* - Trailing dimensions of 1 broadcast: (3×1) + (3×4) → (3×4)
|
|
89
|
+
* - Incompatible shapes error: (3×4) + (2×3) → Error (unless misalignValue set)
|
|
90
|
+
*/
|
|
91
|
+
broadcast<TS extends Scalar = T>(other: IRange | TS, callbackFn: (a: TS, b: TS, coords: IRange.CellCoords, range: IRange<T>) => TS, options?: IRange.BroadcastOptions): this;
|
|
92
|
+
/**
|
|
93
|
+
* Selects values from `from` by dispatching each element of `condition` to a 0-based index.
|
|
94
|
+
*
|
|
95
|
+
* For each cell, `selectFn` receives the corresponding value from `condition` and returns a 0-based
|
|
96
|
+
* index into `from`. If the index is out of bounds, the original value from `this` is kept.
|
|
97
|
+
*
|
|
98
|
+
* **Broadcasting Rules:**
|
|
99
|
+
* - Output shape is the max of `this`, `condition`, and all `from` ranges.
|
|
100
|
+
* - All inputs are clamped to their bounds (NumPy-style broadcasting).
|
|
101
|
+
* - Out-of-bounds index from `selectFn` falls back to `this` at that position.
|
|
102
|
+
*
|
|
103
|
+
* @param from The candidate ranges or scalars to select from (0-based).
|
|
104
|
+
* @param selectFn Maps each condition value to a 0-based index into `from`.
|
|
105
|
+
* Return `-1` (or any out-of-bounds value) to keep the original value from `this`.
|
|
106
|
+
* @param options {@link IRange.ValuesOptions}
|
|
107
|
+
*
|
|
108
|
+
* @returns A new range with selected values overlaid onto `this`.
|
|
109
|
+
*
|
|
110
|
+
* @see {@link mask} for the boolean shorthand (single condition, single value range).
|
|
111
|
+
*/
|
|
112
|
+
choose<TS extends Scalar = T>(from: (IRange<TS> | TS)[], selectFn: (value: TS, coords: IRange.CellCoords, range: IRange<T>) => MaybeError<number>, options?: IRange.ValuesOptions): this;
|
|
113
|
+
/**
|
|
114
|
+
* Similar to {@link empty}, but returns an range with the same size but `null` values.
|
|
115
|
+
*/
|
|
116
|
+
clear(): this;
|
|
117
|
+
/**
|
|
118
|
+
* Returns a new range identical to the current one.
|
|
119
|
+
*/
|
|
120
|
+
clone(): this;
|
|
121
|
+
/**
|
|
122
|
+
* Applies a transformation function to this `IRange`, returning the result.
|
|
123
|
+
*
|
|
124
|
+
* Equivalent to `fn(this)` — useful for applying named or reusable transforms
|
|
125
|
+
* inline without breaking a fluent method chain.
|
|
126
|
+
*
|
|
127
|
+
* @param fn A pure function that transforms this `IRange`.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* const normalize = (r: IRange) => r.replace([[1,2,3]]).repeat(3,1);
|
|
131
|
+
*
|
|
132
|
+
* range.compute(normalize).mask(condition);
|
|
133
|
+
*/
|
|
134
|
+
compute(fn: (range: IRange) => IRange): this;
|
|
135
|
+
/**
|
|
136
|
+
* Concatenates another range along the specified axis, returning a new range.
|
|
137
|
+
*
|
|
138
|
+
* - {@link IRange.Axis.Row} (default) — vertical stack:
|
|
139
|
+
* appends `other` below `this`. Both ranges must have the same column size, or the
|
|
140
|
+
* narrower is padded to the wider with `padWith`.
|
|
141
|
+
* - {@link IRange.Axis.Column} — horizontal stack:
|
|
142
|
+
* appends `other` to the right of `this`. Both ranges must have the same row size,
|
|
143
|
+
* or the shorter is padded to the taller with `padWith`.
|
|
144
|
+
*
|
|
145
|
+
* @param other The range to append.
|
|
146
|
+
* @param axis The axis to concatenate along. Default is {@link IRange.Axis.Row}.
|
|
147
|
+
* @param padWith Value used to pad the narrower/shorter range. Default is `#N/A`.
|
|
148
|
+
*
|
|
149
|
+
* @remarks
|
|
150
|
+
* Analogous to `np.vstack` / `np.hstack` in numpy and `torch.cat` in PyTorch.
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* // Vertical stack
|
|
154
|
+
* a.concat(b)
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* // Horizontal stack
|
|
158
|
+
* a.concat(b, IRange.Axis.Column)
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* // Pad with 0 instead of #N/A
|
|
162
|
+
* a.concat(b, IRange.Axis.Row, 0)
|
|
163
|
+
*/
|
|
164
|
+
concat(other: IRange, axis?: IRange.Axis, padWith?: Scalar): this;
|
|
165
|
+
/**
|
|
166
|
+
* Returns a range of 0x0.
|
|
167
|
+
*/
|
|
168
|
+
empty(): this;
|
|
169
|
+
/**
|
|
170
|
+
* Returns an iterator that allows you to iterate over the individual entries within the range,
|
|
171
|
+
* yielding pairs of `IRange.CellCoords` and `Scalar`.
|
|
172
|
+
*
|
|
173
|
+
* @param options {@link IRange.IteratorOptions}
|
|
174
|
+
* @returns An iterator that yields a IRange.Entry for each value within the range.
|
|
175
|
+
*
|
|
176
|
+
* @remarks
|
|
177
|
+
* Skips empty values by default.
|
|
178
|
+
*
|
|
179
|
+
```typescript entries
|
|
180
|
+
const iterEntries:IterableIterator<IRange.Entry> = array.entries({
|
|
181
|
+
axis: IRange.Axis.Row, // Default but Column and 'fastest' are also available
|
|
182
|
+
includeEmpty: false // Default but true is also available but should be avoided if sparse.
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
let iter: IteratorResult<IRange.Entry> = iterEntries.next();
|
|
186
|
+
while (!iter.done) {
|
|
187
|
+
const entry = iter.value;
|
|
188
|
+
const value = entry.value;
|
|
189
|
+
const coords: Readonly<IRange.CellCoords> = entry.coords;
|
|
190
|
+
iter = iterEntries.next();
|
|
191
|
+
// ...interesting logic here. Perhaps a sparseBuilder.
|
|
192
|
+
}
|
|
193
|
+
```
|
|
194
|
+
*/
|
|
195
|
+
entries<TS extends Scalar = T>(options?: IRange.IteratorOptions): IterableIterator<IRange.Entry<TS>>;
|
|
196
|
+
/**
|
|
197
|
+
* Iterates the range's error members — every stored `FormulaError` — in row-major order
|
|
198
|
+
* by default. Row-major is the identity order: `errors().next()` yields the error that
|
|
199
|
+
* propagates under the engine's reject policy (`#N/A` stays `#N/A`).
|
|
200
|
+
*
|
|
201
|
+
* @param options Narrowed iterator options — only `axis`, `bounds`, and `reverse`.
|
|
202
|
+
* The narrowing is deliberate: `type`/`onError`/`coerce` are meaningless-to-contradictory
|
|
203
|
+
* on an error iterator. Like {@link hasError}, the scan reads RAW stored members,
|
|
204
|
+
* ignoring the range's read type and coercion policy.
|
|
205
|
+
*
|
|
206
|
+
* @see {@link hasError} for existence-only checks.
|
|
207
|
+
*/
|
|
208
|
+
errors(options?: Pick<IRange.IteratorOptions, 'axis' | 'bounds' | 'reverse'>): IterableIterator<IRange.Entry<FormulaError.Known>>;
|
|
209
|
+
/**
|
|
210
|
+
* Tests whether all elements in the range pass the test implemented by the provided function.
|
|
211
|
+
*
|
|
212
|
+
* @param callbackFn The function to test each element.
|
|
213
|
+
* @param options {@link IRange.IteratorOptions}
|
|
214
|
+
* @returns `true` if all elements pass the test, otherwise `false`.
|
|
215
|
+
*/
|
|
216
|
+
every<TS extends Scalar = T>(callbackFn: (value: TS, coords: IRange.CellCoords, range: IRange<T>) => boolean, options?: IRange.IteratorOptions): boolean;
|
|
217
|
+
/**
|
|
218
|
+
* Fill the entire range with a single value.
|
|
219
|
+
*
|
|
220
|
+
* @param value The value to fill.
|
|
221
|
+
* @param coords If provided will fill from the coordinates to this area.
|
|
222
|
+
*
|
|
223
|
+
* @remarks
|
|
224
|
+
* If the coords are outside the range then it will fill to the edge.
|
|
225
|
+
* Does not resize
|
|
226
|
+
*/
|
|
227
|
+
fill(value: Scalar, coords?: Partial<IRange.Coords>): this;
|
|
228
|
+
/**
|
|
229
|
+
* Fill all empty values in the range with a single value.
|
|
230
|
+
*
|
|
231
|
+
* @param value The value to fill
|
|
232
|
+
* @param coords If provided will fill from the coordinates to this area.
|
|
233
|
+
*/
|
|
234
|
+
fillEmpty(value: Scalar, coords?: Partial<IRange.Coords>): this;
|
|
235
|
+
/**
|
|
236
|
+
* Creates a new range with elements that pass the test implemented by the provided function.
|
|
237
|
+
*
|
|
238
|
+
* @param callbackFn The function to test each element.
|
|
239
|
+
* @param options {@link IRange.IteratorOptions}
|
|
240
|
+
* @returns A new range containing only the elements that pass the test.
|
|
241
|
+
*
|
|
242
|
+
* @remarks
|
|
243
|
+
* If the callback returns an error this will propagate the error as the value with the error.
|
|
244
|
+
*/
|
|
245
|
+
filter<TS extends Scalar = T>(callbackFn: (value: TS, coords: IRange.CellCoords, range: IRange<T>) => MaybeError<boolean>, options?: IRange.TraversalAxis | IRange.IteratorOptions): this;
|
|
246
|
+
/**
|
|
247
|
+
* Returns the values as an array of values.
|
|
248
|
+
*
|
|
249
|
+
* @param options {@link IRange.IteratorOptions}
|
|
250
|
+
*/
|
|
251
|
+
flatten<TS extends Scalar = T>(options?: IRange.IteratorOptions): TS[];
|
|
252
|
+
/**
|
|
253
|
+
* Iterates over the cells within the range, applying the provided callback function to each cell.
|
|
254
|
+
*
|
|
255
|
+
* This method is a more scalable alternative to `getValues` or `getCells` for large or sparse ranges,
|
|
256
|
+
* as it:
|
|
257
|
+
* 1. Skips empty values by default.
|
|
258
|
+
* 2. Avoids creating large arrays.
|
|
259
|
+
* 3. Allows for early termination.
|
|
260
|
+
*
|
|
261
|
+
* @param callbackFn A function that will be called for each non-empty cell in the range. The callback receives:
|
|
262
|
+
* - `value`: The `Scalar` at the current cell.
|
|
263
|
+
* - `coords`: A `IRange.CellCoords`.
|
|
264
|
+
* - The callback can return `{ break: T }` to stop the iteration early.
|
|
265
|
+
* @param options {@link IRange.IteratorOptions}
|
|
266
|
+
* @template T The Scalar type.
|
|
267
|
+
* @template B The callback return type.
|
|
268
|
+
* @returns The value returned by the callback function if it explicitly returned to stop the iteration early; otherwise, `void`.
|
|
269
|
+
*/
|
|
270
|
+
forEach<TS extends Scalar = T, B = any>(callbackFn: (value: TS, coords: IRange.CellCoords, range: IRange<T>) => {
|
|
271
|
+
break: B;
|
|
272
|
+
} | void, options?: IRange.IteratorOptions): B | void;
|
|
273
|
+
/**
|
|
274
|
+
* Returns the shape of the range in rows and columns.
|
|
275
|
+
*/
|
|
276
|
+
getShape(): {
|
|
277
|
+
rows: number;
|
|
278
|
+
columns: number;
|
|
279
|
+
};
|
|
280
|
+
/**
|
|
281
|
+
* Returns the statistics for the range.
|
|
282
|
+
*/
|
|
283
|
+
getStats(options?: IRange.IteratorOptions): IRangeStats;
|
|
284
|
+
/**
|
|
285
|
+
* Returns `true` if any stored member of the range is a `FormulaError`.
|
|
286
|
+
*
|
|
287
|
+
* @remarks
|
|
288
|
+
* Reads RAW stored members only — the range's read type and coercion policy are ignored.
|
|
289
|
+
* A `toType(Number)` range with a text member does NOT "have an error"; a `#VALUE!`
|
|
290
|
+
* synthesized by `errorOnInvalidCoercion` doesn't count.
|
|
291
|
+
*
|
|
292
|
+
* The result is EXACT, never conservative: `false` guarantees there are no error members.
|
|
293
|
+
* (Internal fast paths may be conservative; the public result may not be.)
|
|
294
|
+
*
|
|
295
|
+
* Equivalent by definition to
|
|
296
|
+
* `getStats().getTypedCounts([ScalarType.Error])[ScalarType.Error] > 0` — the two
|
|
297
|
+
* definitions of "error member" must never drift.
|
|
298
|
+
*
|
|
299
|
+
* Deliberately takes no options: partial regions compose —
|
|
300
|
+
* `range.slice(coords).hasError()` (slice is a zero-copy view).
|
|
301
|
+
*
|
|
302
|
+
* @see {@link errors} to iterate the error members themselves.
|
|
303
|
+
*/
|
|
304
|
+
hasError(): boolean;
|
|
305
|
+
/**
|
|
306
|
+
* Indicates if the range has a type. Returns `undefined` when no type was set —
|
|
307
|
+
* consumers compare against an (optionally undefined) requested type, so `null`
|
|
308
|
+
* must NOT be substituted here.
|
|
309
|
+
*/
|
|
310
|
+
getType(): ScalarType | undefined;
|
|
311
|
+
/**
|
|
312
|
+
* Returns the values as a materialized 2D array.
|
|
313
|
+
*
|
|
314
|
+
* @param options {@link IRange.GetValuesOptions}
|
|
315
|
+
*
|
|
316
|
+
* @remarks
|
|
317
|
+
* This is useful for libraries or functions that require a
|
|
318
|
+
* a materialized 2D array but is expensive for large ranges.
|
|
319
|
+
*/
|
|
320
|
+
getValues<TS extends Scalar = T>(options?: IRange.GetValuesOptions): TS[][];
|
|
321
|
+
/**
|
|
322
|
+
* Returns a scalar value at a specific row and column index.
|
|
323
|
+
*
|
|
324
|
+
* @param rowIndex The row index of the value to retrieve.
|
|
325
|
+
* @param colIndex The column index of the value to retrieve.
|
|
326
|
+
* @param options {@link IRange.ValueOptions}
|
|
327
|
+
*/
|
|
328
|
+
getValueAt<TS extends Scalar = T>(rowIndex: number, colIndex: number, options?: IRange.ValueOptions): TS;
|
|
329
|
+
/**
|
|
330
|
+
* Returns the number of columns of the range.
|
|
331
|
+
*/
|
|
332
|
+
getColumnCount(): number;
|
|
333
|
+
/**
|
|
334
|
+
* Returns the number of rows of the range.
|
|
335
|
+
*/
|
|
336
|
+
getRowCount(): number;
|
|
337
|
+
/**
|
|
338
|
+
* Applies a transformation function to each value, returning a new `IRange`.
|
|
339
|
+
*
|
|
340
|
+
* @param callbackFn A function applied to each non-empty cell
|
|
341
|
+
* @param options {@link IRange.IteratorOptions}
|
|
342
|
+
*
|
|
343
|
+
* @remarks
|
|
344
|
+
* The result carries NO read type, even when the source had one — the values are the callback's
|
|
345
|
+
* outputs and `map` cannot know what type they are. Use {@link IRange.toType} on the result if
|
|
346
|
+
* you do know. (`options.type` still applies to the values handed TO the callback.)
|
|
347
|
+
*/
|
|
348
|
+
map<TS extends Scalar = T>(callbackFn: (value: TS, coords: IRange.CellCoords, range: IRange<T>) => TS, options?: IRange.IteratorOptions): this;
|
|
349
|
+
/**
|
|
350
|
+
* Returns a new range where each cell is replaced with the value from `ifTrues`
|
|
351
|
+
* where the corresponding `conditions` value is strictly `true`.
|
|
352
|
+
*
|
|
353
|
+
* @param conditions A range of values determining which cells are replaced.
|
|
354
|
+
* @param ifTrues Values to use where `conditions` is `true` (specifically not truthy).
|
|
355
|
+
*
|
|
356
|
+
* @remarks
|
|
357
|
+
* The mask rules are:
|
|
358
|
+
* 1. If the condition is `true` then the value from `ifTrues` is used.
|
|
359
|
+
* 2. If the condition is an error then that error is used.
|
|
360
|
+
* 3. If the condition is outside the bounds of `ifTrues` then `options.misalignValue` is used (default `#N/A`).
|
|
361
|
+
* 4. Else the original value is kept.
|
|
362
|
+
*
|
|
363
|
+
* Similar to pandas `DataFrame.mask(cond, other)`.
|
|
364
|
+
*
|
|
365
|
+
* @example
|
|
366
|
+
* // Formula equivalent: =IF(A1:A10>0, B1:B10, 0)
|
|
367
|
+
* original.mask(conditions, ifTrues)
|
|
368
|
+
*
|
|
369
|
+
* @see {@link broadcast} for element-wise transformation with a callback
|
|
370
|
+
*/
|
|
371
|
+
mask<TS extends Scalar = T>(conditions: IRange | TS, ifTrues: IRange | TS, options?: IRange.MaskOptions): this;
|
|
372
|
+
/**
|
|
373
|
+
* Iterates this range and any number of other ranges in lockstep, yielding the corresponding
|
|
374
|
+
* values at each position — `values[0]` is this range's member, `values[i+1]` is `others[i]`'s.
|
|
375
|
+
*
|
|
376
|
+
* Positions are aligned by OFFSET from each range's start; there is no broadcasting or tiling —
|
|
377
|
+
* dimension rules stay with the caller. Iteration is sparse: a position empty in EVERY range is
|
|
378
|
+
* never visited, and a range with no member at a visited position contributes `null`. Each range
|
|
379
|
+
* reads through its own declared type, so a filtered member (e.g. text under a `number` read)
|
|
380
|
+
* surfaces as `null` exactly like an empty cell.
|
|
381
|
+
*
|
|
382
|
+
* A `null` entry in `others` contributes the 1-based POSITION ordinal instead — the implied
|
|
383
|
+
* `{1,2,...,n}` sequence. The ordinal is positional within this range's shape (empties advance it),
|
|
384
|
+
* so a sparse member keeps the ordinal its cell occupies.
|
|
385
|
+
*
|
|
386
|
+
* @param others The parallel ranges; `null` entries yield the 1-based position ordinal.
|
|
387
|
+
* @param options {@link IRange.IteratorOptions} Applied to this range's read; `axis` orders the
|
|
388
|
+
* iteration for all sides.
|
|
389
|
+
* @returns Pairs of `[values, coords]` with `coords` relative to this range.
|
|
390
|
+
*/
|
|
391
|
+
alignValues<TS extends Scalar = T>(others: (IRange | null)[], options?: IRange.IteratorOptions): IterableIterator<[values: (TS | null)[], coords: IRange.CellCoords]>;
|
|
392
|
+
/**
|
|
393
|
+
* Reduces this range and any number of other ranges in lockstep — the fused, streaming form of
|
|
394
|
+
* "align, apply an elementwise kernel, fold" (numpy's `f(a, b).sum()`, with no intermediate
|
|
395
|
+
* range). Iteration and alignment follow {@link alignValues}: positions align by offset,
|
|
396
|
+
* a position empty on every side is never visited, an absent/filtered member arrives as
|
|
397
|
+
* `null`, and a `null` in `others` contributes the 1-based position ordinal.
|
|
398
|
+
*
|
|
399
|
+
* Value policy (what to skip, how to accumulate) belongs to the callback — this is the same
|
|
400
|
+
* contract as {@link reduce}, over aligned members.
|
|
401
|
+
*
|
|
402
|
+
* @template U The type of the accumulator.
|
|
403
|
+
* @param others The parallel range(s); `null` entries yield the 1-based position ordinal.
|
|
404
|
+
* @param callbackFn The function to execute on each aligned position — `values[0]` is this
|
|
405
|
+
* range's member, `values[i+1]` is `others[i]`'s.
|
|
406
|
+
* @param initialValue The initial value of the accumulator.
|
|
407
|
+
* @param options {@link IRange.ReduceOptions} Applied to this range's read; `axis` orders the
|
|
408
|
+
* iteration for all sides. Supply `emptyFold` when the answer depends on the positions that are
|
|
409
|
+
* empty on every side — they are never visited, so a fold that needs them must state their
|
|
410
|
+
* contribution in closed form.
|
|
411
|
+
* @returns The reduced value.
|
|
412
|
+
*/
|
|
413
|
+
alignReduce<TS extends Scalar = T, U = TS>(others: IRange | null | (IRange | null)[], callbackFn: (accumulator: U, values: (TS | null)[], coords: IRange.CellCoords, range: IRange<T>) => U, initialValue: U, options?: IRange.ReduceOptions<U>): U;
|
|
414
|
+
/**
|
|
415
|
+
* Applies a function against an accumulator and each element of the range (from left to right) to reduce it to a single value.
|
|
416
|
+
*
|
|
417
|
+
* @template U The type of the accumulator.
|
|
418
|
+
* @param callbackFn The function to execute on each element.
|
|
419
|
+
* @param initialValue The initial value of the accumulator.
|
|
420
|
+
* @param options {@link IRange.IteratorOptions}
|
|
421
|
+
* @returns The reduced value.
|
|
422
|
+
*
|
|
423
|
+
* @remarks
|
|
424
|
+
* Iteration is sparse — empty cells are not visited. A fold whose answer DEPENDS on the empty
|
|
425
|
+
* cells wants {@link alignReduce}'s `emptyFold`, which states their contribution in closed form;
|
|
426
|
+
* pass no `others` for the single-range case.
|
|
427
|
+
*/
|
|
428
|
+
reduce<TS extends Scalar = T, U = TS>(callbackFn: (accumulator: U, value: TS, coords: IRange.CellCoords, range: IRange<T>) => U, initialValue: U, options?: IRange.IteratorOptions): U;
|
|
429
|
+
/**
|
|
430
|
+
* Repeat the values in the range to a new width and height by repeating individual values.
|
|
431
|
+
*
|
|
432
|
+
* @param rows Number of times to repeat each value vertically.
|
|
433
|
+
* @param columns Number of times to repeat each value horizontally.
|
|
434
|
+
*
|
|
435
|
+
* @remarks
|
|
436
|
+
* **Note**: Arguments follow matrix convention `(rows, columns)`, not geometric order `(columns, rows)`.
|
|
437
|
+
*
|
|
438
|
+
* `[1, 2].repeat(1, 3)` → `[1, 1, 1, 2, 2, 2]` *(expand horizontally)*
|
|
439
|
+
* `[1, 2].repeat(2, 1)` → `[[1, 2], [1, 2]]` *(expand vertically)*
|
|
440
|
+
*
|
|
441
|
+
* To repeat the entire range as a block, see {@link tile}.
|
|
442
|
+
*/
|
|
443
|
+
repeat(rows: number, columns: number): this;
|
|
444
|
+
/**
|
|
445
|
+
* Reflows the range's members into a new shape by flattened row-major ordinal — numpy's
|
|
446
|
+
* `reshape`: the member at flattened position `i` lands at row `⌊i / columns⌋`, column
|
|
447
|
+
* `i % columns`. Empty positions keep their ordinal (they reflow as gaps), so sparse ranges
|
|
448
|
+
* stay sparse.
|
|
449
|
+
|
|
450
|
+
* Contrast {@link resize}, which trims/pads in place WITHOUT reflowing, and {@link repeat} /
|
|
451
|
+
* {@link tile}, which duplicate values.
|
|
452
|
+
*
|
|
453
|
+
* @param rows The new number of rows.
|
|
454
|
+
* @param columns The new number of columns.
|
|
455
|
+
*/
|
|
456
|
+
reshape(rows: number, columns: number): this;
|
|
457
|
+
/**
|
|
458
|
+
* Returns a new range with values from another range overlaid at the given offset.
|
|
459
|
+
* Where they overlap, other wins. If other extends beyond this, the range expands.
|
|
460
|
+
*
|
|
461
|
+
* @param other The other range or a 2D array.
|
|
462
|
+
* @param offset The starting coordinates for the replacement. Default: { rowIndex: 0, colIndex: 0 }
|
|
463
|
+
*/
|
|
464
|
+
replace<TS extends Scalar = T>(other: IRange | TS[][], offset?: IRange.CellCoords): this;
|
|
465
|
+
/**
|
|
466
|
+
* Returns a new range either trimmed or expanded to the new rows and columns.
|
|
467
|
+
*
|
|
468
|
+
* @param rows The new number of rows of the range.
|
|
469
|
+
* @param cols The new number of columns of the range.
|
|
470
|
+
* @param fillValue If provided then when expanding the new cells will be filled with this value. DefaultValue `null`
|
|
471
|
+
*/
|
|
472
|
+
resize(rows: number, cols: number, fillValue?: Scalar): this;
|
|
473
|
+
/**
|
|
474
|
+
* Returns a sliced subrange of the current range.
|
|
475
|
+
*
|
|
476
|
+
* @param coords A partial range definition ({ rowStart, rowEnd, colStart, colEnd }).
|
|
477
|
+
* @returns A new `IRange` representing the sliced section.
|
|
478
|
+
*
|
|
479
|
+
* @remarks
|
|
480
|
+
* If any coordinate is omitted, it defaults to the full extent of the range.
|
|
481
|
+
*/
|
|
482
|
+
slice(coords: Partial<IRange.Coords>): this;
|
|
483
|
+
/**
|
|
484
|
+
* Tests whether at least one element in the range passes the test implemented by the provided function.
|
|
485
|
+
*
|
|
486
|
+
* @param callbackFn The function to test each element.
|
|
487
|
+
* @param options {@link IRange.IteratorOptions}
|
|
488
|
+
* @returns `true` if at least one element passes the test, otherwise `false`.
|
|
489
|
+
*/
|
|
490
|
+
some<TS extends Scalar = T>(callbackFn: (value: TS, coords: IRange.CellCoords, range: IRange<T>) => boolean, options?: IRange.IteratorOptions): boolean;
|
|
491
|
+
/**
|
|
492
|
+
* Returns a sorted copy of the range along the given axis.
|
|
493
|
+
* Equivalent to `this.take(this.argsort(order, axis), axis)`.
|
|
494
|
+
*
|
|
495
|
+
* @param order - `1` for ascending (default), `-1` for descending.
|
|
496
|
+
* @param axis - Sort rows (`Row`, default) or columns (`Column`).
|
|
497
|
+
*/
|
|
498
|
+
sort(order?: number, axis?: IRange.Axis): this;
|
|
499
|
+
/**
|
|
500
|
+
* Similar to {@link choose}, but matches by value equality rather than by index.
|
|
501
|
+
*
|
|
502
|
+
* For each cell, searches `match` for the first element equal to the expression value and returns
|
|
503
|
+
* the corresponding element from `from`. If no match is found, `defaultValue` is used (or the
|
|
504
|
+
* original value from `this` if no default is provided).
|
|
505
|
+
*
|
|
506
|
+
* @param match Values to match against the expression range.
|
|
507
|
+
* @param from Values to return for each corresponding match.
|
|
508
|
+
* @param defaultValue Fallback value when no match is found.
|
|
509
|
+
* @param selectFn Optional custom equality function. Defaults to strict equality (`===`).
|
|
510
|
+
* @param options {@link IRange.ValuesOptions}
|
|
511
|
+
*
|
|
512
|
+
* @see {@link choose} for index-based dispatch.
|
|
513
|
+
*/
|
|
514
|
+
select<TS extends Scalar = T>(match: IRange<TS> | TS | (IRange<TS> | TS)[], from: (IRange<TS> | TS)[], defaultValue?: IRange<TS> | TS, selectFn?: (value: TS, coords: IRange.CellCoords, matchValue: TS, matchCoords: IRange.CellCoords, range: IRange<T>) => MaybeError<boolean>, options?: IRange.ValuesOptions): this;
|
|
515
|
+
/**
|
|
516
|
+
* Creates a `SparseBuilder` for efficiently updating sparsely populated values in a streaming low memory way.
|
|
517
|
+
*
|
|
518
|
+
* @param options {@link IRange.SparseBuilderOptions}
|
|
519
|
+
*
|
|
520
|
+
* @remarks
|
|
521
|
+
* - **Ordering:** By default updates must be added in either row-major or column-major order, as specified
|
|
522
|
+
* by the `axis` parameter, with major/minor coordinates increasing monotonically. Unless
|
|
523
|
+
* `allowUnorderedWrites` is set to `true` out of order updates will result in an error.
|
|
524
|
+
|
|
525
|
+
* - **Performance:** Setting `axis` to `IRange.Axis.Column` is generally faster as it aligns with the internal data representation.
|
|
526
|
+
* - **Performance:** Setting `allowUnorderedWrites` adds a nominal performance overhead for all updates and will do a full apply when an
|
|
527
|
+
* out of order update is detected.
|
|
528
|
+
* The default `IRange.Axis.Row` is provided for compatibility with common spreadsheet APIs.
|
|
529
|
+
*
|
|
530
|
+
* @example
|
|
531
|
+
```typescript sparseBuilder
|
|
532
|
+
const updatedRange = range.sparseBuilder()
|
|
533
|
+
.pushAt(0, 0, 'A1')
|
|
534
|
+
.pushAt(0, 1, 'B1')
|
|
535
|
+
.build();
|
|
536
|
+
```
|
|
537
|
+
*/
|
|
538
|
+
sparseBuilder<TS extends Scalar = T>(options?: IRange.TraversalAxis | IRange.SparseBuilderOptions): IRange.SparseBuilder<TS>;
|
|
539
|
+
/**
|
|
540
|
+
* Returns a new range by selecting values at the given indices along the specified axis.
|
|
541
|
+
* The output size along the selected axis equals `indices.length` — no resizing is applied.
|
|
542
|
+
*
|
|
543
|
+
* Indices refer to positions within this range (0-based, relative to origin).
|
|
544
|
+
* The same index may appear multiple times to repeat rows or columns.
|
|
545
|
+
*
|
|
546
|
+
* @param indices The positions to select, in the desired output order.
|
|
547
|
+
* @param axis The axis to select along. Default is {@link IRange.Axis.Row}
|
|
548
|
+
* (select rows). Pass {@link IRange.Axis.Column} to select columns.
|
|
549
|
+
*
|
|
550
|
+
* @remarks
|
|
551
|
+
* Analogous to `np.take` in numpy and `torch.index_select` in PyTorch.
|
|
552
|
+
*
|
|
553
|
+
* @example
|
|
554
|
+
* // Reverse rows
|
|
555
|
+
* range.take([2, 1, 0])
|
|
556
|
+
*
|
|
557
|
+
* @example
|
|
558
|
+
* // Select columns 0 and 2, dropping column 1
|
|
559
|
+
* range.take([0, 2], IRange.Axis.Column)
|
|
560
|
+
*
|
|
561
|
+
* @example
|
|
562
|
+
* // Repeat a row
|
|
563
|
+
* range.take([0, 0, 1, 2])
|
|
564
|
+
*/
|
|
565
|
+
take(indices: readonly number[], axis?: IRange.Axis): this;
|
|
566
|
+
/**
|
|
567
|
+
* Tile the range to a new width and height by repeating blocks of values.
|
|
568
|
+
*
|
|
569
|
+
* @param rows Number of times to tile the range vertically.
|
|
570
|
+
* @param columns Number of times to tile the range horizontally.
|
|
571
|
+
*
|
|
572
|
+
* @remarks
|
|
573
|
+
* **Note**: Arguments follow matrix convention `(rows, columns)`, not geometric order `(columns, rows)`.
|
|
574
|
+
*
|
|
575
|
+
* `[1, 2, 3].tile(1, 3)` → `[[1, 2, 3, 1, 2, 3, 1, 2, 3]]` *(tile horizontally 3×)*
|
|
576
|
+
* `[1, 2, 3].tile(3, 1)` → `[[1, 2, 3], [1, 2, 3], [1, 2, 3]]` *(tile vertically 3×)*
|
|
577
|
+
*
|
|
578
|
+
* To repeat individual values instead of blocks, see {@link repeat}.
|
|
579
|
+
*/
|
|
580
|
+
tile(rows: number, columns: number): this;
|
|
581
|
+
/**
|
|
582
|
+
* Returns the scalar value of the range if it is a 1×1 range, otherwise throws an error.
|
|
583
|
+
*/
|
|
584
|
+
toScalar(): Scalar;
|
|
585
|
+
/**
|
|
586
|
+
* Change the type of default range coercion for the values in the range.
|
|
587
|
+
*
|
|
588
|
+
* @param type The new scalar type or value options.
|
|
589
|
+
*/
|
|
590
|
+
toType(type: ScalarType | IRange.ValueOptions): this;
|
|
591
|
+
/**
|
|
592
|
+
* Returns the range transposed.
|
|
593
|
+
*/
|
|
594
|
+
transpose(): this;
|
|
595
|
+
/**
|
|
596
|
+
* Iterate values.
|
|
597
|
+
*
|
|
598
|
+
* @param options {@link IRange.IteratorOptions}
|
|
599
|
+
*
|
|
600
|
+
* @remarks
|
|
601
|
+
* Skips empty values by default.
|
|
602
|
+
*/
|
|
603
|
+
values<TS extends Scalar = T>(options?: IRange.IteratorOptions): IterableIterator<TS>;
|
|
604
|
+
/**
|
|
605
|
+
* Iterates over this range and one or more other ranges or scalars in lockstep,
|
|
606
|
+
* calling the provided function with the broadcast-resolved values at each position,
|
|
607
|
+
* and returns a new range containing the results.
|
|
608
|
+
*
|
|
609
|
+
* Broadcast semantics follow Excel conventions:
|
|
610
|
+
* - A 1×1 scalar or range broadcasts to fill any shape.
|
|
611
|
+
* - A 1×N or N×1 range broadcasts to fill the missing dimension.
|
|
612
|
+
* - Ranges with incompatible shapes produce a `#VALUE!` error.
|
|
613
|
+
*
|
|
614
|
+
* @param others Additional ranges or scalar values to zip with this range.
|
|
615
|
+
* @param callbackFn Called at each position with the broadcast-resolved values from
|
|
616
|
+
* all ranges (`values[0]` is from `this`, `values[1]` onward are from `others`),
|
|
617
|
+
* the current cell coordinates, and the original range. Returns the output value
|
|
618
|
+
* for that position.
|
|
619
|
+
* @param options {@link IRange.IteratorOptions}
|
|
620
|
+
* @returns A new range of the same shape as the broadcast result.
|
|
621
|
+
*/
|
|
622
|
+
zip(others: (IRange | Scalar)[], callbackFn: (values: Scalar[], coords: IRange.CellCoords, range: IRange<T>) => Scalar, options?: IRange.IteratorOptions): this;
|
|
623
|
+
/**
|
|
624
|
+
* Returns a string representation of the range.
|
|
625
|
+
*/
|
|
626
|
+
toString(): string;
|
|
627
|
+
/**
|
|
628
|
+
* Returns `true` if the range is a literal range from a parameter.
|
|
629
|
+
*/
|
|
630
|
+
isLiteral(): boolean;
|
|
631
|
+
/**
|
|
632
|
+
* Returns `true` if the range is a scalar.
|
|
633
|
+
*/
|
|
634
|
+
isScalar(): boolean;
|
|
635
|
+
/**
|
|
636
|
+
* Returns `true` if the range is a vector (1D) either row or column.
|
|
637
|
+
*/
|
|
638
|
+
isVector(): boolean;
|
|
639
|
+
/**
|
|
640
|
+
* Returns `true` if the range is empty.
|
|
641
|
+
*/
|
|
642
|
+
isEmpty(): boolean;
|
|
643
|
+
/**
|
|
644
|
+
* Returns the size of the range this is the rows * columns.
|
|
645
|
+
*/
|
|
646
|
+
get size(): number;
|
|
647
|
+
/**
|
|
648
|
+
* Returns an iterator that allows you to iterate over the individual values within the range.
|
|
649
|
+
*
|
|
650
|
+
* @remarks
|
|
651
|
+
* For more advanced iteration, use {@link IRange.entries} or {@link IRange.forEach}.
|
|
652
|
+
*/
|
|
653
|
+
[Symbol.iterator](): IterableIterator<Scalar>;
|
|
654
|
+
/**
|
|
655
|
+
* Use for introspection.
|
|
656
|
+
*/
|
|
657
|
+
get isIRange(): true;
|
|
658
|
+
}
|
|
659
|
+
/**
|
|
660
|
+
* {@inheritDoc IRange}
|
|
661
|
+
*
|
|
662
|
+
* @see
|
|
663
|
+
* ### **Interface**
|
|
664
|
+
*
|
|
665
|
+
* {@link IRange}
|
|
666
|
+
*/
|
|
667
|
+
export declare namespace IRange {
|
|
668
|
+
/**
|
|
669
|
+
* Options for controlling iteration behavior.
|
|
670
|
+
*/
|
|
671
|
+
interface IteratorOptions extends IRange.ValueOptions {
|
|
672
|
+
/**
|
|
673
|
+
* The direction of the iteration.
|
|
674
|
+
*
|
|
675
|
+
* @defaultValue `IRange.Axis.Row`
|
|
676
|
+
*
|
|
677
|
+
* @remarks
|
|
678
|
+
* If `axis` is set to `'fastest'`, the implementation will automatically choose the fastest axis
|
|
679
|
+
* for iteration based on the internal data layout of the range.
|
|
680
|
+
* This can provide performance benefits without requiring the caller to specify the axis,
|
|
681
|
+
* especially when the optimal axis is not known in advance.
|
|
682
|
+
*/
|
|
683
|
+
axis?: IRange.TraversalAxis;
|
|
684
|
+
/**
|
|
685
|
+
* The bounds of the iteration.
|
|
686
|
+
*
|
|
687
|
+
* @defaultValue `The entire range`
|
|
688
|
+
*/
|
|
689
|
+
bounds?: Partial<IRange.Coords>;
|
|
690
|
+
/**
|
|
691
|
+
* Iterate in reverse order.
|
|
692
|
+
*
|
|
693
|
+
* @defaultValue `false`
|
|
694
|
+
*/
|
|
695
|
+
reverse?: boolean;
|
|
696
|
+
/**
|
|
697
|
+
* If true will visit empty cells.
|
|
698
|
+
* This is a potentially huge performance improvement if set to false.
|
|
699
|
+
*
|
|
700
|
+
* @defaultValue `false`
|
|
701
|
+
*
|
|
702
|
+
* @remarks
|
|
703
|
+
* If includeEmpty is `true` this will always return rectangular results.
|
|
704
|
+
* If bounds are provided it will provide empty spaces before and after.
|
|
705
|
+
* If passing a large bound care must be taken.
|
|
706
|
+
*/
|
|
707
|
+
includeEmpty?: boolean;
|
|
708
|
+
/**
|
|
709
|
+
* Useful for callback functions that want a this.
|
|
710
|
+
*/
|
|
711
|
+
thisArg?: any;
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* Options for a fold — {@link IRange.alignReduce}.
|
|
715
|
+
*/
|
|
716
|
+
interface ReduceOptions<U = any> extends IRange.IteratorOptions {
|
|
717
|
+
/**
|
|
718
|
+
* Folds the positions the iteration never visited — the cells that are empty on EVERY side —
|
|
719
|
+
* in a single call, given how many there are.
|
|
720
|
+
*
|
|
721
|
+
* @param accumulator The accumulator after every visited position has been folded.
|
|
722
|
+
* @param count How many positions of this range's shape went unvisited. Never `0`: the fold
|
|
723
|
+
* is skipped entirely when the iteration was dense.
|
|
724
|
+
*
|
|
725
|
+
* @remarks
|
|
726
|
+
* A sparse fold visits stored members only, which is the whole point of it and is why
|
|
727
|
+
* `SUM(A:A)` is not a million calls. But a reducer whose answer DEPENDS on the blanks —
|
|
728
|
+
* `COUNTIF(A:A,"<>10")` is 1,048,576 — needs their contribution, and iterating them to get
|
|
729
|
+
* it gives the cost back. The contribution is the same term repeated, so it is stated in
|
|
730
|
+
* closed form here and applied once.
|
|
731
|
+
*
|
|
732
|
+
* This is the caller's to supply because only the caller knows the fold: `COUNTIF` adds the
|
|
733
|
+
* count, `SUMIF` and `MAXIFS` are unchanged by a blank and pass nothing at all. It is the
|
|
734
|
+
* open-ufunc analogue of numpy's `initial=` — numpy hard-codes the identity per reduction
|
|
735
|
+
* because its set is closed, and this cannot.
|
|
736
|
+
*
|
|
737
|
+
* Omit it and the unvisited positions contribute nothing, which is the behavior every fold
|
|
738
|
+
* had before this option existed.
|
|
739
|
+
*/
|
|
740
|
+
emptyFold?: (accumulator: U, count: number) => U;
|
|
741
|
+
}
|
|
742
|
+
/**
|
|
743
|
+
* Union type for coercion parameters.
|
|
744
|
+
*/
|
|
745
|
+
type CoerceParams = ScalarType[] | ((fromValue: Scalar, fromType: ScalarType, defaultCoerce: (value: Scalar, type: ScalarType) => Scalar, range: IRange) => Scalar);
|
|
746
|
+
/**
|
|
747
|
+
* Passed to various methods to control coercion behavior when getting or iterating values.
|
|
748
|
+
*/
|
|
749
|
+
interface ValueOptions {
|
|
750
|
+
/**
|
|
751
|
+
* Specifies the target type for coercion and filtering when getting or
|
|
752
|
+
* iterating values.
|
|
753
|
+
*
|
|
754
|
+
* @defaultValue The scalar type of the `IRange` itself (`getType()`)
|
|
755
|
+
*
|
|
756
|
+
* @remarks
|
|
757
|
+
* - Values matching the type pass through unchanged.
|
|
758
|
+
* - Values coercible to the type are coerced.
|
|
759
|
+
* - If not set, the range's own scalar type (`getType()`) is used.
|
|
760
|
+
* - If the range has no scalar type, or set to `null` explicitly,
|
|
761
|
+
* no coercion or filtering is performed.
|
|
762
|
+
*/
|
|
763
|
+
type?: ScalarType | null;
|
|
764
|
+
/**
|
|
765
|
+
* Overrides default coercion behavior per type.
|
|
766
|
+
* When a value cannot be coerced, {@link onError} determines the fallback.
|
|
767
|
+
*/
|
|
768
|
+
coerce?: IRange.CoerceParams;
|
|
769
|
+
/**
|
|
770
|
+
* If `true`, coercion failure produces `#VALUE!` rather than silently skipping the value.
|
|
771
|
+
*
|
|
772
|
+
* @defaultValue `false`
|
|
773
|
+
*/
|
|
774
|
+
errorOnInvalidCoercion?: boolean;
|
|
775
|
+
/**
|
|
776
|
+
* What to do when the visited value is a `FormulaError`.
|
|
777
|
+
*
|
|
778
|
+
* @defaultValue `throw`
|
|
779
|
+
*
|
|
780
|
+
* @remarks
|
|
781
|
+
* 'throw' = throw error.
|
|
782
|
+
* 'ignore' = silently skip the value.
|
|
783
|
+
* 'value' = return a specific error value (default `#VALUE!`).
|
|
784
|
+
* 'function' = custom handler; use this to return a specific error value.
|
|
785
|
+
*/
|
|
786
|
+
onError?: 'throw' | 'ignore' | 'value' | ((error: FormulaError.Known) => void);
|
|
787
|
+
}
|
|
788
|
+
interface GetValuesOptions extends IRange.ValueOptions {
|
|
789
|
+
/**
|
|
790
|
+
* The maximum number of rows of the array.
|
|
791
|
+
*/
|
|
792
|
+
maxRows?: number;
|
|
793
|
+
/**
|
|
794
|
+
* The maximum number of columns of the array.
|
|
795
|
+
*/
|
|
796
|
+
maxCols?: number;
|
|
797
|
+
/**
|
|
798
|
+
* If true will include trailing empties.
|
|
799
|
+
*/
|
|
800
|
+
trailingEmpties?: boolean;
|
|
801
|
+
/**
|
|
802
|
+
* Allow for this to be set. DefaultValue `IRange.Axis.Row`
|
|
803
|
+
*/
|
|
804
|
+
axis?: IRange.Axis;
|
|
805
|
+
/**
|
|
806
|
+
* If provided then before return the callback will be visited for each cell.
|
|
807
|
+
*/
|
|
808
|
+
onValue?: (value: Scalar, row: number, column: number, range: IRange) => Scalar;
|
|
809
|
+
}
|
|
810
|
+
/**
|
|
811
|
+
* Options used during range over range operators.
|
|
812
|
+
*/
|
|
813
|
+
interface ValuesOptions extends IRange.ValueOptions {
|
|
814
|
+
/**
|
|
815
|
+
* Controls how misaligned ranges are handled during broadcasting.
|
|
816
|
+
*
|
|
817
|
+
* - By default, `broadcast()` follows strict alignment rules like NumPy and will
|
|
818
|
+
* throw an error if the input ranges cannot be broadcasted together.
|
|
819
|
+
* - When `misalignValue` is provided, `broadcast()` will instead mimic Excel behavior:
|
|
820
|
+
* - It finds the **largest valid intersection** of the ranges.
|
|
821
|
+
* - It expands the result to the **union of both range sizes**.
|
|
822
|
+
* - Any cells that do not have valid corresponding values from the inputs
|
|
823
|
+
* will be filled with `misalignValue` instead of causing an error.
|
|
824
|
+
*
|
|
825
|
+
* @example
|
|
826
|
+
* // Throws an error if A and B are not naturally broadcast-compatible
|
|
827
|
+
* const result = A.broadcast(B, (a, b) => a + b);
|
|
828
|
+
*
|
|
829
|
+
* @example
|
|
830
|
+
* // Expands to union size and fills gaps with "#N/A" instead of erroring
|
|
831
|
+
* const result = A.broadcast(B, (a, b) => a + b, { misalignValue: "#N/A" });
|
|
832
|
+
*/
|
|
833
|
+
misalignValue?: Scalar;
|
|
834
|
+
}
|
|
835
|
+
/**
|
|
836
|
+
* Options used during broadcasting.
|
|
837
|
+
*/
|
|
838
|
+
interface BroadcastOptions extends IRange.ValuesOptions {
|
|
839
|
+
/**
|
|
840
|
+
* Coerces values from `other` to the specified type before applying the callback.
|
|
841
|
+
* Overrides the type inference that would otherwise be derived from `other`.
|
|
842
|
+
*/
|
|
843
|
+
otherType?: ScalarType;
|
|
844
|
+
}
|
|
845
|
+
/**
|
|
846
|
+
* Options used during masking.
|
|
847
|
+
*/
|
|
848
|
+
interface MaskOptions extends IRange.ValuesOptions {
|
|
849
|
+
/**
|
|
850
|
+
* Determines whether a condition value is considered true.
|
|
851
|
+
*
|
|
852
|
+
* @defaultValue `(cond) => cond === true`
|
|
853
|
+
*/
|
|
854
|
+
isTrue?: (cond: Scalar, cell: IRange.CellCoords) => boolean;
|
|
855
|
+
/**
|
|
856
|
+
* Value substituted for empty cells during masking.
|
|
857
|
+
*
|
|
858
|
+
* @defaultValue `null`
|
|
859
|
+
*/
|
|
860
|
+
emptyValue?: Scalar;
|
|
861
|
+
}
|
|
862
|
+
/**
|
|
863
|
+
* Entry for a Range iterator or visiting.
|
|
864
|
+
*/
|
|
865
|
+
interface Entry<T extends Scalar = Scalar> {
|
|
866
|
+
/**
|
|
867
|
+
* The value of the cell for this entry.
|
|
868
|
+
*/
|
|
869
|
+
value: T;
|
|
870
|
+
/**
|
|
871
|
+
* Coordinates of the cell for this entry.
|
|
872
|
+
*
|
|
873
|
+
* @remarks
|
|
874
|
+
* This should not be used as keys. For performance reasons the same object may be reused across iterations and should be treated as immutable.
|
|
875
|
+
*/
|
|
876
|
+
coords: Readonly<IRange.CellCoords>;
|
|
877
|
+
}
|
|
878
|
+
/**
|
|
879
|
+
* The direction to traverse a range, either by row or by column,
|
|
880
|
+
* or let the implementation decide the fastest way.
|
|
881
|
+
*/
|
|
882
|
+
type TraversalAxis = IRange.Axis | 'fastest';
|
|
883
|
+
interface TraversalAxisOptions {
|
|
884
|
+
/**
|
|
885
|
+
* The direction of the write.
|
|
886
|
+
*
|
|
887
|
+
* @defaultValue `IRange.Axis.Row`
|
|
888
|
+
*/
|
|
889
|
+
axis?: TraversalAxis;
|
|
890
|
+
}
|
|
891
|
+
/**
|
|
892
|
+
* Options for the {@link IRange.sparseBuilder}.
|
|
893
|
+
*
|
|
894
|
+
* @remarks
|
|
895
|
+
* See {@link IRange.sparseBuilder} for details and ordering/performance tradeoffs.
|
|
896
|
+
*/
|
|
897
|
+
interface SparseBuilderOptions extends TraversalAxisOptions {
|
|
898
|
+
}
|
|
899
|
+
/**
|
|
900
|
+
* A Builder for setting values in a streaming sparse grid.
|
|
901
|
+
*
|
|
902
|
+
* @template T The type of values stored within the tuples.
|
|
903
|
+
*
|
|
904
|
+
* @remarks
|
|
905
|
+
* Coordinates must increase monotonically in the current axis — row then
|
|
906
|
+
* column in row-major mode, column then row in column-major mode.
|
|
907
|
+
*/
|
|
908
|
+
interface SparseBuilder<T extends Scalar = Scalar> {
|
|
909
|
+
/**
|
|
910
|
+
* Adds a single value associated with a specific major and minor index.
|
|
911
|
+
*
|
|
912
|
+
* @param row The row index.
|
|
913
|
+
* @param col The column index.
|
|
914
|
+
* @param value The value to be added.
|
|
915
|
+
* @throws `Error` If the point is out of order compared to previously added points
|
|
916
|
+
* (within the same row).
|
|
917
|
+
*
|
|
918
|
+
* @remarks
|
|
919
|
+
* If the value is an Array, it will be treated as a discrete value. If you want to set
|
|
920
|
+
* multiple values along the minor axis, then use {@link pushMultipleAt}.
|
|
921
|
+
*/
|
|
922
|
+
pushAt(row: number, col: number, value: T): this;
|
|
923
|
+
/**
|
|
924
|
+
* Adds multiple values associated with a specific row and column index.
|
|
925
|
+
*
|
|
926
|
+
* @param row The row index.
|
|
927
|
+
* @param col The column index.
|
|
928
|
+
* @param value The values to add. Accepts an {@link IRange} or a native array.
|
|
929
|
+
* 2D ranges are flattened in row-major order.
|
|
930
|
+
* @throws `Error` If the point is out of order compared to previously added points
|
|
931
|
+
* (within the same row).
|
|
932
|
+
*/
|
|
933
|
+
pushMultipleAt(row: number, col: number, value: IRange | T[]): this;
|
|
934
|
+
/**
|
|
935
|
+
* Apply the updates.
|
|
936
|
+
*/
|
|
937
|
+
build(): IRange<T>;
|
|
938
|
+
}
|
|
939
|
+
/**
|
|
940
|
+
* Specifies a single direction for the range.
|
|
941
|
+
*/
|
|
942
|
+
const Axis: {
|
|
943
|
+
readonly Column: "column";
|
|
944
|
+
readonly Row: "row";
|
|
945
|
+
};
|
|
946
|
+
type Axis = typeof Axis[keyof typeof Axis];
|
|
947
|
+
/**
|
|
948
|
+
* Allows for either direction or both. (But not none).
|
|
949
|
+
*/
|
|
950
|
+
const AxisSelection: {
|
|
951
|
+
readonly Both: "both";
|
|
952
|
+
readonly Column: "column";
|
|
953
|
+
readonly Row: "row";
|
|
954
|
+
};
|
|
955
|
+
type AxisSelection = typeof AxisSelection[keyof typeof AxisSelection];
|
|
956
|
+
/**
|
|
957
|
+
* Indicates a direction for a range along an axis.
|
|
958
|
+
*
|
|
959
|
+
* @remarks
|
|
960
|
+
* Used during fill, shift, etc.
|
|
961
|
+
*/
|
|
962
|
+
const Direction: {
|
|
963
|
+
readonly Up: "up";
|
|
964
|
+
readonly Down: "down";
|
|
965
|
+
readonly Left: "left";
|
|
966
|
+
readonly Right: "right";
|
|
967
|
+
};
|
|
968
|
+
type Direction = typeof Direction[keyof typeof Direction];
|
|
969
|
+
/**
|
|
970
|
+
* The coordinates of an Range in a 2D space.
|
|
971
|
+
* The rowStart and colStart must be less than or equal to rowEnd and colEnd respectively.
|
|
972
|
+
*/
|
|
973
|
+
interface Coords {
|
|
974
|
+
/**
|
|
975
|
+
* Left column
|
|
976
|
+
*/
|
|
977
|
+
colStart: number;
|
|
978
|
+
/**
|
|
979
|
+
* Top row
|
|
980
|
+
*/
|
|
981
|
+
rowStart: number;
|
|
982
|
+
/**
|
|
983
|
+
* Right column
|
|
984
|
+
*/
|
|
985
|
+
colEnd: number;
|
|
986
|
+
/**
|
|
987
|
+
* Bottom row
|
|
988
|
+
*/
|
|
989
|
+
rowEnd: number;
|
|
990
|
+
/**
|
|
991
|
+
* An optional identifier for the 2D space these coordinates are relative to.
|
|
992
|
+
*/
|
|
993
|
+
scope?: string | null;
|
|
994
|
+
}
|
|
995
|
+
/**
|
|
996
|
+
* The coordinates of a single cell in a 2D space.
|
|
997
|
+
*/
|
|
998
|
+
interface CellCoords {
|
|
999
|
+
/**
|
|
1000
|
+
* The column index of the cell.
|
|
1001
|
+
*/
|
|
1002
|
+
colIndex: number;
|
|
1003
|
+
/**
|
|
1004
|
+
* The row index of the cell.
|
|
1005
|
+
*/
|
|
1006
|
+
rowIndex: number;
|
|
1007
|
+
/**
|
|
1008
|
+
* An optional identifier for the 2D space these coordinates are relative to.
|
|
1009
|
+
*/
|
|
1010
|
+
scope?: string | null;
|
|
1011
|
+
}
|
|
1012
|
+
/**
|
|
1013
|
+
* Adds `fixed flags` to `IRange.Coords`.
|
|
1014
|
+
*/
|
|
1015
|
+
interface FixableCoords extends IRange.Coords {
|
|
1016
|
+
/**
|
|
1017
|
+
* If `true` then the colStart is fixed.
|
|
1018
|
+
*
|
|
1019
|
+
* @defaultValue false
|
|
1020
|
+
*/
|
|
1021
|
+
$colStart?: boolean;
|
|
1022
|
+
/**
|
|
1023
|
+
* If `true` then the rowStart is fixed.
|
|
1024
|
+
*
|
|
1025
|
+
* @defaultValue false
|
|
1026
|
+
*/
|
|
1027
|
+
$rowStart?: boolean;
|
|
1028
|
+
/**
|
|
1029
|
+
* If `true` then the colEnd is fixed.
|
|
1030
|
+
*
|
|
1031
|
+
* @defaultValue false
|
|
1032
|
+
*/
|
|
1033
|
+
$colEnd?: boolean;
|
|
1034
|
+
/**
|
|
1035
|
+
* If `true` then the rowEnd is fixed.
|
|
1036
|
+
*
|
|
1037
|
+
* @defaultValue false
|
|
1038
|
+
*/
|
|
1039
|
+
$rowEnd?: boolean;
|
|
1040
|
+
}
|
|
1041
|
+
/**
|
|
1042
|
+
* Adds fixable flag to `IRange.CellCoords`.
|
|
1043
|
+
*/
|
|
1044
|
+
interface FixableCellCoords extends CellCoords {
|
|
1045
|
+
/**
|
|
1046
|
+
* If `true`, the column is fixed.
|
|
1047
|
+
*/
|
|
1048
|
+
$colIndex?: boolean;
|
|
1049
|
+
/**
|
|
1050
|
+
* If `true`, the row is fixed.
|
|
1051
|
+
*/
|
|
1052
|
+
$rowIndex?: boolean;
|
|
1053
|
+
}
|
|
1054
|
+
/**
|
|
1055
|
+
* A address can be Either a `Partial<IRange.FixableCoords>`, a `FormulaError.Known` or a named range `string`.
|
|
1056
|
+
*/
|
|
1057
|
+
type Address = Partial<IRange.FixableCoords> | FormulaError.Ref | FormulaError.Name | string;
|
|
1058
|
+
/**
|
|
1059
|
+
* Serialize coords.
|
|
1060
|
+
*
|
|
1061
|
+
* @remarks
|
|
1062
|
+
* A single coords, several coords, or an address string. Items that anchor to several ranges
|
|
1063
|
+
* (data validations, conditional formats, protected ranges) serialize as a comma-separated
|
|
1064
|
+
* string (`'A1:B2,D5:D9'`); the coords-array form is accepted on load but never emitted.
|
|
1065
|
+
*/
|
|
1066
|
+
type JSONCoords = IRange.Coords | readonly IRange.Coords[] | string;
|
|
1067
|
+
/**
|
|
1068
|
+
* A serialized address-value pair as an object.
|
|
1069
|
+
*/
|
|
1070
|
+
interface JSONReferenceEntry<J = any> {
|
|
1071
|
+
/**
|
|
1072
|
+
* Range Address
|
|
1073
|
+
*/
|
|
1074
|
+
r: JSONCoords;
|
|
1075
|
+
/**
|
|
1076
|
+
* Value
|
|
1077
|
+
*/
|
|
1078
|
+
v: J;
|
|
1079
|
+
}
|
|
1080
|
+
/**
|
|
1081
|
+
* A serialized address-value pair as an array tuple.
|
|
1082
|
+
*/
|
|
1083
|
+
type JSONReferenceTuple<J = any> = [range: JSONCoords, value: J];
|
|
1084
|
+
/**
|
|
1085
|
+
* A serialized address-value pair — either object or tuple form.
|
|
1086
|
+
*
|
|
1087
|
+
* @see {@link JSONReferenceEntry} {@link JSONReferenceTuple}
|
|
1088
|
+
*/
|
|
1089
|
+
type JSONReference<J = any> = JSONReferenceEntry<J> | JSONReferenceTuple<J>;
|
|
1090
|
+
}
|
|
1091
|
+
//# sourceMappingURL=IRange.d.ts.map
|