Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Sep 15, 2022
1 parent fd71ee8 commit a071522
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 44 deletions.
1 change: 0 additions & 1 deletion .github/.keepalive

This file was deleted.

17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@ var reviveError = require( '@stdlib/error-reviver' );
Revives a JSON-serialized [error][@stdlib/error/to-json] object.

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

var str = '{"type":"TypeError","message":"beep"}';

var err = JSON.parse( str, reviveError );
var err = parseJSON( str, reviveError );
// returns <TypeError>
```

For details on the JSON serialization format, see [error-to-json][@stdlib/error/to-json].
For details on the JSON serialization format, see [`@stdlib/error/to-json`][@stdlib/error/to-json].

</section>

Expand Down Expand Up @@ -106,14 +108,15 @@ For details on the JSON serialization format, see [error-to-json][@stdlib/error/
<!-- eslint no-undef: "error" -->

```javascript
var parseJSON = require( '@stdlib/utils-parse-json' );
var err2json = require( '@stdlib/error-to-json' );
var reviveError = require( '@stdlib/error-reviver' );

var err1 = new SyntaxError( 'bad syntax' );
// returns <SyntaxError>

var json = err2json( err1 );
/* returns
/* e.g., returns
{
'type': 'SyntaxError',
'name': 'SyntaxError',
Expand All @@ -123,9 +126,9 @@ var json = err2json( err1 );
*/

var str = JSON.stringify( json );
// returns '{"type":"SyntaxError","name":"SyntaxError","message":"bad syntax","stack":"..."}'
// e.g., returns '{"type":"SyntaxError","name":"SyntaxError","message":"bad syntax","stack":"..."}'

var err2 = JSON.parse( str, reviveError );
var err2 = parseJSON( str, reviveError );
// returns <SyntaxError>

var bool = ( err1.message === err2.message );
Expand Down Expand Up @@ -244,12 +247,8 @@ Copyright &copy; 2016-2022. The Stdlib [Authors][stdlib-authors].

[mdn-eval-error]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/EvalError

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

[@stdlib/error/to-json]: https://github.com/stdlib-js/error-to-json

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

</section>

<!-- /.links -->
6 changes: 3 additions & 3 deletions docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
*
* @example
* var str = '{"type":"TypeError","message":"beep"}';
* var err = JSON.parse( str, reviver );
* var err = JSON.parse( str, reviveError );
* // returns <TypeError>
*/
declare function reviver( key: string, value: any ): any;
declare function reviveError( key: string, value: any ): any;


// EXPORTS //

export = reviver;
export = reviveError;
24 changes: 12 additions & 12 deletions docs/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,30 @@
* limitations under the License.
*/

import reviver = require( './index' );
import reviveError = require( './index' );


// TESTS //

// The function can be used to revive a serialized object...
{
JSON.parse( '{"beep":"boop"}', reviver ); // $ExpectType any
JSON.parse( '{"beep":"boop"}', reviveError ); // $ExpectType any
}

// The function does not compile if provided a first argument that is not a string...
{
reviver( true, 1 ); // $ExpectError
reviver( false, 1 ); // $ExpectError
reviver( null, 1 ); // $ExpectError
reviver( undefined, 1 ); // $ExpectError
reviver( 5, 1 ); // $ExpectError
reviver( [], 1 ); // $ExpectError
reviver( {}, 1 ); // $ExpectError
reviver( ( x: number ): number => x, 1 ); // $ExpectError
reviveError( true, 1 ); // $ExpectError
reviveError( false, 1 ); // $ExpectError
reviveError( null, 1 ); // $ExpectError
reviveError( undefined, 1 ); // $ExpectError
reviveError( 5, 1 ); // $ExpectError
reviveError( [], 1 ); // $ExpectError
reviveError( {}, 1 ); // $ExpectError
reviveError( ( x: number ): number => x, 1 ); // $ExpectError
}

// The function does not compile if provided insufficient arguments...
{
reviver(); // $ExpectError
reviver( 'beep' ); // $ExpectError
reviveError(); // $ExpectError
reviveError( 'beep' ); // $ExpectError
}
4 changes: 2 additions & 2 deletions examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var err1 = new SyntaxError( 'bad syntax' );
// returns <SyntaxError>

var json = err2json( err1 );
/* returns
/* e.g., returns
{
'type': 'SyntaxError',
'name': 'SyntaxError',
Expand All @@ -35,7 +35,7 @@ var json = err2json( err1 );
*/

var str = JSON.stringify( json );
// returns '{'type':'SyntaxError','name':'SyntaxError','message':'bad syntax','stack':'...'}'
// e.g., returns '{'type':'SyntaxError','name':'SyntaxError','message':'bad syntax','stack':'...'}'

var err2 = JSON.parse( str, reviveError );
// returns <SyntaxError>
Expand Down
7 changes: 4 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,19 @@
* @module @stdlib/error-reviver
*
* @example
* var parseJSON = require( '@stdlib/utils-parse-json' );
* var reviver = require( '@stdlib/error-reviver' );
*
* var str = '{"type":"TypeError","message":"beep"}';
* var err = JSON.parse( str, reviver );
* var err = parseJSON( str, reviver );
* // returns <TypeError>
*/

// MODULES //

var reviver = require( './reviver.js' );
var main = require( './main.js' );


// EXPORTS //

module.exports = reviver;
module.exports = main;
8 changes: 5 additions & 3 deletions lib/reviver.js → lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ var ctors = require( './ctors.js' );
* @returns {(*|Error|SyntaxError|URIError|EvalError|ReferenceError|RangeError|TypeError)} value or error object
*
* @example
* var parseJSON = require( '@stdlib/utils-parse-json' );
*
* var str = '{"type":"TypeError","message":"beep"}';
* var err = JSON.parse( str, reviver );
* var err = parseJSON( str, reviveError );
* // returns <TypeError>
*/
function reviver( key, value ) {
function reviveError( key, value ) {
var hasStack;
var ctor;
var keys;
Expand Down Expand Up @@ -84,4 +86,4 @@ function reviver( key, value ) {

// EXPORTS //

module.exports = reviver;
module.exports = reviveError;
22 changes: 11 additions & 11 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ tape( 'values which are not recognized as serialized error objects are unaffecte
};
actual = JSON.parse( '{"beep":"boop"}', reviveError );

t.deepEqual( actual, expected, 'deep equal' );
t.deepEqual( actual, expected, 'returns expected value' );

// Null edge case:
actual = JSON.parse( 'null', reviveError );
t.equal( actual, null, 'equals null' );
t.equal( actual, null, 'returns expected value' );

t.end();
});
Expand All @@ -75,7 +75,7 @@ tape( 'an object must have a recognized "type" field in order to be revived', fu

actual = JSON.parse( JSON.stringify( json ), reviveError );

t.deepEqual( actual, expected, 'deep equal' );
t.deepEqual( actual, expected, 'returns expected value' );
t.end();
});

Expand All @@ -91,7 +91,7 @@ tape( 'an object must have a "message" field in order to be revived', function t

actual = JSON.parse( JSON.stringify( json ), reviveError );

t.deepEqual( actual, expected, 'deep equal' );
t.deepEqual( actual, expected, 'returns expected value' );

t.end();
});
Expand Down Expand Up @@ -141,8 +141,8 @@ tape( 'the function will revive a JSON-serialized error object', function test(
actual = JSON.parse( JSON.stringify( json ), reviveError );

t.ok( actual instanceof ctors[ i ], 'instance of type ' + types[i] );
t.equal( actual.message, expected.message, 'equal messages' );
t.equal( actual.stack, expected.stack, 'equal stacks' );
t.equal( actual.message, expected.message, 'returns expected value' );
t.equal( actual.stack, expected.stack, 'returns expected value' );
}
t.end();
});
Expand All @@ -160,7 +160,7 @@ tape( 'non-standard error properties are bound to the revived error instance', f
t.equal( err.beep, json.beep, 'shallow properties' );

t.notEqual( err.arr, json.arr, 'separate instances' );
t.deepEqual( err.arr, json.arr, 'deep equal' );
t.deepEqual( err.arr, json.arr, 'returns expected value' );

t.end();
});
Expand Down Expand Up @@ -219,8 +219,8 @@ tape( 'the function will revive deeply nested serialized error objects', functio
expected.stack = 'boop';

t.ok( actual[i] instanceof ctors[i], 'instance of ' + ctors[ i ] );
t.equal( actual[i].message, expected.message, 'equal messages' );
t.equal( actual[i].stack, expected.stack, 'equal stacks' );
t.equal( actual[i].message, expected.message, 'returns expected value' );
t.equal( actual[i].stack, expected.stack, 'returns expected value' );
}

json = {
Expand All @@ -240,9 +240,9 @@ tape( 'the function will revive deeply nested serialized error objects', functio

t.ok( actual.beep.boop instanceof RangeError, 'instance of RangeError' );

t.equal( actual.beep.boop.message, expected.beep.boop.message, 'equal messages' );
t.equal( actual.beep.boop.message, expected.beep.boop.message, 'returns expected value' );

t.equal( actual.beep.boop.stack, expected.beep.boop.stack, 'equal stacks' );
t.equal( actual.beep.boop.stack, expected.beep.boop.stack, 'returns expected value' );

t.end();
});

0 comments on commit a071522

Please sign in to comment.