Skip to content

Commit 9dfd026

Browse files
authored
feat: add toLocaleString method to array/complex128
PR-URL: stdlib-js#2252 Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 0227bf0 commit 9dfd026

File tree

6 files changed

+442
-0
lines changed

6 files changed

+442
-0
lines changed

lib/node_modules/@stdlib/array/complex128/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,6 +2185,27 @@ im = imag( z );
21852185
// returns 6.0
21862186
```
21872187

2188+
<a name="method-to-locale-string"></a>
2189+
2190+
#### Complex128Array.prototype.toLocaleString( \[locales\[, options]] )
2191+
2192+
Serializes an array as a locale-specific string.
2193+
2194+
```javascript
2195+
var arr = new Complex128Array( 2 );
2196+
2197+
arr.set( [ 1.0, 1.0 ], 0 );
2198+
arr.set( [ 2.0, 2.0 ], 1 );
2199+
2200+
var str = arr.toLocaleString();
2201+
// returns '1 + 1i,2 + 2i'
2202+
```
2203+
2204+
The method supports the following arguments:
2205+
2206+
- **locales**: a string with a BCP 47 language tag or an array of such strings.
2207+
- **options**: configuration properties.
2208+
21882209
<a name="method-to-reversed"></a>
21892210

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

lib/node_modules/@stdlib/array/complex128/docs/types/index.d.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ import ArrayBuffer = require( '@stdlib/array/buffer' );
3030
// Define a union type representing both iterable and non-iterable iterators:
3131
type Iterator = Iter | IterableIterator;
3232

33+
/**
34+
* Locale-specific configuration options.
35+
*/
36+
interface LocaleOptions<T = unknown> {
37+
/**
38+
* Configuration property.
39+
*/
40+
[ key: string | symbol | number ]: T | undefined;
41+
};
42+
3343
/**
3444
* Callback invoked for each element in a source object.
3545
*
@@ -1219,6 +1229,24 @@ declare class Complex128Array implements Complex128ArrayInterface {
12191229
*/
12201230
subarray( begin?: number, end?: number ): Complex64Array;
12211231

1232+
/**
1233+
* Serializes an array as a locale-specific string.
1234+
*
1235+
* @param locales - locale identifier(s)
1236+
* @param options - configuration options
1237+
* @returns string
1238+
*
1239+
* @example
1240+
* var arr = new Complex128Array( 2 );
1241+
*
1242+
* arr.set( [ 1.0, 1.0 ], 0 );
1243+
* arr.set( [ 2.0, 2.0 ], 1 );
1244+
*
1245+
* var str = arr.toLocaleString();
1246+
* // returns '1 + 1i,2 + 2i'
1247+
*/
1248+
toLocaleString( locales?: string | Array<string>, options?: LocaleOptions ): string;
1249+
12221250
/**
12231251
* Returns a new typed array containing the elements in reversed order.
12241252
*

lib/node_modules/@stdlib/array/complex128/lib/main.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var isCollection = require( '@stdlib/assert/is-collection' );
2828
var isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' );
2929
var isObject = require( '@stdlib/assert/is-object' );
3030
var isArray = require( '@stdlib/assert/is-array' );
31+
var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives;
3132
var isString = require( '@stdlib/assert/is-string' );
3233
var isFunction = require( '@stdlib/assert/is-function' );
3334
var isComplexLike = require( '@stdlib/assert/is-complex-like' );
@@ -2493,6 +2494,59 @@ setReadOnly( Complex128Array.prototype, 'subarray', function subarray( begin, en
24932494
return new this.constructor( buf.buffer, offset, ( len < 0 ) ? 0 : len );
24942495
});
24952496

2497+
/**
2498+
* Serializes an array as a locale-specific string.
2499+
*
2500+
* @name toLocaleString
2501+
* @memberof Complex128Array.prototype
2502+
* @type {Function}
2503+
* @param {(string|Array<string>)} [locales] - locale identifier(s)
2504+
* @param {Object} [options] - configuration options
2505+
* @throws {TypeError} `this` must be a complex number array
2506+
* @throws {TypeError} first argument must be a string or an array of strings
2507+
* @throws {TypeError} options argument must be an object
2508+
* @returns {string} string representation
2509+
*
2510+
* @example
2511+
* var arr = new Complex128Array( 2 );
2512+
*
2513+
* arr.set( [ 1.0, 1.0 ], 0 );
2514+
* arr.set( [ 2.0, 2.0 ], 1 );
2515+
*
2516+
* var str = arr.toLocaleString();
2517+
* // returns '1 + 1i,2 + 2i'
2518+
*/
2519+
setReadOnly( Complex128Array.prototype, 'toLocaleString', function toLocaleString( locales, options ) {
2520+
var opts;
2521+
var loc;
2522+
var out;
2523+
var buf;
2524+
var i;
2525+
if ( !isComplexArray( this ) ) {
2526+
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
2527+
}
2528+
if ( arguments.length === 0 ) {
2529+
loc = [];
2530+
} else if ( isString( locales ) || isStringArray( locales ) ) {
2531+
loc = locales;
2532+
} else {
2533+
throw new TypeError( 'invalid argument. First argument must be a string or an array of strings. Value: `' + locales + '`.' );
2534+
}
2535+
if ( arguments.length < 2 ) {
2536+
opts = {};
2537+
} else if ( isObject( options ) ) {
2538+
opts = options;
2539+
} else {
2540+
throw new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' );
2541+
}
2542+
buf = this._buffer;
2543+
out = [];
2544+
for ( i = 0; i < this._length; i++ ) {
2545+
out.push( getComplex128( buf, i ).toLocaleString( loc, opts ) );
2546+
}
2547+
return out.join( ',' );
2548+
});
2549+
24962550
/**
24972551
* Returns a new typed array containing the elements in reversed order.
24982552
*

0 commit comments

Comments
 (0)