Skip to content

Commit ddccd36

Browse files
committed
refactor: make internal properties read-only and add instance check
--- 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: passed - 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: na - 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: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent bd31cd9 commit ddccd36

File tree

4 files changed

+25
-23
lines changed

4 files changed

+25
-23
lines changed

lib/node_modules/@stdlib/ndarray/dtype-ctor/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ var v = dt.enum;
148148
// returns <number>
149149
```
150150

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.
151+
If a data type does not have a corresponding known enumeration constant, the returned value is `-1`.
152152

153153
**Note**: enumeration constants should be treated as **opaque** values. One should **not** assume that a data type has a specific enumeration constant value.
154154

lib/node_modules/@stdlib/ndarray/dtype-ctor/docs/repl.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@
134134
Enumeration constant for the data type.
135135

136136
If a data type does not have a corresponding known enumeration constant, the
137-
returned value is the enumeration constant for a user-defined data type.
137+
returned value is -1.
138138

139139
Returns
140140
-------

lib/node_modules/@stdlib/ndarray/dtype-ctor/docs/types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ declare class DataType<T = unknown> {
145145
*
146146
* ## Notes
147147
*
148-
* - 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.
148+
* - If a data type does not have a corresponding known enumeration constant, the returned value is `-1`.
149149
*
150150
* @example
151151
* var dt = new DataType( 'float64' );

lib/node_modules/@stdlib/ndarray/dtype-ctor/lib/main.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ var dtype2desc = require( '@stdlib/ndarray/base/dtype-desc' );
3535
var dtype2char = require( '@stdlib/ndarray/base/dtype-char' );
3636
var dtype2alignment = require( '@stdlib/ndarray/base/dtype-alignment' );
3737
var bytesPerElement = require( '@stdlib/ndarray/base/bytes-per-element' );
38-
var dtypes = require( '@stdlib/ndarray/dtypes' );
3938
var format = require( '@stdlib/string/format' );
4039

4140

@@ -50,13 +49,16 @@ var format = require( '@stdlib/string/format' );
5049
*/
5150
function isDataType( value ) {
5251
return (
53-
typeof value === 'object' &&
54-
value !== null &&
55-
value.constructor.name === 'DataType' &&
56-
isString( value.char ) &&
57-
isString( value.description ) &&
58-
isString( value.byteOrder ) &&
59-
hasProp( value, 'value' )
52+
value instanceof DataType ||
53+
(
54+
typeof value === 'object' &&
55+
value !== null &&
56+
value.constructor.name === 'DataType' &&
57+
isString( value.char ) &&
58+
isString( value.description ) &&
59+
isString( value.byteOrder ) &&
60+
hasProp( value, 'value' )
61+
)
6062
);
6163
}
6264

@@ -96,13 +98,13 @@ function DataType( value, options ) {
9698
}
9799
if ( isDataTypeString( value ) ) {
98100
type = 'builtin';
99-
} else if ( isStructConstructorLike( value ) ) {
100-
type = 'struct';
101101
} else if ( isDataType( value ) ) {
102102
// Clone the input data type:
103103
return new DataType( value.value, {
104104
'description': value.description
105105
});
106+
} else if ( isStructConstructorLike( value ) ) {
107+
type = 'struct';
106108
} else {
107109
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 ) );
108110
}
@@ -117,14 +119,14 @@ function DataType( value, options ) {
117119
} else {
118120
opts = {};
119121
}
120-
this._value = value;
121-
this._description = opts.description || ( dtype2desc( value ) || '' );
122-
this._char = dtype2char( value ) || '';
123-
this._enum = resolveEnum( value ) || dtypes.userdefined_type;
124-
this._alignment = dtype2alignment( value ) || -1;
125-
this._byteLength = bytesPerElement( value ) || -1;
126-
this._byteOrder = 'host'; // TODO: consider supporting little-endian and big-endian byte orders
127-
this._type = type;
122+
setReadOnly( this, '_value', value );
123+
setReadOnly( this, '_description', opts.description || ( dtype2desc( value ) || '' ) );
124+
setReadOnly( this, '_char', dtype2char( value ) || '' );
125+
setReadOnly( this, '_enum', resolveEnum( value ) || -1 );
126+
setReadOnly( this, '_alignment', dtype2alignment( value ) || -1 );
127+
setReadOnly( this, '_byteLength', bytesPerElement( value ) || -1 );
128+
setReadOnly( this, '_byteOrder', 'host' ); // TODO: consider supporting little-endian and big-endian byte orders
129+
setReadOnly( this, '_type', type );
128130
return this;
129131
}
130132

@@ -266,12 +268,12 @@ setReadOnlyAccessor( DataType.prototype, 'description', function get() {
266268
*
267269
* ## Notes
268270
*
269-
* - 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.
271+
* - If a data type does not have a corresponding known enumeration constant, the returned value is `-1`.
270272
*
271273
* @name enum
272274
* @memberof DataType.prototype
273275
* @readonly
274-
* @type {NonNegativeInteger}
276+
* @type {integer}
275277
*
276278
* @example
277279
* var dt = new DataType( 'float64' );

0 commit comments

Comments
 (0)