Skip to content

Commit

Permalink
Merge pull request #55 from FrameMuse/54-improve-serialize-utill
Browse files Browse the repository at this point in the history
improve `serialize` util
  • Loading branch information
FrameMuse authored Feb 20, 2023
2 parents 5bd80e0 + c4f2ec9 commit aa82d6e
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 72 deletions.
46 changes: 27 additions & 19 deletions coverage/clover.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<coverage generated="1676638758875" clover="3.2.0">
<project timestamp="1676638758875" name="All files">
<metrics statements="99" coveredstatements="97" conditionals="54" coveredconditionals="50" methods="46" coveredmethods="41" elements="199" coveredelements="188" complexity="0" loc="99" ncloc="99" packages="1" files="7" classes="7"/>
<coverage generated="1676823773269" clover="3.2.0">
<project timestamp="1676823773269" name="All files">
<metrics statements="107" coveredstatements="102" conditionals="58" coveredconditionals="51" methods="48" coveredmethods="43" elements="213" coveredelements="196" complexity="0" loc="107" ncloc="107" packages="1" files="7" classes="7"/>
<file name="container.tsx" path="/home/kotto/github/react-modal-global/src/container.tsx">
<metrics statements="13" coveredstatements="13" conditionals="14" coveredconditionals="14" methods="6" coveredmethods="6"/>
<line num="25" count="3" type="stmt"/>
Expand Down Expand Up @@ -99,28 +99,36 @@
<metrics statements="0" coveredstatements="0" conditionals="0" coveredconditionals="0" methods="0" coveredmethods="0"/>
</file>
<file name="utils.ts" path="/home/kotto/github/react-modal-global/src/utils.ts">
<metrics statements="21" coveredstatements="19" conditionals="16" coveredconditionals="12" methods="7" coveredmethods="7"/>
<metrics statements="29" coveredstatements="24" conditionals="20" coveredconditionals="13" methods="9" coveredmethods="9"/>
<line num="26" count="60" type="stmt"/>
<line num="27" count="60" type="cond" truecount="2" falsecount="0"/>
<line num="29" count="25" type="stmt"/>
<line num="30" count="25" type="stmt"/>
<line num="32" count="25" type="stmt"/>
<line num="33" count="25" type="stmt"/>
<line num="36" count="3" type="stmt"/>
<line num="37" count="32" type="stmt"/>
<line num="38" count="32" type="stmt"/>
<line num="39" count="186" type="cond" truecount="4" falsecount="0"/>
<line num="40" count="32" type="cond" truecount="1" falsecount="1"/>
<line num="41" count="0" type="stmt"/>
<line num="43" count="32" type="stmt"/>
<line num="45" count="186" type="stmt"/>
<line num="50" count="32" type="cond" truecount="1" falsecount="1"/>
<line num="52" count="32" type="stmt"/>
<line num="53" count="32" type="stmt"/>
<line num="62" count="29" type="stmt"/>
<line num="63" count="7" type="cond" truecount="3" falsecount="1"/>
<line num="64" count="7" type="cond" truecount="1" falsecount="1"/>
<line num="67" count="0" type="stmt"/>
<line num="38" count="32" type="cond" truecount="1" falsecount="1"/>
<line num="41" count="186" type="stmt"/>
<line num="42" count="186" type="stmt"/>
<line num="43" count="186" type="cond" truecount="4" falsecount="0"/>
<line num="44" count="32" type="cond" truecount="1" falsecount="1"/>
<line num="45" count="0" type="stmt"/>
<line num="47" count="32" type="stmt"/>
<line num="50" count="186" type="stmt"/>
<line num="55" count="186" type="cond" truecount="1" falsecount="1"/>
<line num="58" count="0" type="cond" truecount="0" falsecount="2"/>
<line num="59" count="0" type="stmt"/>
<line num="63" count="0" type="stmt"/>
<line num="66" count="186" type="stmt"/>
<line num="70" count="186" type="stmt"/>
<line num="71" count="186" type="stmt"/>
<line num="73" count="186" type="stmt"/>
<line num="74" count="186" type="stmt"/>
<line num="77" count="32" type="stmt"/>
<line num="78" count="32" type="stmt"/>
<line num="87" count="29" type="stmt"/>
<line num="88" count="7" type="cond" truecount="3" falsecount="1"/>
<line num="89" count="7" type="cond" truecount="1" falsecount="1"/>
<line num="92" count="0" type="stmt"/>
</file>
</project>
</coverage>
100 changes: 58 additions & 42 deletions coverage/lcov.info
Original file line number Diff line number Diff line change
Expand Up @@ -250,59 +250,75 @@ TN:
SF:src/utils.ts
FN:25,classWithModifiers
FN:32,(anonymous_1)
FN:36,(anonymous_2)
FN:38,(anonymous_3)
FN:49,serialize
FN:61,stopPropagation
FN:62,(anonymous_6)
FNF:7
FNH:7
FN:37,serialize
FN:40,getCircularReplacer
FN:42,(anonymous_4)
FN:54,transform
FN:69,replacer
FN:86,stopPropagation
FN:87,(anonymous_8)
FNF:9
FNH:9
FNDA:60,classWithModifiers
FNDA:25,(anonymous_1)
FNDA:32,(anonymous_2)
FNDA:186,(anonymous_3)
FNDA:32,serialize
FNDA:186,getCircularReplacer
FNDA:186,(anonymous_4)
FNDA:186,transform
FNDA:186,replacer
FNDA:29,stopPropagation
FNDA:7,(anonymous_6)
FNDA:7,(anonymous_8)
DA:26,60
DA:27,60
DA:29,25
DA:30,25
DA:32,25
DA:33,25
DA:36,3
DA:37,32
DA:38,32
DA:39,186
DA:40,32
DA:41,0
DA:43,32
DA:45,186
DA:50,32
DA:52,32
DA:53,32
DA:62,29
DA:63,7
DA:64,7
DA:67,0
LF:21
LH:19
DA:41,186
DA:42,186
DA:43,186
DA:44,32
DA:45,0
DA:47,32
DA:50,186
DA:55,186
DA:58,0
DA:59,0
DA:63,0
DA:66,186
DA:70,186
DA:71,186
DA:73,186
DA:74,186
DA:77,32
DA:78,32
DA:87,29
DA:88,7
DA:89,7
DA:92,0
LF:29
LH:24
BRDA:27,0,0,35
BRDA:27,0,1,25
BRDA:39,1,0,32
BRDA:39,1,1,154
BRDA:39,2,0,186
BRDA:39,2,1,32
BRDA:40,3,0,0
BRDA:40,3,1,32
BRDA:50,4,0,0
BRDA:50,4,1,32
BRDA:63,5,0,7
BRDA:63,5,1,0
BRDA:63,6,0,7
BRDA:63,6,1,7
BRDA:64,7,0,7
BRDA:64,7,1,0
BRF:16
BRH:12
BRDA:38,1,0,0
BRDA:38,1,1,32
BRDA:43,2,0,32
BRDA:43,2,1,154
BRDA:43,3,0,186
BRDA:43,3,1,32
BRDA:44,4,0,0
BRDA:44,4,1,32
BRDA:55,5,0,0
BRDA:55,5,1,186
BRDA:58,6,0,0
BRDA:58,6,1,0
BRDA:88,7,0,7
BRDA:88,7,1,0
BRDA:88,8,0,7
BRDA:88,8,1,7
BRDA:89,9,0,7
BRDA:89,9,1,0
BRF:20
BRH:13
end_of_record
47 changes: 36 additions & 11 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,48 @@ export function classWithModifiers(originClass: string, ...modifiers: Array<stri
return originClass + space + modifiers.join(space)
}

const getCircularReplacer = () => {
const seen = new WeakSet()
return (_key: string, value: unknown) => {
if (typeof value === "object" && value !== null) {
if (seen.has(value)) {
return

export function serialize<T = unknown>(value?: T | null) {
if (value == null) return String(value)

function getCircularReplacer() {
const seen = new WeakSet()
return (_key: string, value: unknown) => {
if (typeof value === "object" && value !== null) {
if (seen.has(value)) {
return
}
seen.add(value)
}

return value
}
}

function transform(key: string, value: unknown) {
if (value instanceof Function) {
// If the value is a function, but not an arrow function,
// return the function as a string.
if (value.name === key) {
return value.toString()
}
seen.add(value)

// Otherwise, return the name of the function.
return value.name
}

return value
}
}

export function serialize<T = unknown>(value?: T | null) {
if (value == null) return String(value)
function replacer(key: string, value: unknown) {
const circularReplacer = getCircularReplacer()
const circularReplacedValue = circularReplacer(key, value)

const transformedValue = transform(key, circularReplacedValue)
return transformedValue
}

const serializedValue = JSON.stringify(value, getCircularReplacer())
const serializedValue = JSON.stringify(value, replacer)
return serializedValue
}

Expand Down

0 comments on commit aa82d6e

Please sign in to comment.