Skip to content

Commit 77e25ab

Browse files
committed
Auto-generated commit
1 parent c978868 commit 77e25ab

File tree

8 files changed

+263
-33
lines changed

8 files changed

+263
-33
lines changed

CHANGELOG.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@
44
55
<section class="release" id="unreleased">
66

7-
## Unreleased (2025-05-08)
7+
## Unreleased (2025-05-16)
8+
9+
<section class="features">
10+
11+
### Features
12+
13+
- [`b2cefbe`](https://github.com/stdlib-js/stdlib/commit/b2cefbe2b2192cb705b85c43ffac2f57ca782c42) - add custom `valueOf` method
14+
15+
</section>
16+
17+
<!-- /.features -->
818

919
<section class="bug-fixes">
1020

@@ -23,6 +33,7 @@
2333

2434
<details>
2535

36+
- [`b2cefbe`](https://github.com/stdlib-js/stdlib/commit/b2cefbe2b2192cb705b85c43ffac2f57ca782c42) - **feat:** add custom `valueOf` method _(by Athan Reines)_
2637
- [`7321e29`](https://github.com/stdlib-js/stdlib/commit/7321e294d995d496b35f24f253055190ae6a78d5) - **refactor:** use base assertion utility _(by Athan Reines)_
2738
- [`25abfc6`](https://github.com/stdlib-js/stdlib/commit/25abfc67b400f646304fa1f10b239a051f6569f6) - **refactor:** support non-built-in shape and strides objects _(by Athan Reines)_
2839
- [`5e2bbef`](https://github.com/stdlib-js/stdlib/commit/5e2bbef14efd5937e23047c01af0e740e6cbd4f6) - **fix:** add missing boolean array support _(by Athan Reines)_

CONTRIBUTORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ Utkarsh <http://utkarsh11105@gmail.com>
174174
Utkarsh Raj <rajutkarsh2505@gmail.com>
175175
UtkershBasnet <119008923+UtkershBasnet@users.noreply.github.com>
176176
Vaibhav Patel <98279986+noobCoderVP@users.noreply.github.com>
177+
Vara Rahul Rajana <123227543+rajanarahul93@users.noreply.github.com>
177178
Varad Gupta <varadgupta21@gmail.com>
178179
Vinit Pandit <106718914+MeastroZI@users.noreply.github.com>
179180
Vivek Maurya <vm8118134@gmail.com>

benchmark/benchmark.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,3 +1275,71 @@ bench( pkg+':toString', function benchmark( b ) {
12751275
b.pass( 'benchmark finished' );
12761276
b.end();
12771277
});
1278+
1279+
bench( pkg+':valueOf:ndims=0', function benchmark( b ) {
1280+
var strides;
1281+
var buffer;
1282+
var offset;
1283+
var shape;
1284+
var order;
1285+
var out;
1286+
var v;
1287+
var i;
1288+
1289+
buffer = [ 1.0 ];
1290+
shape = [];
1291+
strides = [ 0 ];
1292+
offset = 0;
1293+
order = 'row-major';
1294+
1295+
out = ndarray( 'generic', buffer, shape, strides, offset, order );
1296+
1297+
b.tic();
1298+
for ( i = 0; i < b.iterations; i++ ) {
1299+
buffer[ 0 ] = i;
1300+
v = out.valueOf();
1301+
if ( typeof v !== 'number' ) {
1302+
b.fail( 'should return a number' );
1303+
}
1304+
}
1305+
b.toc();
1306+
if ( typeof v !== 'number' ) {
1307+
b.fail( 'should return a number' );
1308+
}
1309+
b.pass( 'benchmark finished' );
1310+
b.end();
1311+
});
1312+
1313+
bench( pkg+':valueOf:ndims>=1', function benchmark( b ) {
1314+
var strides;
1315+
var buffer;
1316+
var offset;
1317+
var shape;
1318+
var order;
1319+
var out;
1320+
var v;
1321+
var i;
1322+
1323+
buffer = [ 1.0, 2.0 ];
1324+
shape = [ 2 ];
1325+
strides = [ 1 ];
1326+
offset = 0;
1327+
order = 'row-major';
1328+
1329+
out = ndarray( 'generic', buffer, shape, strides, offset, order );
1330+
1331+
b.tic();
1332+
for ( i = 0; i < b.iterations; i++ ) {
1333+
buffer[ i%buffer.length ] = i;
1334+
v = out.valueOf();
1335+
if ( typeof v !== 'object' ) {
1336+
b.fail( 'should return an object' );
1337+
}
1338+
}
1339+
b.toc();
1340+
if ( typeof v !== 'object' ) {
1341+
b.fail( 'should return an object' );
1342+
}
1343+
b.pass( 'benchmark finished' );
1344+
b.end();
1345+
});

dist/index.js

Lines changed: 30 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/main.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ var igetValue = require( './iget.js' );
3838
var isetValue = require( './iset.js' );
3939
var setValue = require( './set.js' );
4040
var getValue = require( './get.js' );
41+
var valueOf = require( './valueof.js' ); // eslint-disable-line stdlib/no-redeclare
4142
var toJSON = require( './tojson.js' );
4243
var toString = require( './tostring.js' ); // eslint-disable-line stdlib/no-redeclare
4344
var meta2dataview = require( './meta2dataview.js' );
@@ -569,6 +570,42 @@ setReadOnly( ndarray.prototype, 'toString', toString );
569570
*/
570571
setReadOnly( ndarray.prototype, 'toJSON', toJSON );
571572

573+
/**
574+
* Converts an ndarray instance to a primitive value.
575+
*
576+
* ## Notes
577+
*
578+
* - Only zero-dimensional ndarrays are converted to a primitive value. For ndarray instances having one or more dimensions, the method returns the `this` value, as is the default behavior for objects.
579+
*
580+
* @name valueOf
581+
* @memberof ndarray.prototype
582+
* @type {Function}
583+
* @returns {(*|ndarray)} result
584+
*
585+
* @example
586+
* var buffer = [ 3.14 ];
587+
* var shape = [];
588+
* var strides = [ 0 ];
589+
* var offset = 0;
590+
*
591+
* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' );
592+
*
593+
* var v = x.valueOf();
594+
* // returns 3.14
595+
*
596+
* @example
597+
* var buffer = [ 3.14 ];
598+
* var shape = [ 1 ];
599+
* var strides = [ 1 ];
600+
* var offset = 0;
601+
*
602+
* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' );
603+
*
604+
* var v = x.valueOf();
605+
* // returns <ndarray>
606+
*/
607+
setReadOnly( ndarray.prototype, 'valueOf', valueOf );
608+
572609
/**
573610
* Serializes ndarray meta data to a `DataView`.
574611
*

lib/valueof.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MAIN //
22+
23+
/**
24+
* Converts an ndarray instance to a primitive value.
25+
*
26+
* ## Notes
27+
*
28+
* - Only zero-dimensional ndarrays are converted to a primitive value. For ndarray instances having one or more dimensions, the method returns the `this` value, as is the default behavior for objects.
29+
*
30+
* @private
31+
* @returns {(*|ndarray)} result
32+
*/
33+
function valueOf() { // eslint-disable-line stdlib/no-redeclare
34+
/* eslint-disable no-invalid-this */
35+
if ( this._ndims === 0 ) {
36+
return this.iget();
37+
}
38+
return this;
39+
40+
/* eslint-enable no-invalid-this */
41+
}
42+
43+
44+
// EXPORTS //
45+
46+
module.exports = valueOf;

test/test.js

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3551,7 +3551,9 @@ tape( 'an ndarray has a custom `toString()` method (large array)', function test
35513551
var arr;
35523552

35533553
dtype = 'generic';
3554-
buffer = new Array( 1e4 );
3554+
3555+
// Intentionally create a sparse array:
3556+
buffer = new Array( 1e4 ); // eslint-disable-line stdlib/no-new-array
35553557
shape = [ buffer.length ];
35563558
order = 'row-major';
35573559
strides = [ 1 ];
@@ -3817,6 +3819,68 @@ tape( 'an ndarray has a custom `toJSON()` method (0d)', function test( t ) {
38173819
t.end();
38183820
});
38193821

3822+
tape( 'an ndarray has a custom `valueOf()` method (0d)', function test( t ) {
3823+
var expected;
3824+
var strides;
3825+
var actual;
3826+
var buffer;
3827+
var offset;
3828+
var dtype;
3829+
var order;
3830+
var shape;
3831+
var arr;
3832+
3833+
dtype = 'generic';
3834+
buffer = [ 3.14 ];
3835+
shape = [];
3836+
order = 'row-major';
3837+
strides = [ 0 ];
3838+
offset = 0;
3839+
3840+
arr = ndarray( dtype, buffer, shape, strides, offset, order );
3841+
3842+
t.strictEqual( hasOwnProp( arr, 'valueOf' ), false, 'does not have own property' );
3843+
t.strictEqual( hasProp( arr, 'valueOf' ), true, 'has property' );
3844+
t.strictEqual( isFunction( arr.valueOf ), true, 'has method' );
3845+
3846+
expected = 3.14;
3847+
actual = arr.valueOf();
3848+
t.strictEqual( actual, expected, 'returns expected value' );
3849+
3850+
t.end();
3851+
});
3852+
3853+
tape( 'an ndarray has a custom `valueOf()` method (>=1d)', function test( t ) {
3854+
var expected;
3855+
var strides;
3856+
var actual;
3857+
var buffer;
3858+
var offset;
3859+
var dtype;
3860+
var order;
3861+
var shape;
3862+
var arr;
3863+
3864+
dtype = 'generic';
3865+
buffer = [ 1.0, 2.0 ];
3866+
shape = [ 2 ];
3867+
order = 'row-major';
3868+
strides = [ 0 ];
3869+
offset = 0;
3870+
3871+
arr = ndarray( dtype, buffer, shape, strides, offset, order );
3872+
3873+
t.strictEqual( hasOwnProp( arr, 'valueOf' ), false, 'does not have own property' );
3874+
t.strictEqual( hasProp( arr, 'valueOf' ), true, 'has property' );
3875+
t.strictEqual( isFunction( arr.valueOf ), true, 'has method' );
3876+
3877+
expected = arr;
3878+
actual = arr.valueOf();
3879+
t.strictEqual( actual, expected, 'returns expected value' );
3880+
3881+
t.end();
3882+
});
3883+
38203884
tape( 'an ndarray has a protocol method for serializing meta data to a DataView', function test( t ) {
38213885
/* eslint-disable no-underscore-dangle */
38223886
var expected;

0 commit comments

Comments
 (0)