All files tolocalestring.js

98.05% Statements 202/206
92.5% Branches 37/40
100% Functions 1/1
98.05% Lines 202/206

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 2071x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 18x 20x 2x 2x     20x 19x 20x 1x 1x     20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 14x 4x 16x 16x 16x 12x 12x 16x 14x 10x 30x 30x 20x 20x 30x 10x 20x 6x 6x 4x 12x 12x 12x 8x 8x 12x 6x 2x 6x 6x 4x 4x 6x 2x 6x 6x 6x 6x 4x 12x 12x 12x 8x 8x 12x 6x 2x 6x 6x 4x 4x 6x 2x 6x 20x 20x 20x 20x 20x 20x 2x 20x 18x 18x 20x 20x 20x 20x 20x 2x 20x 18x 28x 4x 28x 24x 24x 28x 10x 10x 28x 18x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 1x 1x 1x 1x 1x  
/**
* @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 isString = require( '@stdlib/assert/is-string' ).isPrimitive;
var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives;
var isObject = require( '@stdlib/assert/is-plain-object' );
var format = require( '@stdlib/string/format' );
var replace = require( '@stdlib/string/replace' );
var real = require( '@stdlib/complex/float64/real' );
var imag = require( '@stdlib/complex/float64/imag' );
 
 
// 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}} ] )'
};
 
 
// MAIN //
 
/**
* Serializes an ndarray as a locale-aware string.
*
* ## Notes
*
* -   The method does **not** serialize data outside of the buffer region defined by the array configuration.
*
* @private
* @param {(string|Array<string>)} [locales] - locale identifier(s)
* @param {Object} [options] - configuration options
* @throws {TypeError} first argument must be a string or an array of strings
* @throws {TypeError} options argument must be an object
* @returns {string} string representation
*/
function toLocaleString( locales, options ) { // eslint-disable-line stdlib/no-redeclare
	/* eslint-disable no-invalid-this */
	var buffer;
	var ndims;
	var ctor;
	var opts;
	var loc;
	var str;
	var dt;
	var v;
	var i;
 
	if ( arguments.length === 0 ) {
		loc = [];
	} else if ( isString( locales ) || isStringArray( locales ) ) {
		loc = locales;
	} else {
		throw new TypeError( format( 'invalid argument. First argument must be a string or an array of strings. Value: `%s`.', locales ) );
	}
	if ( arguments.length < 2 ) {
		opts = {};
	} else if ( isObject( options ) ) {
		opts = options;
	} else {
		throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
	}
 
	ndims = this._shape.length;
	dt = this._dtype;
 
	// Function to invoke to create an ndarray:
	str = 'ndarray( \''+dt+'\', ';
 
	// Data buffer parameter...
	buffer = '';
	if ( this._length <= 100 ) {
		if ( dt === 'complex64' || dt === 'complex128' ) {
			for ( i = 0; i < this._length; i++ ) {
				v = this.iget( i );
				buffer += real( v ).toLocaleString( loc, opts ) + ', ' + imag( v ).toLocaleString( loc, opts );
				if ( i < this._length-1 ) {
					buffer += ', ';
				}
			}
		} else {
			for ( i = 0; i < this._length; i++ ) {
				buffer += this.iget( i ).toLocaleString( loc, opts );
				if ( i < this._length-1 ) {
					buffer += ', ';
				}
			}
		}
	} else {
		// First three values...
		if ( dt === 'complex64' || dt === 'complex128' ) {
			for ( i = 0; i < 3; i++ ) {
				v = this.iget( i );
				buffer += real( v ).toLocaleString( loc, opts ) + ', ' + imag( v ).toLocaleString( loc, opts );
				if ( i < 2 ) {
					buffer += ', ';
				}
			}
		} else {
			for ( i = 0; i < 3; i++ ) {
				buffer += this.iget( i ).toLocaleString( loc, opts );
				if ( i < 2 ) {
					buffer += ', ';
				}
			}
		}
		buffer += ', ..., ';
 
		// Last three values...
		if ( dt === 'complex64' || dt === 'complex128' ) {
			for ( i = 2; i >= 0; i-- ) {
				v = this.iget( this._length-1-i );
				buffer += real( v ).toLocaleString( loc, opts ) + ', ' + imag( v ).toLocaleString( loc, opts );
				if ( i > 0 ) {
					buffer += ', ';
				}
			}
		} else {
			for ( i = 2; i >= 0; i-- ) {
				buffer += this.iget( this._length-1-i ).toLocaleString( loc, opts ); // eslint-disable-line max-len
				if ( i > 0 ) {
					buffer += ', ';
				}
			}
		}
	}
	ctor = CTORS[ this.dtype ];
	str += replace( ctor, '{{data}}', buffer );
	str += ', ';
 
	// Array shape...
	if ( ndims === 0 ) {
		str += '[]';
	} else {
		str += '[ ' + this._shape.join( ', ' ) + ' ]';
	}
	str += ', ';
 
	// Stride array...
	str += '[ ';
	if ( ndims === 0 ) {
		str += '0';
	} else {
		for ( i = 0; i < ndims; i++ ) {
			if ( this._strides[ i ] < 0 ) {
				str += -this._strides[ i ];
			} else {
				str += this._strides[ i ];
			}
			if ( i < ndims-1 ) {
				str += ', ';
			}
		}
	}
	str += ' ]';
	str += ', ';
 
	// Buffer offset:
	str += '0';
	str += ', ';
 
	// Order:
	str += '\'' + this._order + '\'';
 
	// Close the function call:
	str += ' )';
	return str;
 
	/* eslint-enable no-invalid-this */
}
 
 
// EXPORTS //
 
module.exports = toLocaleString;