|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2026 The Stdlib Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +
|
| 19 | +--> |
| 20 | + |
| 21 | +# ndarray2localeString |
| 22 | + |
| 23 | +> Serialize an [ndarray][@stdlib/ndarray/ctor] as a locale-aware string. |
| 24 | +
|
| 25 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 26 | + |
| 27 | +<section class="intro"> |
| 28 | + |
| 29 | +</section> |
| 30 | + |
| 31 | +<!-- /.intro --> |
| 32 | + |
| 33 | +<!-- Package usage documentation. --> |
| 34 | + |
| 35 | +<section class="usage"> |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +```javascript |
| 40 | +var ndarray2localeString = require( '@stdlib/ndarray/to-locale-string' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### ndarray2localeString( x\[, locales\[, options]] ) |
| 44 | + |
| 45 | +Serializes an [ndarray][@stdlib/ndarray/ctor] as a locale-aware string. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var array = require( '@stdlib/ndarray/array' ); |
| 49 | + |
| 50 | +var x = array( [ 1, 2, 3, 4 ], { |
| 51 | + 'shape': [ 2, 2 ] |
| 52 | +}); |
| 53 | +// returns <ndarray> |
| 54 | + |
| 55 | +var str = ndarray2localeString( x ); |
| 56 | +// returns <string> |
| 57 | +``` |
| 58 | + |
| 59 | +The function supports the following parameters: |
| 60 | + |
| 61 | +- **x**: input ndarray. |
| 62 | +- **locales**: a [BCP 47][bcp-47] language tag or an array of such strings (_optional_). |
| 63 | +- **options**: configuration options (_optional_). |
| 64 | + |
| 65 | +</section> |
| 66 | + |
| 67 | +<!-- /.usage --> |
| 68 | + |
| 69 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 70 | + |
| 71 | +<section class="notes"> |
| 72 | + |
| 73 | +## Notes |
| 74 | + |
| 75 | +- The function does **not** serialize data outside of the buffer region defined by the [ndarray][@stdlib/ndarray/ctor] view. |
| 76 | +- For ndarrays with more than `100` elements, the function abbreviates the data, showing only the first three and last three values. |
| 77 | + |
| 78 | +</section> |
| 79 | + |
| 80 | +<!-- /.notes --> |
| 81 | + |
| 82 | +<!-- Package usage examples. --> |
| 83 | + |
| 84 | +<section class="examples"> |
| 85 | + |
| 86 | +## Examples |
| 87 | + |
| 88 | +<!-- eslint no-undef: "error" --> |
| 89 | + |
| 90 | +```javascript |
| 91 | +var array = require( '@stdlib/ndarray/array' ); |
| 92 | +var ndarray2localeString = require( '@stdlib/ndarray/to-locale-string' ); |
| 93 | + |
| 94 | +// Create a 2x3 ndarray: |
| 95 | +var x = array( [ 1234.567, 9876.543, 1111.222, 3333.444, 5555.666, 7777.888 ], { |
| 96 | + 'shape': [ 2, 3 ], |
| 97 | + 'dtype': 'generic' |
| 98 | +}); |
| 99 | + |
| 100 | +// Serialize using the default locale: |
| 101 | +var str = ndarray2localeString( x ); |
| 102 | +// e.g., returns 'ndarray( \'generic\', [ 1,234.567, 9,876.543, 1,111.222, 3,333.444, 5,555.666, 7,777.888 ], [ 2, 3 ], [ 3, 1 ], 0, \'row-major\' )' |
| 103 | + |
| 104 | +// Serialize using the English (US) locale: |
| 105 | +str = ndarray2localeString( x, 'en-US' ); |
| 106 | +// returns 'ndarray( \'generic\', [ 1,234.567, 9,876.543, 1,111.222, 3,333.444, 5,555.666, 7,777.888 ], [ 2, 3 ], [ 3, 1 ], 0, \'row-major\' )' |
| 107 | + |
| 108 | +// Serialize using the German locale: |
| 109 | +str = ndarray2localeString( x, 'de-DE' ); |
| 110 | +// returns 'ndarray( \'generic\', [ 1.234,567, 9.876,543, 1.111,222, 3.333,444, 5.555,666, 7.777,888 ], [ 2, 3 ], [ 3, 1 ], 0, \'row-major\' )' |
| 111 | + |
| 112 | +// Serialize with a maximum number of fraction digits: |
| 113 | +str = ndarray2localeString( x, 'en-US', { |
| 114 | + 'maximumFractionDigits': 0 |
| 115 | +}); |
| 116 | +// returns 'ndarray( \'generic\', [ 1,235, 9,877, 1,111, 3,333, 5,556, 7,778 ], [ 2, 3 ], [ 3, 1 ], 0, \'row-major\' )' |
| 117 | +``` |
| 118 | + |
| 119 | +</section> |
| 120 | + |
| 121 | +<!-- /.examples --> |
| 122 | + |
| 123 | +<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 124 | + |
| 125 | +<section class="references"> |
| 126 | + |
| 127 | +</section> |
| 128 | + |
| 129 | +<!-- /.references --> |
| 130 | + |
| 131 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 132 | + |
| 133 | +<section class="related"> |
| 134 | + |
| 135 | +</section> |
| 136 | + |
| 137 | +<!-- /.related --> |
| 138 | + |
| 139 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 140 | + |
| 141 | +<section class="links"> |
| 142 | + |
| 143 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/ndarray/tree/main/ctor |
| 144 | + |
| 145 | +[bcp-47]: https://tools.ietf.org/html/rfc5646 |
| 146 | + |
| 147 | +<!-- <related-links> --> |
| 148 | + |
| 149 | +<!-- </related-links> --> |
| 150 | + |
| 151 | +</section> |
| 152 | + |
| 153 | +<!-- /.links --> |
0 commit comments