2017-12-10 21:51:33 +01:00
|
|
|
"use strict";
|
2017-08-14 05:01:11 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
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");
|
2017-08-14 05:01:11 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
var isArray = Array.isArray, call = Function.prototype.call, some = Array.prototype.some;
|
2017-08-14 05:01:11 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
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";
|
2017-08-14 05:01:11 +02:00
|
|
|
else iterable = get(iterable);
|
|
|
|
|
|
|
|
callable(cb);
|
2017-12-10 21:51:33 +01:00
|
|
|
doBreak = function () {
|
|
|
|
broken = true;
|
|
|
|
};
|
|
|
|
if (mode === "array") {
|
2017-08-14 05:01:11 +02:00
|
|
|
some.call(iterable, function (value) {
|
|
|
|
call.call(cb, thisArg, value, doBreak);
|
2017-12-10 21:51:33 +01:00
|
|
|
return broken;
|
2017-08-14 05:01:11 +02:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
if (mode === "string") {
|
|
|
|
length = iterable.length;
|
|
|
|
for (i = 0; i < length; ++i) {
|
2017-08-14 05:01:11 +02:00
|
|
|
char = iterable[i];
|
2017-12-10 21:51:33 +01:00
|
|
|
if (i + 1 < length) {
|
2017-08-14 05:01:11 +02:00
|
|
|
code = char.charCodeAt(0);
|
2017-12-10 21:51:33 +01:00
|
|
|
if (code >= 0xd800 && code <= 0xdbff) char += iterable[++i];
|
2017-08-14 05:01:11 +02:00
|
|
|
}
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
};
|