Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: define primitive cast for ProcessPromise and ProcessOutput #1107

Merged
merged 2 commits into from
Feb 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
16 changes: 16 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,14 @@ export class ProcessPromise extends Promise<ProcessOutput> {
return this._stage
}

get [Symbol.toStringTag](): string {
return 'ProcessPromise'
}

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

// Configurators
stdio(
stdin: IOType,
Expand Down Expand Up @@ -724,6 +732,14 @@ export class ProcessOutput extends Error {
return this._dto.duration
}

get [Symbol.toStringTag](): string {
return 'ProcessOutput'
}

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

toString(): string {
return this.stdall
}
Expand Down
38 changes: 16 additions & 22 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,10 @@ describe('core', () => {
assert.ok(p.exitCode instanceof Promise)
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 @@ -907,10 +911,7 @@ describe('core', () => {
lines.push(line)
}

assert.equal(lines.length, 3, 'Should have 3 lines')
assert.equal(lines[0], 'Line1', 'First line should be "Line1"')
assert.equal(lines[1], 'Line2', 'Second line should be "Line2"')
assert.equal(lines[2], 'Line3', 'Third line should be "Line3"')
assert.deepEqual(lines, ['Line1', 'Line2', 'Line3'])
})

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

assert.equal(lines.length, 3, 'Should have 3 lines')
assert.equal(
lines[0],
'PartialLine1',
'First line should be "PartialLine1"'
)
assert.equal(lines[1], 'Line2', 'Second line should be "Line2"')
assert.equal(
lines[2],
'PartialLine3',
'Third line should be "PartialLine3"'
)
assert.deepEqual(lines, ['PartialLine1', 'Line2', 'PartialLine3'])
})

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

assert.equal(
lines.length,
1,
'Should have 1 line for single line without trailing newline'
)
assert.equal(lines[0], 'SingleLine', 'The line should be "SingleLine"')
assert.deepEqual(lines, ['SingleLine'])
})

it('should yield all buffered and new chunks when iterated after a delay', async () => {
Expand Down Expand Up @@ -1118,6 +1103,14 @@ describe('core', () => {
assert.equal(o.signal, 'SIGTERM')
assert.equal(o.exitCode, -1)
assert.equal(o.duration, 20)
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 () => {
Expand Down Expand Up @@ -1175,6 +1168,7 @@ describe('core', () => {
}
assert.deepEqual(lines, expected)
assert.deepEqual(o.lines(), expected)
assert.deepEqual([...o], expected) // isConcatSpreadable
})

describe('static', () => {
Expand Down