-
Notifications
You must be signed in to change notification settings - Fork 0
/
Text.ts
83 lines (81 loc) · 2.2 KB
/
Text.ts
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import * as R from "ramda";
import Decimal from "decimal.js";
import { number, maybe } from "./utils";
import * as unified from "string-unified";
const lower = R.toLower;
const upper = R.toUpper;
const lower_ = R.curry(function (text: string) {
return /[a-z]/.test(text) && !/[A-Z]/.test(text);
});
const upper_ = R.curry(function (text: string) {
return !/[a-z]/.test(text) && /[A-Z]/.test(text);
});
const nth = R.curry(function (index: Decimal, text: string) {
return maybe(unified.charAt(text, number(index)));
});
const take = R.curry(function (length: Decimal, text: string) {
const len = number(length);
if (len <= 0) {
return "";
}
return unified.substring(text, 0, len);
});
const takeLast = R.curry(function (length: Decimal, text: string) {
const len = number(length);
if (len <= 0) {
return "";
}
return unified.substring(text, -len);
});
const trim = R.trim;
const split = R.curry(function (splitter: string, text: string) {
return unified.split(text, splitter);
});
const capitalize = R.curry(function (text: string) {
if (text === "") {
return "";
}
return unified.charAt(text, 0) + capitalize(unified.substring(text, 1));
});
const endsWith = R.curry(function (searchText: string, text: string) {
return unified.endsWith(text, searchText);
});
const startsWith = R.curry(function (searchText: string, text: string) {
return unified.startsWith(text, searchText);
});
const slice = R.curry(function (start: Decimal, end: Decimal, text: string) {
return unified.substring(text, number(start), number(end));
});
const member = R.curry(function (searchText: string, text: string) {
return unified.includes(text, searchText);
});
const length = function (text: string) {
return new Decimal(unified.length(text));
}
const char = function (code: Decimal) {
try {
return String.fromCodePoint(code.toNumber());
} catch(ignore) {
return maybe(undefined);
}
}
const join = R.join;
export default {
lower,
upper,
"lower?": lower_,
"upper?": upper_,
nth,
take,
takeLast,
trim,
split,
capitalize,
endsWith,
startsWith,
slice,
member,
length,
char,
join
}