Skip to content

Commit cb54e8e

Browse files
committed
refactor: add support for 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 73f0550 commit cb54e8e

5 files changed

Lines changed: 41 additions & 23 deletions

File tree

lib/node_modules/@stdlib/random/tools/binary-factory/docs/repl.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
- factory: returns a new binary pseudorandom value generator.
1212

13-
idtypes: Array<Array<string>>
13+
idtypes: Array<Array<string|DataType>>
1414
List containing a list of supported input data types for each PRNG
1515
parameter.
1616

17-
odtypes: Array<string>
17+
odtypes: Array<string|DataType>
1818
List of supported output data types.
1919

2020
policies: Object

lib/node_modules/@stdlib/random/tools/binary-factory/lib/main.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ var isOutputDataTypePolicy = require( '@stdlib/ndarray/base/assert/is-output-dat
3333
var isOrder = require( '@stdlib/ndarray/base/assert/is-order' );
3434
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
3535
var everyBy = require( '@stdlib/array/base/every-by' );
36+
var copy = require( '@stdlib/array/base/copy' );
37+
var dtypes2strings = require( '@stdlib/ndarray/base/dtypes2strings' );
3638
var constantFunction = require( '@stdlib/utils/constant-function' );
3739
var noop = require( '@stdlib/utils/noop' );
3840
var Random = require( '@stdlib/random/tools/binary' );
@@ -46,8 +48,8 @@ var format = require( '@stdlib/string/format' );
4648
*
4749
* @param {Function} prng - binary pseudorandom value generator
4850
* @param {Function} prng.factory - method which returns a new binary pseudorandom value generator
49-
* @param {ArrayLikeObject<StringArray>} idtypes - list containing a list of supported input data types for each PRNG parameter
50-
* @param {StringArray} odtypes - list of supported output data types
51+
* @param {ArrayLikeObject<ArrayLikeObject>} idtypes - list containing a list of supported input data types for each PRNG parameter
52+
* @param {ArrayLikeObject} odtypes - list of supported output data types
5153
* @param {Object} policies - policies
5254
* @param {string} policies.output - output data type policy
5355
* @param {Object} options - options
@@ -111,6 +113,7 @@ var format = require( '@stdlib/string/format' );
111113
*/
112114
function createFactory( prng, idtypes, odtypes, policies, options ) {
113115
var OPTIONS;
116+
var dt;
114117
var i;
115118

116119
if ( !isFunction( prng ) ) {
@@ -122,14 +125,17 @@ function createFactory( prng, idtypes, odtypes, policies, options ) {
122125
if ( !isCollection( idtypes ) ) {
123126
throw new TypeError( format( 'invalid argument. Second argument must be an array-like object. Value: `%s`.', idtypes ) );
124127
}
128+
idtypes = copy( idtypes );
125129
for ( i = 0; i < idtypes.length; i++ ) {
130+
dt = idtypes[ i ];
126131
if (
127-
!isCollection( idtypes[ i ] ) ||
128-
idtypes[ i ].length < 1 ||
129-
!everyBy( idtypes[ i ], isDataType )
132+
!isCollection( dt ) ||
133+
dt.length < 1 ||
134+
!everyBy( dt, isDataType )
130135
) {
131136
throw new TypeError( format( 'invalid argument. Second argument must contain arrays of data types. Value: `%s`.', idtypes ) );
132137
}
138+
idtypes[ i ] = dtypes2strings( dt );
133139
}
134140
if (
135141
!isCollection( odtypes ) ||
@@ -138,6 +144,8 @@ function createFactory( prng, idtypes, odtypes, policies, options ) {
138144
) {
139145
throw new TypeError( format( 'invalid argument. Third argument must be an array of data types. Value: `%s`.', odtypes ) );
140146
}
147+
odtypes = dtypes2strings( odtypes );
148+
141149
if ( !isObject( policies ) ) {
142150
throw new TypeError( format( 'invalid argument. Fourth argument must be an object. Value: `%s`.', policies ) );
143151
}
@@ -213,7 +221,7 @@ function createFactory( prng, idtypes, odtypes, policies, options ) {
213221
* @param {(ndarrayLike|*)} param1 - first PRNG parameter
214222
* @param {(ndarrayLike|*)} param2 - second PRNG parameter
215223
* @param {Options} [options] - function options
216-
* @param {string} [options.dtype] - output data type
224+
* @param {*} [options.dtype] - output data type
217225
* @param {string} [options.order] - memory layout (either row-major or column-major)
218226
* @param {string} [options.mode] - specifies how to handle indices which exceed ndarray dimensions
219227
* @param {StringArray} [options.submode] - specifies how to handle subscripts which exceed ndarray dimensions on a per dimension basis

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

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

11-
idtypes: Array<Array<string>>
11+
idtypes: Array<Array<string|DataType>>
1212
List containing a list of supported input data types for each PRNG
1313
parameter.
1414

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

1818
policies: Object
@@ -58,7 +58,7 @@
5858
options: Object (optional)
5959
Function options.
6060

61-
options.dtype: string (optional)
61+
options.dtype: string|DataType (optional)
6262
Output data type. Setting this option overrides the output data type
6363
policy.
6464

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

Lines changed: 21 additions & 11 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' );
@@ -41,11 +42,14 @@ var contains = require( '@stdlib/array/base/assert/contains' );
4142
var filledBy = require( '@stdlib/array/base/filled-by' );
4243
var everyBy = require( '@stdlib/array/base/every-by' );
4344
var join = require( '@stdlib/array/base/join' );
45+
var copy = require( '@stdlib/array/base/copy' );
4446
var nullaryStrided = require( '@stdlib/strided/base/nullary' );
4547
var binary = require( '@stdlib/ndarray/base/binary' );
4648
var broadcast = require( '@stdlib/ndarray/base/maybe-broadcast-array' );
4749
var broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' );
4850
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
51+
var dtypes2strings = require( '@stdlib/ndarray/base/dtypes2strings' );
52+
var resolveStr = require( '@stdlib/ndarray/base/dtype-resolve-str' );
4953
var binaryOutputDataType = require( '@stdlib/ndarray/base/binary-output-dtype' );
5054
var numel = require( '@stdlib/ndarray/base/numel' );
5155
var buffer = require( '@stdlib/ndarray/base/buffer' );
@@ -102,7 +106,7 @@ function initialScan( params ) {
102106
}
103107
} else if ( isndarrayLike( p ) ) {
104108
o.scalar = false;
105-
o.dtype = getDType( p );
109+
o.dtype = resolveStr( getDType( p ) );
106110
o.ndims = ndims( p );
107111
o.order = getOrder( p );
108112
} else {
@@ -227,8 +231,8 @@ function broadcastParameters( shape, params ) {
227231
*
228232
* @constructor
229233
* @param {Function} prng - binary pseudorandom value generator
230-
* @param {ArrayLikeObject<StringArray>} idtypes - list containing a list of supported input data types for each PRNG parameter
231-
* @param {StringArray} odtypes - list of supported output data types
234+
* @param {ArrayLikeObject<ArrayLikeObject>} idtypes - list containing a list of supported input data types for each PRNG parameter
235+
* @param {ArrayLikeObject} odtypes - list of supported output data types
232236
* @param {Object} policies - policies
233237
* @param {string} policies.output - output data type policy
234238
* @param {Object} [options] - function options
@@ -286,6 +290,7 @@ function broadcastParameters( shape, params ) {
286290
*/
287291
function Random( prng, idtypes, odtypes, policies, options ) {
288292
var opts;
293+
var dt;
289294
var i;
290295
if ( !( this instanceof Random ) ) {
291296
if ( arguments.length < 5 ) {
@@ -299,14 +304,17 @@ function Random( prng, idtypes, odtypes, policies, options ) {
299304
if ( !isCollection( idtypes ) ) {
300305
throw new TypeError( format( 'invalid argument. Second argument must be an array-like object. Value: `%s`.', idtypes ) );
301306
}
307+
idtypes = copy( idtypes );
302308
for ( i = 0; i < idtypes.length; i++ ) {
309+
dt = idtypes[ i ];
303310
if (
304-
!isCollection( idtypes[ i ] ) ||
305-
idtypes[ i ].length < 1 ||
306-
!everyBy( idtypes[ i ], isDataType )
311+
!isCollection( dt ) ||
312+
dt.length < 1 ||
313+
!everyBy( dt, isDataType )
307314
) {
308315
throw new TypeError( format( 'invalid argument. Second argument must contain arrays of data types. Value: `%s`.', idtypes ) );
309316
}
317+
idtypes[ i ] = dtypes2strings( dt );
310318
}
311319
if (
312320
!isCollection( odtypes ) ||
@@ -315,6 +323,8 @@ function Random( prng, idtypes, odtypes, policies, options ) {
315323
) {
316324
throw new TypeError( format( 'invalid argument. Third argument must be an array of data types. Value: `%s`.', odtypes ) );
317325
}
326+
odtypes = dtypes2strings( odtypes );
327+
318328
if ( !isObject( policies ) ) {
319329
throw new TypeError( format( 'invalid argument. Fourth argument must be an object. Value: `%s`.', policies ) );
320330
}
@@ -355,7 +365,7 @@ function Random( prng, idtypes, odtypes, policies, options ) {
355365
* @param {(ndarrayLike|*)} param1 - first PRNG parameter
356366
* @param {(ndarrayLike|*)} param2 - second PRNG parameter
357367
* @param {Options} [options] - function options
358-
* @param {string} [options.dtype] - output ndarray data type
368+
* @param {*} [options.dtype] - output ndarray data type
359369
* @param {string} [options.order] - memory layout (either row-major or column-major)
360370
* @param {string} [options.mode] - specifies how to handle indices which exceed ndarray dimensions
361371
* @param {StringArray} [options.submode] - specifies how to handle subscripts which exceed ndarray dimensions on a per dimension basis
@@ -416,7 +426,7 @@ setReadOnly( Random.prototype, 'generate', function generate( shape, param1, par
416426
params = initialScan( [ param1, param2 ] );
417427
for ( i = 0; i < params.length; i++ ) {
418428
dt = params[ i ].dtype;
419-
if ( !contains( this._idtypes[ i ], dt ) ) {
429+
if ( !contains( this._idtypes[ i ], resolveStr( dt ) ) ) {
420430
throw new TypeError( format( 'invalid argument. %s argument must have one of the following data types: "%s". Data type: `%s`.', ORDINALS[ i+1 ], join( this._idtypes[ i ], '", "' ), dt ) );
421431
}
422432
}
@@ -461,7 +471,7 @@ setReadOnly( Random.prototype, 'generate', function generate( shape, param1, par
461471
}
462472
// If provided scalar PRNG parameters, 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...
463473
if ( FLG ) {
464-
if ( dt === 'generic' ) {
474+
if ( isEqualDataType( dt, 'generic' ) ) {
465475
buf = filledBy( len, wrapper );
466476
} else {
467477
buf = buffer( dt, len );
@@ -552,7 +562,7 @@ setReadOnly( Random.prototype, 'assign', function assign( param1, param2, out )
552562
sh = getShape( out );
553563
ord = getOrder( out );
554564
odt = getDType( out );
555-
if ( !contains( this._odtypes, odt ) ) {
565+
if ( !contains( this._odtypes, resolveStr( odt ) ) ) {
556566
throw new TypeError( format( 'invalid argument. %s argument must have one of the following data types: "%s". Data type: `%s`.', ORDINALS[ 2 ], join( this._odtypes, '", "' ), odt ) );
557567
}
558568
params = [ param1, param2 ];
@@ -581,7 +591,7 @@ setReadOnly( Random.prototype, 'assign', function assign( param1, param2, out )
581591
pdt = 'generic';
582592
p = broadcastScalar( p, pdt, sh, ord );
583593
}
584-
if ( !contains( this._idtypes[ i ], pdt ) ) {
594+
if ( !contains( this._idtypes[ i ], resolveStr( pdt ) ) ) {
585595
throw new TypeError( format( 'invalid argument. %s argument must have one of the following data types: "%s". Data type: `%s`.', ORDINALS[ i ], join( this._idtypes[ i ], '", "' ), pdt ) );
586596
}
587597
params[ i ] = p;

lib/node_modules/@stdlib/random/tools/binary/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)