Skip to content

Commit

Permalink
fix: modify fns to adapt to new safe-stable-stringify package update (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
RahulGautamSingh committed Nov 20, 2022
1 parent fa6ec13 commit c45c875
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
13 changes: 13 additions & 0 deletions lib/util/__snapshots__/clone.spec.ts.snap
@@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`util/clone maintains same order: {
name: 'object',
type: 'object',
isObject: true,
} 1`] = `
{
"isObject": true,
"name": "object",
"type": "object",
}
`;
11 changes: 10 additions & 1 deletion lib/util/clone.spec.ts
Expand Up @@ -7,9 +7,18 @@ describe('util/clone', () => {
isObject: true,
};

it('returns null', () => {
const res = clone(null);
expect(res).toBeNull();
});

it('maintains same order', () => {
const res = clone(obj);
expect(Object.keys(res)).toEqual(Object.keys(obj));
expect(res).toMatchSnapshot(`{
name: 'object',
type: 'object',
isObject: true,
}`);
});

it('assigns "[Circular]" to circular references', () => {
Expand Down
3 changes: 2 additions & 1 deletion lib/util/clone.ts
@@ -1,5 +1,6 @@
import { quickStringify } from './stringify';

export function clone<T>(input: T | null = null): T {
return JSON.parse(quickStringify(input));
const stringifiedInput = quickStringify(input);
return stringifiedInput ? JSON.parse(stringifiedInput) : null;
}
27 changes: 27 additions & 0 deletions lib/util/fingerprint.spec.ts
@@ -0,0 +1,27 @@
import { fingerprint } from './fingerprint';

describe('util/fingerprint', () => {
const obj: any = {
name: 'object',
type: 'object',
isObject: true,
};

const obj2: any = {
type: 'object',
name: 'object',
isObject: true,
};

it('returns empty string', () => {
const res = fingerprint(undefined);
expect(res).toBeEmptyString();
});

it('maintains deterministic order', () => {
const res = fingerprint(obj);
const res2 = fingerprint(obj2);
expect(res).not.toEqual(JSON.stringify(obj)); // shows that safeStringify changes the original order
expect(res).toEqual(res2);
});
});
3 changes: 2 additions & 1 deletion lib/util/fingerprint.ts
Expand Up @@ -2,5 +2,6 @@ import hasha from 'hasha';
import { safeStringify } from './stringify';

export function fingerprint(input: unknown): string {
return hasha(safeStringify(input));
const stringifiedInput = safeStringify(input);
return stringifiedInput ? hasha(stringifiedInput) : '';
}

0 comments on commit c45c875

Please sign in to comment.