Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@stdlib/array-to-json

Package Overview
Dependencies
Maintainers
4
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@stdlib/array-to-json - npm Package Compare versions

Comparing version
0.0.6
to
0.0.7
+81
lib/main.js
/**
* @license Apache-2.0
*
* Copyright (c) 2018 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 isTypedArray = require( '@stdlib/assert-is-typed-array' );
var isComplexTypedArray = require( '@stdlib/assert-is-complex-typed-array' );
var reinterpret64 = require( '@stdlib/strided-base-reinterpret-complex64' );
var reinterpret128 = require( '@stdlib/strided-base-reinterpret-complex128' );
var typeName = require( './type.js' );
// MAIN //
/**
* Returns a JSON representation of a typed array.
*
* ## Notes
*
* - We build a JSON object representing a typed array similar to how Node.js `Buffer` objects are represented. See [Buffer][1].
*
* [1]: https://nodejs.org/api/buffer.html#buffer_buf_tojson
*
* @param {TypedArray} arr - typed array to serialize
* @throws {TypeError} first argument must be a typed array
* @returns {Object} JSON representation
*
* @example
* var Float64Array = require( '@stdlib/array-float64' );
*
* var arr = new Float64Array( [ 5.0, 3.0 ] );
* var json = toJSON( arr );
* // returns { 'type': 'Float64Array', 'data': [ 5.0, 3.0 ] }
*/
function toJSON( arr ) {
var data;
var out;
var i;
if ( isTypedArray( arr ) ) {
data = arr;
} else if ( isComplexTypedArray( arr ) ) {
if ( arr.BYTES_PER_ELEMENT === 8 ) {
data = reinterpret64( arr, 0 );
} else { // arr.BYTES_PER_ELEMENT === 16
data = reinterpret128( arr, 0 );
}
} else {
throw new TypeError( 'invalid argument. Must provide a typed array. Value: `' + arr + '`.' );
}
out = {
'type': typeName( arr ),
'data': []
};
for ( i = 0; i < data.length; i++ ) {
out.data.push( data[ i ] );
}
return out;
}
// EXPORTS //
module.exports = toJSON;
+2
-0

@@ -16,2 +16,4 @@

- Uint8ClampedArray
- Complex64Array
- Complex128Array

@@ -18,0 +20,0 @@ The returned JSON object has the following properties:

+8
-3

@@ -23,5 +23,10 @@ /*

import { TypedArray } from '@stdlib/types/array';
import { RealOrComplexTypedArray } from '@stdlib/types/array';
/**
* Typed array data type.
*/
type dtype = 'Float64Array' | 'Float32Array' | 'Int32Array' | 'Uint32Array' | 'Int16Array' | 'Uint16Array' | 'Int8Array' | 'Uint8Array' | 'Uint8ClampedArray' | 'Complex64Array' | 'Complex128Array'; // tslint:disable-line:max-line-length
/**
* JSON representation of typed array.

@@ -33,3 +38,3 @@ */

*/
type: 'Float64Array' | 'Float32Array' | 'Int32Array' | 'Uint32Array' | 'Int16Array' | 'Uint16Array' | 'Int8Array' | 'Uint8Array' | 'Uint8ClampedArray'; // tslint:disable-line:max-line-length
type: dtype;

@@ -61,3 +66,3 @@ /**

*/
declare function toJSON( arr: TypedArray ): JSONRepresentation;
declare function toJSON( arr: RealOrComplexTypedArray ): JSONRepresentation;

@@ -64,0 +69,0 @@

@@ -32,2 +32,4 @@ /**

var Float64Array = require( '@stdlib/array-float64' );
var Complex64Array = require( '@stdlib/array-complex64' );
var Complex128Array = require( '@stdlib/array-complex128' );

@@ -46,3 +48,5 @@

[ Uint8Array, 'Uint8Array' ],
[ Uint8ClampedArray, 'Uint8ClampedArray' ]
[ Uint8ClampedArray, 'Uint8ClampedArray' ],
[ Complex64Array, 'Complex64Array' ],
[ Complex128Array, 'Complex128Array' ]
];

@@ -49,0 +53,0 @@

@@ -37,3 +37,3 @@ /**

var toJSON = require( './to_json.js' );
var main = require( './main.js' );

@@ -43,2 +43,2 @@

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

@@ -1,1 +0,1 @@

Copyright (c) 2016-2021 The Stdlib Authors.
Copyright (c) 2016-2022 The Stdlib Authors.
{
"name": "@stdlib/array-to-json",
"version": "0.0.6",
"version": "0.0.7",
"description": "Return a JSON representation of a typed array.",

@@ -40,2 +40,4 @@ "license": "Apache-2.0",

"dependencies": {
"@stdlib/array-complex128": "^0.0.x",
"@stdlib/array-complex64": "^0.0.x",
"@stdlib/array-float32": "^0.0.x",

@@ -51,3 +53,6 @@ "@stdlib/array-float64": "^0.0.x",

"@stdlib/assert-instance-of": "^0.0.x",
"@stdlib/assert-is-complex-typed-array": "^0.0.x",
"@stdlib/assert-is-typed-array": "^0.0.x",
"@stdlib/strided-base-reinterpret-complex128": "^0.0.x",
"@stdlib/strided-base-reinterpret-complex64": "^0.0.x",
"@stdlib/types": "^0.0.x",

@@ -54,0 +59,0 @@ "@stdlib/utils-constructor-name": "^0.0.x",

@@ -23,3 +23,3 @@ <!--

[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] [![dependencies][dependencies-image]][dependencies-url]
[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] <!-- [![dependencies][dependencies-image]][dependencies-url] -->

@@ -90,2 +90,4 @@ > Return a [JSON][json] representation of a typed array.

- [`Float32Array`][@stdlib/array/float32]
- [`Complex128Array`][@stdlib/array/complex128]
- [`Complex64Array`][@stdlib/array/complex64]
- [`Int32Array`][@stdlib/array/int32]

@@ -101,3 +103,3 @@ - [`Uint32Array`][@stdlib/array/uint32]

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

@@ -144,2 +146,4 @@ ```javascript

var Uint8ClampedArray = require( '@stdlib/array-uint8c' );
var Complex64Array = require( '@stdlib/array-complex64' );
var Complex128Array = require( '@stdlib/array-complex128' );
var toJSON = require( '@stdlib/array-to-json' );

@@ -165,2 +169,20 @@

arr = new Complex128Array( [ 5.0, 3.0 ] );
json = toJSON( arr );
/* returns
{
'type': 'Complex128Array',
'data': [ 5.0, 3.0 ]
}
*/
arr = new Complex64Array( [ 5.0, 3.0 ] );
json = toJSON( arr );
/* returns
{
'type': 'Complex64Array',
'data': [ 5.0, 3.0 ]
}
*/
arr = new Int32Array( [ -5, 3 ] );

@@ -242,2 +264,16 @@ json = toJSON( arr );

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
<section class="related">
* * *
## See Also
- <span class="package-name">[`@stdlib/array/reviver`][@stdlib/array/reviver]</span><span class="delimiter">: </span><span class="description">revive a JSON-serialized typed array.</span>
</section>
<!-- /.related -->
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

@@ -269,3 +305,3 @@

Copyright &copy; 2016-2021. The Stdlib [Authors][stdlib-authors].
Copyright &copy; 2016-2022. The Stdlib [Authors][stdlib-authors].

@@ -289,5 +325,16 @@ </section>

<!--
[dependencies-image]: https://img.shields.io/david/stdlib-js/array-to-json.svg
[dependencies-url]: https://david-dm.org/stdlib-js/array-to-json/main
-->
[umd]: https://github.com/umdjs/umd
[es-module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
[deno-url]: https://github.com/stdlib-js/array-to-json/tree/deno
[umd-url]: https://github.com/stdlib-js/array-to-json/tree/umd
[esm-url]: https://github.com/stdlib-js/array-to-json/tree/esm
[chat-image]: https://img.shields.io/gitter/room/stdlib-js/stdlib.svg

@@ -308,2 +355,6 @@ [chat-url]: https://gitter.im/stdlib-js/stdlib/

[@stdlib/array/complex128]: https://www.npmjs.com/package/@stdlib/array-complex128
[@stdlib/array/complex64]: https://www.npmjs.com/package/@stdlib/array-complex64
[@stdlib/array/int32]: https://www.npmjs.com/package/@stdlib/array-int32

@@ -323,6 +374,10 @@

<!-- <related-links> -->
[@stdlib/array/reviver]: https://www.npmjs.com/package/@stdlib/array-reviver
<!-- </related-links> -->
</section>
<!-- /.links -->
/**
* @license Apache-2.0
*
* Copyright (c) 2018 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 isTypedArray = require( '@stdlib/assert-is-typed-array' );
var typeName = require( './type.js' );
// MAIN //
/**
* Returns a JSON representation of a typed array.
*
* ## Notes
*
* - We build a JSON object representing a typed array similar to how Node.js `Buffer` objects are represented. See [Buffer][1].
*
* [1]: https://nodejs.org/api/buffer.html#buffer_buf_tojson
*
* @param {TypedArray} arr - typed array to serialize
* @throws {TypeError} first argument must be a typed array
* @returns {Object} JSON representation
*
* @example
* var Float64Array = require( '@stdlib/array-float64' );
*
* var arr = new Float64Array( [ 5.0, 3.0 ] );
* var json = toJSON( arr );
* // returns { 'type': 'Float64Array', 'data': [ 5.0, 3.0 ] }
*/
function toJSON( arr ) {
var out;
var i;
if ( !isTypedArray( arr ) ) {
throw new TypeError( 'invalid argument. Must provide a typed array. Value: `' + arr + '`.' );
}
out = {};
out.type = typeName( arr );
out.data = [];
for ( i = 0; i < arr.length; i++ ) {
out.data.push( arr[ i ] );
}
return out;
}
// EXPORTS //
module.exports = toJSON;