Skip to content

Commit

Permalink
Updates and lint
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Jan 3, 2025
1 parent a2df3ae commit c43ef12
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 13 deletions.
6 changes: 3 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Primitive, JsonObject} from 'type-fest';
import {type Primitive, type JsonObject} from 'type-fest';

export {default as errorConstructors} from './error-constructors.js';

Expand All @@ -19,7 +19,7 @@ export type ErrorLike = {
code?: string;
};

export interface Options {
export type Options = {
/**
The maximum depth of properties to preserve when serializing/deserializing.
Expand Down Expand Up @@ -47,7 +47,7 @@ export interface Options {
@default true
*/
readonly useToJSON?: boolean;
}
};

/**
Serialize an `Error` object into a plain object.
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ const destroyCircular = ({
}

for (const {property, enumerable} of commonProperties) {
if (typeof from[property] !== 'undefined' && from[property] !== null) {
if (from[property] !== undefined && from[property] !== null) {
Object.defineProperty(to, property, {
value: isErrorLike(from[property]) ? continueDestroyCircular(from[property]) : from[property],
enumerable: forceEnumerable ? true : enumerable,
Expand Down
4 changes: 3 additions & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {expectType, expectAssignable} from 'tsd';
import {serializeError, deserializeError, ErrorObject, Options} from './index.js';
import {
serializeError, deserializeError, type ErrorObject, type Options,
} from './index.js';

const error = new Error('unicorn');

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
"deserialize"
],
"dependencies": {
"type-fest": "^2.12.2"
"type-fest": "^4.31.0"
},
"devDependencies": {
"ava": "^4.2.0",
"tsd": "^0.20.0",
"xo": "^0.48.0"
"ava": "^6.2.0",
"tsd": "^0.31.2",
"xo": "^0.60.0"
}
}
25 changes: 21 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ test('should drop functions', t => {

const serialized = serializeError(object);
t.deepEqual(serialized, {});
t.false(Object.prototype.hasOwnProperty.call(serialized, 'a'));
t.false(Object.hasOwn(serialized, 'a'));
});

test('should not access deep non-enumerable properties', t => {
Expand Down Expand Up @@ -349,6 +349,17 @@ test('should deserialize properties up to `Options.maxDepth` levels deep', t =>
t.deepEqual(levelThree, error);
});

test('should ignore objects that look like errors but are not', t => {
const object = {
name: 'Error',
message: (new class {}('Foo')),
stack: 'at <anonymous>:3:14',
};

const deserialized = deserializeError(object);
t.deepEqual(deserialized, object);
});

test('should serialize Date as ISO string', t => {
const date = {date: new Date(0)};
const serialized = serializeError(date);
Expand Down Expand Up @@ -467,13 +478,19 @@ test('should serialize properties up to `Options.maxDepth` levels deep', t => {
t.deepEqual(levelZero, {});

const levelOne = serializeError(error, {maxDepth: 1});
t.deepEqual(levelOne, {message, name, stack, one: {}});
t.deepEqual(levelOne, {
message, name, stack, one: {},
});

const levelTwo = serializeError(error, {maxDepth: 2});
t.deepEqual(levelTwo, {message, name, stack, one: {two: {}}});
t.deepEqual(levelTwo, {
message, name, stack, one: {two: {}},
});

const levelThree = serializeError(error, {maxDepth: 3});
t.deepEqual(levelThree, {message, name, stack, one: {two: {three: {}}}});
t.deepEqual(levelThree, {
message, name, stack, one: {two: {three: {}}},
});
});

test('should identify serialized errors', t => {
Expand Down

0 comments on commit c43ef12

Please sign in to comment.