feat: iteration protocols

This commit is contained in:
James Prior
2022-07-09 18:21:54 +08:00
committed by Jun Yang
parent e87f068067
commit a19feea7c4
5 changed files with 100 additions and 2 deletions
+2 -1
View File
@@ -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 []
}