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