Skip to content

Commit d2e3292

Browse files
authored
Merge pull request #79 from michaelficarra/codepoints
add codepoint-based string functions as Data.String.CodePoints
2 parents d1ecf84 + 6edb70f commit d2e3292

File tree

5 files changed

+595
-1
lines changed

5 files changed

+595
-1
lines changed

bower.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
"purescript-either": "^3.0.0",
2121
"purescript-gen": "^1.1.0",
2222
"purescript-maybe": "^3.0.0",
23-
"purescript-partial": "^1.2.0"
23+
"purescript-partial": "^1.2.0",
24+
"purescript-unfoldable": "^3.0.0",
25+
"purescript-arrays": "^4.0.1"
2426
},
2527
"devDependencies": {
2628
"purescript-assert": "^3.0.0",

src/Data/String/CodePoints.js

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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

Comments
 (0)