Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions lib/node_modules/@stdlib/ndarray/to-locale-string/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<!--

@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 ndarray = require( '@stdlib/ndarray/ctor' );

var buffer = [ 1, 2, 3, 4 ];
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ 2, 1 ];
var offset = 0;

var x = ndarray( 'generic', buffer, shape, strides, offset, order );
// 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.
- **options**: configuration options.

</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 -->
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 lib/node_modules/@stdlib/ndarray/to-locale-string/docs/repl.txt
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
--------

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 { typedndarray } 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 - a BCP 47 language tag, or an array of such strings
* @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<T = unknown>( x: typedndarray<T>, locales?: string | Array<string>, options?: Object ): string;


// EXPORTS //

export = ndarray2localeString;
Loading