-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.js
241 lines (224 loc) · 8.11 KB
/
validator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
// MODULES //
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
var isComplexLike = require( '@stdlib/assert/is-complex-like' );
var isRealFloatingDataType = require( './../../base/assert/is-real-floating-point-data-type' );
var isUnsignedIntegerDataType = require( './../../base/assert/is-unsigned-integer-data-type' );
var isSignedIntegerDataType = require( './../../base/assert/is-signed-integer-data-type' );
var isBooleanDataType = require( './../../base/assert/is-boolean-data-type' );
var isSafeCast = require( './../../base/assert/is-safe-data-type-cast' );
var minDataType = require( './../../min-dtype' );
var minSignedIntegerDataType = require( './../../base/min-signed-integer-dtype' );
var complexDataType = require( '@stdlib/complex/dtype' );
var format = require( '@stdlib/string/format' );
// FUNCTIONS //
/**
* Verifies whether a provided value can be safely assigned to an element in an array having a "generic" or unknown data type.
*
* @private
* @param {*} value - input value
* @param {string} dtype - array data type
* @returns {null} null
*
* @example
* var err = validateGeneric( 3, 'generic' );
* // returns null
*/
function validateGeneric() {
return null;
}
/**
* Verifies whether a provided value can be safely assigned to an element in an array having a boolean data type.
*
* @private
* @param {*} value - input value
* @param {string} dtype - array data type
* @returns {(Error|null)} error object or null
*
* @example
* var err = validateBoolean( true, 'bool' );
* // returns null
*
* @example
* var Complex128 = require( '@stdlib/complex/float64/ctor' );
*
* var err = validateBoolean( new Complex128( 5.0, 6.0 ), 'bool' );
* // returns <TypeError>
*/
function validateBoolean( value, dtype ) {
if ( isBoolean( value ) ) {
return null;
}
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 ) );
}
/**
* Verifies whether a provided value can be safely assigned to an element in an array having a real-valued floating-point data type.
*
* @private
* @param {*} value - input value
* @param {string} dtype - array data type
* @returns {(Error|null)} error object or null
*
* @example
* var err = validateRealFloating( 3.14, 'float64' );
* // returns null
*
* @example
* var Complex128 = require( '@stdlib/complex/float64/ctor' );
*
* var err = validateRealFloating( new Complex128( 5.0, 6.0 ), 'float64' );
* // returns <TypeError>
*/
function validateRealFloating( value, dtype ) {
if ( isNumber( value ) ) {
return null;
}
if ( isComplexLike( value ) ) {
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 ) );
}
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 ) );
}
/**
* Verifies whether a provided value can be safely assigned to an element in an array having a complex-valued floating-point data type.
*
* @private
* @param {*} value - input value
* @param {string} dtype - array data type
* @returns {(Error|null)} error object or null
*
* @example
* var Complex128 = require( '@stdlib/complex/float64/ctor' );
*
* var err = validateComplexFloating( new Complex128( 5.0, 6.0 ), 'complex128' );
* // returns null
*
* @example
* var err = validateComplexFloating( {}, 'complex128' );
* // returns <TypeError>
*/
function validateComplexFloating( value, dtype ) {
if ( isNumber( value ) || isComplexLike( value ) ) {
return null;
}
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 ) );
}
/**
* Verifies whether a provided value can be safely assigned to an element in an array having a signed integer data type.
*
* @private
* @param {*} value - input value
* @param {string} dtype - array data type
* @returns {(Error|null)} error object or null
*
* @example
* var err = validateSignedInteger( 3, 'int32' );
* // returns null
*
* @example
* var err = validateSignedInteger( 3.14, 'int32' );
* // returns <TypeError>
*/
function validateSignedInteger( value, dtype ) {
var vdt;
if ( isNumber( value ) ) {
if ( !isInteger( value ) ) {
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 ) );
}
vdt = minSignedIntegerDataType( value );
if ( isSafeCast( vdt, dtype ) ) {
return null;
}
return new TypeError( format( 'invalid operation. Assigned value cannot be safely cast to the target array data type. Data types: [%s, %s].', vdt, dtype ) );
}
if ( isComplexLike( value ) ) {
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 ) );
}
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 ) );
}
/**
* Verifies whether a provided value can be safely assigned to an element in an array having an unsigned integer data type.
*
* @private
* @param {*} value - input value
* @param {string} dtype - array data type
* @returns {(Error|null)} error object or null
*
* @example
* var err = validateUnsignedInteger( 3, 'uint32' );
* // returns null
*
* @example
* var err = validateUnsignedInteger( -3, 'uint32' );
* // returns <TypeError>
*/
function validateUnsignedInteger( value, dtype ) {
var vdt;
if ( isNumber( value ) ) {
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`
if ( isSafeCast( vdt, dtype ) ) {
return null;
}
return new TypeError( format( 'invalid operation. Assigned value cannot be safely cast to the target array data type. Data types: [%s, %s].', vdt, dtype ) );
}
if ( isComplexLike( value ) ) {
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 ) );
}
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 ) );
}
// MAIN //
/**
* 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.
*
* @private
* @param {string} dtype - array data type
* @returns {Function} validation function
*
* @example
* var fcn = validator( 'int32' );
*
* var err = fcn( 3, 'int32' );
* // returns null
*
* err = fcn( 3.14, 'int32' );
* // returns <TypeError>
*/
function validator( dtype ) {
if ( dtype === 'generic' || dtype === '' ) {
return validateGeneric;
}
if ( isRealFloatingDataType( dtype ) ) {
return validateRealFloating;
}
if ( isUnsignedIntegerDataType( dtype ) ) {
return validateUnsignedInteger;
}
if ( isSignedIntegerDataType( dtype ) ) {
return validateSignedInteger;
}
if ( isBooleanDataType( dtype ) ) {
return validateBoolean;
}
// Case: isComplexDataType( dtype ) === true
return validateComplexFloating;
}
// EXPORTS //
module.exports = validator;