mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-18 14:00:39 -07:00
feat: iteration protocols
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import { isNil, isString, isObject, isArray } from './underscore'
|
||||
import { isNil, isString, isObject, isArray, isIterable } from './underscore'
|
||||
|
||||
export function toEnumerable (val: any) {
|
||||
if (isArray(val)) return val
|
||||
if (isString(val) && val.length > 0) return [val]
|
||||
if (isIterable(val)) return Array.from(val)
|
||||
if (isObject(val)) return Object.keys(val).map((key) => [key, val[key]])
|
||||
return []
|
||||
}
|
||||
|
||||
@@ -60,6 +60,10 @@ export function isArray (value: any): value is any[] {
|
||||
return toString.call(value) === '[object Array]'
|
||||
}
|
||||
|
||||
export function isIterable (value: any): value is Iterable<any> {
|
||||
return isObject(value) && Symbol.iterator in value
|
||||
}
|
||||
|
||||
/*
|
||||
* Iterates over own enumerable string keyed properties of an object and invokes iteratee for each property.
|
||||
* The iteratee is invoked with three arguments: (value, key, object).
|
||||
|
||||
Reference in New Issue
Block a user