Skip to content

Commit 5d9761c

Browse files
Gauravkaushik-1206kgrytestdlib-bot
authored
feat: implement toLocaleString method in dstructs/named-typed-tuple
PR-URL: #8009 Closes: #6976 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 11476bc commit 5d9761c

File tree

4 files changed

+201
-5
lines changed

4 files changed

+201
-5
lines changed

lib/node_modules/@stdlib/dstructs/named-typed-tuple/docs/repl.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,10 +1378,11 @@ tuple.toLocaleString( [locales[, options]] )
13781378

13791379
Examples
13801380
--------
1381-
> var factory = {{alias}}( [ 'x', 'y', 'z' ] );
1381+
> var opts = { 'name': 'Point' };
1382+
> var factory = {{alias}}( [ 'x', 'y', 'z' ], opts );
13821383
> var p = factory( [ 1.0, -1.0, 0.0 ], 'int32' );
13831384
> p.toLocaleString()
1384-
'1,-1,0'
1385+
'Point(x=1, y=-1, z=0)'
13851386

13861387

13871388
tuple.toString()
@@ -1394,7 +1395,7 @@ tuple.toString()
13941395

13951396
Examples
13961397
--------
1397-
> opts = { 'name': 'Point' };
1398+
> var opts = { 'name': 'Point' };
13981399
> var factory = {{alias}}( [ 'x', 'y', 'z' ], opts );
13991400
> var p = factory( [ 1.0, -1.0, 0.0 ] );
14001401
> p.toString()

lib/node_modules/@stdlib/dstructs/named-typed-tuple/docs/types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,7 @@ interface Tuple {
12441244
* var tuple = factory( [ 1.0, 0.0, -1.0 ], 'int32' );
12451245
*
12461246
* var str = tuple.toLocaleString();
1247-
* // returns '1,0,-1'
1247+
* // returns 'tuple(x=1, y=0, z=-1)'
12481248
*/
12491249
toLocaleString( locales: string | Array<string>, options?: any ): string;
12501250

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

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
204204
setNonEnumerableProperty( tuple, 'sort', sort );
205205
setNonEnumerableProperty( tuple, 'subtuple', subtuple );
206206
setNonEnumerableProperty( tuple, 'toJSON', toJSON );
207+
setNonEnumerableProperty( tuple, 'toLocaleString', toLocaleString );
207208
setNonEnumerableProperty( tuple, 'toString', toString );
208209

209210
return tuple;
@@ -1198,7 +1199,53 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
11981199
return out;
11991200
}
12001201

1201-
// TODO: consider adding `toLocaleString()` in a manner similar to `toString()` below
1202+
/**
1203+
* Serializes a tuple as a locale-specific string.
1204+
*
1205+
* @private
1206+
* @memberof tuple
1207+
* @param {(string|Array<string>)} [locales] - locale identifier(s)
1208+
* @param {Object} [options] - configuration options
1209+
* @throws {TypeError} `this` must be the host tuple
1210+
* @throws {TypeError} first argument must be a string or an array of strings
1211+
* @throws {TypeError} options argument must be an object
1212+
* @returns {string} string representation
1213+
*/
1214+
function toLocaleString( locales, options ) {
1215+
var loc;
1216+
var out;
1217+
var o;
1218+
var i;
1219+
1220+
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
1221+
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
1222+
}
1223+
if ( arguments.length === 0 ) {
1224+
loc = [];
1225+
} else if ( isString( locales ) || isStringArray( locales ) ) {
1226+
loc = locales;
1227+
} else {
1228+
throw new TypeError( format( 'invalid argument. First argument must be a string or an array of strings. Value: `%s`.', locales ) );
1229+
}
1230+
if ( arguments.length < 2 ) {
1231+
o = {};
1232+
} else if ( isObject( options ) ) {
1233+
o = options;
1234+
} else {
1235+
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
1236+
}
1237+
out = opts.name.toLocaleString( loc, o ) + '(';
1238+
for ( i = 0; i < nfields; i++ ) {
1239+
out += fields[ i ].toLocaleString( loc, o );
1240+
out += '=';
1241+
out += tuple[ indices[ i ] ].toLocaleString( loc, o );
1242+
if ( i < nfields-1 ) {
1243+
out += ', ';
1244+
}
1245+
}
1246+
out += ')';
1247+
return out;
1248+
}
12021249

12031250
/**
12041251
* Serializes a tuple as a string.
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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+
// 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 namedtypedtuple = 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 namedtypedtuple, 'function', 'main export is a function' );
34+
t.end();
35+
});
36+
37+
tape( 'a tuple has a `toLocaleString` method', function test( t ) {
38+
var Point;
39+
var p;
40+
41+
Point = namedtypedtuple( [ 'x', 'y' ] );
42+
p = new Point();
43+
44+
t.strictEqual( hasProp( p, 'toLocaleString' ), true, 'returns expected value' );
45+
t.strictEqual( isFunction( p.toLocaleString ), true, 'returns expected value' );
46+
t.end();
47+
});
48+
49+
tape( 'the method throws an error if invoked with a `this` context which is not a tuple instance', function test( t ) {
50+
var values;
51+
var Point;
52+
var p;
53+
var i;
54+
55+
Point = namedtypedtuple( [ 'x', 'y' ] );
56+
p = new Point();
57+
58+
values = [
59+
'5',
60+
5,
61+
NaN,
62+
true,
63+
false,
64+
null,
65+
void 0,
66+
{},
67+
[]
68+
];
69+
for ( i = 0; i < values.length; i++ ) {
70+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
71+
}
72+
t.end();
73+
74+
function badValue( value ) {
75+
return function badValue() {
76+
return p.toLocaleString.call( value );
77+
};
78+
}
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 = namedtypedtuple( [ 'price', 'quantity' ] );
88+
p = new Point( [ 123456.789, 9876 ] );
89+
90+
expected = 'tuple(price=' + (123456.789).toLocaleString() + ', quantity=' + (9876).toLocaleString() + ')';
91+
actual = p.toLocaleString();
92+
93+
t.strictEqual( actual, expected, 'returns expected value' );
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 = namedtypedtuple( [ 'price', 'quantity' ] );
104+
p = new Point( [ 123456.789, 9876 ] );
105+
106+
expected = 'tuple(price=123.456,789, quantity=9.876)';
107+
actual = p.toLocaleString( 'de-DE' );
108+
109+
t.strictEqual( actual, expected, 'returns expected value' );
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 = namedtypedtuple( [ 'price', 'quantity' ] );
121+
p = new Point( [ 1234.56, 50 ] );
122+
opts = {
123+
'style': 'currency',
124+
'currency': 'USD'
125+
};
126+
127+
expected = 'tuple(price=' + (1234.56).toLocaleString( 'en-US', opts ) + ', quantity=' + (50).toLocaleString( 'en-US', opts ) + ')';
128+
actual = p.toLocaleString( 'en-US', opts );
129+
130+
t.strictEqual( actual, expected, 'returns expected value' );
131+
t.end();
132+
});
133+
134+
tape( 'the method handles NaN values', function test( t ) {
135+
var expected;
136+
var Record;
137+
var actual;
138+
var r;
139+
140+
Record = namedtypedtuple( [ 'isValid', 'value' ] );
141+
r = new Record( [ 1, NaN ] );
142+
143+
expected = 'tuple(isValid=1, value=NaN)';
144+
actual = r.toLocaleString();
145+
146+
t.strictEqual( actual, expected, 'returns expected value' );
147+
t.end();
148+
});

0 commit comments

Comments
 (0)