@stdlib/array-to-fancy 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +177 -0
- package/NOTICE +1 -0
- package/README.md +646 -0
- package/SECURITY.md +5 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +57 -0
- package/dist/index.js.map +7 -0
- package/docs/types/index.d.ts +443 -0
- package/lib/ctor.js +94 -0
- package/lib/defaults.js +48 -0
- package/lib/error_constructor.js +53 -0
- package/lib/error_message.js +42 -0
- package/lib/factory.js +169 -0
- package/lib/get.js +79 -0
- package/lib/get_array_wrapper.js +51 -0
- package/lib/get_element.js +46 -0
- package/lib/get_elements.js +63 -0
- package/lib/get_slice.js +60 -0
- package/lib/get_value.js +70 -0
- package/lib/has_proxy_support.js +40 -0
- package/lib/index.js +64 -0
- package/lib/is_array_index_string.js +51 -0
- package/lib/is_integer_string.js +51 -0
- package/lib/main.js +68 -0
- package/lib/prop2array.js +69 -0
- package/lib/prop2slice.js +163 -0
- package/lib/re_array_index.js +47 -0
- package/lib/re_integer.js +47 -0
- package/lib/re_subseq.js +47 -0
- package/lib/resolve_index.js +66 -0
- package/lib/set.js +86 -0
- package/lib/set_element.js +65 -0
- package/lib/set_element_wrapper.js +65 -0
- package/lib/set_slice.js +83 -0
- package/lib/set_value.js +40 -0
- package/lib/validate.js +74 -0
- package/lib/validator.js +211 -0
- package/package.json +146 -0
|
@@ -0,0 +1,65 @@
|
|
|
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 resolveIndex = require( './resolve_index.js' );
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
// MAIN //
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Sets the element associated with a specified index.
|
|
30
|
+
*
|
|
31
|
+
* @private
|
|
32
|
+
* @param {Object} target - target object
|
|
33
|
+
* @param {string} property - index string
|
|
34
|
+
* @param {*} value - new value
|
|
35
|
+
* @param {Object} ctx - context object
|
|
36
|
+
* @param {Function} ctx.setter - accessor for setting array elements
|
|
37
|
+
* @param {string} ctx.dtype - target array data type
|
|
38
|
+
* @param {boolean} ctx.strict - boolean indicating whether to enforce strict bounds checking
|
|
39
|
+
* @param {Function} ctx.validator - function for validating new values
|
|
40
|
+
* @param {(Function|null)} ctx.preSetElement - function for normalizing new values (if necessary)
|
|
41
|
+
* @throws {TypeError} assigned value cannot be safely cast to the target array data type
|
|
42
|
+
* @throws {TypeError} target array must have a supported data type
|
|
43
|
+
* @returns {boolean} boolean indicating whether assignment succeeded
|
|
44
|
+
*/
|
|
45
|
+
function setElement( target, property, value, ctx ) {
|
|
46
|
+
var err;
|
|
47
|
+
var v;
|
|
48
|
+
|
|
49
|
+
err = ctx.validator( value, ctx.dtype );
|
|
50
|
+
if ( err ) {
|
|
51
|
+
throw err;
|
|
52
|
+
}
|
|
53
|
+
if ( ctx.preSetElement ) {
|
|
54
|
+
v = ctx.preSetElement( value );
|
|
55
|
+
} else {
|
|
56
|
+
v = value;
|
|
57
|
+
}
|
|
58
|
+
ctx.setter( target, resolveIndex( property, target.length, ctx.strict ), v ); // eslint-disable-line max-len
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
// EXPORTS //
|
|
64
|
+
|
|
65
|
+
module.exports = setElement;
|
|
@@ -0,0 +1,65 @@
|
|
|
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 isComplexDataType = require( '@stdlib/array-base-assert-is-complex-floating-point-data-type' );
|
|
24
|
+
var isNumber = require( '@stdlib/assert-is-number' ).isPrimitive;
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
// FUNCTIONS //
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Normalizes values assigned to complex number arrays.
|
|
31
|
+
*
|
|
32
|
+
* @private
|
|
33
|
+
* @param {*} value - input value
|
|
34
|
+
* @returns {(ComplexLike|*)} output value
|
|
35
|
+
*/
|
|
36
|
+
function wrapComplex( value ) {
|
|
37
|
+
// Wrap real-valued scalars as valid input arguments to complex number arrays...
|
|
38
|
+
if ( isNumber( value ) ) {
|
|
39
|
+
return [ value, 0.0 ]; // note: we're assuming that a ComplexXXArray setter accepts an array of interleaved real and imaginary components
|
|
40
|
+
}
|
|
41
|
+
// For everything other than a real-valued scalar, we delegate validation to the target complex number array:
|
|
42
|
+
return value;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
// MAIN //
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Returns a wrapper function for processing scalar input values before assignment.
|
|
50
|
+
*
|
|
51
|
+
* @private
|
|
52
|
+
* @param {string} dtype - array data type
|
|
53
|
+
* @returns {(Function|null)} wrapper function or null
|
|
54
|
+
*/
|
|
55
|
+
function wrapper( dtype ) {
|
|
56
|
+
if ( isComplexDataType( dtype ) ) {
|
|
57
|
+
return wrapComplex;
|
|
58
|
+
}
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
// EXPORTS //
|
|
64
|
+
|
|
65
|
+
module.exports = wrapper;
|
package/lib/set_slice.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
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 isCollection = require( '@stdlib/assert-is-collection' );
|
|
24
|
+
var sliceAssign = require( '@stdlib/array-base-fancy-slice-assign' );
|
|
25
|
+
var scalar2array = require( '@stdlib/array-from-scalar' );
|
|
26
|
+
var prop2slice = require( './prop2slice.js' );
|
|
27
|
+
var errMessage = require( './error_message.js' );
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
// MAIN //
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Sets element values belonging to the array view specified by an indexing expression.
|
|
34
|
+
*
|
|
35
|
+
* @private
|
|
36
|
+
* @param {Object} target - target object
|
|
37
|
+
* @param {string} property - indexing expression
|
|
38
|
+
* @param {*} value - new value
|
|
39
|
+
* @param {Object} receiver - the proxy object or an object inheriting from the proxy
|
|
40
|
+
* @param {Object} ctx - context object
|
|
41
|
+
* @param {string} ctx.dtype - array data type
|
|
42
|
+
* @param {boolean} ctx.strict - boolean indicating whether to enforce strict bounds checking
|
|
43
|
+
* @throws {Error} invalid slice operation
|
|
44
|
+
* @throws {RangeError} slice exceeds array bounds
|
|
45
|
+
* @throws {Error} assigned value must be broadcast compatible with target array view
|
|
46
|
+
* @throws {TypeError} assigned value cannot be safely cast to the target array data type
|
|
47
|
+
* @throws {TypeError} target array must have a supported data type
|
|
48
|
+
* @returns {boolean} boolean indicating whether assignment succeeded
|
|
49
|
+
*/
|
|
50
|
+
function setSlice( target, property, value, receiver, ctx ) {
|
|
51
|
+
var err;
|
|
52
|
+
var s;
|
|
53
|
+
var v;
|
|
54
|
+
|
|
55
|
+
s = prop2slice( target, property, ctx.strict );
|
|
56
|
+
if ( s === null ) {
|
|
57
|
+
// If unable to parse the property as an indexing expression, signal that we were unable to perform slice assignment:
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
if ( isCollection( value ) ) {
|
|
61
|
+
// When handling collections, we delegate to `sliceAssign` (see below) to perform argument validation (e.g., ensuring a (mostly) safe cast, broadcast compatibility, etc), so we just reassign the value here:
|
|
62
|
+
v = value;
|
|
63
|
+
} else {
|
|
64
|
+
// When provided a "scalar", we need to check whether the value can be safely cast to the target array data type:
|
|
65
|
+
err = ctx.validator( value, ctx.dtype );
|
|
66
|
+
if ( err ) {
|
|
67
|
+
throw err;
|
|
68
|
+
}
|
|
69
|
+
// As the scalar can be safely cast, convert the scalar to an array having the same data type as the target array to allow for broadcasting during slice assignment:
|
|
70
|
+
v = scalar2array( value, ctx.dtype );
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
sliceAssign( v, receiver, s, ctx.strict );
|
|
74
|
+
return true;
|
|
75
|
+
} catch ( err ) {
|
|
76
|
+
throw new err.constructor( errMessage( err.message ) );
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
// EXPORTS //
|
|
82
|
+
|
|
83
|
+
module.exports = setSlice;
|
package/lib/set_value.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
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
|
+
// MAIN //
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Sets the value associated with a specified property.
|
|
25
|
+
*
|
|
26
|
+
* @private
|
|
27
|
+
* @param {Object} target - target object
|
|
28
|
+
* @param {string} property - property
|
|
29
|
+
* @param {*} value - new value
|
|
30
|
+
* @returns {boolean} boolean indicating whether assignment succeeded
|
|
31
|
+
*/
|
|
32
|
+
function setValue( target, property, value ) {
|
|
33
|
+
target[ property ] = value;
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
// EXPORTS //
|
|
39
|
+
|
|
40
|
+
module.exports = setValue;
|
package/lib/validate.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
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 isObject = require( '@stdlib/assert-is-plain-object' );
|
|
24
|
+
var hasOwnProp = require( '@stdlib/assert-has-own-property' );
|
|
25
|
+
var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;
|
|
26
|
+
var isMethodIn = require( '@stdlib/assert-is-method-in' );
|
|
27
|
+
var format = require( '@stdlib/string-format' );
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
// MAIN //
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Validates function options.
|
|
34
|
+
*
|
|
35
|
+
* @private
|
|
36
|
+
* @param {Object} opts - destination object
|
|
37
|
+
* @param {Options} options - function options
|
|
38
|
+
* @param {boolean} [options.strict] - boolean indicating whether to enforce strict bounds checking
|
|
39
|
+
* @param {Function} [options.cache] - cache for resolving array index objects
|
|
40
|
+
* @returns {(Error|null)} null or an error object
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* var opts = {};
|
|
44
|
+
* var options = {
|
|
45
|
+
* 'strict': false
|
|
46
|
+
* };
|
|
47
|
+
* var err = validate( opts, options );
|
|
48
|
+
* if ( err ) {
|
|
49
|
+
* throw err;
|
|
50
|
+
* }
|
|
51
|
+
*/
|
|
52
|
+
function validate( opts, options ) {
|
|
53
|
+
if ( !isObject( options ) ) {
|
|
54
|
+
return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
|
|
55
|
+
}
|
|
56
|
+
if ( hasOwnProp( options, 'strict' ) ) {
|
|
57
|
+
opts.strict = options.strict;
|
|
58
|
+
if ( !isBoolean( opts.strict ) ) {
|
|
59
|
+
return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'strict', opts.strict ) );
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if ( hasOwnProp( options, 'cache' ) ) {
|
|
63
|
+
opts.cache = options.cache;
|
|
64
|
+
if ( !isMethodIn( opts.cache, 'get' ) ) {
|
|
65
|
+
return new TypeError( format( 'invalid option. `%s` option is missing a `%s` method. Option: `%s`.', 'cache', 'get', opts.cache ) );
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
// EXPORTS //
|
|
73
|
+
|
|
74
|
+
module.exports = validate;
|
package/lib/validator.js
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
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 isNumber = require( '@stdlib/assert-is-number' ).isPrimitive;
|
|
24
|
+
var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive;
|
|
25
|
+
var isComplexLike = require( '@stdlib/assert-is-complex-like' );
|
|
26
|
+
var isRealFloatingDataType = require( '@stdlib/array-base-assert-is-real-floating-point-data-type' );
|
|
27
|
+
var isUnsignedIntegerDataType = require( '@stdlib/array-base-assert-is-unsigned-integer-data-type' );
|
|
28
|
+
var isSignedIntegerDataType = require( '@stdlib/array-base-assert-is-signed-integer-data-type' );
|
|
29
|
+
var isSafeCast = require( '@stdlib/array-base-assert-is-safe-data-type-cast' );
|
|
30
|
+
var minDataType = require( '@stdlib/array-min-dtype' );
|
|
31
|
+
var minSignedIntegerDataType = require( '@stdlib/array-base-min-signed-integer-dtype' );
|
|
32
|
+
var complexDataType = require( '@stdlib/complex-dtype' );
|
|
33
|
+
var format = require( '@stdlib/string-format' );
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
// FUNCTIONS //
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Verifies whether a provided value can be safely assigned to an element in an array having a "generic" or unknown data type.
|
|
40
|
+
*
|
|
41
|
+
* @private
|
|
42
|
+
* @param {*} value - input value
|
|
43
|
+
* @param {string} dtype - array data type
|
|
44
|
+
* @returns {null} null
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* var err = validateGeneric( 3, 'generic' );
|
|
48
|
+
* // returns null
|
|
49
|
+
*/
|
|
50
|
+
function validateGeneric() {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Verifies whether a provided value can be safely assigned to an element in an array have a real-valued floating-point data type.
|
|
56
|
+
*
|
|
57
|
+
* @private
|
|
58
|
+
* @param {*} value - input value
|
|
59
|
+
* @param {string} dtype - array data type
|
|
60
|
+
* @returns {(Error|null)} error object or null
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* var err = validateRealFloating( 3.14, 'float64' );
|
|
64
|
+
* // returns null
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* var Complex128 = require( '@stdlib/complex-float64' );
|
|
68
|
+
*
|
|
69
|
+
* var err = validateRealFloating( new Complex128( 5.0, 6.0 ), 'float64' );
|
|
70
|
+
* // returns <TypeError>
|
|
71
|
+
*/
|
|
72
|
+
function validateRealFloating( value, dtype ) {
|
|
73
|
+
if ( isNumber( value ) ) {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
if ( isComplexLike( value ) ) {
|
|
77
|
+
return new TypeError( format( 'invalid operation. Assigned value cannot be safely cast to the target array data type. Data types: [%s, %s].', complexDataType( value ), dtype ) );
|
|
78
|
+
}
|
|
79
|
+
return new TypeError( format( 'invalid operation. Assigned value cannot be safely cast to the target array data type. Data types: [%s, %s].', typeof value, dtype ) );
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Verifies whether a provided value can be safely assigned to an element in an array have a complex-valued floating-point data type.
|
|
84
|
+
*
|
|
85
|
+
* @private
|
|
86
|
+
* @param {*} value - input value
|
|
87
|
+
* @param {string} dtype - array data type
|
|
88
|
+
* @returns {(Error|null)} error object or null
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* var Complex128 = require( '@stdlib/complex-float64' );
|
|
92
|
+
*
|
|
93
|
+
* var err = validateComplexFloating( new Complex128( 5.0, 6.0 ), 'complex128' );
|
|
94
|
+
* // returns null
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* var err = validateComplexFloating( {}, 'complex128' );
|
|
98
|
+
* // returns <TypeError>
|
|
99
|
+
*/
|
|
100
|
+
function validateComplexFloating( value, dtype ) {
|
|
101
|
+
if ( isNumber( value ) || isComplexLike( value ) ) {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
return new TypeError( format( 'invalid operation. Assigned value cannot be safely cast to the target array data type. Data types: [%s, %s].', typeof value, dtype ) );
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Verifies whether a provided value can be safely assigned to an element in an array have a signed integer data type.
|
|
109
|
+
*
|
|
110
|
+
* @private
|
|
111
|
+
* @param {*} value - input value
|
|
112
|
+
* @param {string} dtype - array data type
|
|
113
|
+
* @returns {(Error|null)} error object or null
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* var err = validateSignedInteger( 3, 'int32' );
|
|
117
|
+
* // returns null
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* var err = validateSignedInteger( 3.14, 'int32' );
|
|
121
|
+
* // returns <TypeError>
|
|
122
|
+
*/
|
|
123
|
+
function validateSignedInteger( value, dtype ) {
|
|
124
|
+
var vdt;
|
|
125
|
+
if ( isNumber( value ) ) {
|
|
126
|
+
if ( !isInteger( value ) ) {
|
|
127
|
+
return new TypeError( format( 'invalid operation. Assigned value cannot be safely cast to the target array data type. Data types: [%s, %s].', minDataType( value ), dtype ) );
|
|
128
|
+
}
|
|
129
|
+
vdt = minSignedIntegerDataType( value );
|
|
130
|
+
if ( isSafeCast( vdt, dtype ) ) {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
return new TypeError( format( 'invalid operation. Assigned value cannot be safely cast to the target array data type. Data types: [%s, %s].', vdt, dtype ) );
|
|
134
|
+
}
|
|
135
|
+
if ( isComplexLike( value ) ) {
|
|
136
|
+
return new TypeError( format( 'invalid operation. Assigned value cannot be safely cast to the target array data type. Data types: [%s, %s].', complexDataType( value ), dtype ) );
|
|
137
|
+
}
|
|
138
|
+
return new TypeError( format( 'invalid operation. Assigned value cannot be safely cast to the target array data type. Data types: [%s, %s].', typeof value, dtype ) );
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Verifies whether a provided value can be safely assigned to an element in an array have an unsigned integer data type.
|
|
143
|
+
*
|
|
144
|
+
* @private
|
|
145
|
+
* @param {*} value - input value
|
|
146
|
+
* @param {string} dtype - array data type
|
|
147
|
+
* @returns {(Error|null)} error object or null
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* var err = validateUnsignedInteger( 3, 'uint32' );
|
|
151
|
+
* // returns null
|
|
152
|
+
*
|
|
153
|
+
* @example
|
|
154
|
+
* var err = validateUnsignedInteger( -3, 'uint32' );
|
|
155
|
+
* // returns <TypeError>
|
|
156
|
+
*/
|
|
157
|
+
function validateUnsignedInteger( value, dtype ) {
|
|
158
|
+
var vdt;
|
|
159
|
+
if ( isNumber( value ) ) {
|
|
160
|
+
vdt = minDataType( value ); // note: we rely on data type resolution to handle the case where `value` is a non-integer value. In that case, `vdt` will resolve to a floating-point data type and `isSafeCast` will evaluate to `false`
|
|
161
|
+
if ( isSafeCast( vdt, dtype ) ) {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
return new TypeError( format( 'invalid operation. Assigned value cannot be safely cast to the target array data type. Data types: [%s, %s].', vdt, dtype ) );
|
|
165
|
+
}
|
|
166
|
+
if ( isComplexLike( value ) ) {
|
|
167
|
+
return new TypeError( format( 'invalid operation. Assigned value cannot be safely cast to the target array data type. Data types: [%s, %s].', complexDataType( value ), dtype ) );
|
|
168
|
+
}
|
|
169
|
+
return new TypeError( format( 'invalid operation. Assigned value cannot be safely cast to the target array data type. Data types: [%s, %s].', typeof value, dtype ) );
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
// MAIN //
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Returns a validation function for verifying whether a provided value can be safely assigned to an element in an array having a specified data type.
|
|
177
|
+
*
|
|
178
|
+
* @private
|
|
179
|
+
* @param {string} dtype - array data type
|
|
180
|
+
* @returns {Function} validation function
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* var fcn = validator( 'int32' );
|
|
184
|
+
*
|
|
185
|
+
* var err = fcn( 3, 'int32' );
|
|
186
|
+
* // returns null
|
|
187
|
+
*
|
|
188
|
+
* err = fcn( 3.14, 'int32' );
|
|
189
|
+
* // returns <TypeError>
|
|
190
|
+
*/
|
|
191
|
+
function validator( dtype ) {
|
|
192
|
+
if ( dtype === 'generic' || dtype === '' ) {
|
|
193
|
+
return validateGeneric;
|
|
194
|
+
}
|
|
195
|
+
if ( isRealFloatingDataType( dtype ) ) {
|
|
196
|
+
return validateRealFloating;
|
|
197
|
+
}
|
|
198
|
+
if ( isUnsignedIntegerDataType( dtype ) ) {
|
|
199
|
+
return validateUnsignedInteger;
|
|
200
|
+
}
|
|
201
|
+
if ( isSignedIntegerDataType( dtype ) ) {
|
|
202
|
+
return validateSignedInteger;
|
|
203
|
+
}
|
|
204
|
+
// Case: isComplexDataType( dtype ) === true
|
|
205
|
+
return validateComplexFloating;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
// EXPORTS //
|
|
210
|
+
|
|
211
|
+
module.exports = validator;
|
package/package.json
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stdlib/array-to-fancy",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Convert an array to an object supporting fancy indexing.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "The Stdlib Authors",
|
|
8
|
+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
|
|
9
|
+
},
|
|
10
|
+
"contributors": [
|
|
11
|
+
{
|
|
12
|
+
"name": "The Stdlib Authors",
|
|
13
|
+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
|
|
14
|
+
}
|
|
15
|
+
],
|
|
16
|
+
"main": "./lib",
|
|
17
|
+
"directories": {
|
|
18
|
+
"benchmark": "./benchmark",
|
|
19
|
+
"doc": "./docs",
|
|
20
|
+
"example": "./examples",
|
|
21
|
+
"lib": "./lib",
|
|
22
|
+
"test": "./test"
|
|
23
|
+
},
|
|
24
|
+
"types": "./docs/types",
|
|
25
|
+
"scripts": {
|
|
26
|
+
"test": "make test",
|
|
27
|
+
"test-cov": "make test-cov",
|
|
28
|
+
"examples": "make examples",
|
|
29
|
+
"benchmark": "make benchmark"
|
|
30
|
+
},
|
|
31
|
+
"homepage": "https://stdlib.io",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git://github.com/stdlib-js/array-to-fancy.git"
|
|
35
|
+
},
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/stdlib-js/stdlib/issues"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@stdlib/array-base-arraylike2object": "^0.2.0",
|
|
41
|
+
"@stdlib/array-base-assert-is-complex-floating-point-data-type": "^0.2.0",
|
|
42
|
+
"@stdlib/array-base-assert-is-real-floating-point-data-type": "^0.2.0",
|
|
43
|
+
"@stdlib/array-base-assert-is-safe-data-type-cast": "^0.3.0",
|
|
44
|
+
"@stdlib/array-base-assert-is-signed-integer-data-type": "^0.2.0",
|
|
45
|
+
"@stdlib/array-base-assert-is-unsigned-integer-data-type": "^0.2.0",
|
|
46
|
+
"@stdlib/array-base-fancy-slice": "^0.3.0",
|
|
47
|
+
"@stdlib/array-base-fancy-slice-assign": "^0.2.0",
|
|
48
|
+
"@stdlib/array-base-min-signed-integer-dtype": "^0.2.0",
|
|
49
|
+
"@stdlib/array-base-mskfilter": "^0.2.0",
|
|
50
|
+
"@stdlib/array-base-mskreject": "^0.2.0",
|
|
51
|
+
"@stdlib/array-from-scalar": "^0.2.0",
|
|
52
|
+
"@stdlib/array-index": "^0.2.0",
|
|
53
|
+
"@stdlib/array-min-dtype": "^0.2.0",
|
|
54
|
+
"@stdlib/array-take": "^0.1.0",
|
|
55
|
+
"@stdlib/assert-has-own-property": "^0.2.0",
|
|
56
|
+
"@stdlib/assert-has-property": "^0.2.0",
|
|
57
|
+
"@stdlib/assert-is-array-like": "^0.2.0",
|
|
58
|
+
"@stdlib/assert-is-boolean": "^0.2.0",
|
|
59
|
+
"@stdlib/assert-is-collection": "^0.2.0",
|
|
60
|
+
"@stdlib/assert-is-complex-like": "^0.2.0",
|
|
61
|
+
"@stdlib/assert-is-function": "^0.2.0",
|
|
62
|
+
"@stdlib/assert-is-integer": "^0.2.0",
|
|
63
|
+
"@stdlib/assert-is-method-in": "^0.2.0",
|
|
64
|
+
"@stdlib/assert-is-number": "^0.2.0",
|
|
65
|
+
"@stdlib/assert-is-plain-object": "^0.2.0",
|
|
66
|
+
"@stdlib/assert-is-string": "^0.2.0",
|
|
67
|
+
"@stdlib/complex-dtype": "^0.2.0",
|
|
68
|
+
"@stdlib/ndarray-base-normalize-index": "^0.2.0",
|
|
69
|
+
"@stdlib/object-assign": "^0.2.0",
|
|
70
|
+
"@stdlib/proxy-ctor": "^0.2.0",
|
|
71
|
+
"@stdlib/slice-base-seq2slice": "^0.2.0",
|
|
72
|
+
"@stdlib/slice-base-str2slice": "^0.2.0",
|
|
73
|
+
"@stdlib/string-base-replace": "^0.2.0",
|
|
74
|
+
"@stdlib/string-base-starts-with": "^0.2.0",
|
|
75
|
+
"@stdlib/string-base-trim": "^0.2.0",
|
|
76
|
+
"@stdlib/string-format": "^0.2.0",
|
|
77
|
+
"@stdlib/types": "^0.3.1",
|
|
78
|
+
"@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.0",
|
|
79
|
+
"@stdlib/error-tools-fmtprodmsg": "^0.2.0"
|
|
80
|
+
},
|
|
81
|
+
"devDependencies": {
|
|
82
|
+
"@stdlib/array-base-to-accessor-array": "^0.1.0",
|
|
83
|
+
"@stdlib/array-complex128": "^0.1.0",
|
|
84
|
+
"@stdlib/array-complex64": "^0.1.0",
|
|
85
|
+
"@stdlib/array-float32": "^0.2.0",
|
|
86
|
+
"@stdlib/array-float64": "^0.2.0",
|
|
87
|
+
"@stdlib/array-int32": "^0.2.0",
|
|
88
|
+
"@stdlib/array-int8": "^0.2.0",
|
|
89
|
+
"@stdlib/array-uint16": "^0.2.0",
|
|
90
|
+
"@stdlib/array-uint32": "^0.2.0",
|
|
91
|
+
"@stdlib/array-uint8": "^0.2.0",
|
|
92
|
+
"@stdlib/array-zero-to": "^0.1.0",
|
|
93
|
+
"@stdlib/assert-has-proxy-support": "^0.2.0",
|
|
94
|
+
"@stdlib/assert-has-symbol-support": "^0.2.0",
|
|
95
|
+
"@stdlib/assert-is-nan": "^0.2.0",
|
|
96
|
+
"@stdlib/assert-is-range-error": "^0.2.0",
|
|
97
|
+
"@stdlib/assert-is-same-complex128array": "^0.1.0",
|
|
98
|
+
"@stdlib/assert-is-same-complex64array": "^0.1.0",
|
|
99
|
+
"@stdlib/assert-is-syntax-error": "^0.2.0",
|
|
100
|
+
"@stdlib/assert-is-type-error": "^0.2.0",
|
|
101
|
+
"@stdlib/complex-float64": "^0.2.0",
|
|
102
|
+
"@stdlib/slice-ctor": "^0.2.0",
|
|
103
|
+
"@stdlib/symbol-ctor": "^0.2.0",
|
|
104
|
+
"@stdlib/utils-properties-in": "^0.2.0",
|
|
105
|
+
"proxyquire": "^2.0.0",
|
|
106
|
+
"tape": "git+https://github.com/kgryte/tape.git#fix/globby",
|
|
107
|
+
"istanbul": "^0.4.1",
|
|
108
|
+
"tap-min": "git+https://github.com/Planeshifter/tap-min.git",
|
|
109
|
+
"@stdlib/bench-harness": "^0.2.0",
|
|
110
|
+
"@stdlib/bench": "^0.3.1"
|
|
111
|
+
},
|
|
112
|
+
"engines": {
|
|
113
|
+
"node": ">=0.10.0",
|
|
114
|
+
"npm": ">2.7.0"
|
|
115
|
+
},
|
|
116
|
+
"os": [
|
|
117
|
+
"aix",
|
|
118
|
+
"darwin",
|
|
119
|
+
"freebsd",
|
|
120
|
+
"linux",
|
|
121
|
+
"macos",
|
|
122
|
+
"openbsd",
|
|
123
|
+
"sunos",
|
|
124
|
+
"win32",
|
|
125
|
+
"windows"
|
|
126
|
+
],
|
|
127
|
+
"keywords": [
|
|
128
|
+
"stdlib",
|
|
129
|
+
"stdtypes",
|
|
130
|
+
"types",
|
|
131
|
+
"data",
|
|
132
|
+
"structure",
|
|
133
|
+
"array",
|
|
134
|
+
"fancy",
|
|
135
|
+
"subsequence",
|
|
136
|
+
"subseq",
|
|
137
|
+
"slice",
|
|
138
|
+
"indexing",
|
|
139
|
+
"index",
|
|
140
|
+
"vector"
|
|
141
|
+
],
|
|
142
|
+
"funding": {
|
|
143
|
+
"type": "opencollective",
|
|
144
|
+
"url": "https://opencollective.com/stdlib"
|
|
145
|
+
}
|
|
146
|
+
}
|