mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-18 05:50:43 -07:00
refactor: rename toValue to toValueSync
This commit is contained in:
+5
-5
@@ -12,7 +12,7 @@ import { TagMap } from './template/tag/tag-map'
|
||||
import { FilterMap } from './template/filter/filter-map'
|
||||
import { LiquidOptions, normalizeDirectoryList, NormalizedFullOptions, normalize, RenderOptions } from './liquid-options'
|
||||
import { FilterImplOptions } from './template/filter/filter-impl-options'
|
||||
import { toPromise, toValue } from './util/async'
|
||||
import { toPromise, toValueSync } from './util/async'
|
||||
|
||||
export * from './util/error'
|
||||
export * from './types'
|
||||
@@ -47,7 +47,7 @@ export class Liquid {
|
||||
return toPromise(this._render(tpl, scope, { ...renderOptions, sync: false }))
|
||||
}
|
||||
public renderSync (tpl: Template[], scope?: object, renderOptions?: RenderOptions): any {
|
||||
return toValue(this._render(tpl, scope, { ...renderOptions, sync: true }))
|
||||
return toValueSync(this._render(tpl, scope, { ...renderOptions, sync: true }))
|
||||
}
|
||||
public renderToNodeStream (tpl: Template[], scope?: object, renderOptions: RenderOptions = {}): NodeJS.ReadableStream {
|
||||
const ctx = new Context(scope, this.options, renderOptions)
|
||||
@@ -62,7 +62,7 @@ export class Liquid {
|
||||
return toPromise(this._parseAndRender(html, scope, { ...renderOptions, sync: false }))
|
||||
}
|
||||
public parseAndRenderSync (html: string, scope?: object, renderOptions?: RenderOptions): any {
|
||||
return toValue(this._parseAndRender(html, scope, { ...renderOptions, sync: true }))
|
||||
return toValueSync(this._parseAndRender(html, scope, { ...renderOptions, sync: true }))
|
||||
}
|
||||
|
||||
public _parsePartialFile (file: string, sync?: boolean, currentFile?: string) {
|
||||
@@ -75,7 +75,7 @@ export class Liquid {
|
||||
return toPromise<Template[]>(this.parser.parseFile(file, false))
|
||||
}
|
||||
public parseFileSync (file: string): Template[] {
|
||||
return toValue<Template[]>(this.parser.parseFile(file, true))
|
||||
return toValueSync<Template[]>(this.parser.parseFile(file, true))
|
||||
}
|
||||
public async renderFile (file: string, ctx?: object, renderOptions?: RenderOptions) {
|
||||
const templates = await this.parseFile(file)
|
||||
@@ -98,7 +98,7 @@ export class Liquid {
|
||||
return toPromise(this._evalValue(str, ctx))
|
||||
}
|
||||
public evalValueSync (str: string, ctx: Context): any {
|
||||
return toValue(this._evalValue(str, ctx))
|
||||
return toValueSync(this._evalValue(str, ctx))
|
||||
}
|
||||
|
||||
public registerFilter (name: string, filter: FilterImplOptions) {
|
||||
|
||||
+10
-25
@@ -1,23 +1,8 @@
|
||||
import { isFunction } from './underscore'
|
||||
|
||||
type resolver = (x?: any) => any
|
||||
|
||||
export interface Thenable<T> {
|
||||
then (resolve: resolver, reject?: resolver): Thenable<T>;
|
||||
catch (reject: resolver): Thenable<T>;
|
||||
}
|
||||
|
||||
function isThenable<T> (val: any): val is Thenable<T> {
|
||||
return val && isFunction(val.then)
|
||||
}
|
||||
|
||||
function isAsyncIterator (val: any): val is IterableIterator<any> {
|
||||
return val && isFunction(val.next) && isFunction(val.throw) && isFunction(val.return)
|
||||
}
|
||||
import { isPromise, isIterator } from './underscore'
|
||||
|
||||
// convert an async iterator to a Promise
|
||||
export async function toPromise<T> (val: Generator<unknown, T, unknown> | Thenable<T> | T): Promise<T> {
|
||||
if (!isAsyncIterator(val)) return val
|
||||
export async function toPromise<T> (val: Generator<unknown, T, unknown> | Promise<T> | T): Promise<T> {
|
||||
if (!isIterator(val)) return val
|
||||
let value: unknown
|
||||
let done = false
|
||||
let next = 'next'
|
||||
@@ -27,8 +12,8 @@ export async function toPromise<T> (val: Generator<unknown, T, unknown> | Thenab
|
||||
value = state.value
|
||||
next = 'next'
|
||||
try {
|
||||
if (isAsyncIterator(value)) value = toPromise(value)
|
||||
if (isThenable(value)) value = await value
|
||||
if (isIterator(value)) value = toPromise(value)
|
||||
if (isPromise(value)) value = await value
|
||||
} catch (err) {
|
||||
next = 'throw'
|
||||
value = err
|
||||
@@ -37,9 +22,9 @@ export async function toPromise<T> (val: Generator<unknown, T, unknown> | Thenab
|
||||
return value as T
|
||||
}
|
||||
|
||||
// convert an async iterator to a value in a synchronous maner
|
||||
export function toValue<T> (val: Generator<unknown, T, unknown> | T): T {
|
||||
if (!isAsyncIterator(val)) return val
|
||||
// convert an async iterator to a value in a synchronous manner
|
||||
export function toValueSync<T> (val: Generator<unknown, T, unknown> | T): T {
|
||||
if (!isIterator(val)) return val
|
||||
let value: any
|
||||
let done = false
|
||||
let next = 'next'
|
||||
@@ -48,9 +33,9 @@ export function toValue<T> (val: Generator<unknown, T, unknown> | T): T {
|
||||
done = state.done
|
||||
value = state.value
|
||||
next = 'next'
|
||||
if (isAsyncIterator(value)) {
|
||||
if (isIterator(value)) {
|
||||
try {
|
||||
value = toValue(value)
|
||||
value = toValueSync(value)
|
||||
} catch (err) {
|
||||
next = 'throw'
|
||||
value = err
|
||||
|
||||
@@ -14,6 +14,14 @@ export function isFunction (value: any): value is Function {
|
||||
return typeof value === 'function'
|
||||
}
|
||||
|
||||
export function isPromise<T> (val: any): val is Promise<T> {
|
||||
return val && isFunction(val.then)
|
||||
}
|
||||
|
||||
export function isIterator (val: any): val is IterableIterator<any> {
|
||||
return val && isFunction(val.next) && isFunction(val.throw) && isFunction(val.return)
|
||||
}
|
||||
|
||||
export function escapeRegex (str: string) {
|
||||
return str.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user