Skip to content

Commit d82c047

Browse files
committed
Auto-generated commit
1 parent 238d875 commit d82c047

File tree

8 files changed

+115
-0
lines changed

8 files changed

+115
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
### Features
1212

13+
- [`a814ceb`](https://github.com/stdlib-js/stdlib/commit/a814cebcda3422f9d2eebf58b57c955157c8422c) - add custom `valueOf` method
1314
- [`ba1c0f2`](https://github.com/stdlib-js/stdlib/commit/ba1c0f278e120cd42272fa2449bd13f045a469aa) - add `fillSlice` to namespace
1415
- [`f015917`](https://github.com/stdlib-js/stdlib/commit/f0159173d4db9e22bb1bd7c8b56c424b4d3e076e) - add `flattenBy` to namespace
1516
- [`8f440d7`](https://github.com/stdlib-js/stdlib/commit/8f440d7aeadc895a5a7a348e935389619d408a0e) - add `flatten` to namespace
@@ -545,6 +546,7 @@ A total of 24 issues were closed in this release:
545546

546547
<details>
547548

549+
- [`a814ceb`](https://github.com/stdlib-js/stdlib/commit/a814cebcda3422f9d2eebf58b57c955157c8422c) - **feat:** add custom `valueOf` method _(by Athan Reines)_
548550
- [`e700afd`](https://github.com/stdlib-js/stdlib/commit/e700afd0526ff2cc8f78cdb8743c749eceb09dd6) - **docs:** update type _(by Athan Reines)_
549551
- [`4a1b036`](https://github.com/stdlib-js/stdlib/commit/4a1b0361b7491efedfd3983f4a686601ba12c673) - **docs:** update type _(by Athan Reines)_
550552
- [`1285c82`](https://github.com/stdlib-js/stdlib/commit/1285c82a9767afae6ddf57df797eeff5ed1e49f3) - **docs:** update examples _(by Athan Reines)_

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 -->

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+
});

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

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

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

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

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)