Skip to content

Commit cd285b6

Browse files
authored
Merge pull request #4 from neg4n/obj-traverse-recode
Recode the object traverse and improve tests
2 parents 4b9cf41 + fe811a6 commit cd285b6

File tree

5 files changed

+393
-135
lines changed

5 files changed

+393
-135
lines changed

package.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obj-serialize",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "Simple utility to serialize objects to be passed around to another context. Useful in Next.js Pages Router projects.",
55
"author": {
66
"name": "Igor Klepacki",
@@ -52,18 +52,14 @@
5252
"devDependencies": {
5353
"@jest/globals": "^29.7.0",
5454
"@jest/types": "^29.6.3",
55-
"@types/flat": "^5.0.2",
5655
"@types/node": "^18.7.18",
5756
"jest": "^29.7.0",
5857
"next": "^14.0.4",
5958
"prettier": "^2.7.1",
6059
"rimraf": "^3.0.2",
6160
"ts-jest": "^29.1.1",
6261
"tsup": "^8.0.1",
62+
"type-fest": "^4.10.2",
6363
"typescript": "^4.8.3"
64-
},
65-
"dependencies": {
66-
"flattie": "^1.1.0",
67-
"nestie": "^1.0.3"
6864
}
6965
}

pnpm-lock.yaml

Lines changed: 8 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/serialize.ts

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import { flattie } from 'flattie'
2-
import { nestie } from 'nestie'
3-
41
export const SkipSerialization = Symbol(
52
'ba1894562a5b3e00de68679b5e01fed0de0a0aac6da459729c2f4d665d928ccc',
63
)
@@ -9,47 +6,65 @@ type SerializationRules = <T>(
96
unserializedValue: T,
107
) => string | number | boolean | null | undefined | typeof SkipSerialization
118

12-
export function serialize<R>(data: Record<string, unknown>, rules: SerializationRules) {
13-
if (hasCircularReferences(data)) {
14-
throw new Error('Neither Next.js nor obj-serialize supports circular references.')
15-
}
9+
function detectCircularReferences(
10+
object: unknown,
11+
seenObjects = new Set<unknown>(),
12+
): boolean {
13+
if (object !== null && typeof object === 'object') {
14+
if (seenObjects.has(object)) return true
1615

17-
return nestie(
18-
Object.entries(flattie(data) as Record<string, unknown>).reduce(
19-
(newData, [key, value]) => {
20-
let oldValue = value
21-
const serialized = rules(value)
22-
23-
if (typeof serialized === typeof SkipSerialization) {
24-
newData[key] = oldValue
25-
} else if (serialized !== oldValue) {
26-
newData[key] = serialized
27-
}
28-
29-
return newData
30-
},
31-
{} as Record<string, unknown>,
32-
),
33-
) as Record<string, R>
16+
seenObjects.add(object)
17+
for (const key of Object.keys(object as object)) {
18+
if (
19+
detectCircularReferences((object as { [key: string]: unknown })[key], seenObjects)
20+
)
21+
return true
22+
}
23+
24+
seenObjects.delete(object)
25+
}
26+
return false
3427
}
3528

3629
function hasCircularReferences(object: unknown): boolean {
37-
const seenObjects = new Set<unknown>()
30+
return detectCircularReferences(object)
31+
}
3832

39-
const detect = (object: unknown): boolean => {
40-
if (object !== null && typeof object === 'object') {
41-
if (seenObjects.has(object)) return true
33+
function isPlainObject(obj: unknown): obj is Record<string, unknown> {
34+
return Object.prototype.toString.call(obj) === '[object Object]'
35+
}
4236

43-
seenObjects.add(object)
37+
function processValue(value: unknown, rules: SerializationRules): unknown {
38+
if (Array.isArray(value)) {
39+
return value
40+
.map((item) => processValue(item, rules))
41+
.filter((item) => item !== SkipSerialization)
42+
} else if (typeof value === 'object' && value !== null && isPlainObject(value)) {
43+
const processedObject = Object.fromEntries(
44+
Object.entries(value)
45+
.map(([key, val]) => [key, processValue(val, rules)])
46+
.filter(([, val]) => val !== SkipSerialization),
47+
)
4448

45-
for (const key of Object.keys(object as object))
46-
if (detect((object as { [key: string]: unknown })[key])) return true
49+
if (Object.keys(processedObject).length === 0 && !rules(value))
50+
return SkipSerialization
4751

48-
seenObjects.delete(object)
49-
}
52+
return processedObject
53+
} else {
54+
const serialized = rules(value)
55+
if (serialized === SkipSerialization) return value
5056

51-
return false
57+
return serialized
58+
}
59+
}
60+
61+
export function serialize<V>(data: unknown, rules: SerializationRules) {
62+
if (hasCircularReferences(data)) {
63+
throw new Error('Neither Next.js nor obj-serialize supports circular references.')
5264
}
5365

54-
return detect(object)
66+
const result = processValue(data, rules)
67+
return result === SkipSerialization
68+
? ({} as Record<string, V>)
69+
: (result as Record<string, V>)
5570
}

0 commit comments

Comments
 (0)