Skip to content

Commit 793a38c

Browse files
headlessNodekgryte
andauthored
feat: add toLocaleString method to ndarray/base/ctor
PR-URL: #9435 Closes: stdlib-js/metr-issue-tracker#140 Ref: #2656 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 9687965 commit 793a38c

File tree

8 files changed

+778
-256
lines changed

8 files changed

+778
-256
lines changed

lib/node_modules/@stdlib/ndarray/base/ctor/README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,11 +510,44 @@ var d = arr.data;
510510

511511
For zero-dimensional arrays, the first, and **only**, argument should be the value `v` to set. The method returns the `ndarray` instance.
512512

513+
<a name="method-to-locale-string"></a>
514+
515+
#### ndarray.prototype.toLocaleString( \[locales\[, options]] )
516+
517+
Serializes an `ndarray` as a locale-specific string.
518+
519+
```javascript
520+
// Specify the array configuration:
521+
var buffer = [ 1234.567, 9876.543, 1111.222, 3333.444 ];
522+
var shape = [ 2, 2 ];
523+
var order = 'row-major';
524+
var strides = [ 2, 1 ];
525+
var offset = 0;
526+
527+
// Create a new ndarray:
528+
var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
529+
530+
// Serialize to a locale-specific string:
531+
var str = arr.toLocaleString( 'en-US' );
532+
// returns "ndarray( 'generic', [ 1,234.567, 9,876.543, 1,111.222, 3,333.444 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' )"
533+
534+
// Use German locale:
535+
str = arr.toLocaleString( 'de-DE' );
536+
// returns "ndarray( 'generic', [ 1.234,567, 9.876,543, 1.111,222, 3.333,444 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' )"
537+
```
538+
539+
The method accepts the following arguments:
540+
541+
- **locales**: a string with a BCP 47 language tag or an array of such strings.
542+
- **options**: configuration properties.
543+
544+
The method does **not** serialize data outside of the buffer region defined by the array configuration.
545+
513546
<a name="method-to-string"></a>
514547

515548
#### ndarray.prototype.toString()
516549

517-
Serializes an `ndarray` as a `string`.
550+
Serializes an `ndarray` as a string.
518551

519552
```javascript
520553
// Specify the array configuration:

0 commit comments

Comments
 (0)