1 |
|
2 | (function() {
|
3 | var blank, camelCase, capitalize, compose, curry, dashed, htmlEscape, isMatch, match, plainText, ref, replace, split, titleCase, toLower, toString, toUpper, trim, underscored, w;
|
4 |
|
5 | ref = require("fairmont-core"), curry = ref.curry, compose = ref.compose;
|
6 |
|
7 | toString = function(x) {
|
8 | return x.toString();
|
9 | };
|
10 |
|
11 | toUpper = function(s) {
|
12 | return s.toUpperCase();
|
13 | };
|
14 |
|
15 | toLower = function(s) {
|
16 | return s.toLowerCase();
|
17 | };
|
18 |
|
19 | plainText = function(string) {
|
20 | return string.replace(/^[A-Z]/g, function(c) {
|
21 | return c.toLowerCase();
|
22 | }).replace(/[A-Z]/g, function(c) {
|
23 | return " " + (c.toLowerCase());
|
24 | }).replace(/\W+/g, " ");
|
25 | };
|
26 |
|
27 | capitalize = function(string) {
|
28 | return string[0].toUpperCase() + string.slice(1);
|
29 | };
|
30 |
|
31 | titleCase = function(string) {
|
32 | return string.toLowerCase().replace(/^(\w)|\W(\w)/g, function(char) {
|
33 | return char.toUpperCase();
|
34 | });
|
35 | };
|
36 |
|
37 | camelCase = function(string) {
|
38 | return string.toLowerCase().replace(/(\W+\w)/g, function(string) {
|
39 | return string.trim().toUpperCase();
|
40 | });
|
41 | };
|
42 |
|
43 | underscored = function(string) {
|
44 | return plainText(string).replace(/\W+/g, "_");
|
45 | };
|
46 |
|
47 | dashed = function(string) {
|
48 | return plainText(string).replace(/\W+/g, "-");
|
49 | };
|
50 |
|
51 | htmlEscape = (function() {
|
52 | var entities, map, re;
|
53 | map = {
|
54 | "&": "&",
|
55 | "<": "<",
|
56 | ">": ">",
|
57 | '"': '"',
|
58 | "'": ''',
|
59 | "/": '/'
|
60 | };
|
61 | entities = Object.keys(map);
|
62 | re = new RegExp("" + (entities.join('|')), "g");
|
63 | return function(string) {
|
64 | return string.replace(re, function(s) {
|
65 | return map[s];
|
66 | });
|
67 | };
|
68 | })();
|
69 |
|
70 | trim = function(s) {
|
71 | return s.trim();
|
72 | };
|
73 |
|
74 | split = curry(function(re, s) {
|
75 | return s.split(re);
|
76 | });
|
77 |
|
78 | w = compose(split(/\s+/), trim);
|
79 |
|
80 | blank = function(s) {
|
81 | return s.length === 0;
|
82 | };
|
83 |
|
84 | match = curry(function(pattern, string) {
|
85 | return string.match(pattern);
|
86 | });
|
87 |
|
88 | isMatch = curry(function(pattern, string) {
|
89 | return pattern.test(string);
|
90 | });
|
91 |
|
92 | replace = curry(function(pattern, replacement, string) {
|
93 | return string.replace(pattern, replacement);
|
94 | });
|
95 |
|
96 | module.exports = {
|
97 | toString: toString,
|
98 | toUpper: toUpper,
|
99 | toLower: toLower,
|
100 | capitalize: capitalize,
|
101 | titleCase: titleCase,
|
102 | camelCase: camelCase,
|
103 | underscored: underscored,
|
104 | dashed: dashed,
|
105 | plainText: plainText,
|
106 | htmlEscape: htmlEscape,
|
107 | split: split,
|
108 | w: w,
|
109 | blank: blank,
|
110 | match: match,
|
111 | isMatch: isMatch,
|
112 | replace: replace
|
113 | };
|
114 |
|
115 | }).call(this);
|