Skip to content

Commit eab49ad

Browse files
committed
fix: ensure support for elements which are null or undefined
--- 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: passed - 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 16607df commit eab49ad

File tree

2 files changed

+91
-3
lines changed

2 files changed

+91
-3
lines changed

lib/node_modules/@stdlib/ndarray/base/ctor/lib/tolocalestring.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
var isComplexDataType = require( '@stdlib/ndarray/base/assert/is-complex-floating-point-data-type' );
2424
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
2525
var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives;
26+
var isUndefinedOrNull = require( '@stdlib/assert/is-undefined-or-null' );
2627
var isObject = require( '@stdlib/assert/is-plain-object' );
2728
var format = require( '@stdlib/string/format' );
2829
var replace = require( '@stdlib/string/replace' );
@@ -114,7 +115,12 @@ function toLocaleString( locales, options ) { // eslint-disable-line stdlib/no-r
114115
}
115116
} else {
116117
for ( i = 0; i < this._length; i++ ) {
117-
buffer += this.iget( i ).toLocaleString( loc, opts );
118+
v = this.iget( i );
119+
if ( !isUndefinedOrNull( v ) && v.toLocaleString ) {
120+
buffer += v.toLocaleString( loc, opts );
121+
} else {
122+
buffer += String( v );
123+
}
118124
if ( i < this._length-1 ) {
119125
buffer += ', ';
120126
}
@@ -132,7 +138,12 @@ function toLocaleString( locales, options ) { // eslint-disable-line stdlib/no-r
132138
}
133139
} else {
134140
for ( i = 0; i < 3; i++ ) {
135-
buffer += this.iget( i ).toLocaleString( loc, opts );
141+
v = this.iget( i );
142+
if ( !isUndefinedOrNull( v ) && v.toLocaleString ) {
143+
buffer += v.toLocaleString( loc, opts );
144+
} else {
145+
buffer += String( v );
146+
}
136147
if ( i < 2 ) {
137148
buffer += ', ';
138149
}
@@ -151,7 +162,12 @@ function toLocaleString( locales, options ) { // eslint-disable-line stdlib/no-r
151162
}
152163
} else {
153164
for ( i = 2; i >= 0; i-- ) {
154-
buffer += this.iget( this._length-1-i ).toLocaleString( loc, opts ); // eslint-disable-line max-len
165+
v = this.iget( this._length-1-i );
166+
if ( !isUndefinedOrNull( v ) && v.toLocaleString ) {
167+
buffer += v.toLocaleString( loc, opts );
168+
} else {
169+
buffer += String( v );
170+
}
155171
if ( i > 0 ) {
156172
buffer += ', ';
157173
}

lib/node_modules/@stdlib/ndarray/base/ctor/test/test.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3810,6 +3810,37 @@ tape( 'an ndarray has a custom `toLocaleString()` method (boolean type)', functi
38103810
t.end();
38113811
});
38123812

3813+
tape( 'an ndarray has a custom `toLocaleString()` method (generic type)', function test( t ) {
3814+
var expected;
3815+
var strides;
3816+
var actual;
3817+
var buffer;
3818+
var offset;
3819+
var dtype;
3820+
var order;
3821+
var shape;
3822+
var arr;
3823+
3824+
dtype = 'generic';
3825+
buffer = [ null, void 0, null, void 0 ];
3826+
shape = [ 2, 2 ];
3827+
order = 'row-major';
3828+
strides = [ 2, 1 ];
3829+
offset = 0;
3830+
3831+
arr = ndarray( dtype, buffer, shape, strides, offset, order );
3832+
3833+
t.strictEqual( hasOwnProp( arr, 'toLocaleString' ), false, 'does not have own property' );
3834+
t.strictEqual( hasProp( arr, 'toLocaleString' ), true, 'has property' );
3835+
t.strictEqual( isFunction( arr.toLocaleString ), true, 'has method' );
3836+
3837+
expected = 'ndarray( \'generic\', [ null, undefined, null, undefined ], [ 2, 2 ], [ 2, 1 ], 0, \'row-major\' )';
3838+
actual = arr.toLocaleString();
3839+
t.strictEqual( actual, expected, 'returns expected value' );
3840+
3841+
t.end();
3842+
});
3843+
38133844
tape( 'an ndarray has a custom `toLocaleString()` method (0d)', function test( t ) {
38143845
var expected;
38153846
var strides;
@@ -3964,6 +3995,47 @@ tape( 'an ndarray has a custom `toLocaleString()` method (large array; complex t
39643995
t.end();
39653996
});
39663997

3998+
tape( 'an ndarray has a custom `toLocaleString()` method (large array; generic type)', function test( t ) {
3999+
var expected;
4000+
var strides;
4001+
var actual;
4002+
var buffer;
4003+
var offset;
4004+
var dtype;
4005+
var order;
4006+
var shape;
4007+
var arr;
4008+
var i;
4009+
4010+
dtype = 'generic';
4011+
buffer = [];
4012+
for ( i = 0; i < 10000; i++ ) {
4013+
if ( i < 3 ) {
4014+
buffer.push( null );
4015+
} else if ( i >= 9997 ) {
4016+
buffer.push( void 0 );
4017+
} else {
4018+
buffer.push( 0 );
4019+
}
4020+
}
4021+
shape = [ 10000 ];
4022+
order = 'row-major';
4023+
strides = [ 1 ];
4024+
offset = 0;
4025+
4026+
arr = ndarray( dtype, buffer, shape, strides, offset, order );
4027+
4028+
t.strictEqual( hasOwnProp( arr, 'toLocaleString' ), false, 'does not have own property' );
4029+
t.strictEqual( hasProp( arr, 'toLocaleString' ), true, 'has property' );
4030+
t.strictEqual( isFunction( arr.toLocaleString ), true, 'has method' );
4031+
4032+
expected = 'ndarray( \'generic\', [ null, null, null, ..., undefined, undefined, undefined ], [ 10000 ], [ 1 ], 0, \'row-major\' )';
4033+
actual = arr.toLocaleString();
4034+
t.strictEqual( actual, expected, 'returns expected value' );
4035+
4036+
t.end();
4037+
});
4038+
39674039
tape( 'an ndarray has a custom `toLocaleString()` method (locale, options)', function test( t ) {
39684040
var expected;
39694041
var strides;

0 commit comments

Comments
 (0)