Skip to content

Commit a814ceb

Browse files
committed
feat: add custom valueOf method
--- 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: passed - task: lint_javascript_benchmarks status: passed - 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: passed - task: lint_license_headers status: passed ---
1 parent e700afd commit a814ceb

File tree

7 files changed

+113
-0
lines changed

7 files changed

+113
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,20 @@ var str = dt.toString();
194194
// returns 'float64'
195195
```
196196

197+
#### DataType.prototype.valueOf()
198+
199+
Converts a `DataType` instance to a primitive value.
200+
201+
```javascript
202+
var dt = new DataType( 'float64' );
203+
// returns <DataType>
204+
205+
var str = dt.valueOf();
206+
// returns 'float64'
207+
```
208+
209+
This method returns the same value as `#.toString()`.
210+
197211
</section>
198212

199213
<!-- /.usage -->

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,29 @@ bench( pkg+':toString', function benchmark( b ) {
314314
b.pass( 'benchmark finished' );
315315
b.end();
316316
});
317+
318+
bench( pkg+':valueOf', function benchmark( b ) {
319+
var values;
320+
var v;
321+
var i;
322+
323+
values = [
324+
new DataType( 'float64' ),
325+
new DataType( 'float32' ),
326+
new DataType( 'generic' )
327+
];
328+
329+
b.tic();
330+
for ( i = 0; i < b.iterations; i++ ) {
331+
v = values[ i%values.length ].valueOf();
332+
if ( typeof v !== 'string' ) {
333+
b.fail( 'should return a string' );
334+
}
335+
}
336+
b.toc();
337+
if ( typeof v !== 'string' ) {
338+
b.fail( 'should return a string' );
339+
}
340+
b.pass( 'benchmark finished' );
341+
b.end();
342+
});

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,23 @@
192192
> dt.toString()
193193
'float64'
194194

195+
196+
{{alias}}.prototype.valueOf()
197+
Converts a data type instance to a primitive.
198+
199+
This method returns the same value as `#.toString()`.
200+
201+
Returns
202+
-------
203+
out: string
204+
Primitive value.
205+
206+
Examples
207+
--------
208+
> var dt = new {{alias}}( 'float64' );
209+
> dt.valueOf()
210+
'float64'
211+
195212
See Also
196213
--------
197214

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,24 @@ declare class DataType<T = unknown> {
199199
* // returns 'float64'
200200
*/
201201
toString(): string;
202+
203+
/**
204+
* Converts a data type instance to a primitive value.
205+
*
206+
* ## Notes
207+
*
208+
* - This method returns the same value as `#.toString()`.
209+
*
210+
* @returns primitive value
211+
*
212+
* @example
213+
* var dt = new DataType( 'float64' );
214+
* // returns <DataType>
215+
*
216+
* var v = dt.valueOf();
217+
* // returns 'float64'
218+
*/
219+
valueOf(): string;
202220
}
203221

204222

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ import DataType = require( './index' );
6262
x.toJSON(); // $ExpectType Object
6363
}
6464

65+
// Attached to a data type instance is a `valueOf` method which returns a string...
66+
{
67+
const x = new DataType( 'float64' ); // $ExpectType DataType<string>
68+
69+
x.valueOf(); // $ExpectType string
70+
}
71+
6572
// The compiler throws an error if the constructor is provided an unsupported number of arguments...
6673
{
6774
new DataType(); // $ExpectError

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,25 @@ setReadOnly( DataType.prototype, 'toString', function toString() {
350350
return ( this._type === 'struct' ) ? this._value.layout : String( this._value );
351351
});
352352

353+
/**
354+
* Converts a data type instance to a primitive value.
355+
*
356+
* @name valueOf
357+
* @memberof DataType.prototype
358+
* @type {Function}
359+
* @returns {string} primitive value
360+
*
361+
* @example
362+
* var dt = new DataType( 'float64' );
363+
* // returns <DataType>
364+
*
365+
* var v = dt.valueOf();
366+
* // returns 'float64'
367+
*/
368+
setReadOnly( DataType.prototype, 'valueOf', function valueOf() {
369+
return this.toString();
370+
});
371+
353372

354373
// EXPORTS //
355374

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,15 @@ tape( 'the constructor returns an instance which supports serializing an instanc
341341

342342
t.end();
343343
});
344+
345+
tape( 'the constructor returns an instance which supports converting an instance to a primitive value', function test( t ) {
346+
var dt;
347+
348+
dt = new DataType( 'float64' );
349+
t.strictEqual( dt.valueOf(), 'float64', 'returns expected value' );
350+
351+
dt = new DataType( 'float32' );
352+
t.strictEqual( dt.valueOf(), 'float32', 'returns expected value' );
353+
354+
t.end();
355+
});

0 commit comments

Comments
 (0)