Skip to content

Commit 9363dc3

Browse files
committed
feat: add get method for retrieving a single default setting
1 parent 3556d4e commit 9363dc3

File tree

10 files changed

+338
-37
lines changed

10 files changed

+338
-37
lines changed

lib/node_modules/@stdlib/ndarray/defaults/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ The returned object has the following properties:
6767

6868
- **index_mode**: default index mode.
6969

70+
#### defaults.get( name )
71+
72+
Returns the setting value for a provided setting `name`.
73+
74+
```javascript
75+
var v = defaults.get( 'order' );
76+
// returns <string>
77+
78+
v = defaults.get( 'dtypes.floating_point' );
79+
// returns <string>
80+
```
81+
82+
The setting `name` corresponds to a flattened object path. For example, the setting name `'dtypes.default'` maps to the nested path `o.dtypes.default` as found in the object returned by `defaults()`.
83+
7084
</section>
7185

7286
<!-- /.usage -->

lib/node_modules/@stdlib/ndarray/defaults/benchmark/benchmark.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,29 @@ bench( pkg, function benchmark( b ) {
4545
b.pass( 'benchmark finished' );
4646
b.end();
4747
});
48+
49+
bench( pkg+':get', function benchmark( b ) {
50+
var values;
51+
var out;
52+
var i;
53+
54+
values = [
55+
'dtypes.default',
56+
'casting',
57+
'order'
58+
];
59+
60+
b.tic();
61+
for ( i = 0; i < b.iterations; i++ ) {
62+
out = defaults.get( values[ i%values.length ] );
63+
if ( typeof out !== 'string' ) {
64+
b.fail( 'should return a string' );
65+
}
66+
}
67+
b.toc();
68+
if ( typeof out !== 'string' ) {
69+
b.fail( 'should return a string' );
70+
}
71+
b.pass( 'benchmark finished' );
72+
b.end();
73+
});

lib/node_modules/@stdlib/ndarray/defaults/docs/repl.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@
4545
> var out = {{alias}}()
4646
{...}
4747

48+
49+
{{alias}}.get( name )
50+
Returns a default setting.
51+
52+
Parameters
53+
----------
54+
name: string
55+
Setting name.
56+
57+
Returns
58+
-------
59+
out: any
60+
Setting value.
61+
62+
Examples
63+
--------
64+
> var v = {{alias}}.get( 'order' )
65+
<string>
66+
4867
See Also
4968
--------
5069

lib/node_modules/@stdlib/ndarray/defaults/docs/types/index.d.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,34 @@ interface Defaults {
8383
index_mode: 'throw';
8484
}
8585

86+
/**
87+
* Interface for returning default ndarray settings.
88+
*/
89+
interface DefaultsFunction {
90+
/**
91+
* Returns default ndarray settings.
92+
*
93+
* @returns default settings
94+
*
95+
* @example
96+
* var o = defaults();
97+
* // returns {...}
98+
*/
99+
(): Defaults;
100+
101+
/**
102+
* Returns a default ndarray setting.
103+
*
104+
* @param name - setting name
105+
* @returns setting value or null
106+
*
107+
* @example
108+
* var v = defaults.get( 'order' );
109+
* // returns <string>
110+
*/
111+
get( name: string ): any;
112+
}
113+
86114
/**
87115
* Returns default ndarray settings.
88116
*
@@ -91,8 +119,12 @@ interface Defaults {
91119
* @example
92120
* var o = defaults();
93121
* // returns {...}
122+
*
123+
* @example
124+
* var v = defaults.get( 'order' );
125+
* // returns <string>
94126
*/
95-
declare function defaults(): Defaults;
127+
declare var defaults: DefaultsFunction;
96128

97129

98130
// EXPORTS //

lib/node_modules/@stdlib/ndarray/defaults/docs/types/test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,25 @@ import defaults = require( './index' );
3030
{
3131
defaults( 9 ); // $ExpectError
3232
}
33+
34+
// Attached to the function is a `get` method for returning a default setting...
35+
{
36+
defaults.get( 'order' ); // $ExpectType any
37+
}
38+
39+
// The compiler throws an error if the method is not provided a string...
40+
{
41+
defaults.get( 5 ); // $ExpectError
42+
defaults.get( true ); // $ExpectError
43+
defaults.get( false ); // $ExpectError
44+
defaults.get( null ); // $ExpectError
45+
defaults.get( [] ); // $ExpectError
46+
defaults.get( {} ); // $ExpectError
47+
defaults.get( ( x: number ) => x ); // $ExpectError
48+
}
49+
50+
// The compiler throws an error if the method is provided an unsupported number of arguments...
51+
{
52+
defaults.get(); // $ExpectError
53+
defaults.get( 'foo', 5 ); // $ExpectError
54+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 defaults = require( './main.js' );
24+
25+
26+
// VARIABLES //
27+
28+
var DEFAULTS = defaults();
29+
var HASH = {
30+
'dtypes.default': DEFAULTS.dtypes.default,
31+
'dtypes.floating_point': DEFAULTS.dtypes.floating_point,
32+
'dtypes.real_floating_point': DEFAULTS.dtypes.real_floating_point,
33+
'dtypes.complex_floating_point': DEFAULTS.dtypes.complex_floating_point,
34+
'dtypes.integral': DEFAULTS.dtypes.integral,
35+
'dtypes.signed_integer': DEFAULTS.dtypes.signed_integer,
36+
'dtypes.unsigned_integer': DEFAULTS.dtypes.unsigned_integer,
37+
'order': DEFAULTS.order,
38+
'casting': DEFAULTS.casting,
39+
'index_mode': DEFAULTS.index_mode
40+
};
41+
42+
43+
// MAIN //
44+
45+
/**
46+
* Returns a default ndarray setting.
47+
*
48+
* @param {string} name - setting name
49+
* @returns {*} default setting or null
50+
*
51+
* @example
52+
* var v = get( 'dtypes.default' );
53+
* // returns <string>
54+
*/
55+
function get( name ) {
56+
var v = HASH[ name ];
57+
return ( v === void 0 ) ? null : v;
58+
}
59+
60+
61+
// EXPORTS //
62+
63+
module.exports = get;

lib/node_modules/@stdlib/ndarray/defaults/lib/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@
3232

3333
// MODULES //
3434

35+
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
3536
var main = require( './main.js' );
37+
var get = require( './get.js' );
38+
39+
40+
// MAIN //
41+
42+
setReadOnly( main, 'get', get );
3643

3744

3845
// EXPORTS //
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 tape = require( 'tape' );
24+
var defaults = require( './../lib/main.js' );
25+
var get = require( './../lib/get.js' );
26+
27+
28+
// VARIABLES //
29+
30+
var DEFAULTS = defaults();
31+
32+
33+
// TESTS //
34+
35+
tape( 'main export is a function', function test( t ) {
36+
t.ok( true, __filename );
37+
t.strictEqual( typeof get, 'function', 'main export is a function' );
38+
t.end();
39+
});
40+
41+
tape( 'if provided a recognized setting, the function returns a default value', function test( t ) {
42+
var expected;
43+
var actual;
44+
var values;
45+
var i;
46+
47+
values = [
48+
'dtypes.default',
49+
'dtypes.floating_point',
50+
'dtypes.real_floating_point',
51+
'dtypes.complex_floating_point',
52+
'dtypes.integral',
53+
'dtypes.signed_integer',
54+
'dtypes.unsigned_integer',
55+
56+
'casting',
57+
'order',
58+
'index_mode'
59+
];
60+
expected = [
61+
DEFAULTS.dtypes.default,
62+
DEFAULTS.dtypes.floating_point,
63+
DEFAULTS.dtypes.real_floating_point,
64+
DEFAULTS.dtypes.complex_floating_point,
65+
DEFAULTS.dtypes.integral,
66+
DEFAULTS.dtypes.signed_integer,
67+
DEFAULTS.dtypes.unsigned_integer,
68+
69+
DEFAULTS.casting,
70+
DEFAULTS.order,
71+
DEFAULTS.index_mode
72+
];
73+
for ( i = 0; i < values.length; i++ ) {
74+
actual = get( values[ i ] );
75+
t.strictEqual( actual, expected[ i ], 'returns expected value. actual: '+actual+'. expected: '+expected[ i ]+'.' );
76+
}
77+
t.end();
78+
});

lib/node_modules/@stdlib/ndarray/defaults/test/test.js

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,41 +33,8 @@ tape( 'main export is a function', function test( t ) {
3333
t.end();
3434
});
3535

36-
tape( 'the function returns default ndarray settings', function test( t ) {
37-
var o = defaults();
38-
39-
t.strictEqual( hasOwnProp( o, 'dtypes' ), true, 'has property' );
40-
t.strictEqual( typeof o.dtypes, 'object', 'returns expected value' );
41-
42-
t.strictEqual( hasOwnProp( o.dtypes, 'default' ), true, 'has property' );
43-
t.strictEqual( typeof o.dtypes.default, 'string', 'returns expected value' );
44-
45-
t.strictEqual( hasOwnProp( o.dtypes, 'floating_point' ), true, 'has property' );
46-
t.strictEqual( typeof o.dtypes.floating_point, 'string', 'returns expected value' );
47-
48-
t.strictEqual( hasOwnProp( o.dtypes, 'real_floating_point' ), true, 'has property' );
49-
t.strictEqual( typeof o.dtypes.real_floating_point, 'string', 'returns expected value' );
50-
51-
t.strictEqual( hasOwnProp( o.dtypes, 'complex_floating_point' ), true, 'has property' );
52-
t.strictEqual( typeof o.dtypes.complex_floating_point, 'string', 'returns expected value' );
53-
54-
t.strictEqual( hasOwnProp( o.dtypes, 'integral' ), true, 'has property' );
55-
t.strictEqual( typeof o.dtypes.integral, 'string', 'returns expected value' );
56-
57-
t.strictEqual( hasOwnProp( o.dtypes, 'signed_integer' ), true, 'has property' );
58-
t.strictEqual( typeof o.dtypes.signed_integer, 'string', 'returns expected value' );
59-
60-
t.strictEqual( hasOwnProp( o.dtypes, 'unsigned_integer' ), true, 'has property' );
61-
t.strictEqual( typeof o.dtypes.unsigned_integer, 'string', 'returns expected value' );
62-
63-
t.strictEqual( hasOwnProp( o, 'order' ), true, 'has property' );
64-
t.strictEqual( typeof o.order, 'string', 'returns expected value' );
65-
66-
t.strictEqual( hasOwnProp( o, 'casting' ), true, 'has property' );
67-
t.strictEqual( typeof o.casting, 'string', 'returns expected value' );
68-
69-
t.strictEqual( hasOwnProp( o, 'index_mode' ), true, 'has property' );
70-
t.strictEqual( typeof o.index_mode, 'string', 'returns expected value' );
71-
36+
tape( 'attached to the main function is a method to retrieve a setting', function test( t ) {
37+
t.strictEqual( hasOwnProp( defaults, 'get' ), true, 'has property' );
38+
t.strictEqual( typeof defaults.get, 'function', 'returns expected value' );
7239
t.end();
7340
});

0 commit comments

Comments
 (0)