@stdlib/array-reviver
Advanced tools
+62
| /** | ||
| * @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 isArray = require( '@stdlib/assert-is-array' ); | ||
| var ctors = require( './ctors.js' ); | ||
| // MAIN // | ||
| /** | ||
| * Revives a JSON-serialized typed array. | ||
| * | ||
| * @param {string} key - key | ||
| * @param {*} value - value | ||
| * @returns {(*|TypedArray)} value or typed array | ||
| * | ||
| * @example | ||
| * var parseJSON = require( '@stdlib/utils-parse-json' ); | ||
| * | ||
| * var str = '{"type":"Float64Array","data":[5,3]}'; | ||
| * | ||
| * var arr = parseJSON( str, reviver ); | ||
| * // returns <Float64Array>[ 5.0, 3.0 ] | ||
| */ | ||
| function reviver( key, value ) { | ||
| var ctor; | ||
| if ( | ||
| value && | ||
| value.type && | ||
| isArray( value.data ) | ||
| ) { | ||
| ctor = ctors[ value.type ]; | ||
| if ( ctor ) { | ||
| return new ctor( value.data ); | ||
| } | ||
| } | ||
| return value; | ||
| } | ||
| // EXPORTS // | ||
| module.exports = reviver; |
+5
-1
@@ -32,2 +32,4 @@ /** | ||
| var Uint8ClampedArray = require( '@stdlib/array-uint8c' ); | ||
| 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 @@ |
+2
-2
@@ -38,3 +38,3 @@ /** | ||
| var reviver = require( './reviver.js' ); | ||
| var main = require( './main.js' ); | ||
@@ -44,2 +44,2 @@ | ||
| module.exports = reviver; | ||
| module.exports = main; |
+1
-1
@@ -1,1 +0,1 @@ | ||
| Copyright (c) 2016-2021 The Stdlib Authors. | ||
| Copyright (c) 2016-2022 The Stdlib Authors. |
+7
-1
| { | ||
| "name": "@stdlib/array-reviver", | ||
| "version": "0.0.5", | ||
| "version": "0.0.6", | ||
| "description": "Revive a JSON-serialized 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", | ||
@@ -55,2 +57,6 @@ "@stdlib/array-float64": "^0.0.x", | ||
| "@stdlib/bench": "^0.0.x", | ||
| "@stdlib/complex-imag": "^0.0.x", | ||
| "@stdlib/complex-imagf": "^0.0.x", | ||
| "@stdlib/complex-real": "^0.0.x", | ||
| "@stdlib/complex-realf": "^0.0.x", | ||
| "@stdlib/random-base-randu": "^0.0.x", | ||
@@ -57,0 +63,0 @@ "@stdlib/utils-copy": "^0.0.x", |
+31
-2
@@ -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] --> | ||
@@ -122,2 +122,16 @@ > Revive a JSON-serialized typed array. | ||
| <!-- 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/to-json`][@stdlib/array/to-json]</span><span class="delimiter">: </span><span class="description">return a JSON representation of a 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. --> | ||
@@ -149,3 +163,3 @@ | ||
| Copyright © 2016-2021. The Stdlib [Authors][stdlib-authors]. | ||
| Copyright © 2016-2022. The Stdlib [Authors][stdlib-authors]. | ||
@@ -169,5 +183,16 @@ </section> | ||
| <!-- | ||
| [dependencies-image]: https://img.shields.io/david/stdlib-js/array-reviver.svg | ||
| [dependencies-url]: https://david-dm.org/stdlib-js/array-reviver/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-reviver/tree/deno | ||
| [umd-url]: https://github.com/stdlib-js/array-reviver/tree/umd | ||
| [esm-url]: https://github.com/stdlib-js/array-reviver/tree/esm | ||
| [chat-image]: https://img.shields.io/gitter/room/stdlib-js/stdlib.svg | ||
@@ -182,6 +207,10 @@ [chat-url]: https://gitter.im/stdlib-js/stdlib/ | ||
| <!-- <related-links> --> | ||
| [@stdlib/array/to-json]: https://www.npmjs.com/package/@stdlib/array-to-json | ||
| <!-- </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 isArray = require( '@stdlib/assert-is-array' ); | ||
| var ctors = require( './ctors.js' ); | ||
| // MAIN // | ||
| /** | ||
| * Revives a JSON-serialized typed array. | ||
| * | ||
| * @param {string} key - key | ||
| * @param {*} value - value | ||
| * @returns {(*|TypedArray)} value or typed array | ||
| * | ||
| * @example | ||
| * var parseJSON = require( '@stdlib/utils-parse-json' ); | ||
| * | ||
| * var str = '{"type":"Float64Array","data":[5,3]}'; | ||
| * | ||
| * var arr = parseJSON( str, reviver ); | ||
| * // returns <Float64Array>[ 5.0, 3.0 ] | ||
| */ | ||
| function reviver( key, value ) { | ||
| var ctor; | ||
| if ( | ||
| value && | ||
| value.type && | ||
| isArray( value.data ) | ||
| ) { | ||
| ctor = ctors[ value.type ]; | ||
| if ( ctor ) { | ||
| return new ctor( value.data ); | ||
| } | ||
| } | ||
| return value; | ||
| } | ||
| // EXPORTS // | ||
| module.exports = reviver; |
39360
3.15%210
1.94%212
15.85%12
20%12
50%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added