UNPKG

4.63 kBJavaScriptView Raw
1// Some utility functions in js
2
3var u = module.exports = {
4 array: {
5 // Returns a copy of the array with the value removed once
6 //
7 // [1, 2, 3, 1].del 1 #=> [2, 3, 1]
8 // [1, 2, 3].del 4 #=> [1, 2, 3]
9 del: function (arr, val) {
10 var index = arr.indexOf(val);
11 if (index != -1) {
12 if (index == 0) {
13 return arr.slice(1)
14 } else {
15 return arr.slice(0, index).concat(arr.slice(index+1));
16 }
17 } else {
18 return arr;
19 }
20 },
21
22 // Returns the first element of the array
23 //
24 // [1, 2, 3].first() #=> 1
25 first: function(arr) {
26 return arr[0];
27 },
28
29 // Returns the last element of the array
30 //
31 // [1, 2, 3].last() #=> 3
32 last: function(arr) {
33 return arr[arr.length-1];
34 }
35 },
36 string: {
37 // Returns a copy of str with all occurrences of pattern replaced with either replacement or the return value of a function.
38 // The pattern will typically be a Regexp; if it is a String then no regular expression metacharacters will be interpreted
39 // (that is /\d/ will match a digit, but ‘\d’ will match a backslash followed by a ‘d’).
40 //
41 // In the function form, the current match object is passed in as a parameter to the function, and variables such as
42 // $[1], $[2], $[3] (where $ is the match object) will be set appropriately. The value returned by the function will be
43 // substituted for the match on each call.
44 //
45 // The result inherits any tainting in the original string or any supplied replacement string.
46 //
47 // "hello".gsub /[aeiou]/, '*' #=> "h*ll*"
48 // "hello".gsub /[aeiou]/, '<$1>' #=> "h<e>ll<o>"
49 // "hello".gsub /[aeiou]/, ($) {
50 // "<#{$[1]}>" #=> "h<e>ll<o>"
51 //
52 gsub: function (str, pattern, replacement) {
53 var i, match, matchCmpr, matchCmprPrev, replacementStr, result, self;
54 if (!((pattern != null) && (replacement != null))) return u.string.value(str);
55 result = '';
56 self = str;
57 while (self.length > 0) {
58 if ((match = self.match(pattern))) {
59 result += self.slice(0, match.index);
60 if (typeof replacement === 'function') {
61 match[1] = match[1] || match[0];
62 result += replacement(match);
63 } else if (replacement.match(/\$[1-9]/)) {
64 matchCmprPrev = match;
65 matchCmpr = u.array.del(match, void 0);
66 while (matchCmpr !== matchCmprPrev) {
67 matchCmprPrev = matchCmpr;
68 matchCmpr = u.array.del(matchCmpr, void 0);
69 }
70 match[1] = match[1] || match[0];
71 replacementStr = replacement;
72 for (i = 1; i <= 9; i++) {
73 if (matchCmpr[i]) {
74 replacementStr = u.string.gsub(replacementStr, new RegExp("\\\$" + i), matchCmpr[i]);
75 }
76 }
77 result += replacementStr;
78 } else {
79 result += replacement;
80 }
81 self = self.slice(match.index + match[0].length);
82 } else {
83 result += self;
84 self = '';
85 }
86 }
87 return result;
88 },
89
90 // Returns a copy of the String with the first letter being upper case
91 //
92 // "hello".upcase #=> "Hello"
93 upcase: function(str) {
94 var self = u.string.gsub(str, /_([a-z])/, function ($) {
95 return "_" + $[1].toUpperCase();
96 });
97 self = u.string.gsub(self, /\/([a-z])/, function ($) {
98 return "/" + $[1].toUpperCase();
99 });
100 return self[0].toUpperCase() + self.substr(1);
101 },
102
103 // Returns a copy of capitalized string
104 //
105 // "employee salary" #=> "Employee Salary"
106 capitalize: function (str, spaces) {
107 var self = str.toLowerCase();
108 if(!spaces) {
109 self = u.string.gsub(self, /\s([a-z])/, function ($) {
110 return " " + $[1].toUpperCase();
111 });
112 }
113 return self[0].toUpperCase() + self.substr(1);
114 },
115
116 // Returns a copy of the String with the first letter being lower case
117 //
118 // "HELLO".downcase #=> "hELLO"
119 downcase: function(str) {
120 var self = u.string.gsub(str, /_([A-Z])/, function ($) {
121 return "_" + $[1].toLowerCase();
122 });
123 self = u.string.gsub(self, /\/([A-Z])/, function ($) {
124 return "/" + $[1].toLowerCase();
125 });
126 return self[0].toLowerCase() + self.substr(1);
127 },
128
129 // Returns a string value for the String object
130 //
131 // "hello".value() #=> "hello"
132 value: function (str) {
133 return str.substr(0);
134 }
135 }
136}