Skip to content

Commit 5a092db

Browse files
committed
feat: add support for struct and DataType values
--- 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 8526ef8 commit 5a092db

File tree

3 files changed

+174
-28
lines changed
  • lib/node_modules/@stdlib/ndarray/base/assert/is-scalar-mostly-safe-compatible

3 files changed

+174
-28
lines changed

lib/node_modules/@stdlib/ndarray/base/assert/is-scalar-mostly-safe-compatible/docs/repl.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
value: any
99
Input value.
1010

11-
dtype: string
12-
ndarray data type.
11+
dtype: string|DataType
12+
Data type.
1313

1414
Returns
1515
-------

lib/node_modules/@stdlib/ndarray/base/assert/is-scalar-mostly-safe-compatible/lib/main.js

Lines changed: 76 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,18 @@
2323
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
2424
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
2525
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
26+
var isStruct = require( '@stdlib/assert/is-struct' );
2627
var isComplexLike = require( '@stdlib/assert/is-complex-like' );
2728
var isComplexDataType = require( '@stdlib/ndarray/base/assert/is-complex-floating-point-data-type' );
2829
var isBooleanDataType = require( '@stdlib/ndarray/base/assert/is-boolean-data-type' );
2930
var isRealFloatingDataType = require( '@stdlib/ndarray/base/assert/is-real-floating-point-data-type' );
3031
var isUnsignedIntegerDataType = require( '@stdlib/ndarray/base/assert/is-unsigned-integer-data-type' );
3132
var isSignedIntegerDataType = require( '@stdlib/ndarray/base/assert/is-signed-integer-data-type' );
33+
var isStructDataType = require( '@stdlib/ndarray/base/assert/is-struct-data-type' );
3234
var isSafeCast = require( '@stdlib/ndarray/base/assert/is-safe-data-type-cast' );
3335
var minDataType = require( '@stdlib/ndarray/min-dtype' );
3436
var minSignedIntegerDataType = require( '@stdlib/ndarray/base/min-signed-integer-dtype' );
35-
var format = require( '@stdlib/string/format' );
37+
var resolveStr = require( '@stdlib/ndarray/base/dtype-resolve-str' );
3638

3739

3840
// FUNCTIONS //
@@ -179,15 +181,67 @@ function validateBinary( value ) {
179181
return ( isInteger( value ) && minDataType( value ) === 'uint8' );
180182
}
181183

184+
/**
185+
* Verifies whether a provided value can be safely cast to a struct data type.
186+
*
187+
* @private
188+
* @param {*} value - input value
189+
* @param {string} dtype - array data type
190+
* @returns {boolean} boolean result
191+
*
192+
* @example
193+
* var structFactory = require( '@stdlib/dstructs/struct' );
194+
*
195+
* var schema = [
196+
* {
197+
* 'name': 'value',
198+
* 'type': 'float64'
199+
* }
200+
* ];
201+
* var Struct = structFactory( schema );
202+
*
203+
* var data = {
204+
* 'value': 3.0
205+
* };
206+
* var s = new Struct( data );
207+
*
208+
* var out = validateStruct( s, '|<float64>[0,8]|' );
209+
* // returns true
210+
*
211+
* @example
212+
* var structFactory = require( '@stdlib/dstructs/struct' );
213+
*
214+
* var schema = [
215+
* {
216+
* 'name': 'value',
217+
* 'type': 'float32'
218+
* }
219+
* ];
220+
* var Struct = structFactory( schema );
221+
*
222+
* var data = {
223+
* 'value': 3.0
224+
* };
225+
* var s = new Struct( data );
226+
*
227+
* var out = validateStruct( s, '|<float64>[0,8]|' );
228+
* // returns false
229+
*/
230+
function validateStruct( value, dtype ) {
231+
var o = {
232+
'format': 'layout'
233+
};
234+
return ( isStruct( value ) && value.toString( o ) === dtype );
235+
}
236+
182237

183238
// MAIN //
184239

185240
/**
186241
* Returns a boolean indicating whether a scalar value can be safely cast or, for floating-point data types, downcast to specified ndarray data type.
187242
*
188243
* @param {*} value - scalar value
189-
* @param {string} dtype - ndarray data type
190-
* @throws {TypeError} second argument must be a supported data type
244+
* @param {*} dtype - ndarray data type
191245
* @returns {boolean} boolean indicating whether a scalar value can be safely cast
192246
*
193247
* @example
@@ -203,28 +257,32 @@ function validateBinary( value ) {
203257
* // returns false
204258
*/
205259
function isScalarMostlySafeCompatible( value, dtype ) { // eslint-disable-line id-length
206-
if ( dtype === 'generic' ) {
207-
return validateGeneric( value, dtype );
260+
var dt = resolveStr( dtype ) || ''; // note: empty string when unable to resolve a second argument to a data type string
261+
if ( dt === 'generic' ) {
262+
return validateGeneric( value, dt );
263+
}
264+
if ( dt === 'binary' ) {
265+
return validateBinary( value, dt );
208266
}
209-
if ( dtype === 'binary' ) {
210-
return validateBinary( value, dtype );
267+
if ( isRealFloatingDataType( dt ) ) {
268+
return validateRealFloating( value, dt );
211269
}
212-
if ( isRealFloatingDataType( dtype ) ) {
213-
return validateRealFloating( value, dtype );
270+
if ( isUnsignedIntegerDataType( dt ) ) {
271+
return validateUnsignedInteger( value, dt );
214272
}
215-
if ( isUnsignedIntegerDataType( dtype ) ) {
216-
return validateUnsignedInteger( value, dtype );
273+
if ( isSignedIntegerDataType( dt ) ) {
274+
return validateSignedInteger( value, dt );
217275
}
218-
if ( isSignedIntegerDataType( dtype ) ) {
219-
return validateSignedInteger( value, dtype );
276+
if ( isBooleanDataType( dt ) ) {
277+
return validateBoolean( value, dt );
220278
}
221-
if ( isBooleanDataType( dtype ) ) {
222-
return validateBoolean( value, dtype );
279+
if ( isComplexDataType( dt ) ) {
280+
return validateComplexFloating( value, dt );
223281
}
224-
if ( isComplexDataType( dtype ) ) {
225-
return validateComplexFloating( value, dtype );
282+
if ( isStructDataType( dtype ) ) {
283+
return validateStruct( value, dt );
226284
}
227-
throw new TypeError( format( 'invalid argument. Second argument must be a supported data type. Value: `%s`.', dtype ) );
285+
return false;
228286
}
229287

230288

lib/node_modules/@stdlib/ndarray/base/assert/is-scalar-mostly-safe-compatible/test/test.js

Lines changed: 96 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
// MODULES //
2222

2323
var tape = require( 'tape' );
24+
var DataType = require( '@stdlib/ndarray/dtype-ctor' );
25+
var structFactory = require( '@stdlib/dstructs/struct' );
2426
var Complex128 = require( '@stdlib/complex/float64/ctor' );
2527
var isScalarMostlySafeCompatible = require( './../lib' ); // eslint-disable-line id-length
2628

@@ -33,7 +35,7 @@ tape( 'main export is a function', function test( t ) {
3335
t.end();
3436
});
3537

36-
tape( 'the function throws an error if provided a second argument which is not a supported data type', function test( t ) {
38+
tape( 'the function returns `false` if provided a second argument which is not a supported data type', function test( t ) {
3739
var values;
3840
var i;
3941

@@ -50,15 +52,9 @@ tape( 'the function throws an error if provided a second argument which is not a
5052
function noop() {}
5153
];
5254
for ( i = 0; i < values.length; i++ ) {
53-
t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] );
55+
t.strictEqual( isScalarMostlySafeCompatible( 3.14, values[ i ] ), false, 'returns expected value for '+values[ i ] );
5456
}
5557
t.end();
56-
57-
function badValue( value ) {
58-
return function badValue() {
59-
isScalarMostlySafeCompatible( 3.14, value );
60-
};
61-
}
6258
});
6359

6460
tape( 'the function returns a boolean indicating if a value can be cast to an ndarray data type (generic)', function test( t ) {
@@ -90,8 +86,12 @@ tape( 'the function returns a boolean indicating if a value can be cast to an nd
9086
for ( i = 0; i < values.length; i++ ) {
9187
v = values[ i ][ 0 ];
9288
expected = values[ i ][ 1 ];
89+
9390
actual = isScalarMostlySafeCompatible( v, 'generic' );
9491
t.strictEqual( actual, expected, 'returns expected value' );
92+
93+
actual = isScalarMostlySafeCompatible( v, new DataType( 'generic' ) );
94+
t.strictEqual( actual, expected, 'returns expected value' );
9595
}
9696
t.end();
9797
});
@@ -125,8 +125,12 @@ tape( 'the function returns a boolean indicating if a value can be cast to an nd
125125
for ( i = 0; i < values.length; i++ ) {
126126
v = values[ i ][ 0 ];
127127
expected = values[ i ][ 1 ];
128+
128129
actual = isScalarMostlySafeCompatible( v, 'binary' );
129130
t.strictEqual( actual, expected, 'returns expected value' );
131+
132+
actual = isScalarMostlySafeCompatible( v, new DataType( 'binary' ) );
133+
t.strictEqual( actual, expected, 'returns expected value' );
130134
}
131135
t.end();
132136
});
@@ -160,8 +164,12 @@ tape( 'the function returns a boolean indicating if a value can be cast to an nd
160164
for ( i = 0; i < values.length; i++ ) {
161165
v = values[ i ][ 0 ];
162166
expected = values[ i ][ 1 ];
167+
163168
actual = isScalarMostlySafeCompatible( v, 'bool' );
164169
t.strictEqual( actual, expected, 'returns expected value' );
170+
171+
actual = isScalarMostlySafeCompatible( v, new DataType( 'bool' ) );
172+
t.strictEqual( actual, expected, 'returns expected value' );
165173
}
166174
t.end();
167175
});
@@ -195,8 +203,12 @@ tape( 'the function returns a boolean indicating if a value can be cast to an nd
195203
for ( i = 0; i < values.length; i++ ) {
196204
v = values[ i ][ 0 ];
197205
expected = values[ i ][ 1 ];
206+
198207
actual = isScalarMostlySafeCompatible( v, 'float64' );
199208
t.strictEqual( actual, expected, 'returns expected value' );
209+
210+
actual = isScalarMostlySafeCompatible( v, new DataType( 'float64' ) );
211+
t.strictEqual( actual, expected, 'returns expected value' );
200212
}
201213
t.end();
202214
});
@@ -230,8 +242,12 @@ tape( 'the function returns a boolean indicating if a value can be cast to an nd
230242
for ( i = 0; i < values.length; i++ ) {
231243
v = values[ i ][ 0 ];
232244
expected = values[ i ][ 1 ];
245+
233246
actual = isScalarMostlySafeCompatible( v, 'complex128' );
234247
t.strictEqual( actual, expected, 'returns expected value' );
248+
249+
actual = isScalarMostlySafeCompatible( v, new DataType( 'complex128' ) );
250+
t.strictEqual( actual, expected, 'returns expected value' );
235251
}
236252
t.end();
237253
});
@@ -265,8 +281,12 @@ tape( 'the function returns a boolean indicating if a value can be cast to an nd
265281
for ( i = 0; i < values.length; i++ ) {
266282
v = values[ i ][ 0 ];
267283
expected = values[ i ][ 1 ];
284+
268285
actual = isScalarMostlySafeCompatible( v, 'int32' );
269286
t.strictEqual( actual, expected, 'returns expected value' );
287+
288+
actual = isScalarMostlySafeCompatible( v, new DataType( 'int32' ) );
289+
t.strictEqual( actual, expected, 'returns expected value' );
270290
}
271291
t.end();
272292
});
@@ -300,8 +320,76 @@ tape( 'the function returns a boolean indicating if a value can be cast to an nd
300320
for ( i = 0; i < values.length; i++ ) {
301321
v = values[ i ][ 0 ];
302322
expected = values[ i ][ 1 ];
323+
303324
actual = isScalarMostlySafeCompatible( v, 'uint32' );
304325
t.strictEqual( actual, expected, 'returns expected value' );
326+
327+
actual = isScalarMostlySafeCompatible( v, new DataType( 'uint32' ) );
328+
t.strictEqual( actual, expected, 'returns expected value' );
329+
}
330+
t.end();
331+
});
332+
333+
tape( 'the function returns a boolean indicating if a value can be cast to an ndarray data type (struct)', function test( t ) {
334+
var expected;
335+
var Struct1;
336+
var Struct2;
337+
var values;
338+
var actual;
339+
var schema;
340+
var data;
341+
var v;
342+
var i;
343+
344+
schema = [
345+
{
346+
'name': 'value',
347+
'type': 'float64'
348+
}
349+
];
350+
Struct1 = structFactory( schema );
351+
352+
schema = [
353+
{
354+
'name': 'value',
355+
'type': 'float32'
356+
}
357+
];
358+
Struct2 = structFactory( schema );
359+
360+
data = {
361+
'data': 3.0
362+
};
363+
values = [
364+
[ new Struct1( data ), true ],
365+
[ new Struct2( data ), false ],
366+
[ 5, false ],
367+
[ -5, false ],
368+
[ 3.14, false ],
369+
[ -3.14, false ],
370+
[ 127, false ], // int8
371+
[ 255, false ], // uint8
372+
[ 32767, false ], // int16
373+
[ 65535, false ], // uint16
374+
[ 2147483647, false ], // int32
375+
[ 4294967295, false ], // uint32
376+
[ '5', false ],
377+
[ true, false ],
378+
[ false, false ],
379+
[ null, false ],
380+
[ {}, false ],
381+
[ new Complex128( 3.0, 5.0 ), false ]
382+
];
383+
384+
for ( i = 0; i < values.length; i++ ) {
385+
v = values[ i ][ 0 ];
386+
expected = values[ i ][ 1 ];
387+
388+
actual = isScalarMostlySafeCompatible( v, Struct1 );
389+
t.strictEqual( actual, expected, 'returns expected value' );
390+
391+
actual = isScalarMostlySafeCompatible( v, new DataType( Struct1 ) );
392+
t.strictEqual( actual, expected, 'returns expected value' );
305393
}
306394
t.end();
307395
});

0 commit comments

Comments
 (0)