From 63b9777a5b512f3f8300a4ebb4b8716c3e0a43ab Mon Sep 17 00:00:00 2001 From: jongleberry Date: Fri, 22 Sep 2023 21:49:35 -0700 Subject: [PATCH] support null values --- __tests__/__snapshots__/types.js.snap | 2 ++ __tests__/types.js | 9 +++++++++ lib/index.js | 1 + 3 files changed, 12 insertions(+) diff --git a/__tests__/__snapshots__/types.js.snap b/__tests__/__snapshots__/types.js.snap index 7fec84a..dbdce88 100644 --- a/__tests__/__snapshots__/types.js.snap +++ b/__tests__/__snapshots__/types.js.snap @@ -1,5 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`a null value 1`] = `"__1____2____3__"`; + exports[`a promise-returning function 1`] = `"__1__asdf__2__"`; exports[`a stream 1`] = `"__1__asdf__2__"`; diff --git a/__tests__/types.js b/__tests__/types.js index 6061cb8..3a50a20 100644 --- a/__tests__/types.js +++ b/__tests__/types.js @@ -54,3 +54,12 @@ test('a stream', async () => { assert.strictEqual(result, '__1__asdf__2__') expect(result).toMatchSnapshot() }) + +test('a null value', async () => { + const promise1 = new Promise(resolve => setImmediate(() => resolve(''))) + const promise2 = new Promise(resolve => setImmediate(() => resolve(null))) + + const result = await streamToString(render`__1__${promise1}__2__${promise2}__3__`) + assert.strictEqual(result, '__1____2____3__') + expect(result).toMatchSnapshot() +}) \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index 68643c0..d50a840 100644 --- a/lib/index.js +++ b/lib/index.js @@ -30,6 +30,7 @@ function render (template, ...values) { } Promise.resolve(value).then((result) => { + if (!result) return if (typeof result === 'string') return stream.write(result) if (result.readableObjectMode) throw new Error('Readable object streams are not supported.') if (result.readable) return pipe(result)