@stdlib/array-to-fancy 0.1.0

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.
@@ -0,0 +1,443 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2024 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ // TypeScript Version: 4.1
20
+
21
+ /// <reference types="@stdlib/types"/>
22
+
23
+ import { Collection, ArrayLike, AccessorArrayLike, Complex128Array, Complex64Array, DataType } from '@stdlib/types/array';
24
+
25
+ /**
26
+ * Interface describing an index object.
27
+ */
28
+ interface IndexObject {
29
+ /**
30
+ * Underlying array index data.
31
+ */
32
+ data: Collection | AccessorArrayLike<any>;
33
+
34
+ /**
35
+ * Index type.
36
+ */
37
+ type: 'mask' | 'bool' | 'int';
38
+
39
+ /**
40
+ * Underlying array data type.
41
+ */
42
+ dtype: DataType | null;
43
+ }
44
+
45
+ /**
46
+ * Interface describing a cache for resolving array index objects.
47
+ */
48
+ interface Cache {
49
+ /**
50
+ * Returns an array associated with the index object having a provided identifier.
51
+ *
52
+ * @param id - identifier
53
+ * @returns index data
54
+ */
55
+ get( id: any ): IndexObject | null;
56
+ }
57
+
58
+ /**
59
+ * Interface describing function options.
60
+ */
61
+ interface Options {
62
+ /**
63
+ * Boolean indicating whether to enforce strict bounds checking.
64
+ */
65
+ strict?: boolean;
66
+
67
+ /**
68
+ * Cache for resolving array index objects.
69
+ */
70
+ cache?: Cache;
71
+ }
72
+
73
+ /**
74
+ * Interface describing the main export.
75
+ */
76
+ interface Array2Fancy {
77
+ /**
78
+ * Converts an array to an object supporting fancy indexing.
79
+ *
80
+ * @param x - input array
81
+ * @param options - function options
82
+ * @param options.strict - boolean indicating whether to enforce strict bounds checking
83
+ * @param options.cache - cache for resolving array index objects
84
+ * @returns fancy array
85
+ *
86
+ * @example
87
+ * var Float64Array = require( '@stdlib/array-float64' );
88
+ *
89
+ * var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
90
+ *
91
+ * var y = array2fancy( x );
92
+ * // returns <Float64Array>
93
+ *
94
+ * var v = y[ ':' ];
95
+ * // returns <Float64Array>[ 1.0, 2.0, 3.0, 4.0 ]
96
+ */
97
+ ( x: Float64Array, options?: Options ): Float64Array;
98
+
99
+ /**
100
+ * Converts an array to an object supporting fancy indexing.
101
+ *
102
+ * @param x - input array
103
+ * @param options - function options
104
+ * @param options.strict - boolean indicating whether to enforce strict bounds checking
105
+ * @param options.cache - cache for resolving array index objects
106
+ * @returns fancy array
107
+ *
108
+ * @example
109
+ * var Float32Array = require( '@stdlib/array-float32' );
110
+ *
111
+ * var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] );
112
+ *
113
+ * var y = array2fancy( x );
114
+ * // returns <Float32Array>
115
+ *
116
+ * var v = y[ ':' ];
117
+ * // returns <Float32Array>[ 1.0, 2.0, 3.0, 4.0 ]
118
+ */
119
+ ( x: Float32Array, options?: Options ): Float32Array;
120
+
121
+ /**
122
+ * Converts an array to an object supporting fancy indexing.
123
+ *
124
+ * @param x - input array
125
+ * @param options - function options
126
+ * @param options.strict - boolean indicating whether to enforce strict bounds checking
127
+ * @param options.cache - cache for resolving array index objects
128
+ * @returns fancy array
129
+ *
130
+ * @example
131
+ * var Complex128Array = require( '@stdlib/array-complex128' );
132
+ *
133
+ * var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0 ] );
134
+ *
135
+ * var y = array2fancy( x );
136
+ * // returns <Complex128Array>
137
+ *
138
+ * var v = y[ ':' ];
139
+ * // returns <Complex128Array>[ 1.0, 2.0, 3.0, 4.0 ]
140
+ */
141
+ ( x: Complex128Array, options?: Options ): Complex128Array;
142
+
143
+ /**
144
+ * Converts an array to an object supporting fancy indexing.
145
+ *
146
+ * @param x - input array
147
+ * @param options - function options
148
+ * @param options.strict - boolean indicating whether to enforce strict bounds checking
149
+ * @param options.cache - cache for resolving array index objects
150
+ * @returns fancy array
151
+ *
152
+ * @example
153
+ * var Complex64Array = require( '@stdlib/array-complex64' );
154
+ *
155
+ * var x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
156
+ *
157
+ * var y = array2fancy( x );
158
+ * // returns <Complex64Array>
159
+ *
160
+ * var v = y[ ':' ];
161
+ * // returns <Complex64Array>[ 1.0, 2.0, 3.0, 4.0 ]
162
+ */
163
+ ( x: Complex64Array, options?: Options ): Complex64Array;
164
+
165
+ /**
166
+ * Converts an array to an object supporting fancy indexing.
167
+ *
168
+ * @param x - input array
169
+ * @param options - function options
170
+ * @param options.strict - boolean indicating whether to enforce strict bounds checking
171
+ * @param options.cache - cache for resolving array index objects
172
+ * @returns fancy array
173
+ *
174
+ * @example
175
+ * var Int32Array = require( '@stdlib/array-int32' );
176
+ *
177
+ * var x = new Int32Array( [ 1, 2, 3, 4 ] );
178
+ *
179
+ * var y = array2fancy( x );
180
+ * // returns <Int32Array>
181
+ *
182
+ * var v = y[ ':' ];
183
+ * // returns <Int32Array>[ 1, 2, 3, 4 ]
184
+ */
185
+ ( x: Int32Array, options?: Options ): Int32Array;
186
+
187
+ /**
188
+ * Converts an array to an object supporting fancy indexing.
189
+ *
190
+ * @param x - input array
191
+ * @param options - function options
192
+ * @param options.strict - boolean indicating whether to enforce strict bounds checking
193
+ * @param options.cache - cache for resolving array index objects
194
+ * @returns fancy array
195
+ *
196
+ * @example
197
+ * var Int16Array = require( '@stdlib/array-int16' );
198
+ *
199
+ * var x = new Int16Array( [ 1, 2, 3, 4 ] );
200
+ *
201
+ * var y = array2fancy( x );
202
+ * // returns <Int16Array>
203
+ *
204
+ * var v = y[ ':' ];
205
+ * // returns <Int16Array>[ 1, 2, 3, 4 ]
206
+ */
207
+ ( x: Int16Array, options?: Options ): Int16Array;
208
+
209
+ /**
210
+ * Converts an array to an object supporting fancy indexing.
211
+ *
212
+ * @param x - input array
213
+ * @param options - function options
214
+ * @param options.strict - boolean indicating whether to enforce strict bounds checking
215
+ * @param options.cache - cache for resolving array index objects
216
+ * @returns fancy array
217
+ *
218
+ * @example
219
+ * var Int8Array = require( '@stdlib/array-int8' );
220
+ *
221
+ * var x = new Int8Array( [ 1, 2, 3, 4 ] );
222
+ *
223
+ * var y = array2fancy( x );
224
+ * // returns <Int8Array>
225
+ *
226
+ * var v = y[ ':' ];
227
+ * // returns <Int8Array>[ 1, 2, 3, 4 ]
228
+ */
229
+ ( x: Int8Array, options?: Options ): Int8Array;
230
+
231
+ /**
232
+ * Converts an array to an object supporting fancy indexing.
233
+ *
234
+ * @param x - input array
235
+ * @param options - function options
236
+ * @param options.strict - boolean indicating whether to enforce strict bounds checking
237
+ * @param options.cache - cache for resolving array index objects
238
+ * @returns fancy array
239
+ *
240
+ * @example
241
+ * var Uint32Array = require( '@stdlib/array-uint32' );
242
+ *
243
+ * var x = new Uint32Array( [ 1, 2, 3, 4 ] );
244
+ *
245
+ * var y = array2fancy( x );
246
+ * // returns <Uint32Array>
247
+ *
248
+ * var v = y[ ':' ];
249
+ * // returns <Uint32Array>[ 1, 2, 3, 4 ]
250
+ */
251
+ ( x: Uint32Array, options?: Options ): Uint32Array;
252
+
253
+ /**
254
+ * Converts an array to an object supporting fancy indexing.
255
+ *
256
+ * @param x - input array
257
+ * @param options - function options
258
+ * @param options.strict - boolean indicating whether to enforce strict bounds checking
259
+ * @param options.cache - cache for resolving array index objects
260
+ * @returns fancy array
261
+ *
262
+ * @example
263
+ * var Uint16Array = require( '@stdlib/array-uint16' );
264
+ *
265
+ * var x = new Uint16Array( [ 1, 2, 3, 4 ] );
266
+ *
267
+ * var y = array2fancy( x );
268
+ * // returns <Uint16Array>
269
+ *
270
+ * var v = y[ ':' ];
271
+ * // returns <Uint16Array>[ 1, 2, 3, 4 ]
272
+ */
273
+ ( x: Uint16Array, options?: Options ): Uint16Array;
274
+
275
+ /**
276
+ * Converts an array to an object supporting fancy indexing.
277
+ *
278
+ * @param x - input array
279
+ * @param options - function options
280
+ * @param options.strict - boolean indicating whether to enforce strict bounds checking
281
+ * @param options.cache - cache for resolving array index objects
282
+ * @returns fancy array
283
+ *
284
+ * @example
285
+ * var Uint8Array = require( '@stdlib/array-uint8' );
286
+ *
287
+ * var x = new Uint8Array( [ 1, 2, 3, 4 ] );
288
+ *
289
+ * var y = array2fancy( x );
290
+ * // returns <Uint8Array>
291
+ *
292
+ * var v = y[ ':' ];
293
+ * // returns <Uint8Array>[ 1, 2, 3, 4 ]
294
+ */
295
+ ( x: Uint8Array, options?: Options ): Uint8Array;
296
+
297
+ /**
298
+ * Converts an array to an object supporting fancy indexing.
299
+ *
300
+ * @param x - input array
301
+ * @param options - function options
302
+ * @param options.strict - boolean indicating whether to enforce strict bounds checking
303
+ * @param options.cache - cache for resolving array index objects
304
+ * @returns fancy array
305
+ *
306
+ * @example
307
+ * var Uint8ClampedArray = require( '@stdlib/array-uint8c' );
308
+ *
309
+ * var x = new Uint8ClampedArray( [ 1, 2, 3, 4 ] );
310
+ *
311
+ * var y = array2fancy( x );
312
+ * // returns <Uint8ClampedArray>
313
+ *
314
+ * var v = y[ ':' ];
315
+ * // returns <Uint8ClampedArray>[ 1, 2, 3, 4 ]
316
+ */
317
+ ( x: Uint8ClampedArray, options?: Options ): Uint8ClampedArray;
318
+
319
+ /**
320
+ * Converts an array to an object supporting fancy indexing.
321
+ *
322
+ * @param x - input array
323
+ * @param options - function options
324
+ * @param options.strict - boolean indicating whether to enforce strict bounds checking
325
+ * @param options.cache - cache for resolving array index objects
326
+ * @returns fancy array
327
+ *
328
+ * @example
329
+ * var x = [ 1, 2, 3, 4 ];
330
+ *
331
+ * var y = array2fancy( x );
332
+ * // returns <Array>
333
+ *
334
+ * var v = y[ ':' ];
335
+ * // returns [ 1, 2, 3, 4 ]
336
+ */
337
+ <T = unknown>( x: Array<T>, options?: Options ): Array<T>;
338
+
339
+ /**
340
+ * Converts an array to an object supporting fancy indexing.
341
+ *
342
+ * @param x - input array
343
+ * @param options - function options
344
+ * @param options.strict - boolean indicating whether to enforce strict bounds checking
345
+ * @param options.cache - cache for resolving array index objects
346
+ * @returns fancy array
347
+ *
348
+ * @example
349
+ * var toAccessorArray = require( '@stdlib/array-base-to-accessor-array' );
350
+ *
351
+ * var x = toAccessorArray( [ 1, 2, 3, 4 ] );
352
+ *
353
+ * var y = array2fancy( x );
354
+ * var v = y[ ':' ];
355
+ */
356
+ <T = unknown>( x: AccessorArrayLike<T>, options?: Options ): AccessorArrayLike<T>;
357
+
358
+ /**
359
+ * Converts an array to an object supporting fancy indexing.
360
+ *
361
+ * @param x - input array
362
+ * @param options - function options
363
+ * @param options.strict - boolean indicating whether to enforce strict bounds checking
364
+ * @param options.cache - cache for resolving array index objects
365
+ * @returns fancy array
366
+ *
367
+ * @example
368
+ * var x = [ 1, 2, 3, 4 ];
369
+ *
370
+ * var y = array2fancy( x );
371
+ * // returns <Array>
372
+ *
373
+ * var v = y[ ':' ];
374
+ * // returns [ 1, 2, 3, 4 ]
375
+ */
376
+ <T = unknown>( x: Collection<T>, options?: Options ): Collection<T>;
377
+
378
+ /**
379
+ * Converts an array-like value to an object supporting fancy indexing.
380
+ *
381
+ * @param x - input array
382
+ * @param options - function options
383
+ * @param options.strict - boolean indicating whether to enforce strict bounds checking
384
+ * @param options.cache - cache for resolving array index objects
385
+ * @returns fancy array
386
+ *
387
+ * @example
388
+ * var x = [ 1, 2, 3, 4 ];
389
+ *
390
+ * var y = array2fancy( x );
391
+ * // returns <Array>
392
+ *
393
+ * var v = y[ ':' ];
394
+ * // returns [ 1, 2, 3, 4 ]
395
+ */
396
+ <T = unknown>( x: ArrayLike<T>, options?: Options ): ArrayLike<T>;
397
+
398
+ /**
399
+ * Returns a function for converting an array to an object supporting fancy indexing.
400
+ *
401
+ * @param options - function options
402
+ * @param options.strict - boolean indicating whether to enforce strict bounds checking by default
403
+ * @param options.cache - cache for resolving array index objects
404
+ * @returns function for converting an array to an object supporting fancy indexing
405
+ *
406
+ * @example
407
+ * var fcn = array2fancy.factory();
408
+ *
409
+ * var x = [ 1, 2, 3, 4 ];
410
+ *
411
+ * var y = fcn( x );
412
+ * // returns <Array>
413
+ *
414
+ * var v = y[ ':' ];
415
+ * // returns [ 1, 2, 3, 4 ]
416
+ */
417
+ factory( options?: Options ): Array2Fancy;
418
+ }
419
+
420
+ /**
421
+ * Converts an array to an object supporting fancy indexing.
422
+ *
423
+ * @param x - input array
424
+ * @param options - function options
425
+ * @param options.strict - boolean indicating whether to enforce strict bounds checking
426
+ * @param options.cache - cache for resolving array index objects
427
+ * @returns fancy array
428
+ *
429
+ * @example
430
+ * var x = [ 1, 2, 3, 4 ];
431
+ *
432
+ * var y = array2fancy( x );
433
+ * // returns <Array>
434
+ *
435
+ * var v = y[ ':' ];
436
+ * // returns [ 1, 2, 3, 4 ]
437
+ */
438
+ declare var array2fancy: Array2Fancy;
439
+
440
+
441
+ // EXPORTS //
442
+
443
+ export = array2fancy;
package/lib/ctor.js ADDED
@@ -0,0 +1,94 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2024 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ 'use strict';
20
+
21
+ // MAIN //
22
+
23
+ /**
24
+ * Returns a trap for constructing new array instances.
25
+ *
26
+ * @private
27
+ * @param {Function} array2fancy - function for creating a proxied array
28
+ * @param {Object} opts - options
29
+ * @param {boolean} opts.strict - boolean indicating whether to perform strict bounds checking
30
+ * @returns {Function} handler
31
+ */
32
+ function factory( array2fancy, opts ) {
33
+ return constructor;
34
+
35
+ /**
36
+ * Trap for constructing new array instances.
37
+ *
38
+ * @private
39
+ * @param {Object} target - target object
40
+ * @param {Array} args - list of constructor arguments
41
+ * @param {Object} newTarget - constructor that was originally called
42
+ * @returns {*} new instance
43
+ */
44
+ function constructor( target, args ) {
45
+ var x;
46
+ var a;
47
+
48
+ a = args;
49
+ switch ( a.length ) {
50
+ case 0:
51
+ x = new target();
52
+ break;
53
+ case 1:
54
+ x = new target( a[0] );
55
+ break;
56
+ case 2:
57
+ x = new target( a[0], a[1] );
58
+ break;
59
+ case 3:
60
+ x = new target( a[0], a[1], a[2] );
61
+ break;
62
+ case 4:
63
+ x = new target( a[0], a[1], a[2], a[3] );
64
+ break;
65
+ case 5:
66
+ x = new target( a[0], a[1], a[2], a[3], a[4] );
67
+ break;
68
+ case 6:
69
+ x = new target( a[0], a[1], a[2], a[3], a[4], a[5] );
70
+ break;
71
+ case 7:
72
+ x = new target( a[0], a[1], a[2], a[3], a[4], a[5], a[6] );
73
+ break;
74
+ case 8:
75
+ x = new target( a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7] );
76
+ break;
77
+ case 9:
78
+ x = new target( a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8] ); // eslint-disable-line max-len
79
+ break;
80
+ case 10:
81
+ x = new target( a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9] ); // eslint-disable-line max-len
82
+ break;
83
+ default:
84
+ // Fallback to using `apply`; however, some constructors may error if the constructor is not callable (i.e., if a constructor always requires `new`):
85
+ x = target.apply( null, a );
86
+ }
87
+ return array2fancy( x, opts );
88
+ }
89
+ }
90
+
91
+
92
+ // EXPORTS //
93
+
94
+ module.exports = factory;
@@ -0,0 +1,48 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2024 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ 'use strict';
20
+
21
+ // MODULES //
22
+
23
+ var ArrayIndex = require( '@stdlib/array-index' );
24
+
25
+
26
+ // MAIN //
27
+
28
+ /**
29
+ * Returns default options.
30
+ *
31
+ * @private
32
+ * @returns {Object} default options
33
+ *
34
+ * @example
35
+ * var o = defaults();
36
+ * // returns {...}
37
+ */
38
+ function defaults() {
39
+ return {
40
+ 'cache': ArrayIndex,
41
+ 'strict': false
42
+ };
43
+ }
44
+
45
+
46
+ // EXPORTS //
47
+
48
+ module.exports = defaults;
@@ -0,0 +1,53 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2024 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ 'use strict';
20
+
21
+ // MODULES //
22
+
23
+ var isRangeError = require( '@stdlib/assert-is-range-error' );
24
+ var isTypeError = require( '@stdlib/assert-is-type-error' );
25
+ var isSyntaxError = require( '@stdlib/assert-is-syntax-error' );
26
+
27
+
28
+ // MAIN //
29
+
30
+ /**
31
+ * Returns the error constructor for a provided error object.
32
+ *
33
+ * @private
34
+ * @param {Error} err - error object
35
+ * @returns {Function} error constructor
36
+ */
37
+ function errConstructor( err ) {
38
+ if ( isRangeError( err ) ) {
39
+ return RangeError;
40
+ }
41
+ if ( isTypeError( err ) ) {
42
+ return TypeError;
43
+ }
44
+ if ( isSyntaxError( err ) ) {
45
+ return SyntaxError;
46
+ }
47
+ return Error;
48
+ }
49
+
50
+
51
+ // EXPORTS //
52
+
53
+ module.exports = errConstructor;
@@ -0,0 +1,42 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2024 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ 'use strict';
20
+
21
+ // MODULES //
22
+
23
+ var replace = require( '@stdlib/string-base-replace' );
24
+
25
+
26
+ // MAIN //
27
+
28
+ /**
29
+ * Returns an updated error message for trapped errors.
30
+ *
31
+ * @private
32
+ * @param {string} msg - error message
33
+ * @returns {string} updated message
34
+ */
35
+ function errMessage( msg ) {
36
+ return replace( msg, /^invalid argument/, 'invalid operation' );
37
+ }
38
+
39
+
40
+ // EXPORTS //
41
+
42
+ module.exports = errMessage;