Skip to content

Commit 20c24f1

Browse files
committed
fix: apply suggestions from code review
--- 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: na - task: lint_package_json status: na - task: lint_repl_help status: na - 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: 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: na - task: lint_license_headers status: passed ---
1 parent b1ec706 commit 20c24f1

3 files changed

Lines changed: 185 additions & 25 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
25+
var format = require( '@stdlib/string/format' );
26+
var pkg = require( './../package.json' ).name;
27+
var namedtypedtuple = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( format( '%s:at', pkg ), function benchmark( b ) {
33+
var Point;
34+
var out;
35+
var p;
36+
var i;
37+
38+
Point = namedtypedtuple( [ 'x', 'y' ] );
39+
p = new Point( [ 1.0, -1.0 ] );
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
out = p.at( i % 2 );
44+
if ( typeof out !== 'number' ) {
45+
b.fail( 'should return a number' );
46+
}
47+
}
48+
b.toc();
49+
if ( !isNumber( out ) ) {
50+
b.fail( 'should return a number' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var format = require( '@stdlib/string/format' );
27+
var pkg = require( './../package.json' ).name;
28+
var namedtypedtuple = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - tuple length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var fields;
42+
var Point;
43+
var p;
44+
var i;
45+
46+
fields = [];
47+
for ( i = 0; i < len+1; i++ ) {
48+
fields.push( '_'+i.toString() );
49+
}
50+
Point = namedtypedtuple( fields );
51+
p = new Point();
52+
53+
return benchmark;
54+
55+
/**
56+
* Benchmark function.
57+
*
58+
* @private
59+
* @param {Benchmark} b - benchmark instance
60+
*/
61+
function benchmark( b ) {
62+
var out;
63+
var i;
64+
65+
b.tic();
66+
for ( i = 0; i < b.iterations; i++ ) {
67+
out = p.at( i % len );
68+
if ( typeof out !== 'number' ) {
69+
b.fail( 'should return a number' );
70+
}
71+
}
72+
b.toc();
73+
if ( !isNumber( out ) ) {
74+
b.fail( 'should return a number' );
75+
}
76+
b.pass( 'benchmark finished' );
77+
b.end();
78+
}
79+
}
80+
81+
82+
// MAIN //
83+
84+
/**
85+
* Main execution sequence.
86+
*
87+
* @private
88+
*/
89+
function main() {
90+
var len;
91+
var min;
92+
var max;
93+
var f;
94+
var i;
95+
96+
min = 1; // 10^min
97+
max = 6; // 10^max
98+
99+
for ( i = min; i <= max; i++ ) {
100+
len = pow( 10, i );
101+
f = createBenchmark( len );
102+
bench( format( '%s:at:len=%d', pkg, len ), f );
103+
}
104+
}
105+
106+
main();

lib/node_modules/@stdlib/dstructs/named-typed-tuple/lib/main.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
191191
setNonEnumerableProperty( tuple, 'fieldOf', fieldOf );
192192
setNonEnumerableProperty( tuple, 'filter', filter );
193193
setNonEnumerableProperty( tuple, 'find', find );
194-
setNonEnumerableProperty( tuple, 'findIndex', findIndex );
195194
setNonEnumerableProperty( tuple, 'findField', findField );
195+
setNonEnumerableProperty( tuple, 'findIndex', findIndex );
196196
setNonEnumerableProperty( tuple, 'findLast', findLast );
197197
setNonEnumerableProperty( tuple, 'findLastField', findLastField );
198198
setNonEnumerableProperty( tuple, 'findLastIndex', findLastIndex );
@@ -557,21 +557,21 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
557557
}
558558

559559
/**
560-
* Returns the last tuple element for which a provided predicate function returns a truthy value.
560+
* Returns the index of the first tuple element for which a provided predicate function returns a truthy value.
561561
*
562562
* ## Notes
563563
*
564-
* - The method iterates from right to left.
564+
* - If the predicate function never returns a truthy value, the function returns `-1`.
565565
*
566566
* @private
567567
* @memberof tuple
568568
* @param {Function} predicate - predicate function
569569
* @param {*} [thisArg] - execution context
570570
* @throws {TypeError} `this` must be the host tuple
571571
* @throws {TypeError} first argument must be a function
572-
* @returns {(number|void)} tuple element
572+
* @returns {integer} tuple index or `-1`
573573
*/
574-
function findLast( predicate, thisArg ) {
574+
function findIndex( predicate, thisArg ) {
575575
var bool;
576576
var i;
577577
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
@@ -580,33 +580,32 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
580580
if ( !isFunction( predicate ) ) {
581581
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
582582
}
583-
for ( i = nfields - 1; i >= 0; i-- ) {
583+
for ( i = 0; i < nfields; i++ ) {
584584
bool = predicate.call( thisArg, tuple[ i ], i, fields[ indices[i] ], tuple );
585585
if ( bool ) {
586-
return tuple[ i ];
586+
return i;
587587
}
588588
}
589+
return -1;
589590
}
590591

591592
/**
592-
* Returns the field of the last tuple element for which a provided predicate function returns a truthy value.
593+
* Returns the last tuple element for which a provided predicate function returns a truthy value.
593594
*
594595
* ## Notes
595596
*
596597
* - The method iterates from right to left.
597-
* - If the predicate function never returns a truthy value, the function returns `undefined`.
598598
*
599599
* @private
600600
* @memberof tuple
601601
* @param {Function} predicate - predicate function
602602
* @param {*} [thisArg] - execution context
603603
* @throws {TypeError} `this` must be the host tuple
604604
* @throws {TypeError} first argument must be a function
605-
* @returns {(string|void)} tuple field name or `undefined`
605+
* @returns {(number|void)} tuple element
606606
*/
607-
function findLastField( predicate, thisArg ) {
607+
function findLast( predicate, thisArg ) {
608608
var bool;
609-
var f;
610609
var i;
611610
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
612611
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
@@ -615,32 +614,32 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
615614
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
616615
}
617616
for ( i = nfields - 1; i >= 0; i-- ) {
618-
f = fields[ indices[ i ] ];
619-
bool = predicate.call( thisArg, tuple[ i ], i, f, tuple );
617+
bool = predicate.call( thisArg, tuple[ i ], i, fields[ indices[i] ], tuple );
620618
if ( bool ) {
621-
return f;
619+
return tuple[ i ];
622620
}
623621
}
624622
}
625623

626624
/**
627-
* Returns the index of the last tuple element for which a provided predicate function returns a truthy value.
625+
* Returns the field of the last tuple element for which a provided predicate function returns a truthy value.
628626
*
629627
* ## Notes
630628
*
631629
* - The method iterates from right to left.
632-
* - If the predicate function never returns a truthy value, the function returns `-1`.
630+
* - If the predicate function never returns a truthy value, the function returns `undefined`.
633631
*
634632
* @private
635633
* @memberof tuple
636634
* @param {Function} predicate - predicate function
637635
* @param {*} [thisArg] - execution context
638636
* @throws {TypeError} `this` must be the host tuple
639637
* @throws {TypeError} first argument must be a function
640-
* @returns {integer} tuple index or `-1`
638+
* @returns {(string|void)} tuple field name or `undefined`
641639
*/
642-
function findLastIndex( predicate, thisArg ) {
640+
function findLastField( predicate, thisArg ) {
643641
var bool;
642+
var f;
644643
var i;
645644
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
646645
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
@@ -649,19 +648,20 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
649648
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
650649
}
651650
for ( i = nfields - 1; i >= 0; i-- ) {
652-
bool = predicate.call( thisArg, tuple[ i ], i, fields[ indices[i] ], tuple );
651+
f = fields[ indices[ i ] ];
652+
bool = predicate.call( thisArg, tuple[ i ], i, f, tuple );
653653
if ( bool ) {
654-
return i;
654+
return f;
655655
}
656656
}
657-
return -1;
658657
}
659658

660659
/**
661-
* Returns the index of the first tuple element for which a provided predicate function returns a truthy value.
660+
* Returns the index of the last tuple element for which a provided predicate function returns a truthy value.
662661
*
663662
* ## Notes
664663
*
664+
* - The method iterates from right to left.
665665
* - If the predicate function never returns a truthy value, the function returns `-1`.
666666
*
667667
* @private
@@ -672,7 +672,7 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
672672
* @throws {TypeError} first argument must be a function
673673
* @returns {integer} tuple index or `-1`
674674
*/
675-
function findIndex( predicate, thisArg ) {
675+
function findLastIndex( predicate, thisArg ) {
676676
var bool;
677677
var i;
678678
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
@@ -681,7 +681,7 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
681681
if ( !isFunction( predicate ) ) {
682682
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
683683
}
684-
for ( i = 0; i < nfields; i++ ) {
684+
for ( i = nfields - 1; i >= 0; i-- ) {
685685
bool = predicate.call( thisArg, tuple[ i ], i, fields[ indices[i] ], tuple );
686686
if ( bool ) {
687687
return i;

0 commit comments

Comments
 (0)