Lazy
优质
小牛编辑
131浏览
2023-12-01
Wraps an object and returns a Sequence. For null
or undefined
, simply returns an empty sequence (see Lazy.strict for a stricter implementation).
- For arrays, Lazy will create a sequence comprising the elements in the array (an ArrayLikeSequence).
- For objects, Lazy will create a sequence of key/value pairs (an ObjectLikeSequence).
- For strings, Lazy will create a sequence of characters (a StringLikeSequence).
Signature
function Lazy(source) { /*...*/ }
function Lazy(source) { if (isArray(source)) { return new ArrayWrapper(source); } else if (typeof source === "string") { return new StringWrapper(source); } else if (source instanceof Sequence) { return source; } if (Lazy.extensions) { var extensions = Lazy.extensions, length = extensions.length, result; while (!result && length--) { result = extensions[length](source); } if (result) { return result; } } return new ObjectWrapper(source); }
Name | Type(s) | Description |
---|---|---|
source | Array|Object|string | An array, object, or string to wrap. |
returns | Sequence | The wrapped lazy object. |
Examples
Lazy([1, 2, 4]) // instanceof Lazy.ArrayLikeSequence Lazy({ foo: "bar" }) // instanceof Lazy.ObjectLikeSequence Lazy("hello, world!") // instanceof Lazy.StringLikeSequence Lazy() // sequence: [] Lazy(null) // sequence: []