diff --git a/lib/node_modules/@stdlib/ndarray/to-locale-string/README.md b/lib/node_modules/@stdlib/ndarray/to-locale-string/README.md new file mode 100644 index 000000000000..65f142c8cfc8 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-locale-string/README.md @@ -0,0 +1,168 @@ + + +# ndarray2localeString + +> Serialize an [ndarray][@stdlib/ndarray/ctor] as a locale-aware string. + + + +
+ +
+ + + + + +
+ +## 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 + +var str = ndarray2localeString( x ); +// returns +``` + +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. + +
+ + + + + +
+ +## 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. + +
+ + + + + +
+ +## Examples + + + +```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 ) ); +} +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/to-locale-string/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/to-locale-string/benchmark/benchmark.js new file mode 100644 index 000000000000..4fe6d1d17306 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-locale-string/benchmark/benchmark.js @@ -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(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/to-locale-string/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/to-locale-string/docs/repl.txt new file mode 100644 index 000000000000..43ebda7a53c5 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-locale-string/docs/repl.txt @@ -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 (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 ) + + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/to-locale-string/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/to-locale-string/docs/types/index.d.ts new file mode 100644 index 000000000000..10c1755e8d22 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-locale-string/docs/types/index.d.ts @@ -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 + +/// + +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 +* +* var str = ndarray2localeString( x ); +* // returns +*/ +declare function ndarray2localeString( x: typedndarray, locales?: string | Array, options?: Object ): string; + + +// EXPORTS // + +export = ndarray2localeString; diff --git a/lib/node_modules/@stdlib/ndarray/to-locale-string/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/to-locale-string/docs/types/test.ts new file mode 100644 index 000000000000..be805d0ae429 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-locale-string/docs/types/test.ts @@ -0,0 +1,55 @@ +/* +* @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. +*/ + +import array = require( '@stdlib/ndarray/array' ); +import ndarray2localeString = require( './index' ); + + +// TESTS // + +// The function returns a string... +{ + ndarray2localeString( array( [ [ 1, 2 ], [ 3, 4 ] ] ) ); // $ExpectType string + ndarray2localeString( array( [ [ 1, 2 ], [ 3, 4 ] ] ), 'en-US' ); // $ExpectType string + ndarray2localeString( array( [ [ 1, 2 ], [ 3, 4 ] ] ), [ 'en-US' ] ); // $ExpectType string + ndarray2localeString( array( [ [ 1, 2 ], [ 3, 4 ] ] ), 'en-US', {} ); // $ExpectType string +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + ndarray2localeString( 10 ); // $ExpectError + ndarray2localeString( '10' ); // $ExpectError + ndarray2localeString( true ); // $ExpectError + ndarray2localeString( false ); // $ExpectError + ndarray2localeString( null ); // $ExpectError + ndarray2localeString( undefined ); // $ExpectError + ndarray2localeString( [ 1, 2 ] ); // $ExpectError + ndarray2localeString( {} ); // $ExpectError + ndarray2localeString( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a string or array of strings... +{ + const arr = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + + ndarray2localeString( arr, 10 ); // $ExpectError + ndarray2localeString( arr, true ); // $ExpectError + ndarray2localeString( arr, false ); // $ExpectError + ndarray2localeString( arr, null ); // $ExpectError + ndarray2localeString( arr, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/to-locale-string/examples/index.js b/lib/node_modules/@stdlib/ndarray/to-locale-string/examples/index.js new file mode 100644 index 000000000000..938be42828a5 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-locale-string/examples/index.js @@ -0,0 +1,57 @@ +/** +* @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'; + +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( './../lib' ); + +// 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 ) ); +} diff --git a/lib/node_modules/@stdlib/ndarray/to-locale-string/lib/index.js b/lib/node_modules/@stdlib/ndarray/to-locale-string/lib/index.js new file mode 100644 index 000000000000..a8411e582761 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-locale-string/lib/index.js @@ -0,0 +1,44 @@ +/** +* @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'; + +/** +* Serialize an ndarray as a locale-aware string. +* +* @module @stdlib/ndarray/to-locale-string +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var ndarray2localeString = require( '@stdlib/ndarray/to-locale-string' ); +* +* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); +* // returns +* +* var str = ndarray2localeString( x ); +* // returns +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/to-locale-string/lib/main.js b/lib/node_modules/@stdlib/ndarray/to-locale-string/lib/main.js new file mode 100644 index 000000000000..5580756db777 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-locale-string/lib/main.js @@ -0,0 +1,292 @@ +/** +* @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. +*/ + +/* eslint-disable max-statements */ + +'use strict'; + +// MODULES // + +var isComplexDataType = require( '@stdlib/ndarray/base/assert/is-complex-floating-point-data-type' ); +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives; +var isUndefinedOrNull = require( '@stdlib/assert/is-undefined-or-null' ); +var isObject = require( '@stdlib/assert/is-plain-object' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getStrides = require( '@stdlib/ndarray/strides' ); +var getOffset = require( '@stdlib/ndarray/offset' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var numel = require( '@stdlib/ndarray/base/numel' ); +var ind2sub = require( '@stdlib/ndarray/base/ind2sub' ); +var join = require( '@stdlib/array/base/join' ); +var replace = require( '@stdlib/string/replace' ); +var real = require( '@stdlib/complex/float64/real' ); +var imag = require( '@stdlib/complex/float64/imag' ); +var format = require( '@stdlib/string/format' ); + + +// VARIABLES // + +var CTORS = { + 'int8': 'new Int8Array( [ {{data}} ] )', + 'uint8': 'new Uint8Array( [ {{data}} ] )', + 'uint8c': 'new Uint8ClampedArray( [ {{data}} ] )', + 'int16': 'new Int16Array( [ {{data}} ] )', + 'uint16': 'new Uint16Array( [ {{data}} ] )', + 'int32': 'new Int32Array( [ {{data}} ] )', + 'uint32': 'new Uint32Array( [ {{data}} ] )', + 'float32': 'new Float32Array( [ {{data}} ] )', + 'float64': 'new Float64Array( [ {{data}} ] )', + 'generic': '[ {{data}} ]', + 'binary': 'new Buffer( [ {{data}} ] )', + 'complex64': 'new Complex64Array( [ {{data}} ] )', + 'complex128': 'new Complex128Array( [ {{data}} ] )', + 'bool': 'new BooleanArray( [ {{data}} ] )' +}; + + +// FUNCTIONS // + +/** +* Tests whether an object has a specified method. +* +* @private +* @param {Object} obj - input object +* @param {string} method - method name +* @returns {boolean} boolean indicating whether an object has a specified method +* +* @example +* var bool = hasMethod( [], 'toLocaleString' ); +* // returns true +*/ +function hasMethod( obj, method ) { + return ( typeof obj[ method ] === 'function' && obj[ method ] !== Object.prototype[ method ] ); +} + + +// MAIN // + +/** +* 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 {ndarrayLike} x - input ndarray +* @param {(string|Array)} [locales] - locale identifier(s) +* @param {Object} [options] - configuration options +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} second argument must be a string or an array of strings +* @throws {TypeError} third argument must be an object +* @returns {string} string representation +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); +* // returns +* +* var str = ndarray2localeString( x ); +* // returns +*/ +function ndarray2localeString( x, locales, options ) { + var isCmplx; + var buffer; + var ndims; + var nargs; + var ctor; + var opts; + var ord; + var loc; + var len; + var str; + var sh; + var st; + var dt; + var o; + var v; + var i; + + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) ); + } + nargs = arguments.length; + + // Defer to input argument's custom implementation, if already defined... + if ( hasMethod( x, 'toLocaleString' ) ) { + if ( nargs < 2 ) { + return x.toLocaleString(); + } + if ( nargs < 3 ) { + return x.toLocaleString( locales ); + } + return x.toLocaleString( locales, options ); + } + + // Resolves arguments: + if ( nargs < 2 ) { + loc = []; + } else if ( isString( locales ) || isStringArray( locales ) ) { + loc = locales; + } else { + throw new TypeError( format( 'invalid argument. Second argument must be a string or an array of strings. Value: `%s`.', locales ) ); + } + if ( nargs < 3 ) { + opts = {}; + } else if ( isObject( options ) ) { + opts = options; + } else { + throw new TypeError( format( 'invalid argument. Third argument must be an object. Value: `%s`.', options ) ); + } + + // Resolve array meta data: + dt = getDType( x ); + sh = getShape( x ); + st = getStrides( x ); + o = getOffset( x ); + ord = getOrder( x ); + ndims = sh.length; + isCmplx = isComplexDataType( dt ); + + len = numel( sh ); + + // Function to invoke to create an ndarray: + str = format( 'ndarray( \'%s\', ', String( dt ) ); + + // Data buffer parameter... + buffer = ''; + if ( len <= 100 ) { + if ( isCmplx ) { + for ( i = 0; i < len; i++ ) { + v = x.get.apply( x, ind2sub( sh, st, o, ord, i, 'throw' ) ); + buffer += real( v ).toLocaleString( loc, opts ) + ', ' + imag( v ).toLocaleString( loc, opts ); + if ( i < len-1 ) { + buffer += ', '; + } + } + } else { + for ( i = 0; i < len; i++ ) { + v = x.get.apply( x, ind2sub( sh, st, o, ord, i, 'throw' ) ); + if ( !isUndefinedOrNull( v ) && v.toLocaleString ) { + buffer += v.toLocaleString( loc, opts ); + } else { + buffer += String( v ); + } + if ( i < len-1 ) { + buffer += ', '; + } + } + } + } else { + // First three values... + if ( isCmplx ) { + for ( i = 0; i < 3; i++ ) { + v = x.get.apply( x, ind2sub( sh, st, o, ord, i, 'throw' ) ); + buffer += real( v ).toLocaleString( loc, opts ) + ', ' + imag( v ).toLocaleString( loc, opts ); + if ( i < 2 ) { + buffer += ', '; + } + } + } else { + for ( i = 0; i < 3; i++ ) { + v = x.get.apply( x, ind2sub( sh, st, o, ord, i, 'throw' ) ); + if ( !isUndefinedOrNull( v ) && v.toLocaleString ) { + buffer += v.toLocaleString( loc, opts ); + } else { + buffer += String( v ); + } + if ( i < 2 ) { + buffer += ', '; + } + } + } + buffer += ', ..., '; + + // Last three values... + if ( isCmplx ) { + for ( i = 2; i >= 0; i-- ) { + v = x.get.apply( x, ind2sub( sh, st, o, ord, len-1-i, 'throw' ) ); + buffer += real( v ).toLocaleString( loc, opts ) + ', ' + imag( v ).toLocaleString( loc, opts ); + if ( i > 0 ) { + buffer += ', '; + } + } + } else { + for ( i = 2; i >= 0; i-- ) { + v = x.get.apply( x, ind2sub( sh, st, o, ord, len-1-i, 'throw' ) ); + if ( !isUndefinedOrNull( v ) && v.toLocaleString ) { + buffer += v.toLocaleString( loc, opts ); + } else { + buffer += String( v ); + } + if ( i > 0 ) { + buffer += ', '; + } + } + } + } + ctor = CTORS[ dt ]; + str += replace( ctor, '{{data}}', buffer ); + str += ', '; + + // Array shape... + if ( ndims === 0 ) { + str += '[]'; + } else { + str += format( '[ %s ]', join( sh, ', ' ) ); + } + str += ', '; + + // Stride array... + str += '[ '; + if ( ndims === 0 ) { + str += '0'; + } else { + for ( i = 0; i < ndims; i++ ) { + if ( st[ i ] < 0 ) { + str += -st[ i ]; + } else { + str += st[ i ]; + } + if ( i < ndims-1 ) { + str += ', '; + } + } + } + str += ' ]'; + str += ', '; + + // Buffer offset: + str += '0'; + str += ', '; + + // Order: + str += format( '\'%s\'', ord ); + + // Close the function call: + str += ' )'; + return str; +} + + +// EXPORTS // + +module.exports = ndarray2localeString; diff --git a/lib/node_modules/@stdlib/ndarray/to-locale-string/package.json b/lib/node_modules/@stdlib/ndarray/to-locale-string/package.json new file mode 100644 index 000000000000..f744c84940b0 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-locale-string/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/ndarray/to-locale-string", + "version": "0.0.0", + "description": "Serialize an ndarray as a locale-aware string.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdtypes", + "types", + "ndarray", + "multidimensional", + "array", + "utilities", + "utility", + "utils", + "util", + "view", + "convert", + "tolocalestring", + "locale", + "transform", + "string", + "serialize" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/to-locale-string/test/test.js b/lib/node_modules/@stdlib/ndarray/to-locale-string/test/test.js new file mode 100644 index 000000000000..70df6cc788c0 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-locale-string/test/test.js @@ -0,0 +1,991 @@ +/** +* @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 tape = require( 'tape' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Complex64Array = require( '@stdlib/array/complex64' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var BooleanArray = require( '@stdlib/array/bool' ); +var Complex64 = require( '@stdlib/complex/float32/ctor' ); +var Complex128 = require( '@stdlib/complex/float64/ctor' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var sub2ind = require( '@stdlib/ndarray/sub2ind' ); +var noop = require( '@stdlib/utils/noop' ); +var ndarray2localeString = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof ndarray2localeString, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not provided an ndarray-like object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + ndarray2localeString( value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not a string or array of strings', function test( t ) { + var values; + var arr; + var i; + + arr = ndarray( 'generic', [ 1, 2, 3, 4 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + values = [ + 5, + NaN, + true, + false, + null, + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + ndarray2localeString( arr, value ); + }; + } +}); + +tape( 'the function throws an error if provided a third argument which is not an object', function test( t ) { + var values; + var arr; + var i; + + arr = ndarray( 'generic', [ 1, 2, 3, 4 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + ndarray2localeString( arr, 'en-US', value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not a string or array of strings (ndarray-like)', function test( t ) { + var values; + var arr; + var i; + + arr = { + 'dtype': 'generic', + 'data': [ 1, 2, 3, 4 ], + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'length': 4, + 'flags': { + 'READONLY': false + }, + 'get': getter, + 'set': noop + }; + + values = [ + 5, + NaN, + true, + false, + null, + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function getter( i, j ) { + return arr.data[ arr.offset + ( arr.strides[0]*i ) + ( arr.strides[1]*j ) ]; + } + + function badValue( value ) { + return function badValue() { + ndarray2localeString( arr, value ); + }; + } +}); + +tape( 'the function throws an error if provided a third argument which is not an object (ndarray-like)', function test( t ) { + var values; + var arr; + var i; + + arr = { + 'dtype': 'generic', + 'data': [ 1, 2, 3, 4 ], + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'length': 4, + 'flags': { + 'READONLY': false + }, + 'get': getter, + 'set': noop + }; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function getter( i, j ) { + return arr.data[ arr.offset + ( arr.strides[0]*i ) + ( arr.strides[1]*j ) ]; + } + + function badValue( value ) { + return function badValue() { + ndarray2localeString( arr, 'en-US', value ); + }; + } +}); + +tape( 'the function serializes an ndarray as a string (row-major)', function test( t ) { + var expected; + var strides; + var actual; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + + dtype = 'float64'; + buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + shape = [ 2, 2 ]; + order = 'row-major'; + strides = [ 2, 1 ]; + offset = 2; + + arr = ndarray( dtype, buffer, shape, strides, offset, order ); + + expected = 'ndarray( \'float64\', new Float64Array( [ 3, 4, 5, 6 ] ), [ 2, 2 ], [ 2, 1 ], 0, \'row-major\' )'; + actual = ndarray2localeString( arr ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes an ndarray as a string (row-major, negative strides)', function test( t ) { + var expected; + var strides; + var actual; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + + dtype = 'float64'; + buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + shape = [ 2, 2 ]; + order = 'row-major'; + strides = [ -2, -1 ]; + offset = 5; + + arr = ndarray( dtype, buffer, shape, strides, offset, order ); + + expected = 'ndarray( \'float64\', new Float64Array( [ 6, 5, 4, 3 ] ), [ 2, 2 ], [ 2, 1 ], 0, \'row-major\' )'; + actual = ndarray2localeString( arr ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes an ndarray as a string (column-major)', function test( t ) { + var expected; + var strides; + var actual; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + + dtype = 'generic'; + buffer = [ 1, 2, 3, 4 ]; + shape = [ 2, 2 ]; + order = 'column-major'; + strides = [ 1, 2 ]; + offset = 0; + + arr = ndarray( dtype, buffer, shape, strides, offset, order ); + + expected = 'ndarray( \'generic\', [ 1, 2, 3, 4 ], [ 2, 2 ], [ 1, 2 ], 0, \'column-major\' )'; + actual = ndarray2localeString( arr ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes an ndarray as a string (column-major, negative strides)', function test( t ) { + var expected; + var strides; + var actual; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + + dtype = 'float64'; + buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + shape = [ 2, 2 ]; + order = 'column-major'; + strides = [ -1, -2 ]; + offset = 5; + + arr = ndarray( dtype, buffer, shape, strides, offset, order ); + + expected = 'ndarray( \'float64\', new Float64Array( [ 6, 5, 4, 3 ] ), [ 2, 2 ], [ 1, 2 ], 0, \'column-major\' )'; + actual = ndarray2localeString( arr ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes an ndarray as a string (0d)', function test( t ) { + var expected; + var strides; + var actual; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + + dtype = 'generic'; + buffer = [ 1, 2, 3, 4 ]; + shape = []; + order = 'column-major'; + strides = [ 0 ]; + offset = 2; + + arr = ndarray( dtype, buffer, shape, strides, offset, order ); + + expected = 'ndarray( \'generic\', [ 3 ], [], [ 0 ], 0, \'column-major\' )'; + actual = ndarray2localeString( arr ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes an ndarray as a string (dtype=complex)', function test( t ) { + var expected; + var strides; + var actual; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + + dtype = 'complex64'; + buffer = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + shape = [ 2, 2 ]; + order = 'row-major'; + strides = [ 2, 1 ]; + offset = 0; + + arr = ndarray( dtype, buffer, shape, strides, offset, order ); + + expected = 'ndarray( \'complex64\', new Complex64Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] ), [ 2, 2 ], [ 2, 1 ], 0, \'row-major\' )'; + actual = ndarray2localeString( arr ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes an ndarray-like object as a string (no toLocaleString method)', function test( t ) { + var expected; + var actual; + var arr; + + arr = { + 'dtype': 'float64', + 'data': new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ), + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 2, + 'order': 'row-major', + 'ndims': 2, + 'length': 4, + 'flags': { + 'READONLY': true + }, + 'get': getter, + 'set': noop + }; + + expected = 'ndarray( \'float64\', new Float64Array( [ 3, 4, 5, 6 ] ), [ 2, 2 ], [ 2, 1 ], 0, \'row-major\' )'; + actual = ndarray2localeString( arr ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); + + function getter( i, j ) { + return arr.data[ arr.offset+sub2ind( [ 2, 2 ], i, j ) ]; + } +}); + +tape( 'the function serializes an ndarray-like object as a string (column-major, no toLocaleString method)', function test( t ) { + var expected; + var actual; + var arr; + + arr = { + 'dtype': 'float64', + 'data': new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ), + 'shape': [ 2, 2 ], + 'strides': [ 1, 2 ], + 'offset': 2, + 'order': 'column-major', + 'ndims': 2, + 'length': 4, + 'flags': { + 'READONLY': true + }, + 'get': getter, + 'set': noop + }; + + expected = 'ndarray( \'float64\', new Float64Array( [ 3, 5, 4, 6 ] ), [ 2, 2 ], [ 1, 2 ], 0, \'column-major\' )'; + actual = ndarray2localeString( arr ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); + + function getter( i, j ) { + return arr.data[ arr.offset+sub2ind( [ 2, 2 ], i, j ) ]; + } +}); + +tape( 'the function serializes an ndarray-like object as a string (dtype=complex, no toLocaleString method)', function test( t ) { + var expected; + var actual; + var arr; + + arr = { + 'dtype': 'complex64', + 'data': new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ), + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'length': 4, + 'flags': { + 'READONLY': true + }, + 'get': getter, + 'set': noop + }; + + expected = 'ndarray( \'complex64\', new Complex64Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] ), [ 2, 2 ], [ 2, 1 ], 0, \'row-major\' )'; + actual = ndarray2localeString( arr ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); + + function getter( i, j ) { + return arr.data.get( arr.offset+sub2ind( [ 2, 2 ], i, j ) ); + } +}); + +tape( 'the function serializes an ndarray as a string (column-major, negative strides, delegation)', function test( t ) { + var expected; + var strides; + var actual; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + + dtype = 'generic'; + buffer = [ 1.0, 2.0, 3.0, 4.0 ]; + shape = [ 2, 2 ]; + order = 'column-major'; + strides = [ -1, -2 ]; + offset = 3; + + arr = ndarray( dtype, buffer, shape, strides, offset, order ); + + expected = 'ndarray( \'generic\', [ 4, 3, 2, 1 ], [ 2, 2 ], [ 1, 2 ], 0, \'column-major\' )'; + actual = ndarray2localeString( arr ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes an ndarray as a string (complex128, delegation)', function test( t ) { + var expected; + var strides; + var actual; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + + dtype = 'complex128'; + buffer = new Complex128Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] ); + shape = [ 2, 2 ]; + order = 'row-major'; + strides = [ 2, 1 ]; + offset = 0; + + arr = ndarray( dtype, buffer, shape, strides, offset, order ); + + expected = 'ndarray( \'complex128\', new Complex128Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] ), [ 2, 2 ], [ 2, 1 ], 0, \'row-major\' )'; + actual = ndarray2localeString( arr ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes an ndarray as a string (boolean type, delegation)', function test( t ) { + var expected; + var strides; + var actual; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + + dtype = 'bool'; + buffer = new BooleanArray( [ true, false, true, false ] ); + shape = [ 2, 2 ]; + order = 'row-major'; + strides = [ 2, 1 ]; + offset = 0; + + arr = ndarray( dtype, buffer, shape, strides, offset, order ); + + expected = 'ndarray( \'bool\', new BooleanArray( [ true, false, true, false ] ), [ 2, 2 ], [ 2, 1 ], 0, \'row-major\' )'; + actual = ndarray2localeString( arr ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes an ndarray as a string (generic with null/undefined, delegation)', function test( t ) { + var expected; + var strides; + var actual; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + + dtype = 'generic'; + buffer = [ null, void 0, null, void 0 ]; + shape = [ 2, 2 ]; + order = 'row-major'; + strides = [ 2, 1 ]; + offset = 0; + + arr = ndarray( dtype, buffer, shape, strides, offset, order ); + + expected = 'ndarray( \'generic\', [ null, undefined, null, undefined ], [ 2, 2 ], [ 2, 1 ], 0, \'row-major\' )'; + actual = ndarray2localeString( arr ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes an ndarray as a string (0d, delegation)', function test( t ) { + var expected; + var strides; + var actual; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + + dtype = 'generic'; + buffer = [ 4.0 ]; + shape = []; + order = 'row-major'; + strides = [ 0 ]; + offset = 0; + + arr = ndarray( dtype, buffer, shape, strides, offset, order ); + + expected = 'ndarray( \'generic\', [ 4 ], [], [ 0 ], 0, \'row-major\' )'; + actual = ndarray2localeString( arr ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes an ndarray as a string (large array, delegation)', function test( t ) { + var expected; + var strides; + var actual; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + var i; + + dtype = 'generic'; + buffer = []; + for ( i = 0; i < 10000; i++ ) { + if ( i < 3 ) { + buffer.push( i + 1 ); + } else if ( i >= 9997 ) { + buffer.push( i - 9993 ); + } else { + buffer.push( 0 ); + } + } + shape = [ 10000 ]; + order = 'row-major'; + strides = [ 1 ]; + offset = 0; + + arr = ndarray( dtype, buffer, shape, strides, offset, order ); + + expected = 'ndarray( \'generic\', [ 1, 2, 3, ..., 4, 5, 6 ], [ 10000 ], [ 1 ], 0, \'row-major\' )'; + actual = ndarray2localeString( arr ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes an ndarray as a string (large array, complex64, delegation)', function test( t ) { + var expected; + var strides; + var actual; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + var i; + + dtype = 'complex64'; + buffer = new Complex64Array( 10000 ); + for ( i = 0; i < 10000; i++ ) { + if ( i < 3 ) { + buffer.set( new Complex64( i+1, i+1 ), i ); + } else if ( i >= 9997 ) { + buffer.set( new Complex64( i-9993, i-9993 ), i ); + } else { + buffer.set( new Complex64( 0, 0 ), i ); + } + } + shape = [ 10000 ]; + order = 'row-major'; + strides = [ 1 ]; + offset = 0; + + arr = ndarray( dtype, buffer, shape, strides, offset, order ); + + expected = 'ndarray( \'complex64\', new Complex64Array( [ 1, 1, 2, 2, 3, 3, ..., 4, 4, 5, 5, 6, 6 ] ), [ 10000 ], [ 1 ], 0, \'row-major\' )'; + actual = ndarray2localeString( arr ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes an ndarray as a string (large array, complex128, delegation)', function test( t ) { + var expected; + var strides; + var actual; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + var i; + + dtype = 'complex128'; + buffer = new Complex128Array( 10000 ); + for ( i = 0; i < 10000; i++ ) { + if ( i < 3 ) { + buffer.set( new Complex128( i+1, i+1 ), i ); + } else if ( i >= 9997 ) { + buffer.set( new Complex128( i-9993, i-9993 ), i ); + } else { + buffer.set( new Complex128( 0, 0 ), i ); + } + } + shape = [ 10000 ]; + order = 'row-major'; + strides = [ 1 ]; + offset = 0; + + arr = ndarray( dtype, buffer, shape, strides, offset, order ); + + expected = 'ndarray( \'complex128\', new Complex128Array( [ 1, 1, 2, 2, 3, 3, ..., 4, 4, 5, 5, 6, 6 ] ), [ 10000 ], [ 1 ], 0, \'row-major\' )'; + actual = ndarray2localeString( arr ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes an ndarray as a string (large array, null/undefined, delegation)', function test( t ) { + var expected; + var strides; + var actual; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + var i; + + dtype = 'generic'; + buffer = []; + for ( i = 0; i < 10000; i++ ) { + if ( i < 3 ) { + buffer.push( null ); + } else if ( i >= 9997 ) { + buffer.push( void 0 ); + } else { + buffer.push( 0 ); + } + } + shape = [ 10000 ]; + order = 'row-major'; + strides = [ 1 ]; + offset = 0; + + arr = ndarray( dtype, buffer, shape, strides, offset, order ); + + expected = 'ndarray( \'generic\', [ null, null, null, ..., undefined, undefined, undefined ], [ 10000 ], [ 1 ], 0, \'row-major\' )'; + actual = ndarray2localeString( arr ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes an ndarray as a string (locale, delegation)', function test( t ) { + var expected; + var strides; + var actual; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + + dtype = 'generic'; + buffer = [ 1234.567, 9876.543 ]; + shape = [ 2 ]; + order = 'row-major'; + strides = [ 1 ]; + offset = 0; + + arr = ndarray( dtype, buffer, shape, strides, offset, order ); + + expected = 'ndarray( \'generic\', [ 1,234.567, 9,876.543 ], [ 2 ], [ 1 ], 0, \'row-major\' )'; + actual = ndarray2localeString( arr, 'en-US' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes an ndarray as a string (locale, options, delegation)', function test( t ) { + var expected; + var strides; + var actual; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + + dtype = 'generic'; + buffer = [ 1234.567, 9876.543 ]; + shape = [ 2 ]; + order = 'row-major'; + strides = [ 1 ]; + offset = 0; + + arr = ndarray( dtype, buffer, shape, strides, offset, order ); + + expected = 'ndarray( \'generic\', [ 1,234.6, 9,876.5 ], [ 2 ], [ 1 ], 0, \'row-major\' )'; + actual = ndarray2localeString( arr, 'en-US', { + 'maximumFractionDigits': 1 + }); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function serializes an ndarray-like object as a string (large array, no toLocaleString method)', function test( t ) { + var actual; + var buffer; + var arr; + var i; + + buffer = new Float64Array( 101 ); + for ( i = 0; i < 101; i++ ) { + buffer[ i ] = i; + } + arr = { + 'dtype': 'float64', + 'data': buffer, + 'shape': [ 101 ], + 'strides': [ 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 1, + 'length': 101, + 'flags': { + 'READONLY': false + }, + 'get': getter, + 'set': noop + }; + + actual = ndarray2localeString( arr ); + t.strictEqual( typeof actual, 'string', 'returns expected value' ); + t.notEqual( actual.indexOf( '...' ), -1, 'contains ellipsis' ); + t.notEqual( actual.indexOf( '0, 1, 2' ), -1, 'contains first three values' ); + t.notEqual( actual.indexOf( '98, 99, 100' ), -1, 'contains last three values' ); + + t.end(); + + function getter( i ) { + return arr.data[ arr.offset + ( arr.strides[0]*i ) ]; + } +}); + +tape( 'the function serializes an ndarray-like object as a string (large array, complex, no toLocaleString method)', function test( t ) { + var actual; + var buffer; + var arr; + var i; + + buffer = new Complex64Array( 101 ); + for ( i = 0; i < 101; i++ ) { + buffer.set( new Complex64( i, i ), i ); + } + arr = { + 'dtype': 'complex64', + 'data': buffer, + 'shape': [ 101 ], + 'strides': [ 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 1, + 'length': 101, + 'flags': { + 'READONLY': false + }, + 'get': getter, + 'set': noop + }; + + actual = ndarray2localeString( arr ); + t.strictEqual( typeof actual, 'string', 'returns expected value' ); + t.notEqual( actual.indexOf( '...' ), -1, 'contains ellipsis' ); + + t.end(); + + function getter( i ) { + return arr.data.get( arr.offset + ( arr.strides[0]*i ) ); + } +}); + +tape( 'the function serializes an ndarray-like object as a string (null/undefined values, no toLocaleString method)', function test( t ) { + var expected; + var actual; + var arr; + + arr = { + 'dtype': 'generic', + 'data': [ null, void 0, null, void 0 ], + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'length': 4, + 'flags': { + 'READONLY': false + }, + 'get': getter, + 'set': noop + }; + + expected = 'ndarray( \'generic\', [ null, undefined, null, undefined ], [ 2, 2 ], [ 2, 1 ], 0, \'row-major\' )'; + actual = ndarray2localeString( arr ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); + + function getter( i, j ) { + return arr.data[ arr.offset+sub2ind( [ 2, 2 ], i, j ) ]; + } +}); + +tape( 'the function serializes an ndarray-like object as a string (with locale, no toLocaleString method)', function test( t ) { + var expected; + var actual; + var arr; + + arr = { + 'dtype': 'generic', + 'data': [ 1234.567, 9876.543, 111.222, 333.444 ], + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'length': 4, + 'flags': { + 'READONLY': false + }, + 'get': getter, + 'set': noop + }; + + expected = 'ndarray( \'generic\', [ 1,234.567, 9,876.543, 111.222, 333.444 ], [ 2, 2 ], [ 2, 1 ], 0, \'row-major\' )'; + actual = ndarray2localeString( arr, 'en-US' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); + + function getter( i, j ) { + return arr.data[ arr.offset+sub2ind( [ 2, 2 ], i, j ) ]; + } +}); + +tape( 'the function serializes an ndarray-like object as a string (with locale and options, no toLocaleString method)', function test( t ) { + var expected; + var actual; + var arr; + + arr = { + 'dtype': 'generic', + 'data': [ 1234.567, 9876.543, 111.222, 333.444 ], + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'length': 4, + 'flags': { + 'READONLY': false + }, + 'get': getter, + 'set': noop + }; + + expected = 'ndarray( \'generic\', [ 1,234.6, 9,876.5, 111.2, 333.4 ], [ 2, 2 ], [ 2, 1 ], 0, \'row-major\' )'; + actual = ndarray2localeString( arr, 'en-US', { + 'maximumFractionDigits': 1 + }); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); + + function getter( i, j ) { + return arr.data[ arr.offset+sub2ind( [ 2, 2 ], i, j ) ]; + } +});