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