Skip to content

Commit 498395e

Browse files
committed
refactor: apply suggestions from code review
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent feaff07 commit 498395e

File tree

2 files changed

+163
-3
lines changed

2 files changed

+163
-3
lines changed

lib/node_modules/@stdlib/ndarray/to-string/lib/main.js

Lines changed: 162 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,40 @@
2020

2121
// MODULES //
2222

23+
var isComplexDataType = require( '@stdlib/ndarray/base/assert/is-complex-floating-point-data-type' );
2324
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
24-
var ndarray2localeString = require( '@stdlib/ndarray/to-locale-string' );
25+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
26+
var getDType = require( '@stdlib/ndarray/dtype' );
27+
var getShape = require( '@stdlib/ndarray/shape' );
28+
var getStrides = require( '@stdlib/ndarray/strides' );
29+
var getOrder = require( '@stdlib/ndarray/order' );
30+
var numel = require( '@stdlib/ndarray/base/numel' );
31+
var ind2sub = require( '@stdlib/ndarray/base/ind2sub' );
32+
var replace = require( '@stdlib/string/replace' );
2533
var format = require( '@stdlib/string/format' );
34+
var join = require( '@stdlib/array/base/join' );
35+
var real = require( '@stdlib/complex/float64/real' );
36+
var imag = require( '@stdlib/complex/float64/imag' );
37+
38+
39+
// VARIABLES //
40+
41+
var CTORS = {
42+
'int8': 'new Int8Array( [ {{data}} ] )',
43+
'uint8': 'new Uint8Array( [ {{data}} ] )',
44+
'uint8c': 'new Uint8ClampedArray( [ {{data}} ] )',
45+
'int16': 'new Int16Array( [ {{data}} ] )',
46+
'uint16': 'new Uint16Array( [ {{data}} ] )',
47+
'int32': 'new Int32Array( [ {{data}} ] )',
48+
'uint32': 'new Uint32Array( [ {{data}} ] )',
49+
'float32': 'new Float32Array( [ {{data}} ] )',
50+
'float64': 'new Float64Array( [ {{data}} ] )',
51+
'generic': '[ {{data}} ]',
52+
'binary': 'new Buffer( [ {{data}} ] )',
53+
'complex64': 'new Complex64Array( [ {{data}} ] )',
54+
'complex128': 'new Complex128Array( [ {{data}} ] )',
55+
'bool': 'new BooleanArray( [ {{data}} ] )'
56+
};
2657

2758

2859
// FUNCTIONS //
@@ -43,6 +74,32 @@ function hasMethod( obj, method ) {
4374
return ( typeof obj[ method ] === 'function' && obj[ method ] !== Object.prototype[ method ] );
4475
}
4576

77+
/**
78+
* Serializes an ndarray element to a string.
79+
*
80+
* @private
81+
* @param {ndarrayLike} x - input ndarray
82+
* @param {NonNegativeIntegerArray} sub - subscripts
83+
* @returns {string} serialized element
84+
*/
85+
function serializeElement( x, sub ) {
86+
var v = x.get.apply( x, sub );
87+
return String( v );
88+
}
89+
90+
/**
91+
* Serializes a complex ndarray element to a string.
92+
*
93+
* @private
94+
* @param {ndarrayLike} x - input ndarray
95+
* @param {NonNegativeIntegerArray} sub - subscripts
96+
* @returns {string} serialized element
97+
*/
98+
function serializeComplexElement( x, sub ) {
99+
var v = x.get.apply( x, sub );
100+
return String( real( v ) ) + ', ' + String( imag( v ) );
101+
}
102+
46103

47104
// MAIN //
48105

@@ -67,14 +124,117 @@ function hasMethod( obj, method ) {
67124
* // returns <string>
68125
*/
69126
function ndarray2string( x ) {
127+
var serialize;
128+
var isCmplx;
129+
var buffer;
130+
var ndims;
131+
var ctor;
132+
var len;
133+
var str;
134+
var ord;
135+
var ust;
136+
var sh;
137+
var st;
138+
var dt;
139+
var i;
140+
70141
if ( !isndarrayLike( x ) ) {
71142
throw new TypeError( format( 'invalid argument. Must provide an ndarray. Value: `%s`.', x ) );
72143
}
73144
// Defer to input argument's custom implementation, if already defined...
74145
if ( hasMethod( x, 'toString' ) ) {
75146
return x.toString();
76147
}
77-
return ndarray2localeString( x );
148+
// Resolve array meta data:
149+
dt = getDType( x );
150+
sh = getShape( x );
151+
st = getStrides( x );
152+
ord = getOrder( x );
153+
ndims = sh.length;
154+
isCmplx = isComplexDataType( dt );
155+
len = numel( sh );
156+
157+
// Compute unit strides for iteration (as `x.get` handles stride/offset mapping):
158+
ust = shape2strides( sh, ord );
159+
160+
// Resolve element serialization function:
161+
if ( isCmplx ) {
162+
serialize = serializeComplexElement;
163+
} else {
164+
serialize = serializeElement;
165+
}
166+
167+
// Function to invoke to create an ndarray:
168+
str = format( 'ndarray( \'%s\', ', String( dt ) );
169+
170+
// Data buffer parameter...
171+
buffer = '';
172+
if ( len <= 100 ) {
173+
for ( i = 0; i < len; i++ ) {
174+
buffer += serialize( x, ind2sub( sh, ust, 0, ord, i, 'throw' ) );
175+
if ( i < len-1 ) {
176+
buffer += ', ';
177+
}
178+
}
179+
} else {
180+
// First three values...
181+
for ( i = 0; i < 3; i++ ) {
182+
buffer += serialize( x, ind2sub( sh, ust, 0, ord, i, 'throw' ) );
183+
if ( i < 2 ) {
184+
buffer += ', ';
185+
}
186+
}
187+
buffer += ', ..., ';
188+
189+
// Last three values...
190+
for ( i = 2; i >= 0; i-- ) {
191+
buffer += serialize( x, ind2sub( sh, ust, 0, ord, len-1-i, 'throw' ) );
192+
if ( i > 0 ) {
193+
buffer += ', ';
194+
}
195+
}
196+
}
197+
ctor = CTORS[ dt ] || CTORS[ 'generic' ];
198+
str += replace( ctor, '{{data}}', buffer );
199+
str += ', ';
200+
201+
// Array shape...
202+
if ( ndims === 0 ) {
203+
str += '[]';
204+
} else {
205+
str += format( '[ %s ]', join( sh, ', ' ) );
206+
}
207+
str += ', ';
208+
209+
// Stride array...
210+
str += '[ ';
211+
if ( ndims === 0 ) {
212+
str += '0';
213+
} else {
214+
for ( i = 0; i < ndims; i++ ) {
215+
if ( st[ i ] < 0 ) {
216+
str += -st[ i ];
217+
} else {
218+
str += st[ i ];
219+
}
220+
if ( i < ndims-1 ) {
221+
str += ', ';
222+
}
223+
}
224+
}
225+
str += ' ]';
226+
str += ', ';
227+
228+
// Buffer offset:
229+
str += '0';
230+
str += ', ';
231+
232+
// Order:
233+
str += format( '\'%s\'', ord );
234+
235+
// Close the function call:
236+
str += ' )';
237+
return str;
78238
}
79239

80240

lib/node_modules/@stdlib/ndarray/to-string/test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ tape( 'the function serializes an ndarray-like object as a string (column-major)
350350
'set': noop
351351
};
352352

353-
expected = 'ndarray( \'generic\', [ 1, 2, 3, 4 ], [ 2, 2 ], [ 1, 2 ], 0, \'column-major\' )';
353+
expected = 'ndarray( \'generic\', [ 4, 3, 2, 1 ], [ 2, 2 ], [ 1, 2 ], 0, \'column-major\' )';
354354
actual = ndarray2string( arr );
355355
t.strictEqual( actual, expected, 'returns expected value' );
356356

0 commit comments

Comments
 (0)