Skip to content

Commit a7e58b7

Browse files
committed
Auto-generated commit
1 parent d202a06 commit a7e58b7

File tree

7 files changed

+165
-69
lines changed

7 files changed

+165
-69
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@
564564

565565
### Bug Fixes
566566

567+
- [`ebd5886`](https://github.com/stdlib-js/stdlib/commit/ebd5886f97aef9dad8daf7df0d850882a767ca6b) - preserve formatting of original string serialization and rename internal files
567568
- [`eab49ad`](https://github.com/stdlib-js/stdlib/commit/eab49ad435f6080ec28a8fbb75de4d9520bdd7e4) - ensure support for elements which are null or undefined
568569
- [`f10a6aa`](https://github.com/stdlib-js/stdlib/commit/f10a6aaf98c37bb630ac75e1a50dd0bd4a0eb417) - ensure unique indices
569570
- [`11cc2cf`](https://github.com/stdlib-js/stdlib/commit/11cc2cf8869a4b6ebf48545d5678eda25513529e) - use ndarray assertion utility
@@ -792,6 +793,7 @@ A total of 44 issues were closed in this release:
792793

793794
<details>
794795

796+
- [`ebd5886`](https://github.com/stdlib-js/stdlib/commit/ebd5886f97aef9dad8daf7df0d850882a767ca6b) - **fix:** preserve formatting of original string serialization and rename internal files _(by Athan Reines)_
795797
- [`42656f4`](https://github.com/stdlib-js/stdlib/commit/42656f425904ee6bb2608459bc6be406cd8e827f) - **feat:** add `ndarray/to-locale-string` [(#10898)](https://github.com/stdlib-js/stdlib/pull/10898) _(by Muhammad Haris, Athan Reines)_
796798
- [`25e5ccb`](https://github.com/stdlib-js/stdlib/commit/25e5ccb8759c3f7b409e58454fafb52cc8c89394) - **bench:** refactor to use string interpolation in `ndarray/base/ndarraylike2ndarray` [(#11060)](https://github.com/stdlib-js/stdlib/pull/11060) _(by Partha Das)_
797799
- [`f5c5dfa`](https://github.com/stdlib-js/stdlib/commit/f5c5dfaf00b470450be52e27649c745019b61aa3) - **bench:** refactor to use string interpolation in `ndarray/base/pop` [(#11061)](https://github.com/stdlib-js/stdlib/pull/11061) _(by Partha Das)_

base/ctor/lib/main.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ var igetValue = require( './iget.js' );
3838
var isetValue = require( './iset.js' );
3939
var setValue = require( './set.js' );
4040
var getValue = require( './get.js' );
41-
var valueOf = require( './valueof.js' ); // eslint-disable-line stdlib/no-redeclare
42-
var toJSON = require( './tojson.js' );
43-
var toString = require( './tostring.js' ); // eslint-disable-line stdlib/no-redeclare
44-
var toLocaleString = require( './tolocalestring.js' ); // eslint-disable-line stdlib/no-redeclare
41+
var valueOf = require( './value_of.js' ); // eslint-disable-line stdlib/no-redeclare
42+
var toJSON = require( './to_json.js' );
43+
var toString = require( './to_string.js' ); // eslint-disable-line stdlib/no-redeclare
44+
var toLocaleString = require( './to_locale_string.js' ); // eslint-disable-line stdlib/no-redeclare
4545
var meta2dataview = require( './meta2dataview.js' );
4646
var meta2dataviewPolyfill = require( './meta2dataview.polyfill.js' );
4747

Lines changed: 68 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616
* limitations under the License.
1717
*/
1818

19+
/* eslint-disable no-underscore-dangle */
20+
1921
'use strict';
2022

2123
// MODULES //
2224

2325
var isComplexDataType = require( './../../../base/assert/is-complex-floating-point-data-type' );
24-
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
25-
var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives;
2626
var isUndefinedOrNull = require( '@stdlib/assert/is-undefined-or-null' );
27-
var isObject = require( '@stdlib/assert/is-plain-object' );
2827
var format = require( '@stdlib/string/format' );
2928
var replace = require( '@stdlib/string/replace' );
3029
var join = require( '@stdlib/array/base/join' );
@@ -52,76 +51,91 @@ var CTORS = {
5251
};
5352

5453

54+
// FUNCTIONS //
55+
56+
/**
57+
* Serializes a value to a string.
58+
*
59+
* @private
60+
* @param {*} value - input value
61+
* @returns {string} output string
62+
*/
63+
function toStr( value ) {
64+
return String( value );
65+
}
66+
67+
/**
68+
* Serializes a value to a locale-aware string.
69+
*
70+
* @private
71+
* @param {*} value - input value
72+
* @param {(string|Array<string>)} [locales] - locale identifier(s)
73+
* @param {Object} [options] - configuration options
74+
* @returns {string} output string
75+
*/
76+
function toLocaleStr( value, locales, options ) {
77+
if ( !isUndefinedOrNull( value ) && value.toLocaleString ) {
78+
return value.toLocaleString( locales, options );
79+
}
80+
return toStr( value );
81+
}
82+
83+
5584
// MAIN //
5685

5786
/**
58-
* Serializes an ndarray as a locale-aware string.
87+
* Serializes an ndarray to a string.
5988
*
6089
* ## Notes
6190
*
6291
* - The method does **not** serialize data outside of the buffer region defined by the array configuration.
6392
*
6493
* @private
65-
* @param {(string|Array<string>)} [locales] - locale identifier(s)
66-
* @param {Object} [options] - configuration options
67-
* @throws {TypeError} first argument must be a string or an array of strings
68-
* @throws {TypeError} options argument must be an object
94+
* @param {ndarray} ctx - ndarray object
95+
* @param {string} method - element serialization method (i.e., `toString` or `toLocaleString`)
96+
* @param {(string|Array<string>|null)} locales - locale identifier(s)
97+
* @param {(Object|null)} options - configuration options
6998
* @returns {string} string representation
7099
*/
71-
function toLocaleString( locales, options ) { // eslint-disable-line stdlib/no-redeclare
72-
/* eslint-disable no-invalid-this */
100+
function serialize2string( ctx, method, locales, options ) {
73101
var isCmplx;
74102
var buffer;
75103
var ndims;
76104
var ctor;
77-
var opts;
78-
var loc;
79105
var str;
80106
var dt;
107+
var f;
81108
var v;
82109
var i;
83110

84-
if ( arguments.length === 0 ) {
85-
loc = [];
86-
} else if ( isString( locales ) || isStringArray( locales ) ) {
87-
loc = locales;
111+
if ( method === 'toLocaleString' ) {
112+
f = toLocaleStr;
88113
} else {
89-
throw new TypeError( format( 'invalid argument. First argument must be a string or an array of strings. Value: `%s`.', locales ) );
114+
f = toStr;
90115
}
91-
if ( arguments.length < 2 ) {
92-
opts = {};
93-
} else if ( isObject( options ) ) {
94-
opts = options;
95-
} else {
96-
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
97-
}
98-
ndims = this._shape.length;
99-
dt = this._dtype;
116+
ndims = ctx._shape.length;
117+
dt = ctx._dtype;
100118
isCmplx = isComplexDataType( dt );
101119

102120
// Function to invoke to create an ndarray:
103121
str = format( 'ndarray( \'%s\', ', String( dt ) );
104122

105123
// Data buffer parameter...
106124
buffer = '';
107-
if ( this._length <= 100 ) {
125+
if ( ctx._length <= 100 ) {
108126
if ( isCmplx ) {
109-
for ( i = 0; i < this._length; i++ ) {
110-
v = this.iget( i );
111-
buffer += real( v ).toLocaleString( loc, opts ) + ', ' + imag( v ).toLocaleString( loc, opts );
112-
if ( i < this._length-1 ) {
127+
for ( i = 0; i < ctx._length; i++ ) {
128+
v = ctx.iget( i );
129+
buffer += f( real( v ), locales, options ) + ', ' + f( imag( v ), locales, options );
130+
if ( i < ctx._length-1 ) {
113131
buffer += ', ';
114132
}
115133
}
116134
} else {
117-
for ( i = 0; i < this._length; i++ ) {
118-
v = this.iget( i );
119-
if ( !isUndefinedOrNull( v ) && v.toLocaleString ) {
120-
buffer += v.toLocaleString( loc, opts );
121-
} else {
122-
buffer += String( v );
123-
}
124-
if ( i < this._length-1 ) {
135+
for ( i = 0; i < ctx._length; i++ ) {
136+
v = ctx.iget( i );
137+
buffer += f( v, locales, options );
138+
if ( i < ctx._length-1 ) {
125139
buffer += ', ';
126140
}
127141
}
@@ -130,20 +144,16 @@ function toLocaleString( locales, options ) { // eslint-disable-line stdlib/no-r
130144
// First three values...
131145
if ( isCmplx ) {
132146
for ( i = 0; i < 3; i++ ) {
133-
v = this.iget( i );
134-
buffer += real( v ).toLocaleString( loc, opts ) + ', ' + imag( v ).toLocaleString( loc, opts );
147+
v = ctx.iget( i );
148+
buffer += f( real( v ), locales, options ) + ', ' + f( imag( v ), locales, options );
135149
if ( i < 2 ) {
136150
buffer += ', ';
137151
}
138152
}
139153
} else {
140154
for ( i = 0; i < 3; i++ ) {
141-
v = this.iget( i );
142-
if ( !isUndefinedOrNull( v ) && v.toLocaleString ) {
143-
buffer += v.toLocaleString( loc, opts );
144-
} else {
145-
buffer += String( v );
146-
}
155+
v = ctx.iget( i );
156+
buffer += f( v, locales, options );
147157
if ( i < 2 ) {
148158
buffer += ', ';
149159
}
@@ -154,35 +164,31 @@ function toLocaleString( locales, options ) { // eslint-disable-line stdlib/no-r
154164
// Last three values...
155165
if ( isCmplx ) {
156166
for ( i = 2; i >= 0; i-- ) {
157-
v = this.iget( this._length-1-i );
158-
buffer += real( v ).toLocaleString( loc, opts ) + ', ' + imag( v ).toLocaleString( loc, opts );
167+
v = ctx.iget( ctx._length-1-i );
168+
buffer += f( real( v ), locales, options ) + ', ' + f( imag( v ), locales, options );
159169
if ( i > 0 ) {
160170
buffer += ', ';
161171
}
162172
}
163173
} else {
164174
for ( i = 2; i >= 0; i-- ) {
165-
v = this.iget( this._length-1-i );
166-
if ( !isUndefinedOrNull( v ) && v.toLocaleString ) {
167-
buffer += v.toLocaleString( loc, opts );
168-
} else {
169-
buffer += String( v );
170-
}
175+
v = ctx.iget( ctx._length-1-i );
176+
buffer += f( v, locales, options );
171177
if ( i > 0 ) {
172178
buffer += ', ';
173179
}
174180
}
175181
}
176182
}
177-
ctor = CTORS[ this.dtype ];
183+
ctor = CTORS[ ctx.dtype ];
178184
str += replace( ctor, '{{data}}', buffer );
179185
str += ', ';
180186

181187
// Array shape...
182188
if ( ndims === 0 ) {
183189
str += '[]';
184190
} else {
185-
str += format( '[ %s ]', join( this._shape, ', ' ) );
191+
str += format( '[ %s ]', join( ctx._shape, ', ' ) );
186192
}
187193
str += ', ';
188194

@@ -192,10 +198,10 @@ function toLocaleString( locales, options ) { // eslint-disable-line stdlib/no-r
192198
str += '0';
193199
} else {
194200
for ( i = 0; i < ndims; i++ ) {
195-
if ( this._strides[ i ] < 0 ) {
196-
str += -this._strides[ i ];
201+
if ( ctx._strides[ i ] < 0 ) {
202+
str += -ctx._strides[ i ];
197203
} else {
198-
str += this._strides[ i ];
204+
str += ctx._strides[ i ];
199205
}
200206
if ( i < ndims-1 ) {
201207
str += ', ';
@@ -210,16 +216,14 @@ function toLocaleString( locales, options ) { // eslint-disable-line stdlib/no-r
210216
str += ', ';
211217

212218
// Order:
213-
str += format( '\'%s\'', this._order );
219+
str += format( '\'%s\'', ctx._order );
214220

215221
// Close the function call:
216222
str += ' )';
217223
return str;
218-
219-
/* eslint-enable no-invalid-this */
220224
}
221225

222226

223227
// EXPORTS //
224228

225-
module.exports = toLocaleString;
229+
module.exports = serialize2string;

base/ctor/lib/to_locale_string.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 isString = require( '@stdlib/assert/is-string' ).isPrimitive;
24+
var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives;
25+
var isObject = require( '@stdlib/assert/is-plain-object' );
26+
var format = require( '@stdlib/string/format' );
27+
var serialize2string = require( './serialize2string.js' );
28+
29+
30+
// VARIABLES //
31+
32+
var METHOD = 'toLocaleString';
33+
34+
35+
// MAIN //
36+
37+
/**
38+
* Serializes an ndarray as a locale-aware string.
39+
*
40+
* ## Notes
41+
*
42+
* - The method does **not** serialize data outside of the buffer region defined by the array configuration.
43+
*
44+
* @private
45+
* @param {(string|Array<string>)} [locales] - locale identifier(s)
46+
* @param {Object} [options] - configuration options
47+
* @throws {TypeError} first argument must be a string or an array of strings
48+
* @throws {TypeError} options argument must be an object
49+
* @returns {string} string representation
50+
*/
51+
function toLocaleString( locales, options ) { // eslint-disable-line stdlib/no-redeclare
52+
/* eslint-disable no-invalid-this */
53+
var nargs;
54+
var opts;
55+
var loc;
56+
57+
nargs = arguments.length;
58+
if ( nargs === 0 ) {
59+
loc = [];
60+
} else if ( isString( locales ) || isStringArray( locales ) ) {
61+
loc = locales;
62+
} else {
63+
throw new TypeError( format( 'invalid argument. First argument must be a string or an array of strings. Value: `%s`.', locales ) );
64+
}
65+
if ( nargs < 2 ) {
66+
opts = {};
67+
} else if ( isObject( options ) ) {
68+
opts = options;
69+
} else {
70+
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
71+
}
72+
return serialize2string( this, METHOD, loc, opts );
73+
74+
/* eslint-enable no-invalid-this */
75+
}
76+
77+
78+
// EXPORTS //
79+
80+
module.exports = toLocaleString;
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@
1818

1919
'use strict';
2020

21+
// MODULES //
22+
23+
var serialize2string = require( './serialize2string.js' );
24+
25+
26+
// VARIABLES //
27+
28+
var METHOD = 'toString';
29+
30+
2131
// MAIN //
2232

2333
/**
@@ -32,7 +42,7 @@
3242
*/
3343
function toString() { // eslint-disable-line stdlib/no-redeclare
3444
/* eslint-disable no-invalid-this */
35-
return this.toLocaleString();
45+
return serialize2string( this, METHOD, null, null );
3646

3747
/* eslint-enable no-invalid-this */
3848
}

0 commit comments

Comments
 (0)