|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2025 The Stdlib Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +
|
| 19 | +--> |
| 20 | + |
| 21 | +# DataType |
| 22 | + |
| 23 | +> Data type constructor. |
| 24 | +
|
| 25 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 26 | + |
| 27 | +<section class="intro"> |
| 28 | + |
| 29 | +</section> |
| 30 | + |
| 31 | +<!-- /.intro --> |
| 32 | + |
| 33 | +<!-- Package usage documentation. --> |
| 34 | + |
| 35 | +<section class="usage"> |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +```javascript |
| 40 | +var DataType = require( '@stdlib/ndarray/dtype-ctor' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### DataType( value\[, options] ) |
| 44 | + |
| 45 | +Returns a data type instance. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var dt = new DataType( 'float64' ); |
| 49 | +// returns <DataType> |
| 50 | +``` |
| 51 | + |
| 52 | +The constructor supports the following parameters: |
| 53 | + |
| 54 | +- **value**: data type value. Must be either a supported [data type][@stdlib/ndarray/dtypes] string, a [struct][@stdlib/dstructs/struct] constructor, or another data type instance. |
| 55 | +- **options**: constructor options (_optional_). |
| 56 | + |
| 57 | +The constructor supports the following options: |
| 58 | + |
| 59 | +- **description**: data type description. |
| 60 | + |
| 61 | +* * * |
| 62 | + |
| 63 | +## Properties |
| 64 | + |
| 65 | +#### DataType.prototype.alignment |
| 66 | + |
| 67 | +Alignment (in bytes) for the data type. |
| 68 | + |
| 69 | +```javascript |
| 70 | +var dt = new DataType( 'float64' ); |
| 71 | +// returns <DataType> |
| 72 | + |
| 73 | +var v = dt.alignment; |
| 74 | +// returns 8 |
| 75 | +``` |
| 76 | + |
| 77 | +If a data type does not have a known alignment, the returned value is `-1`. |
| 78 | + |
| 79 | +#### DataType.prototype.byteLength |
| 80 | + |
| 81 | +Size (in bytes) for the data type. |
| 82 | + |
| 83 | +```javascript |
| 84 | +var dt = new DataType( 'float64' ); |
| 85 | +// returns <DataType> |
| 86 | + |
| 87 | +var v = dt.byteLength; |
| 88 | +// returns 8 |
| 89 | +``` |
| 90 | + |
| 91 | +If a data type does not have a known size, the returned value is `-1`. |
| 92 | + |
| 93 | +#### DataType.prototype.byteOrder |
| 94 | + |
| 95 | +Data type byte order. |
| 96 | + |
| 97 | +```javascript |
| 98 | +var dt = new DataType( 'float64' ); |
| 99 | +// returns <DataType> |
| 100 | + |
| 101 | +var v = dt.byteOrder; |
| 102 | +// returns 'host' |
| 103 | +``` |
| 104 | + |
| 105 | +The byte order may be one of the following values: |
| 106 | + |
| 107 | +- **host**: host platform byte order. |
| 108 | +- **little-endian**: little-endian byte order. |
| 109 | +- **big-endian**: big-endian byte order. |
| 110 | + |
| 111 | +#### DataType.prototype.char |
| 112 | + |
| 113 | +Single letter character abbreviation for the data type. |
| 114 | + |
| 115 | +```javascript |
| 116 | +var dt = new DataType( 'float64' ); |
| 117 | +// returns <DataType> |
| 118 | + |
| 119 | +var v = dt.char; |
| 120 | +// returns 'd' |
| 121 | +``` |
| 122 | + |
| 123 | +If a data type does not have a corresponding single letter character abbreviation, the returned value is an empty string. |
| 124 | + |
| 125 | +#### DataType.prototype.description |
| 126 | + |
| 127 | +Data type description. |
| 128 | + |
| 129 | +```javascript |
| 130 | +var dt = new DataType( 'float64' ); |
| 131 | +// returns <DataType> |
| 132 | + |
| 133 | +var v = dt.description; |
| 134 | +// returns '...' |
| 135 | +``` |
| 136 | + |
| 137 | +If a data type does not have an associated description, the returned value is an empty string. |
| 138 | + |
| 139 | +#### DataType.prototype.enum |
| 140 | + |
| 141 | +Enumeration constant for the data type. |
| 142 | + |
| 143 | +```javascript |
| 144 | +var dt = new DataType( 'float64' ); |
| 145 | +// returns <DataType> |
| 146 | + |
| 147 | +var v = dt.enum; |
| 148 | +// returns <number> |
| 149 | +``` |
| 150 | + |
| 151 | +If a data type does not have a corresponding known enumeration constant, the returned value is the enumeration constant for a user-defined data type. |
| 152 | + |
| 153 | +**Note**: enumeration constants should be treated as **opaque** values. One should **not** assume that a data type has a specific enumeration constant value. |
| 154 | + |
| 155 | +#### DataType.prototype.value |
| 156 | + |
| 157 | +Raw (original) data type value. |
| 158 | + |
| 159 | +```javascript |
| 160 | +var dt = new DataType( 'float64' ); |
| 161 | +// returns <DataType> |
| 162 | + |
| 163 | +var v = dt.value; |
| 164 | +// returns 'float64' |
| 165 | +``` |
| 166 | + |
| 167 | +* * * |
| 168 | + |
| 169 | +## Methods |
| 170 | + |
| 171 | +#### DataType.prototype.toJSON() |
| 172 | + |
| 173 | +Returns a [JSON][json] representation of a `DataType` instance. |
| 174 | + |
| 175 | +```javascript |
| 176 | +var dt = new DataType( 'float64' ); |
| 177 | +// returns <DataType> |
| 178 | + |
| 179 | +var o = dt.toJSON(); |
| 180 | +// e.g., returns { 'type': 'DataType', 'value': 'float64', 'byteOrder': 'host', ... } |
| 181 | +``` |
| 182 | + |
| 183 | +[`JSON.stringify()`][mdn-json-stringify] implicitly calls this method when stringifying a `DataType` instance. |
| 184 | + |
| 185 | +#### DataType.prototype.toString() |
| 186 | + |
| 187 | +Returns a string representation of a `DataType` instance. |
| 188 | + |
| 189 | +```javascript |
| 190 | +var dt = new DataType( 'float64' ); |
| 191 | +// returns <DataType> |
| 192 | + |
| 193 | +var str = dt.toString(); |
| 194 | +// returns 'float64' |
| 195 | +``` |
| 196 | + |
| 197 | +</section> |
| 198 | + |
| 199 | +<!-- /.usage --> |
| 200 | + |
| 201 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 202 | + |
| 203 | +<section class="notes"> |
| 204 | + |
| 205 | +</section> |
| 206 | + |
| 207 | +<!-- /.notes --> |
| 208 | + |
| 209 | +* * * |
| 210 | + |
| 211 | +<!-- Package usage examples. --> |
| 212 | + |
| 213 | +<section class="examples"> |
| 214 | + |
| 215 | +## Examples |
| 216 | + |
| 217 | +<!-- eslint no-undef: "error" --> |
| 218 | + |
| 219 | +```javascript |
| 220 | +var DataType = require( '@stdlib/ndarray/dtype-ctor' ); |
| 221 | + |
| 222 | +var dt = new DataType( 'complex128' ); |
| 223 | + |
| 224 | +console.log( 'type: %s', typeof dt ); |
| 225 | +// => 'type: object' |
| 226 | + |
| 227 | +console.log( 'alignment: %d', dt.alignment ); |
| 228 | +// => 'alignment: 8' |
| 229 | + |
| 230 | +console.log( 'byteLength: %d', dt.byteLength ); |
| 231 | +// => 'byteLength: 16' |
| 232 | + |
| 233 | +console.log( 'byteOrder: %s', dt.byteOrder ); |
| 234 | +// => 'byteOrder: host' |
| 235 | + |
| 236 | +console.log( 'char: %s', dt.char ); |
| 237 | +// => 'char: z' |
| 238 | + |
| 239 | +console.log( 'JSON: %s', JSON.stringify( dt ) ); |
| 240 | +// e.g., => 'JSON: {"type": "DataType","value":"float64","byteOrder":"host",...}' |
| 241 | +``` |
| 242 | + |
| 243 | +</section> |
| 244 | + |
| 245 | +<!-- /.examples --> |
| 246 | + |
| 247 | +<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 248 | + |
| 249 | +<section class="references"> |
| 250 | + |
| 251 | +</section> |
| 252 | + |
| 253 | +<!-- /.references --> |
| 254 | + |
| 255 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 256 | + |
| 257 | +<section class="related"> |
| 258 | + |
| 259 | +</section> |
| 260 | + |
| 261 | +<!-- /.related --> |
| 262 | + |
| 263 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 264 | + |
| 265 | +<section class="links"> |
| 266 | + |
| 267 | +[json]: http://www.json.org/ |
| 268 | + |
| 269 | +[mdn-json-stringify]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify |
| 270 | + |
| 271 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes |
| 272 | + |
| 273 | +[@stdlib/dstructs/struct]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/dstructs/struct |
| 274 | + |
| 275 | +</section> |
| 276 | + |
| 277 | +<!-- /.links --> |
0 commit comments