Skip to content
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
fd9a4f3
feat(named-typed-tuple): implement toLocaleString function
Gauravkaushik-1206 Sep 3, 2025
6591629
Discard changes to lib/node_modules/@stdlib/dstructs/named-typed-tupl…
kgryte Sep 3, 2025
2075abe
fix: update `fromIndex` handling in `blas/ext/base/ndarray/slast-inde…
headlessNode Sep 3, 2025
d86f3ef
fix: update `fromIndex` handling in `blas/ext/base/ndarray/glast-inde…
headlessNode Sep 3, 2025
65c9403
feat: add argument validation to toLocaleString
Gauravkaushik-1206 Sep 3, 2025
09c587f
feat: changed copyright year to 2025
Gauravkaushik-1206 Sep 5, 2025
e63aa17
Discard changes to lib/node_modules/@stdlib/blas/ext/base/ndarray/gla…
kgryte Sep 7, 2025
c4eda5f
Discard changes to lib/node_modules/@stdlib/blas/ext/base/ndarray/gla…
kgryte Sep 7, 2025
f97d9f8
Discard changes to lib/node_modules/@stdlib/blas/ext/base/ndarray/gla…
kgryte Sep 7, 2025
0c57f34
Discard changes to lib/node_modules/@stdlib/blas/ext/base/ndarray/gla…
kgryte Sep 7, 2025
71ef02d
Discard changes to lib/node_modules/@stdlib/blas/ext/base/ndarray/gla…
kgryte Sep 7, 2025
5ab6214
Discard changes to lib/node_modules/@stdlib/blas/ext/base/ndarray/gla…
kgryte Sep 7, 2025
27dfeaa
Discard changes to lib/node_modules/@stdlib/blas/ext/base/ndarray/gla…
kgryte Sep 7, 2025
ba670f7
Discard changes to lib/node_modules/@stdlib/blas/ext/base/ndarray/sla…
kgryte Sep 7, 2025
8951632
Discard changes to lib/node_modules/@stdlib/blas/ext/base/ndarray/sla…
kgryte Sep 7, 2025
9f98254
Discard changes to lib/node_modules/@stdlib/blas/ext/base/ndarray/sla…
kgryte Sep 7, 2025
30c2f33
Discard changes to lib/node_modules/@stdlib/blas/ext/base/ndarray/sla…
kgryte Sep 7, 2025
f894a17
Discard changes to lib/node_modules/@stdlib/blas/ext/base/ndarray/sla…
kgryte Sep 7, 2025
28ed5ca
Discard changes to lib/node_modules/@stdlib/blas/ext/base/ndarray/sla…
kgryte Sep 7, 2025
fbcae92
Discard changes to lib/node_modules/@stdlib/blas/ext/base/ndarray/sla…
kgryte Sep 7, 2025
5eb4b37
Merge remote-tracking branch 'upstream/develop' into fix-tolocale-string
stdlib-bot Sep 7, 2025
23b2e2b
Apply suggestions from code review
kgryte Sep 7, 2025
b38c3a9
fix: change to custom serialization instead of array-style serialization
Gauravkaushik-1206 Sep 7, 2025
8f2650c
docs: fix missing declaration
kgryte Sep 7, 2025
7fa7688
docs: fix missing declaration
kgryte Sep 7, 2025
15c46bf
refactor: rename variable
kgryte Sep 7, 2025
ee7116d
test: update description
kgryte Sep 8, 2025
0fde4ab
test: remove test
kgryte Sep 8, 2025
95fdf7f
test: remove tests
kgryte Sep 8, 2025
1d807f3
test: update description
kgryte Sep 8, 2025
dbce613
test: update test value
kgryte Sep 8, 2025
272428a
test: rename variable and update test messages
kgryte Sep 8, 2025
b4a0d74
test: reorder tests
kgryte Sep 8, 2025
17daba7
test: update message
kgryte Sep 8, 2025
885162e
Merge remote-tracking branch 'upstream/develop' into fix-tolocale-string
stdlib-bot Sep 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1378,10 +1378,11 @@ tuple.toLocaleString( [locales[, options]] )

Examples
--------
> var factory = {{alias}}( [ 'x', 'y', 'z' ] );
> var opts = { 'name': 'Point' };
> var factory = {{alias}}( [ 'x', 'y', 'z' ], opts );
> var p = factory( [ 1.0, -1.0, 0.0 ], 'int32' );
> p.toLocaleString()
'1,-1,0'
'Point(x=1, y=-1, z=0)'


tuple.toString()
Expand All @@ -1394,7 +1395,7 @@ tuple.toString()

Examples
--------
> opts = { 'name': 'Point' };
> var opts = { 'name': 'Point' };
> var factory = {{alias}}( [ 'x', 'y', 'z' ], opts );
> var p = factory( [ 1.0, -1.0, 0.0 ] );
> p.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
* @param field - tuple field name
* @param tuple - tuple on which the method is invoked
*/
type Quaternary = ( value: any, index: number, field: string, tuple: Tuple ) => void;

Check warning on line 85 in lib/node_modules/@stdlib/dstructs/named-typed-tuple/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

/**
* Function invoked for each tuple element.
Expand Down Expand Up @@ -137,7 +137,7 @@
* @param tuple - tuple on which the method is invoked
* @returns boolean indicating whether tuple element passes a test
*/
type QuaternaryPredicate = ( value: any, index: number, field: string, tuple: Tuple ) => boolean;

Check warning on line 140 in lib/node_modules/@stdlib/dstructs/named-typed-tuple/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

/**
* Predicate function which tests tuple elements.
Expand All @@ -155,7 +155,7 @@
*
* @returns value assigned to the accumulator
*/
type NullaryReducer = () => any;

Check warning on line 158 in lib/node_modules/@stdlib/dstructs/named-typed-tuple/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

/**
* Function applied against an accumulator.
Expand All @@ -163,7 +163,7 @@
* @param acc - accumulated result
* @returns value assigned to the accumulator
*/
type UnaryReducer = ( acc: any ) => any;

Check warning on line 166 in lib/node_modules/@stdlib/dstructs/named-typed-tuple/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

Check warning on line 166 in lib/node_modules/@stdlib/dstructs/named-typed-tuple/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

/**
* Function applied against an accumulator.
Expand All @@ -172,7 +172,7 @@
* @param value - tuple element
* @returns value assigned to the accumulator
*/
type BinaryReducer = ( acc: any, value: number ) => any;

Check warning on line 175 in lib/node_modules/@stdlib/dstructs/named-typed-tuple/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

Check warning on line 175 in lib/node_modules/@stdlib/dstructs/named-typed-tuple/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

/**
* Function applied against an accumulator.
Expand All @@ -182,7 +182,7 @@
* @param index - tuple index
* @returns value assigned to the accumulator
*/
type TernaryReducer = ( acc: any, value: number, index: number ) => any;

Check warning on line 185 in lib/node_modules/@stdlib/dstructs/named-typed-tuple/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

Check warning on line 185 in lib/node_modules/@stdlib/dstructs/named-typed-tuple/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

/**
* Function applied against an accumulator.
Expand All @@ -193,7 +193,7 @@
* @param field - tuple field name
* @returns value assigned to the accumulator
*/
type QuaternaryReducer = ( acc: any, value: number, index: number, field: string ) => any;

Check warning on line 196 in lib/node_modules/@stdlib/dstructs/named-typed-tuple/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

/**
* Function applied against an accumulator.
Expand Down Expand Up @@ -1244,7 +1244,7 @@
* var tuple = factory( [ 1.0, 0.0, -1.0 ], 'int32' );
*
* var str = tuple.toLocaleString();
* // returns '1,0,-1'
* // returns 'tuple(x=1, y=0, z=-1)'
*/
toLocaleString( locales: string | Array<string>, options?: any ): string;

Expand Down
49 changes: 48 additions & 1 deletion lib/node_modules/@stdlib/dstructs/named-typed-tuple/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
setNonEnumerableProperty( tuple, 'sort', sort );
setNonEnumerableProperty( tuple, 'subtuple', subtuple );
setNonEnumerableProperty( tuple, 'toJSON', toJSON );
setNonEnumerableProperty( tuple, 'toLocaleString', toLocaleString );
setNonEnumerableProperty( tuple, 'toString', toString );

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

// TODO: consider adding `toLocaleString()` in a manner similar to `toString()` below
/**
* Serializes a tuple as a locale-specific string.
*
* @private
* @memberof tuple
* @param {(string|Array<string>)} [locales] - locale identifier(s)
* @param {Object} [options] - configuration options
* @throws {TypeError} `this` must be the host tuple
* @throws {TypeError} first argument must be a string or an array of strings
* @throws {TypeError} options argument must be an object
* @returns {string} string representation
*/
function toLocaleString( locales, options ) {
var loc;
var out;
var o;
var i;

if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
if ( arguments.length === 0 ) {
loc = [];
} else if ( isString( locales ) || isStringArray( locales ) ) {
loc = locales;
} else {
throw new TypeError( format( 'invalid argument. First argument must be a string or an array of strings. Value: `%s`.', locales ) );
}
if ( arguments.length < 2 ) {
o = {};
} else if ( isObject( options ) ) {
o = options;
} else {
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
}
out = opts.name.toLocaleString( loc, o ) + '(';
for ( i = 0; i < nfields; i++ ) {
out += fields[ i ].toLocaleString( loc, o );
out += '=';
out += tuple[ indices[ i ] ].toLocaleString( loc, o );
if ( i < nfields-1 ) {
out += ', ';
}
}
out += ')';
return out;
}

/**
* Serializes a tuple as a string.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var tape = require( 'tape' );
var hasProp = require( '@stdlib/assert/has-property' );
var isFunction = require( '@stdlib/assert/is-function' );
var namedtypetuple = require( './../lib' );


// TESTS //

tape( 'main export is a function', function test( t ) {
t.ok( true, __filename );
t.strictEqual( typeof namedtypetuple, 'function', 'main export is a function' );
t.end();
});

tape( 'the method throws an error if invoked with a `this` context which is not a tuple instance', function test( t ) {
var values;
var Point;
var p;
var i;

Point = namedtypetuple( [ 'x', 'y' ] );
p = new Point();

values = [
'5',
5,
NaN,
true,
false,
null,
void 0,
{},
[]
];
for ( i = 0; i < values.length; i++ ) {
t.throws( badValue( values[i] ), TypeError, 'throws a TypeError when provided '+values[i] );
}
t.end();

function badValue( value ) {
return function badValue() {
return p.toLocaleString.call( value );
};
}
});

tape( 'a tuple has a `toLocaleString` method', function test( t ) {
var Point;
var p;

Point = namedtypetuple( [ 'x', 'y' ] );
p = new Point();

t.strictEqual( hasProp( p, 'toLocaleString' ), true, 'has property' );
t.strictEqual( isFunction( p.toLocaleString ), true, 'is a function' );
t.end();
});

tape( 'the method serializes a tuple as a locale-specific string (default locale)', function test( t ) {
var expected;
var actual;
var Point;
var p;

Point = namedtypetuple( [ 'price', 'quantity' ] );
p = new Point( [ 123456.789, 9876 ] );

expected = 'tuple(price=' + (123456.789).toLocaleString() + ', quantity=' + (9876).toLocaleString() + ')';
actual = p.toLocaleString();

t.strictEqual( actual, expected, 'returns expected string' );
t.end();
});

tape( 'the method serializes a tuple as a locale-specific string (specified locale)', function test( t ) {
var expected;
var actual;
var Point;
var p;

Point = namedtypetuple( [ 'price', 'quantity' ] );
p = new Point( [ 123456.789, 9876 ] );

expected = 'tuple(price=123.456,789, quantity=9.876)';
actual = p.toLocaleString( 'de-DE' );

t.strictEqual( actual, expected, 'returns expected string for German locale' );
t.end();
});

tape( 'the method serializes a tuple as a locale-specific string (locale and options)', function test( t ) {
var expected;
var actual;
var Point;
var opts;
var p;

Point = namedtypetuple( [ 'price', 'quantity' ] );
p = new Point( [ 1234.56, 50 ] );
opts = {
'style': 'currency',
'currency': 'USD'
};

expected = 'tuple(price=' + (1234.56).toLocaleString( 'en-US', opts ) + ', quantity=' + (50).toLocaleString( 'en-US', opts ) + ')';
actual = p.toLocaleString( 'en-US', opts );

t.strictEqual( actual, expected, 'returns expected string for currency format' );
t.end();
});

tape( 'the method handles boolean and NaN values', function test( t ) {
var expected;
var Record;
var actual;
var r;

Record = namedtypetuple( [ 'isValid', 'value' ] );
r = new Record( [ true, NaN ] );

expected = 'tuple(isValid=1, value=NaN)';
actual = r.toLocaleString();

t.strictEqual( actual, expected, 'handles boolean and NaN values' );
t.end();
});

Loading