Skip to content

Commit d4055ef

Browse files
committed
fix: ensure field resolution is preserved after sorting
--- 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: 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: 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 a410628 commit d4055ef

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

lib/node_modules/@stdlib/dstructs/named-typed-tuple/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1888,7 +1888,7 @@ var y = tuple[ 0 ];
18881888

18891889
// Tuple field assignments do NOT change:
18901890
x = tuple.x;
1891-
// returns 0.0
1891+
// returns 2.0
18921892
```
18931893

18941894
By default, the method sorts tuple elements in ascending order. To impose a custom order, provide a `compareFunction`.

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ var isCollection = require( '@stdlib/assert/is-collection' );
3333
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
3434
var hasIteratorSymbolSupport = require( '@stdlib/assert/has-iterator-symbol-support' );
3535
var propertiesIn = require( '@stdlib/utils/properties-in' );
36+
var copy = require( '@stdlib/array/base/copy' );
3637
var typedarray = require( '@stdlib/array/typed' );
3738
var Int8Array = require( '@stdlib/array/int8' );
38-
var getDtype = require( '@stdlib/array/dtype' );
39+
var getDType = require( '@stdlib/array/dtype' );
3940
var defineProperty = require( '@stdlib/utils/define-property' );
4041
var setNonEnumerableProperty = require( '@stdlib/utils/define-nonenumerable-property' );
4142
var setNonEnumerableReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' ); // eslint-disable-line id-length
@@ -102,7 +103,7 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
102103
if ( !hasDistinctElements( names ) ) {
103104
throw new TypeError( format( 'invalid argument. Field names must be distinct. Value: `%s`.', names ) );
104105
}
105-
fields = names.slice();
106+
fields = copy( names );
106107
nfields = fields.length;
107108
for ( i = 0; i < nfields; i++ ) {
108109
if ( contains( RESERVED_PROPS, fields[ i ] ) ) {
@@ -172,7 +173,7 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
172173
if ( tuple.length !== nfields ) {
173174
throw new RangeError( format( 'invalid arguments. Arguments are incompatible with the number of tuple fields. Number of fields: `%u`. Number of data elements: `%u`.', nfields, tuple.length ) );
174175
}
175-
dtype = getDtype( tuple );
176+
dtype = getDType( tuple );
176177

177178
indices = []; // indirect index look-up table
178179
for ( i = 0; i < nfields; i++ ) {
@@ -227,7 +228,7 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
227228
* @returns {number} tuple value
228229
*/
229230
function get() {
230-
return tuple[ indices[ i ] ];
231+
return tuple[ indices.indexOf( i ) ];
231232
}
232233
}
233234

@@ -248,7 +249,7 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
248249
* @param {number} v - value to set
249250
*/
250251
function set( v ) {
251-
tuple[ indices[ i ] ] = v;
252+
tuple[ indices.indexOf( i ) ] = v;
252253
}
253254
}
254255

@@ -1040,6 +1041,7 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
10401041
*/
10411042
function sort( compareFunction ) {
10421043
var clbk;
1044+
var idx;
10431045
var tmp;
10441046
var i;
10451047
var j;
@@ -1056,6 +1058,10 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
10561058
} else {
10571059
clbk = ascending;
10581060
}
1061+
// Create a copy of the list of indices so that we can access the original sort order when invoking the comparison function:
1062+
idx = indices.slice();
1063+
1064+
// Sort the list of indices:
10591065
indices.sort( wrapper );
10601066

10611067
// Create a temporary indices array which we'll reorder as we rearrange the tuple elements:
@@ -1091,8 +1097,8 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
10911097
* @returns {*} value specifying the sort order
10921098
*/
10931099
function wrapper( ia, ib ) {
1094-
var a = tuple[ indices[ ia ] ];
1095-
var b = tuple[ indices[ ib ] ];
1100+
var a = tuple[ idx[ ia ] ];
1101+
var b = tuple[ idx[ ib ] ];
10961102
return clbk( a, b );
10971103
}
10981104
}

0 commit comments

Comments
 (0)