-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add @stdlib/ndarray/to-locale-string
#10898
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
headlessNode
wants to merge
7
commits into
stdlib-js:develop
Choose a base branch
from
headlessNode:ndarray-toLocaleString
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,854
−0
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6a3e500
feat: add ndarray/to-locale-string
headlessNode f5526dd
Apply suggestions from code review
kgryte b39d951
fix: relax type specificity
kgryte 75bf0d6
Apply suggestion
kgryte c2f62dc
Apply suggestions from code review
kgryte ea994d2
Apply suggestions from code review
kgryte 5ddaba4
docs: fix example
kgryte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
164 changes: 164 additions & 0 deletions
164
lib/node_modules/@stdlib/ndarray/to-locale-string/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| <!-- | ||
|
|
||
| @license Apache-2.0 | ||
|
|
||
| Copyright (c) 2026 The Stdlib Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
|
|
||
| --> | ||
|
|
||
| # ndarray2localeString | ||
|
|
||
| > Serialize an [ndarray][@stdlib/ndarray/ctor] as a locale-aware string. | ||
|
|
||
| <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <!-- Package usage documentation. --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var ndarray2localeString = require( '@stdlib/ndarray/to-locale-string' ); | ||
| ``` | ||
|
|
||
| #### ndarray2localeString( x\[, locales\[, options]] ) | ||
|
|
||
| Serializes an [ndarray][@stdlib/ndarray/ctor] as a locale-aware string. | ||
|
|
||
| ```javascript | ||
| var array = require( '@stdlib/ndarray/array' ); | ||
|
|
||
| var x = array( [ 1, 2, 3, 4 ], { | ||
| 'shape': [ 2, 2 ] | ||
| }); | ||
| // returns <ndarray> | ||
|
|
||
| var str = ndarray2localeString( x ); | ||
| // returns <string> | ||
| ``` | ||
|
|
||
| The function supports the following parameters: | ||
|
|
||
| - **x**: input ndarray. | ||
| - **locales**: a [BCP 47][bcp-47] language tag or an array of such strings (_optional_). | ||
| - **options**: configuration options (_optional_). | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| ## Notes | ||
|
|
||
| - The function does **not** serialize data outside of the buffer defined by the [ndarray][@stdlib/ndarray/ctor] view. | ||
| - For ndarrays with more than `100` elements, the function abbreviates the data, showing only the first and last three values. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <!-- Package usage examples. --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); | ||
| var strides2offset = require( '@stdlib/ndarray/base/strides2offset' ); | ||
| var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); | ||
| var zeroTo = require( '@stdlib/array/base/zero-to' ); | ||
| var ndarray = require( '@stdlib/ndarray/ctor' ); | ||
| var ndarray2localeString = require( '@stdlib/ndarray/to-locale-string' ); | ||
|
|
||
| // Create a data buffer: | ||
| var buffer = zeroTo( 27 ); | ||
|
|
||
| // Specify array meta data: | ||
| var shape = [ 3, 3, 3 ]; | ||
| var order = 'column-major'; | ||
| var ndims = shape.length; | ||
|
|
||
| // Compute array meta data: | ||
| var strides = shape2strides( shape, order ); | ||
| var offset = strides2offset( shape, strides ); | ||
|
|
||
| // Print array information: | ||
| console.log( '' ); | ||
| console.log( 'Dims: %s', shape.join( 'x' ) ); | ||
|
|
||
| // Randomly flip strides and convert an ndarray to a locale-aware string... | ||
| var arr; | ||
| var i; | ||
| for ( i = 0; i < 20; i++ ) { | ||
| strides[ discreteUniform( 0, ndims-1 ) ] *= -1; | ||
| offset = strides2offset( shape, strides ); | ||
|
|
||
| console.log( '' ); | ||
| console.log( 'Strides: %s', strides.join( ',' ) ); | ||
| console.log( 'Offset: %d', offset ); | ||
|
|
||
| arr = ndarray( 'generic', buffer, shape, strides, offset, order ); | ||
| console.log( ndarray2localeString( arr ) ); | ||
| } | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| <!-- 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. --> | ||
|
|
||
| <section class="references"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.references --> | ||
|
|
||
| <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
|
||
| <section class="related"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.related --> | ||
|
|
||
| <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="links"> | ||
|
|
||
| [@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor | ||
|
|
||
| [bcp-47]: https://tools.ietf.org/html/rfc5646 | ||
|
|
||
| <!-- <related-links> --> | ||
|
|
||
| <!-- </related-links> --> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> | ||
99 changes: 99 additions & 0 deletions
99
lib/node_modules/@stdlib/ndarray/to-locale-string/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // MODULES // | ||
|
|
||
| var bench = require( '@stdlib/bench' ); | ||
| var isString = require( '@stdlib/assert/is-string' ).isPrimitive; | ||
| var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); | ||
| var strides2offset = require( '@stdlib/ndarray/base/strides2offset' ); | ||
| var numel = require( '@stdlib/ndarray/base/numel' ); | ||
| var zeroTo = require( '@stdlib/array/base/zero-to' ); | ||
| var format = require( '@stdlib/string/format' ); | ||
| var ndarray = require( '@stdlib/ndarray/ctor' ); | ||
| var pkg = require( './../package.json' ).name; | ||
| var ndarray2localeString = require( './../lib' ); | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( format( '%s:order=row-major', pkg ), function benchmark( b ) { | ||
| var strides; | ||
| var buffer; | ||
| var offset; | ||
| var order; | ||
| var shape; | ||
| var arr; | ||
| var out; | ||
| var i; | ||
|
|
||
| shape = [ 10, 10, 10 ]; | ||
| order = 'row-major'; | ||
| buffer = zeroTo( numel( shape ) ); | ||
| strides = shape2strides( shape, order ); | ||
| offset = strides2offset( shape, strides ); | ||
| arr = ndarray( 'generic', buffer, shape, strides, offset, order ); | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| out = ndarray2localeString( arr ); | ||
| if ( typeof out !== 'string' ) { | ||
| b.fail( 'should return a string' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( !isString( out ) ) { | ||
| b.fail( 'should return a string' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); | ||
|
|
||
| bench( format( '%s:order=column-major', pkg ), function benchmark( b ) { | ||
| var strides; | ||
| var buffer; | ||
| var offset; | ||
| var order; | ||
| var shape; | ||
| var arr; | ||
| var out; | ||
| var i; | ||
|
|
||
| shape = [ 10, 10, 10 ]; | ||
| order = 'column-major'; | ||
| buffer = zeroTo( numel( shape ) ); | ||
| strides = shape2strides( shape, order ); | ||
| offset = strides2offset( shape, strides ); | ||
| arr = ndarray( 'generic', buffer, shape, strides, offset, order ); | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| out = ndarray2localeString( arr ); | ||
| if ( typeof out !== 'string' ) { | ||
| b.fail( 'should return a string' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( !isString( out ) ) { | ||
| b.fail( 'should return a string' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); |
32 changes: 32 additions & 0 deletions
32
lib/node_modules/@stdlib/ndarray/to-locale-string/docs/repl.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
|
|
||
| {{alias}}( x[, locales[, options]] ) | ||
| Serializes an ndarray as a locale-aware string. | ||
|
|
||
| This function does *not* serialize data outside of the buffer region | ||
| defined by the ndarray view. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| x: ndarray | ||
| Input ndarray. | ||
|
|
||
| locales: string|Array<string> (optional) | ||
| A BCP 47 language tag or an array of such strings. | ||
|
|
||
| options: Object (optional) | ||
| Configuration options. | ||
|
|
||
| Returns | ||
| ------- | ||
| out: string | ||
| String representation. | ||
|
|
||
| Examples | ||
| -------- | ||
| > var arr = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] ); | ||
| > var out = {{alias}}( arr ) | ||
| <string> | ||
|
|
||
| See Also | ||
| -------- | ||
|
|
51 changes: 51 additions & 0 deletions
51
lib/node_modules/@stdlib/ndarray/to-locale-string/docs/types/index.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /* | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| // TypeScript Version: 4.1 | ||
|
|
||
| /// <reference types="@stdlib/types"/> | ||
|
|
||
| import { ndarray } from '@stdlib/types/ndarray'; | ||
|
|
||
| /** | ||
| * Serializes an ndarray as a locale-aware string. | ||
| * | ||
| * ## Notes | ||
| * | ||
| * - The function does **not** serialize data outside of the buffer region defined by the ndarray view. | ||
| * | ||
| * @param x - input ndarray | ||
| * @param locales - locale identifier(s) | ||
| * @param options - configuration options | ||
| * @returns string representation | ||
| * | ||
| * @example | ||
| * var array = require( `@stdlib/ndarray/array` ); | ||
| * | ||
| * var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); | ||
| * // returns <ndarray> | ||
| * | ||
| * var str = ndarray2localeString( x ); | ||
| * // returns <string> | ||
| */ | ||
| declare function ndarray2localeString( x: ndarray, locales?: string | Array<string>, options?: Object ): string; | ||
|
|
||
|
|
||
| // EXPORTS // | ||
|
|
||
| export = ndarray2localeString; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For a top-level package, I don't find this example particularly compelling. Instead, I suggest simplifying and simply (1) using, say,
random/uniformto generate an ndarray of random values and (2) serializing that ndarray to a string using different locale identifiers (e.g., for English, German, etc) and potentially different configuration options (e.g., maximum number of digits, etc).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And I think it would be useful to use doctest return annotations for
console.logstatements to show the expected output. This would allow readers to develop a visual intuition regarding expected output.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment also applies to the corresponding example file.