Commit 0073c51

Anil Kumar Behera <ark845612@gmail.com>
2024-05-19 21:41:18
feat: provide formatters shortcuts (#811)
* feat request: provide formatters shortcuts #764 Signed-off-by: Anil Kumar Behera <ark845612@gmail.com> * Revert "feat request: provide formatters shortcuts #764" This reverts commit ebf8048b7267488d239e5fc3956136448b069969. * feat: provide formatters shortcuts Signed-off-by: Anil Kumar Behera <ark845612@gmail.com> * fix: correct formatting * fix: simplified json test --------- Signed-off-by: Anil Kumar Behera <ark845612@gmail.com>
1 parent 23fe548
Changed files (2)
src/core.ts
@@ -515,6 +515,18 @@ export class ProcessOutput extends Error {
     return this._combined
   }
 
+  json() {
+    return JSON.parse(this._combined)
+  }
+
+  buffer() {
+    return Buffer.from(this._combined, 'utf8')
+  }
+
+  text() {
+    return this._combined
+  }
+
   valueOf() {
     return this._combined.trim()
   }
test/core.test.js
@@ -226,6 +226,21 @@ describe('core', () => {
     assert.equal((await p).toString(), 'foo\n')
   })
 
+  test('ProcessPromise: implements json()', async () => {
+    const p = $`echo '{"key":"value"}'`
+    assert.deepEqual((await p).json(), { key: 'value' })
+  })
+
+  test('ProcessPromise: implements text()', async () => {
+    const p = $`echo foo`
+    assert.equal((await p).toString(), 'foo\n')
+  })
+
+  test('ProcessPromise: implements buffer()', async () => {
+    const p = $`echo foo`
+    assert.equal((await p).buffer().compare(Buffer.from('foo\n', 'utf-8')), 0)
+  })
+
   test('ProcessPromise: implements valueOf()', async () => {
     const p = $`echo foo`
     assert.equal((await p).valueOf(), 'foo')