Skip to content

Commit

Permalink
Merge pull request #50 from shadowtime2000/render-async
Browse files Browse the repository at this point in the history
feat: render async function
  • Loading branch information
shadowtime2000 authored Nov 28, 2020
2 parents a51e374 + 559b02d commit e4a16b5
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
20 changes: 19 additions & 1 deletion src/file-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,22 @@ function renderFile(
return tryHandleCache(data, renderConfig, callback)
}

export { includeFile, renderFile }
function renderFileAsync(
filename: string,
data: DataObj,
config?: PartialConfig,
cb?: CallbackFn
): Promise<string> | void

function renderFileAsync(filename: string, data: DataObj, cb?: CallbackFn): Promise<string> | void

function renderFileAsync(
filename: string,
data: DataObj,
config?: PartialConfig,
cb?: CallbackFn
): Promise<string> | void {
return renderFile(filename, data, { ...config, async: true }, cb)
}

export { includeFile, renderFile, renderFileAsync }
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { config } from './config'
config.includeFile = includeFileHelper
config.filepathCache = {}

export { loadFile, renderFile, renderFile as __express } from './file-handlers'
export { loadFile, renderFile, renderFileAsync, renderFile as __express } from './file-handlers'

/* End file stuff */

Expand Down
4 changes: 4 additions & 0 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,7 @@ export default function render(
return handleCache(template, options)(data, options)
}
}

export function renderAsync(template: string | TemplateFunction, data: object, config?: PartialConfig, cb?: CallbackFn): string | Promise<string> | void {
return render(template, data, { ...config, async: true }, cb)
}
8 changes: 7 additions & 1 deletion test/file-handlers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global it, expect, describe */

import { renderFile, __express, templates, compile } from '../src/index'
import { renderFile, renderFileAsync, __express, templates, compile } from '../src/index'

import { buildRegEx } from './err.spec'

Expand Down Expand Up @@ -137,6 +137,12 @@ if(cb){cb(null,tR)} return tR
})
})

describe('renderFileAsync function', () => {
it('should render properly', async () => {
expect(await renderFileAsync(filePath, { name: 'foo' })).toBe('Hi foo')
})
})

// NOTE: the errors will really look like this:

/*
Expand Down
15 changes: 13 additions & 2 deletions test/render.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global it, expect, describe */

import render from '../src/render'
import render, { renderAsync } from '../src/render'
import compile from '../src/compile'
import { templates } from '../src/containers'

Expand All @@ -20,7 +20,7 @@ describe('Simple Render checks', () => {
)
})
it('Rendering function works', async () => {
let template = 'Hello <%= await it.getName() %>!'
const template = 'Hello <%= await it.getName() %>!'
let getName = () => {
return new Promise((res) => {
setTimeout(() => {
Expand All @@ -30,6 +30,17 @@ describe('Simple Render checks', () => {
}
expect(await render(template, { getName: getName }, { async: true })).toEqual('Hello Ada!')
})
it('Rendering async function works', async () => {
const template = 'Hello <%= await it.getName() %>!'
let getName = () => {
return new Promise((res) => {
setTimeout(() => {
res('Ada')
}, 20)
})
}
expect(await renderAsync(template, { getName })).toEqual('Hello Ada!')
})
})
})

Expand Down

0 comments on commit e4a16b5

Please sign in to comment.