|
| 1 | +"use strict"; |
| 2 | +/* global Symbol */ |
| 3 | + |
| 4 | +var hasArrayFrom = typeof Array.from === "function"; |
| 5 | +var hasStringIterator = |
| 6 | + typeof Symbol !== "undefined" && |
| 7 | + Symbol != null && |
| 8 | + typeof Symbol.iterator !== "undefined" && |
| 9 | + typeof String.prototype[Symbol.iterator] === "function"; |
| 10 | +var hasFromCodePoint = typeof String.prototype.fromCodePoint === "function"; |
| 11 | +var hasCodePointAt = typeof String.prototype.codePointAt === "function"; |
| 12 | + |
| 13 | +exports._unsafeCodePointAt0 = function (fallback) { |
| 14 | + return hasCodePointAt |
| 15 | + ? function (str) { return str.codePointAt(0); } |
| 16 | + : fallback; |
| 17 | +}; |
| 18 | + |
| 19 | +exports._codePointAt = function (fallback) { |
| 20 | + return function (Just) { |
| 21 | + return function (Nothing) { |
| 22 | + return function (unsafeCodePointAt0) { |
| 23 | + return function (index) { |
| 24 | + return function (str) { |
| 25 | + var length = str.length; |
| 26 | + if (index < 0 || index >= length) return Nothing; |
| 27 | + if (hasStringIterator) { |
| 28 | + var iter = str[Symbol.iterator](); |
| 29 | + for (var i = index;; --i) { |
| 30 | + var o = iter.next(); |
| 31 | + if (o.done) return Nothing; |
| 32 | + if (i === 0) return Just(unsafeCodePointAt0(o.value)); |
| 33 | + } |
| 34 | + } |
| 35 | + return fallback(index)(str); |
| 36 | + }; |
| 37 | + }; |
| 38 | + }; |
| 39 | + }; |
| 40 | + }; |
| 41 | +}; |
| 42 | + |
| 43 | +exports._count = function (fallback) { |
| 44 | + return function (unsafeCodePointAt0) { |
| 45 | + if (hasStringIterator) { |
| 46 | + return function (pred) { |
| 47 | + return function (str) { |
| 48 | + var iter = str[Symbol.iterator](); |
| 49 | + for (var cpCount = 0; ; ++cpCount) { |
| 50 | + var o = iter.next(); |
| 51 | + if (o.done) return cpCount; |
| 52 | + var cp = unsafeCodePointAt0(o.value); |
| 53 | + if (!pred(cp)) return cpCount; |
| 54 | + } |
| 55 | + }; |
| 56 | + }; |
| 57 | + } |
| 58 | + return fallback; |
| 59 | + }; |
| 60 | +}; |
| 61 | + |
| 62 | +exports._fromCodePointArray = function (singleton) { |
| 63 | + return hasFromCodePoint |
| 64 | + ? function (cps) { |
| 65 | + // Function.prototype.apply will fail for very large second parameters, |
| 66 | + // so we don't use it for arrays with 10,000 or more entries. |
| 67 | + if (cps.length < 10e3) { |
| 68 | + return String.fromCodePoint.apply(String, cps); |
| 69 | + } |
| 70 | + return cps.map(singleton).join(""); |
| 71 | + } |
| 72 | + : function (cps) { |
| 73 | + return cps.map(singleton).join(""); |
| 74 | + }; |
| 75 | +}; |
| 76 | + |
| 77 | +exports._singleton = function (fallback) { |
| 78 | + return hasFromCodePoint ? String.fromCodePoint : fallback; |
| 79 | +}; |
| 80 | + |
| 81 | +exports._take = function (fallback) { |
| 82 | + return function (n) { |
| 83 | + if (hasStringIterator) { |
| 84 | + return function (str) { |
| 85 | + var accum = ""; |
| 86 | + var iter = str[Symbol.iterator](); |
| 87 | + for (var i = 0; i < n; ++i) { |
| 88 | + var o = iter.next(); |
| 89 | + if (o.done) return accum; |
| 90 | + accum += o.value; |
| 91 | + } |
| 92 | + return accum; |
| 93 | + }; |
| 94 | + } |
| 95 | + return fallback(n); |
| 96 | + }; |
| 97 | +}; |
| 98 | + |
| 99 | +exports._toCodePointArray = function (fallback) { |
| 100 | + return function (unsafeCodePointAt0) { |
| 101 | + if (hasArrayFrom) { |
| 102 | + return function (str) { |
| 103 | + return Array.from(str, unsafeCodePointAt0); |
| 104 | + }; |
| 105 | + } |
| 106 | + return fallback; |
| 107 | + }; |
| 108 | +}; |
0 commit comments