Skip to content

Commit b709f2b

Browse files
authored
feat: define explicit primitive cast for ProcessPromise and ProcessOutput (#1107)
* feat: define `Symbol.toStringTag` for `ProcessOutput` and `ProcessPromise` * feat: define `Symbol.toPrimitive` closes #1028
1 parent 5f48c73 commit b709f2b

File tree

3 files changed

+34
-24
lines changed

3 files changed

+34
-24
lines changed

.size-limit.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
{
1010
"name": "zx/index",
1111
"path": "build/*.{js,cjs}",
12-
"limit": "811 kB",
12+
"limit": "812 kB",
1313
"brotli": false,
1414
"gzip": false
1515
},
1616
{
1717
"name": "dts libdefs",
1818
"path": "build/*.d.ts",
19-
"limit": "38.7 kB",
19+
"limit": "39 kB",
2020
"brotli": false,
2121
"gzip": false
2222
},

src/core.ts

+16
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,14 @@ export class ProcessPromise extends Promise<ProcessOutput> {
485485
return this._stage
486486
}
487487

488+
get [Symbol.toStringTag](): string {
489+
return 'ProcessPromise'
490+
}
491+
492+
[Symbol.toPrimitive](): string {
493+
return this.toString()
494+
}
495+
488496
// Configurators
489497
stdio(
490498
stdin: IOType,
@@ -724,6 +732,14 @@ export class ProcessOutput extends Error {
724732
return this._dto.duration
725733
}
726734

735+
get [Symbol.toStringTag](): string {
736+
return 'ProcessOutput'
737+
}
738+
739+
[Symbol.toPrimitive](): string {
740+
return this.valueOf()
741+
}
742+
727743
toString(): string {
728744
return this.stdall
729745
}

test/core.test.js

+16-22
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,10 @@ describe('core', () => {
422422
assert.ok(p.exitCode instanceof Promise)
423423
assert.ok(p.signal instanceof AbortSignal)
424424
assert.equal(p.output, null)
425+
assert.equal(Object.prototype.toString.call(p), '[object ProcessPromise]')
426+
assert.equal('' + p, '[object ProcessPromise]')
427+
assert.equal(`${p}`, '[object ProcessPromise]')
428+
assert.equal(+p, NaN)
425429

426430
await p
427431
assert.ok(p.output instanceof ProcessOutput)
@@ -907,10 +911,7 @@ describe('core', () => {
907911
lines.push(line)
908912
}
909913

910-
assert.equal(lines.length, 3, 'Should have 3 lines')
911-
assert.equal(lines[0], 'Line1', 'First line should be "Line1"')
912-
assert.equal(lines[1], 'Line2', 'Second line should be "Line2"')
913-
assert.equal(lines[2], 'Line3', 'Third line should be "Line3"')
914+
assert.deepEqual(lines, ['Line1', 'Line2', 'Line3'])
914915
})
915916

916917
it('should handle partial lines correctly', async () => {
@@ -920,18 +921,7 @@ describe('core', () => {
920921
lines.push(line)
921922
}
922923

923-
assert.equal(lines.length, 3, 'Should have 3 lines')
924-
assert.equal(
925-
lines[0],
926-
'PartialLine1',
927-
'First line should be "PartialLine1"'
928-
)
929-
assert.equal(lines[1], 'Line2', 'Second line should be "Line2"')
930-
assert.equal(
931-
lines[2],
932-
'PartialLine3',
933-
'Third line should be "PartialLine3"'
934-
)
924+
assert.deepEqual(lines, ['PartialLine1', 'Line2', 'PartialLine3'])
935925
})
936926

937927
it('should handle empty stdout', async () => {
@@ -951,12 +941,7 @@ describe('core', () => {
951941
lines.push(line)
952942
}
953943

954-
assert.equal(
955-
lines.length,
956-
1,
957-
'Should have 1 line for single line without trailing newline'
958-
)
959-
assert.equal(lines[0], 'SingleLine', 'The line should be "SingleLine"')
944+
assert.deepEqual(lines, ['SingleLine'])
960945
})
961946

962947
it('should yield all buffered and new chunks when iterated after a delay', async () => {
@@ -1118,6 +1103,14 @@ describe('core', () => {
11181103
assert.equal(o.signal, 'SIGTERM')
11191104
assert.equal(o.exitCode, -1)
11201105
assert.equal(o.duration, 20)
1106+
assert.equal(Object.prototype.toString.call(o), '[object ProcessOutput]')
1107+
})
1108+
1109+
test('[Symbol.toPrimitive]', () => {
1110+
const o = new ProcessOutput(-1, 'SIGTERM', '', '', 'foo\n', 'msg', 20)
1111+
assert.equal('' + o, 'foo')
1112+
assert.equal(`${o}`, 'foo')
1113+
assert.equal(+o, NaN)
11211114
})
11221115

11231116
test('toString()', async () => {
@@ -1175,6 +1168,7 @@ describe('core', () => {
11751168
}
11761169
assert.deepEqual(lines, expected)
11771170
assert.deepEqual(o.lines(), expected)
1171+
assert.deepEqual([...o], expected) // isConcatSpreadable
11781172
})
11791173

11801174
describe('static', () => {

0 commit comments

Comments
 (0)