Skip to content

Commit bf3f48b

Browse files
committed
Refactor to support complex number typed arrays
1 parent b21f7f6 commit bf3f48b

File tree

9 files changed

+112
-21
lines changed

9 files changed

+112
-21
lines changed

lib/node_modules/@stdlib/array/to-json/README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ For guidance on reviving a JSON-serialized typed array, see [`reviver()`][@stdli
7474

7575
- [`Float64Array`][@stdlib/array/float64]
7676
- [`Float32Array`][@stdlib/array/float32]
77+
- [`Complex128Array`][@stdlib/array/complex128]
78+
- [`Complex64Array`][@stdlib/array/complex64]
7779
- [`Int32Array`][@stdlib/array/int32]
7880
- [`Uint32Array`][@stdlib/array/uint32]
7981
- [`Int16Array`][@stdlib/array/int16]
@@ -84,7 +86,7 @@ For guidance on reviving a JSON-serialized typed array, see [`reviver()`][@stdli
8486

8587
- The implementation provides basic support for custom typed arrays and sets the `type` field to the closest known typed array type.
8688

87-
<!-- eslint-disable no-restricted-syntax, no-useless-constructor, new-cap, stdlib/require-globals -->
89+
<!-- eslint-disable no-restricted-syntax, no-useless-constructor, new-cap, stdlib/require-globals, node/no-unsupported-features/es-syntax -->
8890

8991
```javascript
9092
class CustomArray extends Float64Array() {
@@ -126,6 +128,8 @@ var Uint16Array = require( '@stdlib/array/uint16' );
126128
var Int8Array = require( '@stdlib/array/int8' );
127129
var Uint8Array = require( '@stdlib/array/uint8' );
128130
var Uint8ClampedArray = require( '@stdlib/array/uint8c' );
131+
var Complex64Array = require( '@stdlib/array/complex64' );
132+
var Complex128Array = require( '@stdlib/array/complex128' );
129133
var toJSON = require( '@stdlib/array/to-json' );
130134
131135
var arr = new Float64Array( [ 5.0, 3.0 ] );
@@ -146,6 +150,24 @@ json = toJSON( arr );
146150
}
147151
*/
148152
153+
arr = new Complex128Array( [ 5.0, 3.0 ] );
154+
json = toJSON( arr );
155+
/* returns
156+
{
157+
'type': 'Complex128Array',
158+
'data': [ 5.0, 3.0 ]
159+
}
160+
*/
161+
162+
arr = new Complex64Array( [ 5.0, 3.0 ] );
163+
json = toJSON( arr );
164+
/* returns
165+
{
166+
'type': 'Complex64Array',
167+
'data': [ 5.0, 3.0 ]
168+
}
169+
*/
170+
149171
arr = new Int32Array( [ -5, 3 ] );
150172
json = toJSON( arr );
151173
/* returns
@@ -246,6 +268,10 @@ json = toJSON( arr );
246268

247269
[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32
248270

271+
[@stdlib/array/complex128]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex128
272+
273+
[@stdlib/array/complex64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex64
274+
249275
[@stdlib/array/int32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/int32
250276

251277
[@stdlib/array/uint32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/uint32

lib/node_modules/@stdlib/array/to-json/docs/repl.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
- Int8Array
1414
- Uint8Array
1515
- Uint8ClampedArray
16+
- Complex64Array
17+
- Complex128Array
1618

1719
The returned JSON object has the following properties:
1820

lib/node_modules/@stdlib/array/to-json/docs/types/index.d.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020

2121
/// <reference types="@stdlib/types"/>
2222

23-
import { TypedArray } from '@stdlib/types/array';
23+
import { TypedArray, ComplexTypedArray } from '@stdlib/types/array';
24+
25+
/**
26+
* Typed array data type.
27+
*/
28+
type dtype = 'Float64Array' | 'Float32Array' | 'Int32Array' | 'Uint32Array' | 'Int16Array' | 'Uint16Array' | 'Int8Array' | 'Uint8Array' | 'Uint8ClampedArray' | 'Complex64Array' | 'Complex128Array'; // tslint:disable-line:max-line-length
2429

2530
/**
2631
* JSON representation of typed array.
@@ -29,7 +34,7 @@ interface JSONRepresentation {
2934
/**
3035
* Typed array type.
3136
*/
32-
type: 'Float64Array' | 'Float32Array' | 'Int32Array' | 'Uint32Array' | 'Int16Array' | 'Uint16Array' | 'Int8Array' | 'Uint8Array' | 'Uint8ClampedArray'; // tslint:disable-line:max-line-length
37+
type: dtype;
3338

3439
/**
3540
* Typed array data as a generic array.
@@ -56,7 +61,7 @@ interface JSONRepresentation {
5661
* var json = toJSON( arr );
5762
* // returns { 'type': 'Float64Array', 'data': [ 5.0, 3.0 ] }
5863
*/
59-
declare function toJSON( arr: TypedArray ): JSONRepresentation;
64+
declare function toJSON( arr: TypedArray | ComplexTypedArray ): JSONRepresentation; // tslint:disable-line:max-line-length
6065

6166

6267
// EXPORTS //

lib/node_modules/@stdlib/array/to-json/examples/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ var Uint16Array = require( '@stdlib/array/uint16' );
2727
var Int8Array = require( '@stdlib/array/int8' );
2828
var Uint8Array = require( '@stdlib/array/uint8' );
2929
var Uint8ClampedArray = require( '@stdlib/array/uint8c' );
30+
var Complex64Array = require( '@stdlib/array/complex64' );
31+
var Complex128Array = require( '@stdlib/array/complex128' );
3032
var toJSON = require( './../lib' );
3133

3234
var arr = new Float64Array( [ 5.0, 3.0 ] );
@@ -47,6 +49,24 @@ console.log( toJSON( arr ) );
4749
}
4850
*/
4951

52+
arr = new Complex128Array( [ 5.0, -3.0 ] );
53+
console.log( toJSON( arr ) );
54+
/* =>
55+
{
56+
'type': 'Complex128Array',
57+
'data': [ 5.0, -3.0 ]
58+
}
59+
*/
60+
61+
arr = new Complex64Array( [ 5.0, -3.0 ] );
62+
console.log( toJSON( arr ) );
63+
/* =>
64+
{
65+
'type': 'Complex64Array',
66+
'data': [ 5.0, -3.0 ]
67+
}
68+
*/
69+
5070
arr = new Int32Array( [ -5, 3 ] );
5171
console.log( toJSON( arr ) );
5272
/* =>

lib/node_modules/@stdlib/array/to-json/lib/ctors.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ var Int32Array = require( '@stdlib/array/int32' );
2929
var Uint32Array = require( '@stdlib/array/uint32' );
3030
var Float32Array = require( '@stdlib/array/float32' );
3131
var Float64Array = require( '@stdlib/array/float64' );
32+
var Complex64Array = require( '@stdlib/array/complex64' );
33+
var Complex128Array = require( '@stdlib/array/complex128' );
3234

3335

3436
// MAIN //
@@ -42,7 +44,9 @@ var CTORS = [
4244
[ Uint16Array, 'Uint16Array' ],
4345
[ Int8Array, 'Int8Array' ],
4446
[ Uint8Array, 'Uint8Array' ],
45-
[ Uint8ClampedArray, 'Uint8ClampedArray' ]
47+
[ Uint8ClampedArray, 'Uint8ClampedArray' ],
48+
[ Complex64Array, 'Complex64Array' ],
49+
[ Complex128Array, 'Complex128Array' ]
4650
];
4751

4852

lib/node_modules/@stdlib/array/to-json/lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434

3535
// MODULES //
3636

37-
var toJSON = require( './to_json.js' );
37+
var main = require( './main.js' );
3838

3939

4040
// EXPORTS //
4141

42-
module.exports = toJSON;
42+
module.exports = main;

lib/node_modules/@stdlib/array/to-json/lib/to_json.js renamed to lib/node_modules/@stdlib/array/to-json/lib/main.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
// MODULES //
2222

2323
var isTypedArray = require( '@stdlib/assert/is-typed-array' );
24+
var isComplexTypedArray = require( '@stdlib/assert/is-complex-typed-array' );
25+
var reinterpret64 = require( '@stdlib/strided/base/reinterpret-complex64' );
26+
var reinterpret128 = require( '@stdlib/strided/base/reinterpret-complex128' );
2427
var typeName = require( './type.js' );
2528

2629

@@ -47,16 +50,27 @@ var typeName = require( './type.js' );
4750
* // returns { 'type': 'Float64Array', 'data': [ 5.0, 3.0 ] }
4851
*/
4952
function toJSON( arr ) {
53+
var data;
5054
var out;
5155
var i;
52-
if ( !isTypedArray( arr ) ) {
56+
57+
if ( isTypedArray( arr ) ) {
58+
data = arr;
59+
} else if ( isComplexTypedArray( arr ) ) {
60+
if ( arr.BYTES_PER_ELEMENT === 8 ) {
61+
data = reinterpret64( arr, 0 );
62+
} else { // arr.BYTES_PER_ELEMENT === 16
63+
data = reinterpret128( arr, 0 );
64+
}
65+
} else {
5366
throw new TypeError( 'invalid argument. Must provide a typed array. Value: `' + arr + '`.' );
5467
}
55-
out = {};
56-
out.type = typeName( arr );
57-
out.data = [];
58-
for ( i = 0; i < arr.length; i++ ) {
59-
out.data.push( arr[ i ] );
68+
out = {
69+
'type': typeName( arr ),
70+
'data': []
71+
};
72+
for ( i = 0; i < data.length; i++ ) {
73+
out.data.push( data[ i ] );
6074
}
6175
return out;
6276
}

lib/node_modules/@stdlib/array/to-json/test/test.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ var Int32Array = require( '@stdlib/array/int32' );
3434
var Uint32Array = require( '@stdlib/array/uint32' );
3535
var Float32Array = require( '@stdlib/array/float32' );
3636
var Float64Array = require( '@stdlib/array/float64' );
37+
var Complex64Array = require( '@stdlib/array/complex64' );
38+
var Complex128Array = require( '@stdlib/array/complex128' );
3739
var toJSON = require( './../lib' );
3840

3941

@@ -115,7 +117,9 @@ tape( 'the JSON object includes a typed array type', function test( t ) {
115117
new Uint16Array( 1 ),
116118
new Int8Array( 1 ),
117119
new Uint8Array( 1 ),
118-
new Uint8ClampedArray( 1 )
120+
new Uint8ClampedArray( 1 ),
121+
new Complex64Array( 1 ),
122+
new Complex128Array( 1 )
119123
];
120124

121125
expected = [
@@ -127,7 +131,9 @@ tape( 'the JSON object includes a typed array type', function test( t ) {
127131
'Uint16Array',
128132
'Int8Array',
129133
'Uint8Array',
130-
'Uint8ClampedArray'
134+
'Uint8ClampedArray',
135+
'Complex64Array',
136+
'Complex128Array'
131137
];
132138

133139
for ( i = 0; i < values.length; i++ ) {
@@ -152,7 +158,9 @@ tape( 'the JSON object includes a data property', function test( t ) {
152158
new Uint16Array( [ 6.0 ] ),
153159
new Int8Array( [ 7.0 ] ),
154160
new Uint8Array( [ 8.0 ] ),
155-
new Uint8ClampedArray( [ 9.0 ] )
161+
new Uint8ClampedArray( [ 9.0 ] ),
162+
new Complex64Array( [ 1.0, 2.0 ] ),
163+
new Complex128Array( [ 1.0, 2.0 ] )
156164
];
157165

158166
expected = [
@@ -164,7 +172,9 @@ tape( 'the JSON object includes a data property', function test( t ) {
164172
[ 6.0 ],
165173
[ 7.0 ],
166174
[ 8.0 ],
167-
[ 9.0 ]
175+
[ 9.0 ],
176+
[ 1.0, 2.0 ],
177+
[ 1.0, 2.0 ]
168178
];
169179

170180
for ( i = 0; i < values.length; i++ ) {

lib/node_modules/@stdlib/array/to-json/test/test.type.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ var Int32Array = require( '@stdlib/array/int32' );
3131
var Uint32Array = require( '@stdlib/array/uint32' );
3232
var Float32Array = require( '@stdlib/array/float32' );
3333
var Float64Array = require( '@stdlib/array/float64' );
34+
var Complex64Array = require( '@stdlib/array/complex64' );
35+
var Complex128Array = require( '@stdlib/array/complex128' );
3436
var typeName = require( './../lib/type.js' );
3537

3638

@@ -56,7 +58,9 @@ tape( 'if provided a typed array, the function returns the closest typed array t
5658
new Uint16Array( [ 5, 3 ] ),
5759
new Int8Array( [ 5, 3 ] ),
5860
new Uint8Array( [ 5, 3 ] ),
59-
new Uint8ClampedArray( [ 5, 3 ] )
61+
new Uint8ClampedArray( [ 5, 3 ] ),
62+
new Complex64Array( [ 5.0, 3.0 ] ),
63+
new Complex128Array( [ 5.0, 3.0 ] )
6064
];
6165

6266
expected = [
@@ -68,7 +72,9 @@ tape( 'if provided a typed array, the function returns the closest typed array t
6872
'Uint16Array',
6973
'Int8Array',
7074
'Uint8Array',
71-
'Uint8ClampedArray'
75+
'Uint8ClampedArray',
76+
'Complex64Array',
77+
'Complex128Array'
7278
];
7379

7480
for ( i = 0; i < values.length; i++ ) {
@@ -96,7 +102,9 @@ tape( 'if provided a typed array from a different realm, the function returns th
96102
new Uint16Array( [ 5, 3 ] ),
97103
new Int8Array( [ 5, 3 ] ),
98104
new Uint8Array( [ 5, 3 ] ),
99-
new Uint8ClampedArray( [ 5, 3 ] )
105+
new Uint8ClampedArray( [ 5, 3 ] ),
106+
new Complex64Array( [ 5.0, 3.0 ] ),
107+
new Complex128Array( [ 5.0, 3.0 ] )
100108
];
101109

102110
expected = [
@@ -108,7 +116,9 @@ tape( 'if provided a typed array from a different realm, the function returns th
108116
'Uint16Array',
109117
'Int8Array',
110118
'Uint8Array',
111-
'Uint8ClampedArray'
119+
'Uint8ClampedArray',
120+
'Complex64Array',
121+
'Complex128Array'
112122
];
113123

114124
for ( i = 0; i < values.length; i++ ) {

0 commit comments

Comments
 (0)