Skip to content

Commit e92cdd8

Browse files
Jaysukh-409kgryte
andauthored
feat: add toLocaleString method to array/complex64
PR-URL: #2208 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com>
1 parent 05f8597 commit e92cdd8

File tree

6 files changed

+442
-0
lines changed

6 files changed

+442
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,6 +2192,27 @@ im = imagf( z );
21922192
// returns 6.0
21932193
```
21942194

2195+
<a name="method-to-locale-string"></a>
2196+
2197+
#### Complex64Array.prototype.toLocaleString( \[locales\[, options]] )
2198+
2199+
Serializes an array as a locale-specific string.
2200+
2201+
```javascript
2202+
var arr = new Complex64Array( 2 );
2203+
2204+
arr.set( [ 1.0, 1.0 ], 0 );
2205+
arr.set( [ 2.0, 2.0 ], 1 );
2206+
2207+
var str = arr.toLocaleString();
2208+
// returns '1 + 1i,2 + 2i'
2209+
```
2210+
2211+
The method supports the following arguments:
2212+
2213+
- **locales**: a string with a BCP 47 language tag or an array of such strings.
2214+
- **options**: configuration properties.
2215+
21952216
<a name="method-to-reversed"></a>
21962217

21972218
#### Complex64Array.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 Complex64Array = 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 Complex64Array( [ 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 Complex64 = require( '@stdlib/complex/float32' );
26+
var pkg = require( './../package.json' ).name;
27+
var Complex64Array = 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 Complex64( i, i ) );
46+
}
47+
arr = new Complex64Array( 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/complex64/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
*
@@ -1221,6 +1231,24 @@ declare class Complex64Array implements Complex64ArrayInterface {
12211231
*/
12221232
subarray( begin?: number, end?: number ): Complex64Array;
12231233

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

lib/node_modules/@stdlib/array/complex64/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' ).isPrimitive;
3233
var isFunction = require( '@stdlib/assert/is-function' );
3334
var isComplexLike = require( '@stdlib/assert/is-complex-like' );
@@ -2491,6 +2492,59 @@ setReadOnly( Complex64Array.prototype, 'subarray', function subarray( begin, end
24912492
return new this.constructor( buf.buffer, offset, ( len < 0 ) ? 0 : len );
24922493
});
24932494

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

0 commit comments

Comments
 (0)