Skip to content

Commit

Permalink
feat: provide formatter inside custom symbol (#221)
Browse files Browse the repository at this point in the history
* feat: provide formatter inside custom symbol

* Create khaki-experts-refuse.md

Co-authored-by: Kiko Beats <josefrancisco.verdu@gmail.com>
  • Loading branch information
akitaSummer and Kikobeats authored Dec 15, 2022
1 parent 2d74158 commit 345cdf1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-experts-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@edge-runtime/format": major
---

feat: provide formatter inside custom symbol
13 changes: 8 additions & 5 deletions packages/format/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function createFormat(opts: FormatterOptions = {}) {

if (!kind(firstArg, 'string')) {
if (hasCustomSymbol(firstArg, customInspectSymbol)) {
return format(firstArg[customInspectSymbol]())
return format(firstArg[customInspectSymbol]({ format }))
} else {
return args
.map((item) => inspect(item, { customInspectSymbol }))
Expand All @@ -70,7 +70,7 @@ export function createFormat(opts: FormatterOptions = {}) {
case '%s': {
const arg = args[index++]
if (hasCustomSymbol(arg, customInspectSymbol)) {
return format(arg[customInspectSymbol]())
return format(arg[customInspectSymbol]({ format }))
} else if (isDate(arg) || isError(arg) || kind(arg, 'bigint')) {
return format(arg)
} else {
Expand Down Expand Up @@ -127,7 +127,7 @@ export function createFormat(opts: FormatterOptions = {}) {
recurseTimes: number | null | undefined
): string {
if (hasCustomSymbol(value, customInspectSymbol)) {
return format(value[customInspectSymbol]())
return format(value[customInspectSymbol]({ format }))
}

const formattedPrimitive = formatPrimitive(value)
Expand Down Expand Up @@ -260,7 +260,7 @@ export function createFormat(opts: FormatterOptions = {}) {
}
base = ' ' + base
} else if (hasCustomSymbol(value, ctx.customInspectSymbol)) {
base = format(value[ctx.customInspectSymbol]())
base = format(value[ctx.customInspectSymbol]({ format }))
if (keys.length === 0) {
return base
}
Expand Down Expand Up @@ -464,7 +464,10 @@ function formatPrimitive(value: unknown) {
function hasCustomSymbol<CustomSymbol extends symbol>(
value: unknown,
customInspectSymbol: CustomSymbol
): value is Record<CustomSymbol, () => unknown> {
): value is Record<
CustomSymbol,
(options: { format: (...args: unknown[]) => string }) => unknown
> {
return (
value !== null &&
kind(value, 'object') &&
Expand Down
43 changes: 43 additions & 0 deletions packages/format/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,4 +424,47 @@ it('custom inspect symbol', () => {
expect(format('%O', new Password('r0sebud'))).toBe('{xxx}')
expect(format('%j', new Password('r0sebud'))).toBe('{}')
})()
;(() => {
const customInspectSymbol = Symbol.for('edge-runtime.inspect.custom')

class Password {
private deepObject: Record<string, any>
constructor(value) {
Object.defineProperty(this, 'password', {
value,
enumerable: false,
})
const o = Object.create(null)
o.name = 'name'
o.a = { o }
o.b = { a: o.a }
this.deepObject = o
this.deepObject.c = this.deepObject
}

[customInspectSymbol]({
format,
}: {
format: (...args: unknown[]) => string
}) {
return format(this.deepObject)
}
}

expect(format(new Password('r0sebud'))).toBe(
`<ref *1> {\n name: 'name',\n a: { o: [Circular *1] },\n b: { a: { o: [Circular *1] } },\n c: [Circular *1]\n}`
)
expect(format('%s', new Password('r0sebud'))).toBe(
`<ref *1> {\n name: 'name',\n a: { o: [Circular *1] },\n b: { a: { o: [Circular *1] } },\n c: [Circular *1]\n}`
)
expect(format('%o', new Password('r0sebud'))).toBe(
`<ref *1> {\n name: 'name',\n a: { o: [Circular *1] },\n b: { a: { o: [Circular *1] } },\n c: [Circular *1]\n}`
)
expect(format('%O', new Password('r0sebud'))).toBe(
`<ref *1> {\n name: 'name',\n a: { o: [Circular *1] },\n b: { a: { o: [Circular *1] } },\n c: [Circular *1]\n}`
)
expect(format('%j', new Password('r0sebud'))).toBe(
`{"deepObject":{"name":"name","a":{"o":"[Circular]"},"b":{"a":"[Circular]"},"c":"[Circular]"}}`
)
})()
})

1 comment on commit 345cdf1

@vercel
Copy link

@vercel vercel bot commented on 345cdf1 Dec 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

edge-runtime – ./

edge-runtime.vercel.app
edge-runtime.vercel.sh
edge-runtime-git-main.vercel.sh

Please sign in to comment.