Skip to content

Commit 65c9403

Browse files
feat: add argument validation to toLocaleString
--- 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent d86f3ef commit 65c9403

File tree

2 files changed

+225
-6
lines changed

2 files changed

+225
-6
lines changed

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,19 +1210,34 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
12101210
* @returns {string} tuple locale string representation
12111211
*/
12121212
function toLocaleString( locales, options ) {
1213-
var out = [];
1213+
var opts;
1214+
var loc;
1215+
var out;
12141216
var val;
12151217
var i;
12161218
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
12171219
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
12181220
}
1221+
1222+
if ( arguments.length === 0 ) {
1223+
loc = [];
1224+
} else if ( isString( locales ) || isStringArray( locales ) ) {
1225+
loc = locales;
1226+
} else {
1227+
throw new TypeError( format( 'invalid argument. First argument must be a string or an array of strings. Value: `%s`.', locales ) );
1228+
}
1229+
1230+
if ( arguments.length < 2 ) {
1231+
opts = {};
1232+
} else if ( isObject( options ) ) {
1233+
opts = options;
1234+
} else {
1235+
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
1236+
}
1237+
out = [];
12191238
for ( i = 0; i < nfields; i++ ) {
12201239
val = tuple[ indices[ i ] ];
1221-
if ( val && typeof val.toLocaleString === 'function' ) {
1222-
out.push( val.toLocaleString( locales, options ) );
1223-
} else {
1224-
out.push( String( val ) );
1225-
}
1240+
out.push( val.toLocaleString( loc, opts ) );
12261241
}
12271242
return out.join(',');
12281243
}
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 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 tape = require( 'tape' );
24+
var hasProp = require( '@stdlib/assert/has-property' );
25+
var isFunction = require( '@stdlib/assert/is-function' );
26+
var namedtypetuple = require( './../lib' );
27+
28+
29+
// TESTS //
30+
31+
tape( 'main export is a function', function test( t ) {
32+
t.ok( true, __filename );
33+
t.strictEqual( typeof namedtypetuple, 'function', 'main export is a function' );
34+
t.end();
35+
});
36+
37+
tape( 'the method throws an error if invoked with a `this` context which is not a tuple instance', function test( t ) {
38+
var values;
39+
var Point;
40+
var p;
41+
var i;
42+
43+
Point = namedtypetuple( [ 'x', 'y' ] );
44+
p = new Point();
45+
46+
values = [
47+
'5',
48+
5,
49+
NaN,
50+
true,
51+
false,
52+
null,
53+
void 0,
54+
{},
55+
[]
56+
];
57+
for ( i = 0; i < values.length; i++ ) {
58+
t.throws( badValue( values[i] ), TypeError, 'throws a TypeError when provided '+values[i] );
59+
}
60+
t.end();
61+
62+
function badValue( value ) {
63+
return function badValue() {
64+
return p.toLocaleString.call( value );
65+
};
66+
}
67+
});
68+
69+
tape( 'a tuple has a `toLocaleString` method', function test( t ) {
70+
var Point;
71+
var p;
72+
73+
Point = namedtypetuple( [ 'x', 'y' ] );
74+
p = new Point();
75+
76+
t.strictEqual( hasProp( p, 'toLocaleString' ), true, 'has property' );
77+
t.strictEqual( isFunction( p.toLocaleString ), true, 'is a function' );
78+
t.end();
79+
});
80+
81+
tape( 'the method serializes a tuple as a locale-specific string (default locale)', function test( t ) {
82+
var expected;
83+
var actual;
84+
var Point;
85+
var p;
86+
87+
Point = namedtypetuple( [ 'price', 'quantity' ] );
88+
p = new Point( [ 123456.789, 9876 ] );
89+
90+
expected = [ (123456.789).toLocaleString(), (9876).toLocaleString() ].join( ',' );
91+
actual = p.toLocaleString();
92+
93+
t.strictEqual( actual, expected, 'returns expected string' );
94+
t.end();
95+
});
96+
97+
tape( 'the method serializes a tuple as a locale-specific string (specified locale)', function test( t ) {
98+
var expected;
99+
var actual;
100+
var Point;
101+
var p;
102+
103+
Point = namedtypetuple( [ 'price', 'quantity' ] );
104+
p = new Point( [ 123456.789, 9876 ] );
105+
106+
expected = [ '123.456,789', '9.876' ].join( ',' );
107+
actual = p.toLocaleString( 'de-DE' );
108+
109+
t.strictEqual( actual, expected, 'returns expected string for German locale' );
110+
t.end();
111+
});
112+
113+
tape( 'the method serializes a tuple as a locale-specific string (locale and options)', function test( t ) {
114+
var expected;
115+
var actual;
116+
var Point;
117+
var opts;
118+
var p;
119+
120+
Point = namedtypetuple( [ 'price', 'quantity' ] );
121+
p = new Point( [ 1234.56, 50 ] );
122+
opts = {
123+
'style': 'currency',
124+
'currency': 'USD'
125+
};
126+
127+
expected = [ (1234.56).toLocaleString( 'en-US', opts ), (50).toLocaleString( 'en-US', opts ) ].join( ',' );
128+
actual = p.toLocaleString( 'en-US', opts );
129+
130+
t.strictEqual( actual, expected, 'returns expected string for currency format' );
131+
t.end();
132+
});
133+
134+
tape( 'the method handles null and undefined values using String()', function test( t ) {
135+
var expected;
136+
var actual;
137+
var Data;
138+
var d;
139+
140+
Data = namedtypetuple( [ 'value', 'notes', 'status' ] );
141+
d = new Data( [ 1000, null, void 0 ] );
142+
143+
expected = [ (1000).toLocaleString(), '0', 'NaN' ].join( ',' );
144+
actual = d.toLocaleString();
145+
146+
t.strictEqual( actual, expected, 'returns expected string with null and undefined' );
147+
t.end();
148+
});
149+
150+
tape( 'the method serializes a tuple containing a Date object', function test( t ) {
151+
var expected;
152+
var actual;
153+
var Event;
154+
var date;
155+
var e;
156+
157+
Event = namedtypetuple( [ 'id', 'timestamp' ] );
158+
date = new Date( '2025-09-02T18:30:00.000Z' );
159+
e = new Event( [ 123, date ] );
160+
161+
expected = [ (123).toLocaleString(), date.getTime().toLocaleString() ].join(',');
162+
actual = e.toLocaleString();
163+
164+
t.strictEqual( actual, expected, 'calls toLocaleString on the Date object' );
165+
t.end();
166+
});
167+
168+
tape( 'the method handles boolean and NaN values', function test( t ) {
169+
var expected;
170+
var Record;
171+
var actual;
172+
var r;
173+
174+
Record = namedtypetuple( [ 'isValid', 'value' ] );
175+
r = new Record( [ true, NaN ] );
176+
177+
expected = [ '1', 'NaN' ].join(',');
178+
actual = r.toLocaleString();
179+
180+
t.strictEqual( actual, expected, 'handles boolean and NaN values' );
181+
t.end();
182+
});
183+
184+
tape( 'the method serializes a tuple containing an object with a custom toLocaleString', function test( t ) {
185+
var customObject;
186+
var expected;
187+
var Custom;
188+
var actual;
189+
var c;
190+
191+
Custom = namedtypetuple( [ 'item' ] );
192+
customObject = {
193+
'toLocaleString': function customLocaleString() {
194+
return 'CUSTOM_FORMAT';
195+
}
196+
};
197+
c = new Custom( [ customObject ] );
198+
199+
expected = 'NaN';
200+
actual = c.toLocaleString();
201+
202+
t.strictEqual( actual, expected, 'uses the custom toLocaleString method from the object' );
203+
t.end();
204+
});

0 commit comments

Comments
 (0)