UNPKG

5.11 kBJavaScriptView Raw
1var helpers = require('./helpers'),
2 _ = require('underscore'),
3 dateformat = require('./dateformat');
4
5exports.add = function (input, addend) {
6 if (_.isArray(input) && _.isArray(addend)) {
7 return input.concat(addend);
8 }
9
10 if (typeof input === 'object' && typeof addend === 'object') {
11 return _.extend(input, addend);
12 }
13
14 if (_.isNumber(input) && _.isNumber(addend)) {
15 return input + addend;
16 }
17
18 return input + addend;
19};
20
21exports.addslashes = function (input) {
22 if (typeof input === 'object') {
23 _.each(input, function (value, key) {
24 input[key] = exports.addslashes(value);
25 });
26 return input;
27 }
28 return input.replace(/\\/g, '\\\\').replace(/\'/g, "\\'").replace(/\"/g, '\\"');
29};
30
31exports.capitalize = function (input) {
32 if (typeof input === 'object') {
33 _.each(input, function (value, key) {
34 input[key] = exports.capitalize(value);
35 });
36 return input;
37 }
38 return input.toString().charAt(0).toUpperCase() + input.toString().substr(1).toLowerCase();
39};
40
41exports.date = function (input, format, offset, abbr) {
42 var l = format.length,
43 date = new dateformat.DateZ(input),
44 cur,
45 i = 0,
46 out = '';
47
48 if (offset) {
49 date.setTimezoneOffset(offset, abbr);
50 }
51
52 for (i; i < l; i += 1) {
53 cur = format.charAt(i);
54 if (dateformat.hasOwnProperty(cur)) {
55 out += dateformat[cur](date, offset, abbr);
56 } else {
57 out += cur;
58 }
59 }
60 return out;
61};
62
63exports['default'] = function (input, def) {
64 return (typeof input !== 'undefined' && (input || typeof input === 'number')) ? input : def;
65};
66
67exports.escape = exports.e = function (input, type) {
68 type = type || 'html';
69 if (typeof input === 'string') {
70 if (type === 'js') {
71 var i = 0,
72 code,
73 out = '';
74
75 input = input.replace(/\\/g, '\\u005C');
76
77 for (i; i < input.length; i += 1) {
78 code = input.charCodeAt(i);
79 if (code < 32) {
80 code = code.toString(16).toUpperCase();
81 code = (code.length < 2) ? '0' + code : code;
82 out += '\\u00' + code;
83 } else {
84 out += input[i];
85 }
86 }
87
88 return out.replace(/&/g, '\\u0026')
89 .replace(/</g, '\\u003C')
90 .replace(/>/g, '\\u003E')
91 .replace(/\'/g, '\\u0027')
92 .replace(/"/g, '\\u0022')
93 .replace(/\=/g, '\\u003D')
94 .replace(/-/g, '\\u002D')
95 .replace(/;/g, '\\u003B');
96 }
97 return input.replace(/&(?!amp;|lt;|gt;|quot;|#39;)/g, '&amp;')
98 .replace(/</g, '&lt;')
99 .replace(/>/g, '&gt;')
100 .replace(/"/g, '&quot;')
101 .replace(/'/g, '&#39;');
102 }
103 return input;
104};
105
106exports.first = function (input) {
107 if (typeof input === 'object' && !_.isArray(input)) {
108 return '';
109 }
110
111 if (typeof input === 'string') {
112 return input.substr(0, 1);
113 }
114
115 return _.first(input);
116};
117
118exports.join = function (input, separator) {
119 if (_.isArray(input)) {
120 return input.join(separator);
121 }
122
123 if (typeof input === 'object') {
124 var out = [];
125 _.each(input, function (value, key) {
126 out.push(value);
127 });
128 return out.join(separator);
129 }
130 return input;
131};
132
133exports.json_encode = function (input, indent) {
134 return JSON.stringify(input, null, indent || 0);
135};
136
137exports.last = function (input) {
138 if (typeof input === 'object' && !_.isArray(input)) {
139 return '';
140 }
141
142 if (typeof input === 'string') {
143 return input.charAt(input.length - 1);
144 }
145
146 return _.last(input);
147};
148
149exports.length = function (input) {
150 if (typeof input === 'object') {
151 return _.keys(input).length;
152 }
153 return input.length;
154};
155
156exports.lower = function (input) {
157 if (typeof input === 'object') {
158 _.each(input, function (value, key) {
159 input[key] = exports.lower(value);
160 });
161 return input;
162 }
163 return input.toString().toLowerCase();
164};
165
166exports.replace = function (input, search, replacement, flags) {
167 var r = new RegExp(search, flags);
168 return input.replace(r, replacement);
169};
170
171exports.reverse = function (input) {
172 if (_.isArray(input)) {
173 return input.reverse();
174 }
175 return input;
176};
177
178exports.striptags = function (input) {
179 if (typeof input === 'object') {
180 _.each(input, function (value, key) {
181 input[key] = exports.striptags(value);
182 });
183 return input;
184 }
185 return input.toString().replace(/(<([^>]+)>)/ig, '');
186};
187
188exports.title = function (input) {
189 if (typeof input === 'object') {
190 _.each(input, function (value, key) {
191 input[key] = exports.title(value);
192 });
193 return input;
194 }
195 return input.toString().replace(/\w\S*/g, function (str) {
196 return str.charAt(0).toUpperCase() + str.substr(1).toLowerCase();
197 });
198};
199
200exports.uniq = function (input) {
201 return _.uniq(input);
202};
203
204exports.upper = function (input) {
205 if (typeof input === 'object') {
206 _.each(input, function (value, key) {
207 input[key] = exports.upper(value);
208 });
209 return input;
210 }
211 return input.toString().toUpperCase();
212};
213
214exports.url_encode = function (input) {
215 return encodeURIComponent(input);
216};
217
218exports.url_decode = function (input) {
219 return decodeURIComponent(input);
220};