Skip to content

Commit

Permalink
feat: define Symbol.toPrimitive
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Feb 23, 2025
1 parent 147104a commit d38269b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
{
"name": "zx/index",
"path": "build/*.{js,cjs}",
"limit": "811 kB",
"limit": "812 kB",
"brotli": false,
"gzip": false
},
{
"name": "dts libdefs",
"path": "build/*.d.ts",
"limit": "38.7 kB",
"limit": "39 kB",
"brotli": false,
"gzip": false
},
Expand Down
8 changes: 8 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,10 @@ export class ProcessPromise extends Promise<ProcessOutput> {
return 'ProcessPromise'
}

[Symbol.toPrimitive](): string {
return `${this}`
}

// Configurators
stdio(
stdin: IOType,
Expand Down Expand Up @@ -732,6 +736,10 @@ export class ProcessOutput extends Error {
return 'ProcessOutput'
}

[Symbol.toPrimitive](): string {
return this.valueOf()
}

toString(): string {
return this.stdall
}
Expand Down
10 changes: 10 additions & 0 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,9 @@ describe('core', () => {
assert.ok(p.signal instanceof AbortSignal)
assert.equal(p.output, null)
assert.equal(Object.prototype.toString.call(p), '[object ProcessPromise]')
assert.equal('' + p, '[object ProcessPromise]')
assert.equal(`${p}`, '[object ProcessPromise]')
assert.equal(+p, NaN)

await p
assert.ok(p.output instanceof ProcessOutput)
Expand Down Expand Up @@ -1103,6 +1106,13 @@ describe('core', () => {
assert.equal(Object.prototype.toString.call(o), '[object ProcessOutput]')
})

test('[Symbol.toPrimitive]', () => {
const o = new ProcessOutput(-1, 'SIGTERM', '', '', 'foo\n', 'msg', 20)
assert.equal('' + o, 'foo')
assert.equal(`${o}`, 'foo')
assert.equal(+o, NaN)
})

test('toString()', async () => {
const o = new ProcessOutput(null, null, '', '', 'foo\n')
assert.equal(o.toString(), 'foo\n')
Expand Down

0 comments on commit d38269b

Please sign in to comment.