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 []
}
+4
View File
@@ -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).