-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle "new" expressions in MDX
- Loading branch information
1 parent
be140e0
commit 8ebf546
Showing
3 changed files
with
57 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import jsStringEscape from 'js-string-escape'; | ||
|
||
export const stringifyObject = ( | ||
val: any, | ||
sep: string = ' ', | ||
depth: number = 1, | ||
): string => { | ||
switch (typeof val) { | ||
case 'string': | ||
return `"${jsStringEscape(val)}"`; | ||
case 'function': | ||
return val.name || val.toString(); | ||
case 'object': | ||
if (val instanceof Date) { | ||
return '"' + val.toISOString() + '"'; | ||
} | ||
if (val instanceof String) { | ||
return val.toString(); | ||
} | ||
if (Array.isArray(val)) { | ||
return ( | ||
'[' + | ||
val.map(v => stringifyObject(v, sep, depth + 1)).join(', ') + | ||
']' | ||
); | ||
} | ||
if (val === null) { | ||
return 'null'; | ||
} | ||
return ` | ||
{ | ||
${Object.keys(val) | ||
.map(key => { | ||
return typeof val[key] === 'function' | ||
? null | ||
: `${key}: ${stringifyObject(val[key], sep, depth + 1)}`; | ||
}) | ||
.filter(v => v)} | ||
} | ||
`; | ||
default: | ||
return val.toString(); | ||
} | ||
}; |