Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22x 22x 22x 21x 21x 21x 21x 21x 21x 21x 21x 22x 22x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 61x 61x 61x 61x 61x 61x 61x 2x 1x 1x 1x 1x 61x 37x 61x 1x 1x 1x 1x 22x 1x 21x 20x 20x 61x 21x 21x 9x 9x 21x 8x 8x 61x 17x 17x 21x 61x 61x 61x 61x 61x 61x 61x 61x 61x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x | /**
* @license Apache-2.0
*
* Copyright (c) 2025 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.
*/
/* eslint-disable no-restricted-syntax, no-invalid-this */
'use strict';
// MODULES //
var isStructConstructorLike = require( '@stdlib/assert/is-struct-constructor-like' );
var isPlainObject = require( '@stdlib/assert/is-plain-object' );
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
var isDataTypeString = require( '@stdlib/ndarray/base/assert/is-data-type-string' );
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
var hasProp = require( '@stdlib/assert/has-property' );
var setReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' );
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
var resolveEnum = require( '@stdlib/ndarray/base/dtype-resolve-enum' );
var dtype2desc = require( '@stdlib/ndarray/base/dtype-desc' );
var dtype2char = require( '@stdlib/ndarray/base/dtype-char' );
var dtype2alignment = require( '@stdlib/ndarray/base/dtype-alignment' );
var bytesPerElement = require( '@stdlib/ndarray/base/bytes-per-element' );
var format = require( '@stdlib/string/format' );
// FUNCTIONS //
/**
* Returns a boolean indicating if a value is a `DataType`.
*
* @private
* @param {*} value - value to test
* @returns {boolean} boolean indicating if a value is a `DataType`
*/
function isDataType( value ) { // NOTE: we do not use `ndarray/base/assert/is-data-type-object` in order to avoid circular dependencies
return (
value instanceof DataType ||
(
typeof value === 'object' &&
value !== null &&
value.constructor.name === 'DataType' &&
isString( value.char ) &&
isString( value.description ) &&
isString( value.byteOrder ) &&
hasProp( value, 'value' )
)
);
}
// MAIN //
/**
* Data type constructor.
*
* @constructor
* @param {*} value - data type value
* @param {Options} [options] - constructor options
* @param {string} [options.description] - data type description
* @throws {TypeError} first argument must be a supported data type value
* @throws {TypeError} options argument must be an object
* @throws {TypeError} must provide valid options
* @returns {DataType} data type instance
*
* @example
* var dt = new DataType( 'float64' );
* // returns <DataType>
*
* var str = dt.toString();
* // returns 'float64'
*/
function DataType( value, options ) {
var nargs;
var type;
var opts;
nargs = arguments.length;
if ( !( this instanceof DataType ) ) {
if ( nargs < 2 ) {
return new DataType( value );
}
return new DataType( value, options );
}
if ( isDataTypeString( value ) ) {
type = 'builtin';
} else if ( isDataType( value ) ) {
// Clone the input data type:
return new DataType( value.value, {
'description': value.description
});
} else if ( isStructConstructorLike( value ) ) {
type = 'struct';
} else {
throw new TypeError( format( 'invalid argument. First argument must be either a supported data type string, a struct constructor, or another data type instance. Value: `%s`.', value ) );
}
if ( nargs > 1 ) {
opts = options;
if ( !isPlainObject( opts ) ) {
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', opts ) );
}
if ( hasOwnProp( opts, 'description' ) && !isString( opts.description ) ) {
throw new TypeError( format( 'invalid option. `%s` option must be a string. Option: `%s`.', 'description', opts.description ) );
}
} else {
opts = {};
}
setReadOnly( this, '_value', value );
setReadOnly( this, '_description', opts.description || ( dtype2desc( value ) || '' ) );
setReadOnly( this, '_char', dtype2char( value ) || '' );
setReadOnly( this, '_enum', resolveEnum( value ) || -1 );
setReadOnly( this, '_alignment', dtype2alignment( value ) || -1 );
setReadOnly( this, '_byteLength', bytesPerElement( value ) || -1 );
setReadOnly( this, '_byteOrder', 'host' ); // TODO: consider supporting little-endian and big-endian byte orders
setReadOnly( this, '_type', type );
return this;
}
/**
* Constructor name.
*
* @name name
* @memberof DataType
* @type {string}
* @default 'DataType'
*
* @example
* var v = DataType.name;
* // returns 'DataType'
*/
setReadOnly( DataType, 'name', 'DataType' );
/**
* Alignment (in bytes) for the data type.
*
* ## Notes
*
* - If a data type does not have a known alignment, the returned value is `-1`.
*
* @name alignment
* @memberof DataType.prototype
* @readonly
* @type {integer}
*
* @example
* var dt = new DataType( 'float64' );
* // returns <DataType>
*
* var v = dt.alignment;
* // returns 8
*/
setReadOnlyAccessor( DataType.prototype, 'alignment', function get() {
return this._alignment;
});
/**
* Size (in bytes) of the data type.
*
* ## Notes
*
* - If a data type does not have a known size, the returned value is `-1`.
*
* @name byteLength
* @memberof DataType.prototype
* @readonly
* @type {integer}
*
* @example
* var dt = new DataType( 'float64' );
* // returns <DataType>
*
* var v = dt.byteLength;
* // returns 8
*/
setReadOnlyAccessor( DataType.prototype, 'byteLength', function get() {
return this._byteLength;
});
/**
* Data type byte order.
*
* ## Notes
*
* - Potential values:
*
* - **host**: host platform byte order.
* - **little-endian**: little-endian byte order.
* - **big-endian**: big-endian byte order.
*
* @name byteOrder
* @memberof DataType.prototype
* @readonly
* @type {string}
*
* @example
* var dt = new DataType( 'float64' );
* // returns <DataType>
*
* var v = dt.byteOrder;
* // returns 'host'
*/
setReadOnlyAccessor( DataType.prototype, 'byteOrder', function get() {
return this._byteOrder;
});
/**
* Single letter character abbreviation for the data type.
*
* ## Notes
*
* - If a data type does not have a corresponding single letter character abbreviation, the returned value is an empty string.
*
* @name char
* @memberof DataType.prototype
* @readonly
* @type {string}
*
* @example
* var dt = new DataType( 'float64' );
* // returns <DataType>
*
* var v = dt.char;
* // returns 'd'
*/
setReadOnlyAccessor( DataType.prototype, 'char', function get() {
return this._char;
});
/**
* Data type description.
*
* ## Notes
*
* - If a data type does not have an associated description, the returned value is an empty string.
*
* @name description
* @memberof DataType.prototype
* @readonly
* @type {string}
*
* @example
* var dt = new DataType( 'float64' );
* // returns <DataType>
*
* var v = dt.description;
* // returns <string>
*/
setReadOnlyAccessor( DataType.prototype, 'description', function get() {
return this._description;
});
/**
* Enumeration constant for the data type.
*
* ## Notes
*
* - If a data type does not have a corresponding known enumeration constant, the returned value is `-1`.
*
* @name enum
* @memberof DataType.prototype
* @readonly
* @type {integer}
*
* @example
* var dt = new DataType( 'float64' );
* // returns <DataType>
*
* var v = dt.enum;
* // returns <number>
*/
setReadOnlyAccessor( DataType.prototype, 'enum', function get() {
return this._enum;
});
/**
* Raw (original) data type value.
*
* @name value
* @memberof DataType.prototype
* @readonly
* @type {*}
*
* @example
* var dt = new DataType( 'float64' );
* // returns <DataType>
*
* var v = dt.value;
* // returns 'float64'
*/
setReadOnlyAccessor( DataType.prototype, 'value', function get() {
return this._value;
});
/**
* Serializes a data type instance as a JSON object.
*
* ## Notes
*
* - `JSON.stringify()` implicitly calls this method when stringifying a `DataType` instance.
*
* @name toJSON
* @memberof DataType.prototype
* @type {Function}
* @returns {Object} serialized instance
*
* @example
* var dt = new DataType( 'float64' );
* // returns <DataType>
*
* var o = dt.toJSON();
* // returns {...}
*/
setReadOnly( DataType.prototype, 'toJSON', function toJSON() {
return {
'type': 'DataType',
'value': this.toString(),
'byteOrder': this._byteOrder,
'description': this._description
};
});
/**
* Serializes a data type instance to a string.
*
* @name toString
* @memberof DataType.prototype
* @type {Function}
* @returns {string} serialized string
*
* @example
* var dt = new DataType( 'float64' );
* // returns <DataType>
*
* var v = dt.toString();
* // returns 'float64'
*/
setReadOnly( DataType.prototype, 'toString', function toString() {
return ( this._type === 'struct' ) ? this._value.layout : String( this._value );
});
/**
* Converts a data type instance to a primitive value.
*
* @name valueOf
* @memberof DataType.prototype
* @type {Function}
* @returns {string} primitive value
*
* @example
* var dt = new DataType( 'float64' );
* // returns <DataType>
*
* var v = dt.valueOf();
* // returns 'float64'
*/
setReadOnly( DataType.prototype, 'valueOf', function valueOf() {
return this.toString();
});
// EXPORTS //
module.exports = DataType;
|