Skip to content

Commit 13e7d2c

Browse files
committed
refactor: support data type instances
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 1add791 commit 13e7d2c

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

lib/node_modules/@stdlib/random/tools/unary/docs/repl.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
prng: Function
99
Unary pseudorandom value generator.
1010

11-
idtypes: Array<string>
11+
idtypes: Array<string|DataType>
1212
List of supported input data types.
1313

14-
odtypes: Array<string>
14+
odtypes: Array<string|DataType>
1515
List of supported output data types.
1616

1717
policies: Object
@@ -53,7 +53,7 @@
5353
options: Object (optional)
5454
Function options.
5555

56-
options.dtype: string (optional)
56+
options.dtype: string|DataType (optional)
5757
Output data type. Setting this option overrides the output data type
5858
policy.
5959

lib/node_modules/@stdlib/random/tools/unary/lib/main.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var isPlainObject = require( '@stdlib/assert/is-plain-object' );
3333
var isObject = require( '@stdlib/assert/is-object' );
3434
var isCollection = require( '@stdlib/assert/is-collection' );
3535
var isDataType = require( '@stdlib/ndarray/base/assert/is-data-type' );
36+
var isEqualDataType = require( '@stdlib/ndarray/base/assert/is-equal-data-type' );
3637
var isOutputDataTypePolicy = require( '@stdlib/ndarray/base/assert/is-output-data-type-policy' );
3738
var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' );
3839
var isOrder = require( '@stdlib/ndarray/base/assert/is-order' );
@@ -46,6 +47,8 @@ var unary = require( '@stdlib/ndarray/base/unary' );
4647
var broadcast = require( '@stdlib/ndarray/base/maybe-broadcast-array' );
4748
var broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' );
4849
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
50+
var dtypes2strings = require( '@stdlib/ndarray/base/dtypes2strings' );
51+
var resolveStr = require( '@stdlib/ndarray/base/dtype-resolve-str' );
4952
var unaryOutputDataType = require( '@stdlib/ndarray/base/unary-output-dtype' );
5053
var numel = require( '@stdlib/ndarray/base/numel' );
5154
var buffer = require( '@stdlib/ndarray/base/buffer' );
@@ -68,8 +71,8 @@ var validate = require( './validate.js' );
6871
*
6972
* @constructor
7073
* @param {Function} prng - unary pseudorandom value generator
71-
* @param {StringArray} idtypes - list of supported input data types
72-
* @param {StringArray} odtypes - list of supported output data types
74+
* @param {ArrayLikeObject} idtypes - list of supported input data types
75+
* @param {ArrayLikeObject} odtypes - list of supported output data types
7376
* @param {Object} policies - policies
7477
* @param {string} policies.output - output data type policy
7578
* @param {Object} [options] - function options
@@ -143,13 +146,17 @@ function Random( prng, idtypes, odtypes, policies, options ) {
143146
) {
144147
throw new TypeError( format( 'invalid argument. Second argument must be an array of data types. Value: `%s`.', idtypes ) );
145148
}
149+
idtypes = dtypes2strings( idtypes );
150+
146151
if (
147152
!isCollection( odtypes ) ||
148153
odtypes.length < 1 ||
149154
!everyBy( odtypes, isDataType )
150155
) {
151156
throw new TypeError( format( 'invalid argument. Third argument must be an array of data types. Value: `%s`.', odtypes ) );
152157
}
158+
odtypes = dtypes2strings( odtypes );
159+
153160
if ( !isObject( policies ) ) {
154161
throw new TypeError( format( 'invalid argument. Fourth argument must be an object. Value: `%s`.', policies ) );
155162
}
@@ -189,10 +196,10 @@ function Random( prng, idtypes, odtypes, policies, options ) {
189196
* @param {NonNegativeIntegerArray} shape - output shape
190197
* @param {(ndarrayLike|*)} param1 - PRNG parameter
191198
* @param {Options} [options] - function options
192-
* @param {string} [options.dtype] - output ndarray data type
199+
* @param {*} [options.dtype] - output ndarray data type
193200
* @param {string} [options.order] - memory layout (either row-major or column-major)
194201
* @param {string} [options.mode] - specifies how to handle indices which exceed ndarray dimensions
195-
* @param {StringArray} [options.submode] - specifies how to handle subscripts which exceed ndarray dimensions on a per dimension basis
202+
* @param {ArrayLikeObject<string>} [options.submode] - specifies how to handle subscripts which exceed ndarray dimensions on a per dimension basis
196203
* @param {boolean} [options.readonly] - boolean indicating whether an ndarray should be read-only
197204
* @throws {TypeError} first argument must be a valid shape
198205
* @throws {TypeError} must provide valid PRNG parameters
@@ -276,7 +283,7 @@ setReadOnly( Random.prototype, 'generate', function generate( shape, param1, opt
276283
dt = 'generic';
277284
FLG = true;
278285
}
279-
if ( !contains( this._idtypes, dt ) ) {
286+
if ( !contains( this._idtypes, resolveStr( dt ) ) ) {
280287
throw new TypeError( format( 'invalid argument. Second argument must have one of the following data types: "%s". Data type: `%s`.', join( this._idtypes, '", "' ), dt ) );
281288
}
282289
prng = this._prng;
@@ -311,7 +318,7 @@ setReadOnly( Random.prototype, 'generate', function generate( shape, param1, opt
311318
}
312319
// If provided a scalar PRNG parameter, we can simply fill a linear buffer with pseudorandom values (as all pseudorandom values are drawn from the same PRNG) and then wrap as an ndarray...
313320
if ( FLG ) {
314-
if ( dt === 'generic' ) {
321+
if ( isEqualDataType( dt, 'generic' ) ) {
315322
buf = filledBy( len, wrapper );
316323
} else {
317324
buf = buffer( dt, len );
@@ -415,11 +422,11 @@ setReadOnly( Random.prototype, 'assign', function assign( param1, out ) {
415422
pdt = 'generic';
416423
p1 = broadcastScalar( param1, pdt, getShape( out ), getOrder( out ) );
417424
}
418-
if ( !contains( this._idtypes, pdt ) ) {
425+
if ( !contains( this._idtypes, resolveStr( pdt ) ) ) {
419426
throw new TypeError( format( 'invalid argument. First argument must have one of the following data types: "%s". Data type: `%s`.', join( this._idtypes, '", "' ), pdt ) );
420427
}
421428
odt = getDType( out );
422-
if ( !contains( this._odtypes, odt ) ) {
429+
if ( !contains( this._odtypes, resolveStr( odt ) ) ) {
423430
throw new TypeError( format( 'invalid argument. Second argument must have one of the following data types: "%s". Data type: `%s`.', join( this._odtypes, '", "' ), odt ) );
424431
}
425432
// Fill the output array with pseudorandom values:

lib/node_modules/@stdlib/random/tools/unary/lib/validate.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var isObject = require( '@stdlib/assert/is-plain-object' );
2424
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
2525
var contains = require( '@stdlib/array/base/assert/contains' );
2626
var join = require( '@stdlib/array/base/join' );
27+
var resolveStr = require( '@stdlib/ndarray/base/dtype-resolve-str' );
2728
var format = require( '@stdlib/string/format' );
2829

2930

@@ -34,12 +35,12 @@ var format = require( '@stdlib/string/format' );
3435
*
3536
* @private
3637
* @param {Object} opts - destination object
37-
* @param {StringArray} dtypes - list of supported output data types
38+
* @param {Array<string>} dtypes - list of supported output data types
3839
* @param {Options} options - function options
39-
* @param {string} [options.dtype] - array data type
40+
* @param {*} [options.dtype] - array data type
4041
* @param {string} [options.order] - memory layout (either row-major or column-major)
4142
* @param {string} [options.mode] - specifies how to handle indices which exceed array dimensions
42-
* @param {StringArray} [options.submode] - specifies how to handle subscripts which exceed array dimensions on a per dimension basis
43+
* @param {ArrayLikeObject<string>} [options.submode] - specifies how to handle subscripts which exceed array dimensions on a per dimension basis
4344
* @param {boolean} [options.readonly] - boolean indicating whether an array should be read-only
4445
* @returns {(Error|null)} null or an error object
4546
*
@@ -60,9 +61,9 @@ function validate( opts, dtypes, options ) {
6061
return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
6162
}
6263
if ( hasOwnProp( options, 'dtype' ) ) {
63-
opts.dtype = options.dtype;
64+
opts.dtype = resolveStr( options.dtype );
6465
if ( !contains( dtypes, opts.dtype ) ) {
65-
return new TypeError( format( 'invalid option. `%s` option must be one of the following: "%s". Option: `%s`.', 'dtype', join( dtypes, '", "' ), opts.dtype ) );
66+
return new TypeError( format( 'invalid option. `%s` option must be one of the following: "%s". Option: `%s`.', 'dtype', join( dtypes, '", "' ), options.dtype ) );
6667
}
6768
}
6869
// Pass-through options...

lib/node_modules/@stdlib/random/tools/unary/test/test.validate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ tape( 'the function returns an error if provided a `dtype` option which is not a
7272

7373
values = [
7474
'5',
75-
-5,
75+
'foo',
7676
NaN,
7777
true,
7878
false,

0 commit comments

Comments
 (0)