Skip to content

Commit

Permalink
feat: add complex/float32/reviver
Browse files Browse the repository at this point in the history
This commit copies `@stdlib/complex/reviver-float32` to a new
package within the `complex/float32` sub-namespace.

Ref: #2260
  • Loading branch information
kgryte committed May 25, 2024
1 parent fddbd39 commit 933ebe4
Show file tree
Hide file tree
Showing 10 changed files with 780 additions and 0 deletions.
145 changes: 145 additions & 0 deletions lib/node_modules/@stdlib/complex/float32/reviver/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<!--
@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.
-->

# reviveComplex64

> Revive a JSON-serialized 64-bit [complex number][@stdlib/complex/float32].
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var reviveComplex64 = require( '@stdlib/complex/float32/reviver' );
```

#### reviveComplex64( key, value )

Revives a JSON-serialized 64-bit [complex number][@stdlib/complex/float32].

```javascript
var parseJSON = require( '@stdlib/utils/parse-json' );

var str = '{"type":"Complex64","re":5,"im":3}';

var z = parseJSON( str, reviveComplex64 );
// returns <Complex64>
```

For details on the JSON serialization format, see [`Complex64`][@stdlib/complex/float32].

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var Complex64 = require( '@stdlib/complex/float32/ctor' );
var parseJSON = require( '@stdlib/utils/parse-json' );
var reviveComplex64 = require( '@stdlib/complex/float32/reviver' );

var z = new Complex64( 5.0, 3.0 );
var str = JSON.stringify( z );
// returns '{"type":"Complex64","re":5,"im":3}'

var w = parseJSON( str, reviveComplex64 );
if ( w instanceof Error ) {
throw w;
}
var bool = ( w instanceof z.constructor );
// returns true

bool = ( w.re === z.re );
// returns true

bool = ( w.im === z.im );
// returns true
```

</section>

<!-- /.examples -->

<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="references">

</section>

<!-- /.references -->

<!-- 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/complex/float32`][@stdlib/complex/float32]</span><span class="delimiter">: </span><span class="description">64-bit complex number.</span>
- <span class="package-name">[`@stdlib/complex/reviver-float64`][@stdlib/complex/reviver-float64]</span><span class="delimiter">: </span><span class="description">revive a JSON-serialized 128-bit complex number.</span>
- <span class="package-name">[`@stdlib/complex/reviver`][@stdlib/complex/reviver]</span><span class="delimiter">: </span><span class="description">revive a JSON-serialized complex number.</span>

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

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

<!-- <related-links> -->

[@stdlib/complex/reviver-float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/complex/reviver-float64

[@stdlib/complex/reviver]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/complex/reviver

<!-- </related-links> -->

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* @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 bench = require( '@stdlib/bench' );
var parseJSON = require( '@stdlib/utils/parse-json' );
var pkg = require( './../package.json' ).name;
var reviver = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var str;
var o;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
str = '{"type":"Complex64","re":'+i+',"im":'+(i+1)+'}';
o = parseJSON( str, reviver );
if ( typeof o !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( typeof o !== 'object' ) {
b.fail( 'should return an object' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::no_reviver', function benchmark( b ) {
var str;
var o;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
str = '{"type":"Complex64","re":'+i+',"im":'+(i+1)+'}';
o = parseJSON( str );
if ( typeof o !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( typeof o !== 'object' ) {
b.fail( 'should return an object' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::no_reviver,built-in', function benchmark( b ) {
var str;
var o;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
str = '{"type":"Complex64","re":'+i+',"im":'+(i+1)+'}';
o = JSON.parse( str );
if ( typeof o !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( typeof o !== 'object' ) {
b.fail( 'should return an object' );
}
b.pass( 'benchmark finished' );
b.end();
});
26 changes: 26 additions & 0 deletions lib/node_modules/@stdlib/complex/float32/reviver/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

{{alias}}( key, value )
Revives a JSON-serialized 64-bit complex number.

Parameters
----------
key: string
Key.

value: any
Value.

Returns
-------
out: any
Value or complex number.

Examples
--------
> var str = '{"type":"Complex64","re":5,"im":3}';
> var z = {{alias:@stdlib/utils/parse-json}}( str, {{alias}} )
<Complex64>

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2021 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.
*/

// TypeScript Version: 4.1

/**
* Revives a JSON-serialized 64-bit complex number.
*
* @param key - key
* @param value - value
* @returns value or 64-bit complex number
*
* @example
* var parseJSON = require( '@stdlib/utils/parse-json' );
*
* var str = '{"type":"Complex64","re":5,"im":3}';
*
* var z = parseJSON( str, reviveComplex64 );
* // returns <Complex64>
*/
declare function reviveComplex64( key: string, value: any ): any;


// EXPORTS //

export = reviveComplex64;
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2021 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.
*/

import reviveComplex64 = require( './index' );


// TESTS //

// The function revives a serialized object...
{
const o = {
'type': 'Complex64',
're': 5,
'im': 3
};
reviveComplex64( 'foo', o ); // $ExpectType any
}

// The compiler throws an error if the function is provided a first argument that is not a string...
{
reviveComplex64( true, 1 ); // $ExpectError
reviveComplex64( false, 1 ); // $ExpectError
reviveComplex64( null, 1 ); // $ExpectError
reviveComplex64( undefined, 1 ); // $ExpectError
reviveComplex64( 5, 1 ); // $ExpectError
reviveComplex64( [], 1 ); // $ExpectError
reviveComplex64( {}, 1 ); // $ExpectError
reviveComplex64( ( x: number ): number => x, 1 ); // $ExpectError
}

// The compiler throws an error if the function is provided insufficient arguments...
{
reviveComplex64(); // $ExpectError
reviveComplex64( 'beep' ); // $ExpectError
}
Loading

0 comments on commit 933ebe4

Please sign in to comment.