Skip to content

Commit

Permalink
Replace fast-json-stable-stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten committed Sep 16, 2019
1 parent 2144ae0 commit 14fc98b
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 9 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@
"@testing-library/react": "^9.1.3",
"@testing-library/react-hooks": "^2.0.1",
"@types/enzyme": "3.10.3",
"@types/fast-json-stable-stringify": "^2.0.0",
"@types/jest": "^24.0.18",
"@types/react": "^16.9.2",
"@types/react-test-renderer": "^16.9.0",
Expand Down Expand Up @@ -134,7 +133,6 @@
"react-dom": ">= 16.8.0"
},
"dependencies": {
"fast-json-stable-stringify": "^2.0.0",
"wonka": "^3.2.1"
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from './types';

export {
CombinedError,
stringifyVariables,
createRequest,
makeResult,
makeErrorResult,
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './request';
export * from './result';
export * from './typenames';
export * from './toSuspenseSource';
export * from './stringifyVariables';
export * from './withPromise';

export const noop = () => {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DocumentNode, parse, print } from 'graphql';
import { hash, phash } from './hash';
import stringify from 'fast-json-stable-stringify';
import { stringifyVariables } from './stringifyVariables';
import { GraphQLRequest, Operation, OperationContext } from '../types';

interface Documents {
Expand Down Expand Up @@ -33,7 +33,7 @@ export const createRequest = (
(query as any)[keyProp] = key;

return {
key: vars ? phash(key, stringify(vars)) >>> 0 : key,
key: vars ? phash(key, stringifyVariables(vars)) >>> 0 : key,
query,
variables: vars || {},
};
Expand Down
46 changes: 46 additions & 0 deletions src/utils/stringifyVariables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const seen = new Set();

const stringify = (x: any): string => {
if (x === undefined) {
return '';
} else if (typeof x == 'number') {
return isFinite(x) ? '' + x : 'null';
} else if (typeof x !== 'object') {
return JSON.stringify(x);
} else if (x === null) {
return 'null';
}

let out = '[';
if (Array.isArray(x)) {
for (let i = 0, l = x.length; i < l; i++) {
if (i > 0) out += ',';
const value = stringify(x[i]);
out += value.length > 0 ? value : 'null';
}

return out + ']';
} else if (seen.has(x)) {
throw new TypeError('Converting circular structure to JSON');
}

const keys = Object.keys(x).sort();

seen.add(x);
for (let i = 0, l = keys.length; i < l; i++) {
const key = stringify(keys[i]);
const value = stringify(x[key]);
if (value.length !== 0) {
if (out.length > 0) out += ',';
out += key + ':' + value;
}
}

seen.delete(x);
return '{' + out + '}';
};

export const stringifyVariables = (x: any): string => {
seen.clear();
return stringify(x);
};
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -456,11 +456,6 @@
resolved "https://registry.yarnpkg.com/@types/events/-/events-3.0.0.tgz#2862f3f58a9a7f7c3e78d79f130dd4d71c25c2a7"
integrity sha512-EaObqwIvayI5a8dCzhFrjKzVwKLxjoG9T6Ppd5CEo07LRKfQ8Yokw54r5+Wq7FaBQ+yXRvQAYPrHwya1/UFt9g==

"@types/fast-json-stable-stringify@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@types/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz#40363bb847cb86b2c2e1599f1398d11e8329c921"
integrity sha512-mky/O83TXmGY39P1H9YbUpjV6l6voRYlufqfFCvel8l1phuy8HRjdWc1rrPuN53ITBJlbyMSV6z3niOySO5pgQ==

"@types/glob@^7.1.1":
version "7.1.1"
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.1.1.tgz#aa59a1c6e3fbc421e07ccd31a944c30eba521575"
Expand Down

0 comments on commit 14fc98b

Please # to comment.