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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | 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 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 | /**
* @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 no-underscore-dangle */
'use strict';
// MODULES //
var isComplexDataType = require( '@stdlib/ndarray/base/assert/is-complex-floating-point-data-type' );
var isUndefinedOrNull = require( '@stdlib/assert/is-undefined-or-null' );
var format = require( '@stdlib/string/format' );
var replace = require( '@stdlib/string/replace' );
var join = require( '@stdlib/array/base/join' );
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}} ] )'
};
// FUNCTIONS //
/**
* Serializes a value to a string.
*
* @private
* @param {*} value - input value
* @returns {string} output string
*/
function toStr( value ) {
return String( value );
}
/**
* Serializes a value to a locale-aware string.
*
* @private
* @param {*} value - input value
* @param {(string|Array<string>)} [locales] - locale identifier(s)
* @param {Object} [options] - configuration options
* @returns {string} output string
*/
function toLocaleStr( value, locales, options ) {
if ( !isUndefinedOrNull( value ) && value.toLocaleString ) {
return value.toLocaleString( locales, options );
}
return toStr( value );
}
// MAIN //
/**
* Serializes an ndarray to a string.
*
* ## Notes
*
* - The method does **not** serialize data outside of the buffer region defined by the array configuration.
*
* @private
* @param {ndarray} ctx - ndarray object
* @param {string} method - element serialization method (i.e., `toString` or `toLocaleString`)
* @param {(string|Array<string>|null)} locales - locale identifier(s)
* @param {(Object|null)} options - configuration options
* @returns {string} string representation
*/
function serialize2string( ctx, method, locales, options ) {
var isCmplx;
var buffer;
var ndims;
var ctor;
var str;
var dt;
var f;
var v;
var i;
if ( method === 'toLocaleString' ) {
f = toLocaleStr;
} else {
f = toStr;
}
ndims = ctx._shape.length;
dt = String( ctx._dtype );
isCmplx = isComplexDataType( dt );
// Function to invoke to create an ndarray:
str = format( 'ndarray( \'%s\', ', dt );
// Data buffer parameter...
buffer = '';
if ( ctx._length <= 100 ) {
if ( isCmplx ) {
for ( i = 0; i < ctx._length; i++ ) {
v = ctx.iget( i );
buffer += f( real( v ), locales, options ) + ', ' + f( imag( v ), locales, options );
if ( i < ctx._length-1 ) {
buffer += ', ';
}
}
} else {
for ( i = 0; i < ctx._length; i++ ) {
v = ctx.iget( i );
buffer += f( v, locales, options );
if ( i < ctx._length-1 ) {
buffer += ', ';
}
}
}
} else {
// First three values...
if ( isCmplx ) {
for ( i = 0; i < 3; i++ ) {
v = ctx.iget( i );
buffer += f( real( v ), locales, options ) + ', ' + f( imag( v ), locales, options );
if ( i < 2 ) {
buffer += ', ';
}
}
} else {
for ( i = 0; i < 3; i++ ) {
v = ctx.iget( i );
buffer += f( v, locales, options );
if ( i < 2 ) {
buffer += ', ';
}
}
}
buffer += ', ..., ';
// Last three values...
if ( isCmplx ) {
for ( i = 2; i >= 0; i-- ) {
v = ctx.iget( ctx._length-1-i );
buffer += f( real( v ), locales, options ) + ', ' + f( imag( v ), locales, options );
if ( i > 0 ) {
buffer += ', ';
}
}
} else {
for ( i = 2; i >= 0; i-- ) {
v = ctx.iget( ctx._length-1-i );
buffer += f( v, locales, options );
if ( i > 0 ) {
buffer += ', ';
}
}
}
}
ctor = CTORS[ dt ] || CTORS[ 'generic' ]; // NOTE: we fallback to "generic" here, even though the underlying data buffer may be an exotic array type (e.g., struct array or specialized accessor array, etc). In which case, one would not be able to simply copy-paste the output string in order to create an equivalent ndarray, but this is likely okay, as such an end-user use case is likely not common.
str += replace( ctor, '{{data}}', buffer );
str += ', ';
// Array shape...
if ( ndims === 0 ) {
str += '[]';
} else {
str += format( '[ %s ]', join( ctx._shape, ', ' ) );
}
str += ', ';
// Stride array...
str += '[ ';
if ( ndims === 0 ) {
str += '0';
} else {
for ( i = 0; i < ndims; i++ ) {
if ( ctx._strides[ i ] < 0 ) {
str += -ctx._strides[ i ];
} else {
str += ctx._strides[ i ];
}
if ( i < ndims-1 ) {
str += ', ';
}
}
}
str += ' ]';
str += ', ';
// Buffer offset:
str += '0';
str += ', ';
// Order:
str += format( '\'%s\'', ctx._order );
// Close the function call:
str += ' )';
return str;
}
// EXPORTS //
module.exports = serialize2string;
|