Skip to content

Commit

Permalink
chore: formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
maraisr committed Jun 22, 2023
1 parent ee332f0 commit 2216d58
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
6 changes: 5 additions & 1 deletion bench/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ const getObject = () => {
const c = [1];
// @ts-ignore
c.push(c);
return { a: { b: ['c', new Set(['d', new Map([['e', 'f']]), c, 'g'])] }, d: new Date(), r: /a/ };
return {
a: { b: ['c', new Set(['d', new Map([['e', 'f']]), c, 'g'])] },
d: new Date(),
r: /a/,
};
};

suite(
Expand Down
2 changes: 1 addition & 1 deletion bench/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Objects('circular', () => {
});

Objects('partial circular', () => {
const o = {v: 1};
const o = { v: 1 };
const a = ['a', o];
assert.equal(identify(a), identify(a));
});
Expand Down
43 changes: 25 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,36 @@ function walk(input: any, ref_index: number) {
let type = Object.prototype.toString.call(input);

if (input == null || typeof input !== 'object') return String(input);
if (!(type === '[object RegExp]' || type === '[object Date]') && seen.has(input)) return seen.get(input)!;
if (!(type === '[object RegExp]' || type === '[object Date]') && seen.has(input))
return seen.get(input)!;
seen.set(input, '~' + ++ref_index);

switch (type) {
case '[object Set]':
tmp = Array.from(input);
case '[object Array]': {
tmp ||= input;
out += 'a';
for (; i < tmp.length; out += walk(tmp[i++], ref_index));
} break;

case '[object Object]': {
out += 'o';
tmp = Object.keys(input).sort();
for (; i < tmp.length; out += tmp[i] + walk(input[tmp[i++]], ref_index));
} break;

case '[object Map]': {
out += 'o';
tmp = Array.from((input as Map<string, unknown>).keys()).sort();
for (; i < tmp.length; out += tmp[i] + walk(input.get(tmp[i++]), ref_index));
} break;
case '[object Array]':
{
tmp ||= input;
out += 'a';
for (; i < tmp.length; out += walk(tmp[i++], ref_index));
}
break;

case '[object Object]':
{
out += 'o';
tmp = Object.keys(input).sort();
for (; i < tmp.length; out += tmp[i] + walk(input[tmp[i++]], ref_index));
}
break;

case '[object Map]':
{
out += 'o';
tmp = Array.from((input as Map<string, unknown>).keys()).sort();
for (; i < tmp.length; out += tmp[i] + walk(input.get(tmp[i++]), ref_index));
}
break;

case '[object Date]':
return 'd' + +input;
Expand Down

0 comments on commit 2216d58

Please sign in to comment.