Skip to content

Commit 2833fcd

Browse files
feat: add logic, tests, and benchmarks for toLocaleString method
1 parent 37e85cf commit 2833fcd

File tree

4 files changed

+408
-0
lines changed

4 files changed

+408
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 bench = require( '@stdlib/bench' );
24+
var pkg = require( './../package.json' ).name;
25+
var factory = require( './../lib' );
26+
27+
28+
// VARIABLES //
29+
30+
var Float64ArrayFE = factory( 'float64' );
31+
32+
33+
// MAIN //
34+
35+
bench( pkg+':toLocaleString', function benchmark( b ) {
36+
var out;
37+
var arr;
38+
var i;
39+
40+
arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 2.0, 1.0 ] );
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
out = arr.toLocaleString();
45+
if ( typeof out !== 'string' ) {
46+
b.fail( 'should return a string' );
47+
}
48+
}
49+
b.toc();
50+
if ( typeof out !== 'string' ) {
51+
b.fail( 'should return a string' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 bench = require( '@stdlib/bench' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var zeroTo = require( '@stdlib/array/zero-to' );
26+
var pkg = require( './../package.json' ).name;
27+
var factory = require( './../lib' );
28+
29+
30+
// VARIABLES //
31+
32+
var Float64ArrayFE = factory( 'float64' );
33+
34+
35+
// FUNCTIONS //
36+
37+
/**
38+
* Creates a benchmark function.
39+
*
40+
* @private
41+
* @param {PositiveInteger} len - array length
42+
* @returns {Function} benchmark function
43+
*/
44+
function createBenchmark( len ) {
45+
var arr = new Float64ArrayFE( 'little-endian', zeroTo( len ) );
46+
return benchmark;
47+
48+
/**
49+
* Benchmark function.
50+
*
51+
* @private
52+
* @param {Benchmark} b - benchmark instance
53+
*/
54+
function benchmark( b ) {
55+
var out;
56+
var i;
57+
58+
b.tic();
59+
for ( i = 0; i < b.iterations; i++ ) {
60+
out = arr.toLocaleString();
61+
if ( typeof out !== 'string' ) {
62+
b.fail( 'should return a string' );
63+
}
64+
}
65+
b.toc();
66+
if ( typeof out !== 'string' ) {
67+
b.fail( 'should return a string' );
68+
}
69+
b.pass( 'benchmark finished' );
70+
b.end();
71+
}
72+
}
73+
74+
75+
// MAIN //
76+
77+
/**
78+
* Main execution sequence.
79+
*
80+
* @private
81+
*/
82+
function main() {
83+
var len;
84+
var min;
85+
var max;
86+
var f;
87+
var i;
88+
89+
min = 1; // 10^min
90+
max = 6; // 10^max
91+
92+
for ( i = min; i <= max; i++ ) {
93+
len = pow( 10, i );
94+
f = createBenchmark( len );
95+
bench( pkg+':toLocaleString:len='+len, f );
96+
}
97+
}
98+
99+
main();

lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' );
2929
var isObject = require( '@stdlib/assert/is-object' );
3030
var isFunction = require( '@stdlib/assert/is-function' );
3131
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
32+
var isStringArray = require( '@stdlib/assert/is-string-array' );
3233
var isByteOrder = require( '@stdlib/array/base/assert/is-byte-order' );
3334
var lowercase = require( '@stdlib/string/base/lowercase' );
3435
var hasIteratorSymbolSupport = require( '@stdlib/assert/has-iterator-symbol-support' );
@@ -1039,6 +1040,54 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
10391040
return out.join( ',' );
10401041
});
10411042

1043+
/**
1044+
* Serializes an array as a locale-specific string.
1045+
*
1046+
* @private
1047+
* @name toLocaleString
1048+
* @memberof TypedArray.prototype
1049+
* @type {Function}
1050+
* @param {(string|Array<string>)} [locales] - locale identifier(s)
1051+
* @param {Object} [options] - configuration options
1052+
* @throws {TypeError} `this` must be a typed array
1053+
* @throws {TypeError} first argument must be a string or an array of strings
1054+
* @throws {TypeError} options argument must be an object
1055+
* @returns {string} string representation
1056+
*
1057+
*/
1058+
setReadOnly( TypedArray.prototype, 'toLocaleString', function toLocaleString( locales, options ) {
1059+
var opts;
1060+
var loc;
1061+
var out;
1062+
var buf;
1063+
var i;
1064+
1065+
if ( !isTypedArray( this ) ) {
1066+
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
1067+
}
1068+
1069+
if ( arguments.length === 0 ) {
1070+
loc = [];
1071+
} else if ( isString( locales ) || isStringArray( locales ) ) {
1072+
loc = locales;
1073+
} else {
1074+
throw new TypeError( format( 'invalid argument. First argument must be a string or an array of strings. Value: `%s`.', locales ) );
1075+
}
1076+
if ( arguments.length < 2 ) {
1077+
opts = {};
1078+
} else if ( isObject( options ) ) {
1079+
opts = options;
1080+
} else {
1081+
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
1082+
}
1083+
buf = this._buffer;
1084+
out = [];
1085+
for ( i = 0; i < this._length; i++ ) {
1086+
out.push( buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ).toLocaleString( loc, opts ) );
1087+
}
1088+
return out.join( ',' );
1089+
});
1090+
10421091
/**
10431092
* Serializes the array elements into a string, with elements separated by the specified `separator`.
10441093
*

0 commit comments

Comments
 (0)