1 | "use strict";
|
2 |
|
3 | var isArguments = require("es5-ext/function/is-arguments")
|
4 | , callable = require("es5-ext/object/valid-callable")
|
5 | , isString = require("es5-ext/string/is-string")
|
6 | , get = require("./get");
|
7 |
|
8 | var isArray = Array.isArray, call = Function.prototype.call, some = Array.prototype.some;
|
9 |
|
10 | module.exports = function (iterable, cb /*, thisArg*/) {
|
11 | var mode, thisArg = arguments[2], result, doBreak, broken, i, length, char, code;
|
12 | if (isArray(iterable) || isArguments(iterable)) mode = "array";
|
13 | else if (isString(iterable)) mode = "string";
|
14 | else iterable = get(iterable);
|
15 |
|
16 | callable(cb);
|
17 | doBreak = function () {
|
18 | broken = true;
|
19 | };
|
20 | if (mode === "array") {
|
21 | some.call(iterable, function (value) {
|
22 | call.call(cb, thisArg, value, doBreak);
|
23 | return broken;
|
24 | });
|
25 | return;
|
26 | }
|
27 | if (mode === "string") {
|
28 | length = iterable.length;
|
29 | for (i = 0; i < length; ++i) {
|
30 | char = iterable[i];
|
31 | if (i + 1 < length) {
|
32 | code = char.charCodeAt(0);
|
33 | if (code >= 0xd800 && code <= 0xdbff) char += iterable[++i];
|
34 | }
|
35 | call.call(cb, thisArg, char, doBreak);
|
36 | if (broken) break;
|
37 | }
|
38 | return;
|
39 | }
|
40 | result = iterable.next();
|
41 |
|
42 | while (!result.done) {
|
43 | call.call(cb, thisArg, result.value, doBreak);
|
44 | if (broken) return;
|
45 | result = iterable.next();
|
46 | }
|
47 | };
|