mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-18 05:50:43 -07:00
chore: upgrade typescript
This commit is contained in:
+7
-9
@@ -34,10 +34,8 @@ function isAsyncIterator (val: any): val is IterableIterator<any> {
|
||||
return val && isFunction(val.next) && isFunction(val.throw) && isFunction(val.return)
|
||||
}
|
||||
|
||||
type Task<T> = Thenable<T>
|
||||
|
||||
// convert an async iterator to a thenable (Promise compatible)
|
||||
export function toThenable<T> (val: IterableIterator<any> | Thenable<T> | any): Thenable<T> {
|
||||
export function toThenable<T> (val: IteratorResult<unknown, T> | Thenable<T> | any): Thenable<T> {
|
||||
if (isThenable(val)) return val
|
||||
if (isAsyncIterator(val)) return reduce()
|
||||
return createResolvedThenable(val)
|
||||
@@ -45,18 +43,18 @@ export function toThenable<T> (val: IterableIterator<any> | Thenable<T> | any):
|
||||
function reduce<T> (prev?: T): Thenable<T> {
|
||||
let state
|
||||
try {
|
||||
state = (val as IterableIterator<any>).next(prev)
|
||||
state = val.next(prev)
|
||||
} catch (err) {
|
||||
return createRejectedThenable(err)
|
||||
return createRejectedThenable(err as Error)
|
||||
}
|
||||
|
||||
if (state.done) return createResolvedThenable(state.value)
|
||||
return toThenable(state.value!).then(reduce, err => {
|
||||
let state
|
||||
try {
|
||||
state = (val as IterableIterator<any>).throw!(err)
|
||||
state = val.throw!(err)
|
||||
} catch (e) {
|
||||
return createRejectedThenable(e)
|
||||
return createRejectedThenable(e as Error)
|
||||
}
|
||||
if (state.done) return createResolvedThenable(state.value)
|
||||
return reduce(state.value)
|
||||
@@ -64,12 +62,12 @@ export function toThenable<T> (val: IterableIterator<any> | Thenable<T> | any):
|
||||
}
|
||||
}
|
||||
|
||||
export function toPromise<T> (val: IterableIterator<any> | Thenable<T> | T): Promise<T> {
|
||||
export function toPromise<T> (val: Generator<unknown, T, unknown> | Thenable<T> | T): Promise<T> {
|
||||
return Promise.resolve(toThenable(val))
|
||||
}
|
||||
|
||||
// get the value of async iterator in synchronous manner
|
||||
export function toValue<T> (val: IterableIterator<any> | Thenable<T> | T): T {
|
||||
export function toValue<T> (val: Generator<unknown, T, unknown> | Thenable<T> | T): T {
|
||||
let ret: T
|
||||
toThenable(val)
|
||||
.then((x: any) => {
|
||||
|
||||
@@ -7,6 +7,7 @@ export function isString (value: any): value is string {
|
||||
return typeof value === 'string'
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
export function isFunction (value: any): value is Function {
|
||||
return typeof value === 'function'
|
||||
}
|
||||
@@ -65,16 +66,16 @@ export function isArray (value: any): value is any[] {
|
||||
* @return {Object} Returns object.
|
||||
*/
|
||||
export function forOwn <T> (
|
||||
object: {[key: string]: T} | undefined,
|
||||
obj: {[key: string]: T} | undefined,
|
||||
iteratee: ((val: T, key: string, obj: {[key: string]: T}) => boolean | void)
|
||||
) {
|
||||
object = object || {}
|
||||
for (const k in object) {
|
||||
if (object.hasOwnProperty(k)) {
|
||||
if (iteratee(object[k], k, object) === false) break
|
||||
obj = obj || {}
|
||||
for (const k in obj) {
|
||||
if (Object.hasOwnProperty.call(obj, k)) {
|
||||
if (iteratee(obj[k], k, obj) === false) break
|
||||
}
|
||||
}
|
||||
return object
|
||||
return obj
|
||||
}
|
||||
|
||||
export function last <T>(arr: T[]): T;
|
||||
|
||||
Reference in New Issue
Block a user