Skip to content

Commit 4394246

Browse files
committed
feat: add factory method for specifying defaults
1 parent 07a4dcb commit 4394246

File tree

14 files changed

+1373
-737
lines changed

14 files changed

+1373
-737
lines changed

lib/node_modules/@stdlib/array/to-fancy/README.md

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ v = y[ ':' ];
114114
The function supports the following options:
115115

116116
- **strict**: boolean indicating whether to enforce strict bounds checking. Default: `false`.
117+
- **cache**: cache for resolving array index objects. Default: [`ArrayIndex`][@stdlib/array/index].
117118

118119
By default, the function returns a fancy array which does **not** enforce strict bounds checking. For example,
119120

@@ -137,16 +138,64 @@ var v = y[ 10 ];
137138
// throws <RangeError>
138139
```
139140

141+
#### array2fancy.factory( \[options] )
142+
143+
Returns a function for converting an array to an object supporting fancy indexing.
144+
145+
```javascript
146+
var fcn = array2fancy.factory();
147+
148+
var x = [ 1, 2, 3, 4 ];
149+
150+
var y = fcn( x );
151+
// returns <Array>
152+
153+
var v = y[ ':' ];
154+
// returns [ 1, 2, 3, 4 ]
155+
```
156+
157+
The function supports the following options:
158+
159+
- **strict**: boolean indicating whether to enforce strict bounds checking by default. Default: `false`.
160+
- **cache**: cache for resolving array index objects. Default: [`ArrayIndex`][@stdlib/array/index].
161+
162+
By default, the function returns a function which, by default, does **not** enforce strict bounds checking. For example,
163+
164+
```javascript
165+
var fcn = array2fancy.factory();
166+
167+
var y = fcn( [ 1, 2, 3, 4 ] );
168+
169+
var v = y[ 10 ];
170+
// returns undefined
171+
```
172+
173+
To enforce strict bounds checking by default, set the `strict` option to `true`.
174+
175+
<!-- run throws: true -->
176+
177+
```javascript
178+
var fcn = array2fancy.factory({
179+
'strict': true
180+
});
181+
var y = fcn( [ 1, 2, 3, 4 ] );
182+
183+
var v = y[ 10 ];
184+
// throws <RangeError>
185+
```
186+
187+
The returned function supports the same options as above. When the returned function is provided option values, those values override the factory method defaults.
188+
140189
</section>
141190

142191
<!-- /.usage -->
143192

144193
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
145194

146-
<section class="notes">
147-
148195
* * *
149196

197+
<section class="notes">
198+
150199
## Notes
151200

152201
- A fancy array shares the **same** data as the provided input array. Hence, any mutations to the returned array will affect the underlying input array and vice versa.
@@ -313,10 +362,10 @@ y[ ':' ] = new Float64Array( [ 5.0, 6.0 ] ); // is this a single complex number
313362

314363
<!-- Package usage examples. -->
315364

316-
<section class="examples">
317-
318365
* * *
319366

367+
<section class="examples">
368+
320369
## Examples
321370

322371
<!-- eslint no-undef: "error" -->
@@ -388,6 +437,8 @@ z = y[ ':' ];
388437

389438
[@stdlib/array/complex64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex64
390439

440+
[@stdlib/array/index]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/index
441+
391442
</section>
392443

393444
<!-- /.links -->
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 bench = require( '@stdlib/bench' );
24+
var isFunction = require( '@stdlib/assert/is-function' );
25+
var pkg = require( './../package.json' ).name;
26+
var array2fancy = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg+':factory', function benchmark( b ) {
32+
var v;
33+
var i;
34+
35+
b.tic();
36+
for ( i = 0; i < b.iterations; i++ ) {
37+
v = array2fancy.factory();
38+
if ( typeof v !== 'function' ) {
39+
b.fail( 'should return a function' );
40+
}
41+
}
42+
b.toc();
43+
if ( !isFunction( v ) ) {
44+
b.fail( 'should return a function' );
45+
}
46+
b.pass( 'benchmark finished' );
47+
b.end();
48+
});

lib/node_modules/@stdlib/array/to-fancy/docs/repl.txt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@
6969
Boolean indicating whether to enforce strict bounds checking. Default:
7070
false.
7171

72+
options.cache: Object (optional)
73+
Cache for resolving array index objects. Must have a 'get' method which
74+
accepts a single argument: an identifier associated with an array index.
75+
If an array index associated with a provided identifier exists, the
76+
'get' method should return an object having the following properties:
77+
78+
- data: the underlying index array.
79+
- type: the index type. Must be either 'mask', 'bool', or 'int'.
80+
- dtype: the data type of the underlying array.
81+
82+
If an array index is not associated with a provided identifier, the
83+
'get' method should return `null`.
84+
7285
Returns
7386
-------
7487
out: Array|TypedArray|Object
@@ -82,6 +95,47 @@
8295
> y[ '::-1' ]
8396
[ 4, 3, 2, 1 ]
8497

98+
99+
{{alias}}.factory( [options] )
100+
Returns a function for converting an array to an object supporting fancy
101+
indexing.
102+
103+
Parameters
104+
----------
105+
options: Object (optional)
106+
Function options.
107+
108+
options.strict: boolean (optional)
109+
Boolean indicating whether to enforce strict bounds checking by default.
110+
Default: false.
111+
112+
options.cache: Object (optional)
113+
Cache for resolving array index objects. Must have a 'get' method which
114+
accepts a single argument: an identifier associated with an array index.
115+
If an array index associated with a provided identifier exists, the
116+
'get' method should return an object having the following properties:
117+
118+
- data: the underlying index array.
119+
- type: the index type. Must be either 'mask', 'bool', or 'int'.
120+
- dtype: the data type of the underlying array.
121+
122+
If an array index is not associated with a provided identifier, the
123+
'get' method should return `null`.
124+
125+
Returns
126+
-------
127+
fcn: Function
128+
Function for converting an array to an object supporting fancy indexing.
129+
130+
Examples
131+
--------
132+
> var f = {{alias}}.factory();
133+
> var y = f( [ 1, 2, 3, 4 ] );
134+
> y[ '1::2' ]
135+
[ 2, 4 ]
136+
> y[ '::-1' ]
137+
[ 4, 3, 2, 1 ]
138+
85139
See Also
86140
--------
87141

0 commit comments

Comments
 (0)