Skip to content

Commit ff3ac32

Browse files
test(helpers): refactor decycle.js to decycle.ts
1 parent 9ebab36 commit ff3ac32

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed
Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* eslint-disable */
2-
31
/**
42
* @see {@link https://github.com/douglascrockford/JSON-js/blob/master/cycle.js}
53
*/
@@ -23,7 +21,14 @@
2321

2422
/* jslint eval */
2523

26-
export function decycle(object, replacer) {
24+
type ReplacerFunction = (value: unknown) => unknown;
25+
26+
interface DecycledObject {
27+
[key: string]: unknown;
28+
$ref?: string;
29+
}
30+
31+
export function decycle(object: unknown, replacer?: ReplacerFunction) {
2732
'use strict';
2833

2934
// Make a deep copy of an object or array, assuring that there is at most
@@ -50,13 +55,13 @@ export function decycle(object, replacer) {
5055
// the object or array. [NUMBER] or [STRING] indicates a child element or
5156
// property.
5257

53-
var objects = new window.WeakMap(); // object to path mappings
58+
const objects = new WeakMap<object, string>(); // object to path mappings
5459

55-
return (function derez(value, path) {
60+
return (function derez(value: unknown, path: string): unknown {
5661
// The derez function recurses through the object, producing the deep copy.
5762

58-
var old_path; // The path of an earlier occurance of value
59-
var nu; // The new object or array
63+
let old_path: string | undefined; // The path of an earlier occurance of value
64+
let nu: unknown[] | DecycledObject; // The new object or array
6065

6166
// If a replacer function was provided, then call it to get a replacement value.
6267

@@ -93,16 +98,18 @@ export function decycle(object, replacer) {
9398

9499
if (Array.isArray(value)) {
95100
nu = [];
96-
value.forEach(function (element, i) {
97-
nu[i] = derez(element, path + '[' + i + ']');
101+
(value as unknown[]).forEach(function (element: unknown, i: number) {
102+
(nu as unknown[])[i] = derez(element, path + '[' + String(i) + ']');
98103
});
99104
} else {
100105
// If it is an object, replicate the object.
101106

102-
nu = {};
103-
Object.keys(value).forEach(function (name) {
104-
nu[name] = derez(
105-
value[name],
107+
nu = {} as DecycledObject;
108+
Object.keys(value as Record<string, unknown>).forEach(function (
109+
name: string,
110+
) {
111+
(nu as DecycledObject)[name] = derez(
112+
(value as Record<string, unknown>)[name],
106113
path + '[' + JSON.stringify(name) + ']',
107114
);
108115
});

test/helpers/run-tests.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,16 @@ export function runTests(
1111
const _it = testCase.only ? it.only : testCase.skip ? it.skip : it;
1212

1313
_it('parses ' + testCase.name, function () {
14-
let actualOutput = actualParser(testCase.data);
15-
let expectedOutput = unsetRootParent(
16-
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument
17-
expectedParser(testCase.data) as any,
14+
let actualOutput: unknown = actualParser(testCase.data);
15+
let expectedOutput: unknown = unsetRootParent(
16+
expectedParser(testCase.data) as Parameters<typeof unsetRootParent>[0],
1817
);
1918

2019
// use `JSON.decycle` since `assert.deepEqual` fails
2120
// when instance types are different in the browser
2221
if (typeof window !== 'undefined') {
23-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
2422
actualOutput = decycle(actualOutput);
25-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
23+
2624
expectedOutput = decycle(expectedOutput);
2725
}
2826

0 commit comments

Comments
 (0)