Line | Hits | Source |
---|---|---|
1 | 1 | "use strict"; |
2 | ||
3 | 1 | Object.defineProperty(exports, "__esModule", { |
4 | value: true | |
5 | }); | |
6 | /** | |
7 | * @module array | |
8 | */ | |
9 | ||
10 | /** | |
11 | * Slice the array by one element | |
12 | * | |
13 | * @param {Array} array | |
14 | * @return {Array} | |
15 | */ | |
16 | 1 | var slice1 = function slice1(array) { |
17 | 3 | return Array.prototype.slice.call(array, 1); |
18 | }; | |
19 | ||
20 | 1 | exports.slice1 = slice1; |
21 | /** | |
22 | * Search if the array has an element. | |
23 | * Shortcut for indexOf !== -1 | |
24 | * | |
25 | * @param {Array} array | |
26 | * @param {*} searchElement | |
27 | * @param {Number} i | |
28 | * @return {Boolean} | |
29 | */ | |
30 | 1 | var has = function has(array, searchElement, i) { |
31 | 2 | return Array.prototype.indexOf.call(array, searchElement, i) !== -1; |
32 | }; | |
33 | ||
34 | 1 | exports.has = has; |
35 | /** | |
36 | * Search if the array has an element among provided elements. | |
37 | * Shortcut for indexOf !== -1 | |
38 | * | |
39 | * @param {Array} array | |
40 | * @param {Array} searchElements | |
41 | * @param {Number} i | |
42 | * @return {Boolean} | |
43 | */ | |
44 | 1 | var hasAmong = function hasAmong(array, searchElements, i) { |
45 | for (var j = 0, l = searchElements.length; j < l; j++) { | |
46 | if (Array.prototype.indexOf.call(array, searchElements[j], i) !== -1) { | |
47 | 4 | return true; |
48 | } | |
49 | } | |
50 | 2 | return false; |
51 | }; | |
52 | ||
53 | 1 | exports.hasAmong = hasAmong; |
54 | /** | |
55 | * Remove an element in an array | |
56 | * | |
57 | * @param {Array} array | |
58 | * @param {*} element | |
59 | * @return {Boolean} | |
60 | */ | |
61 | 1 | var remove = function remove(array, element) { |
62 | 4 | var index = array.indexOf(element); |
63 | if (index === -1) { | |
64 | 1 | return false; |
65 | } | |
66 | ||
67 | 3 | array.splice(index, 1); |
68 | 3 | return index; |
69 | }; | |
70 | ||
71 | 1 | exports.remove = remove; |
72 | /** | |
73 | * Clone an array | |
74 | * | |
75 | * @param {Array} array | |
76 | * @return {Array} cloned array | |
77 | */ | |
78 | 1 | var clone = function clone(array) { |
79 | 1 | return array.slice(0); |
80 | }; | |
81 | ||
82 | 1 | exports.clone = clone; |
83 | /** | |
84 | * Last element in an array | |
85 | * | |
86 | * @param {Array} array | |
87 | * @return {*} last element | |
88 | */ | |
89 | 1 | var last = function last(array) { |
90 | 2 | return array[array.length - 1]; |
91 | }; | |
92 | ||
93 | 1 | exports.last = last; |
94 | /** | |
95 | * Random element in an array | |
96 | * | |
97 | * @param {Array} array | |
98 | * @return {*} last element | |
99 | */ | |
100 | 1 | var random = function random(array) { |
101 | 1 | return array[Math.floor(Math.random() * array.length)]; |
102 | }; | |
103 | ||
104 | 1 | exports.random = random; |
105 | /** | |
106 | * Sort functions | |
107 | * | |
108 | * @type {Object} | |
109 | */ | |
110 | 1 | var sortFunctions = { |
111 | "": function (a, b) { | |
112 | if (a < b) { | |
113 | 1 | return -1; |
114 | } | |
115 | if (a > b) { | |
116 | 1 | return 1; |
117 | } | |
118 | 1 | return 0; |
119 | }, | |
120 | number: function number(a, b) { | |
121 | 22 | return a - b; |
122 | }, | |
123 | string: function string(a, b) { | |
124 | 5 | return a.localeCompare(b); |
125 | } | |
126 | }; | |
127 | ||
128 | 1 | exports.sortFunctions = sortFunctions; |
129 | 1 | var sortF = sortFunctions; |
130 | ||
131 | 1 | exports.sortF = sortF; |
132 | /** | |
133 | * Sorts an array by a property | |
134 | * | |
135 | * @param {Array} array | |
136 | * @param {String} propertyName | |
137 | * @param {?Boolean} descending | |
138 | * @param {Function|String|undefined} sortFunction | |
139 | * @return {Array} | |
140 | */ | |
141 | 1 | var sortBy = function sortBy(array, propertyName, descending, sortFunction) { |
142 | if (typeof descending === "string" || typeof descending === "function") { | |
143 | 4 | sortFunction = descending; |
144 | 4 | descending = undefined; |
145 | } | |
146 | ||
147 | if (typeof sortFunction !== "function") { | |
148 | 8 | sortFunction = sortFunctions[sortFunction == null ? "" : sortFunction]; |
149 | } | |
150 | ||
151 | 8 | return array.sort(descending ? function (b, a) { |
152 | 4 | return sortFunction(a[propertyName], b[propertyName]); |
153 | } : function (a, b) { | |
154 | 26 | return sortFunction(a[propertyName], b[propertyName]); |
155 | }); | |
156 | }; | |
157 | ||
158 | 1 | exports.sortBy = sortBy; |
159 | /* findBy: use Array.prototype.find((v) => v.propertyName === value); */ | |
160 | /* findIndexBy: use Array.prototype.findIndex((v) => v.propertyName === value); */ | |
161 | ||
162 | /** | |
163 | * The removeWhen() method removes elements | |
164 | * when element pass the test implemented by the provided function. | |
165 | * | |
166 | * @param {Array} array | |
167 | * @param {Function} callback | |
168 | * @return {Number} the new array's length | |
169 | */ | |
170 | 1 | var removeWhen = function removeWhen(array, callback) { |
171 | 1 | array.forEach(function (elt, index) { |
172 | if (callback(elt, index)) { | |
173 | 2 | array.splice(index, 1); |
174 | } | |
175 | }); | |
176 | 1 | return array.length; |
177 | }; | |
178 | ||
179 | 1 | exports.removeWhen = removeWhen; |
180 | /** | |
181 | * Tests if an array equals another | |
182 | * | |
183 | * @param {Array} array1 | |
184 | * @param {Array} array2 | |
185 | * @return {Boolean} | |
186 | */ | |
187 | 1 | var equals = function equals(array1, array2) { |
188 | 5 | var length = array1.length; |
189 | if (!Array.isArray(array1) || !Array.isArray(array2) || length != array2.length) { | |
190 | 1 | return false; |
191 | } | |
192 | for (var i = 0; i < length; i++) { | |
193 | if (array1[i] !== array2[i]) { | |
194 | 2 | return false; |
195 | } | |
196 | } | |
197 | 2 | return true; |
198 | }; | |
199 | 1 | exports.equals = equals; |
200 | //# sourceMappingURL=array.js.map |
Line | Hits | Source |
---|---|---|
1 | 1 | "use strict"; |
2 | ||
3 | 1 | Object.defineProperty(exports, "__esModule", { |
4 | value: true | |
5 | }); | |
6 | /** | |
7 | * @module date | |
8 | */ | |
9 | ||
10 | /** | |
11 | * Returns if the year in parameter is a leap year | |
12 | * | |
13 | * @param {Number} year | |
14 | * @return {Boolean} | |
15 | */ | |
16 | 1 | var isLeapYear = function isLeapYear(year) { |
17 | 7 | return year % 400 === 0 || year % 4 === 0 && year % 100 !== 0 || year === 0; |
18 | }; | |
19 | ||
20 | 1 | exports.isLeapYear = isLeapYear; |
21 | 1 | var _daysInMonth = [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; |
22 | /** | |
23 | * Returns the number of days in the month and year in parameters | |
24 | * | |
25 | * @param {Number} month from 0 to 11 | |
26 | * @param {Number} year | |
27 | * @return {Number} 28, 29, 30 or 31 | |
28 | */ | |
29 | 1 | var daysInMonth = function daysInMonth(month, year) { |
30 | if (month === 1) { | |
31 | 2 | return exports.isLeapYear(year) ? 29 : 28; |
32 | } | |
33 | 11 | return _daysInMonth[month]; |
34 | }; | |
35 | ||
36 | 1 | exports.daysInMonth = daysInMonth; |
37 | /** | |
38 | * Transform a date into a SQL date : YYYY-MM-DD HH:MM:SS | |
39 | * | |
40 | * @param {Date} date | |
41 | * @param {Boolean} withHours | |
42 | * @return {String} string date with format YYYY-MM-DD HH:MM:SS | |
43 | */ | |
44 | 1 | var toSqlDate = function toSqlDate(date, withHours) { |
45 | 4 | var day = date.getDate(), |
46 | month = date.getMonth(); | |
47 | 4 | var result = date.getFullYear() + "-" + (month < 9 ? "0" : "") + (month + 1) + "-" + (day < 10 ? "0" : "") + day; |
48 | if (withHours === false) { | |
49 | 2 | return result; |
50 | } | |
51 | 2 | var hours = date.getHours(), |
52 | minutes = date.getMinutes(), | |
53 | seconds = date.getSeconds(); | |
54 | 2 | return result + " " + (hours < 10 ? "0" : "") + hours + (minutes < 10 ? ":0" : ":") + minutes + (seconds < 10 ? ":0" : ":") + seconds; |
55 | }; | |
56 | ||
57 | 1 | exports.toSqlDate = toSqlDate; |
58 | /** | |
59 | * Transform a SQL string date into a Date | |
60 | * | |
61 | * @param {String} sqlDate | |
62 | * @return {Date} date | |
63 | */ | |
64 | 1 | var parseSqlDate = function parseSqlDate(date) { |
65 | 4 | date = date.split(" "); |
66 | 4 | date[0] = date[0].split("-"); |
67 | if (date.length === 2) { | |
68 | 2 | date[1] = date[1].split(":"); |
69 | 2 | return new Date(date[0][0], date[0][1] - 1, date[0][2], date[1][0], date[1][1], date[1][2]); |
70 | } | |
71 | 2 | return new Date(date[0][0], date[0][1] - 1, date[0][2]); |
72 | }; | |
73 | 1 | exports.parseSqlDate = parseSqlDate; |
74 | //# sourceMappingURL=date.js.map |
Line | Hits | Source |
---|---|---|
1 | 1 | "use strict"; |
2 | ||
3 | 1 | Object.defineProperty(exports, "__esModule", { |
4 | value: true | |
5 | }); | |
6 | /* jshint maxlen: 200 */ | |
7 | /** | |
8 | * fs API with Promises | |
9 | * | |
10 | * @module fs | |
11 | */ | |
12 | ||
13 | 1 | var fs = require("fs"); |
14 | 1 | var promisesUtils = require("./promises"); |
15 | 1 | var YAML = require("js-yaml"); |
16 | ||
17 | /** | |
18 | * Rename a file synchronously | |
19 | * | |
20 | * @param {String} oldPath | |
21 | * @param {String} newPath | |
22 | * @return {Boolean} | |
23 | */ | |
24 | ||
25 | 1 | [ |
26 | /** | |
27 | * Rename a file | |
28 | * | |
29 | * @function module:fs.rename | |
30 | * @param {String} oldPath | |
31 | * @param {String} newPath | |
32 | * @return {Promise} | |
33 | */ | |
34 | "rename", | |
35 | /** | |
36 | * Truncate a file to a specified length | |
37 | * | |
38 | * @function module:fs.ftruncate | |
39 | * @param {FileDescriptor} fd file descriptor | |
40 | * @param {Number=} len | |
41 | * @return {Promise} | |
42 | */ | |
43 | "ftruncate", | |
44 | /** | |
45 | * Truncate a file to a specified length | |
46 | * | |
47 | * @function module:fs.truncate | |
48 | * @param {String} path | |
49 | * @param {Number=} len | |
50 | * @return {Promise} | |
51 | */ | |
52 | "truncate", | |
53 | /** | |
54 | * Change ownership of a file | |
55 | * | |
56 | * @function module:fs.chown | |
57 | * @param {String} path | |
58 | * @param {Number=} len | |
59 | * @return {Promise} | |
60 | */ | |
61 | "chown", | |
62 | /** | |
63 | * Change ownership of a file | |
64 | * | |
65 | * @function module:fs.fchown | |
66 | * @param {FileDescriptor} fd file descriptor | |
67 | * @param {Number=} len | |
68 | * @return {Promise} | |
69 | */ | |
70 | "fchown", | |
71 | /** | |
72 | * Change ownership of a file | |
73 | * is like chown(), but does not dereference symbolic links. | |
74 | * | |
75 | * @function module:fs.lchown | |
76 | * @param {String} path | |
77 | * @param {Number=} len | |
78 | * @return {Promise} | |
79 | */ | |
80 | "lchown", | |
81 | /** | |
82 | * Change permissions of a file | |
83 | * | |
84 | * Changes the permissions of the file specified whose pathname is given in path, | |
85 | * which is dereferenced if it is a symbolic link. | |
86 | * | |
87 | * @function module:fs.chmod | |
88 | * @param {String} path | |
89 | * @param {Number=} len | |
90 | * @return {Promise} | |
91 | */ | |
92 | "chmod", | |
93 | /** | |
94 | * Change permissions of a file | |
95 | * | |
96 | * @function module:fs.fchmod | |
97 | * @param {FileDescriptor} fd file descriptor | |
98 | * @param {Number=} len | |
99 | * @return {Promise} | |
100 | */ | |
101 | "fchmod", | |
102 | /** | |
103 | * Change permissions of a file | |
104 | * | |
105 | * @function module:fs.lchmod | |
106 | * @param {String} path | |
107 | * @param {Number=} len | |
108 | * @return {Promise} | |
109 | */ | |
110 | "lchmod", | |
111 | /** | |
112 | * Get file status | |
113 | * | |
114 | * @function module:fs.stat | |
115 | * @param {String} path | |
116 | * @param {Number=} len | |
117 | * @return {Promise} | |
118 | */ | |
119 | "stat", | |
120 | /** | |
121 | * Get file status | |
122 | * | |
123 | * Identical to stat(), except that if path is a symbolic link, | |
124 | * then the link itself is stat-ed, not the file that it refers to. | |
125 | * | |
126 | * @function module:fs.lstat | |
127 | * @param {String} path | |
128 | * @param {Number=} len | |
129 | * @return {Promise} | |
130 | */ | |
131 | "lstat", | |
132 | /** | |
133 | * Get file status | |
134 | * | |
135 | * @function module:fs.fstat | |
136 | * @param {FileDescriptor} fd file descriptor | |
137 | * @param {Number=} len | |
138 | * @return {Promise} | |
139 | */ | |
140 | "fstat", | |
141 | /** | |
142 | * Make a new name for a file | |
143 | * | |
144 | * link() creates a new link (also known as a hard link) to an existing file. | |
145 | * | |
146 | * @function module:fs.stat | |
147 | * @param {FileDescriptor} fd file descriptor | |
148 | * @param {Number=} len | |
149 | * @return {Promise} | |
150 | */ | |
151 | "link", | |
152 | /** | |
153 | * Make a new name for a file | |
154 | * | |
155 | * symlink() creates a symbolic link named newpath which contains the string oldpath. | |
156 | * | |
157 | * @function module:fs.symlink | |
158 | * @param {String} oldPath | |
159 | * @param {String} newPath | |
160 | * @return {Promise} | |
161 | */ | |
162 | "symlink", | |
163 | /** | |
164 | * Read value of a symbolic link | |
165 | * | |
166 | * readlink() places the contents of the symbolic link path in the buffer buf, | |
167 | * which has size bufsiz. readlink() does not append a null byte to buf. | |
168 | * It will truncate the contents (to a length of bufsiz characters), | |
169 | * in case the buffer is too small to hold all of the contents. | |
170 | * | |
171 | * @function module:fs.readlink | |
172 | * @param {String} path | |
173 | * @return {Promise} | |
174 | */ | |
175 | "readlink", | |
176 | /** | |
177 | * The callback gets two arguments (err, resolvedPath). | |
178 | * | |
179 | * May use process.cwd to resolve relative paths. cache is an object literal of mapped paths | |
180 | * that can be used to force a specific path resolution or avoid additional fs.stat calls for known real paths. | |
181 | * | |
182 | * @example | |
183 | * var cache = {'/etc':'/private/etc'}; | |
184 | * fs.realpath('/etc/passwd', cache).then(function(resolvedPath) { | |
185 | * console.log(resolvedPath); | |
186 | * }); | |
187 | * | |
188 | * @function module:fs.realpath | |
189 | * @param {String} path | |
190 | * @return {Promise} | |
191 | */ | |
192 | "realpath", | |
193 | /** | |
194 | * Delete a name and possibly the file it refers to | |
195 | * | |
196 | * unlink() deletes a name from the file system. | |
197 | * | |
198 | * If that name was the last link to a file and no processes have the file open | |
199 | * the file is deleted and the space it was using is made available for reuse. | |
200 | * | |
201 | * If the name was the last link to a file but any processes still have the file open | |
202 | * the file will remain in existence until the last file descriptor referring to it is closed. | |
203 | * | |
204 | * If the name referred to a symbolic link the link is removed. | |
205 | * | |
206 | * If the name referred to a socket, fifo or device the name for it is removed | |
207 | * but processes which have the object open may continue to use it. | |
208 | * | |
209 | * @function module:fs.unlink | |
210 | * @param {String} path | |
211 | * @return {Promise} | |
212 | */ | |
213 | "unlink", | |
214 | /** | |
215 | * Delete a directory | |
216 | * | |
217 | * rmdir() deletes a directory, which must be empty. | |
218 | * | |
219 | * @function module:fs.rmdir | |
220 | * @param {String} path | |
221 | * @return {Promise} | |
222 | */ | |
223 | "rmdir", | |
224 | /** | |
225 | * Create a directory | |
226 | * | |
227 | * rmdir() deletes a directory, which must be empty. | |
228 | * | |
229 | * mkdir() attempts to create a directory named path. | |
230 | * | |
231 | * The argument mode specifies the permissions to use. | |
232 | * It is modified by the process's umask in the usual way: | |
233 | * the permissions of the created directory are (mode & ~umask & 0777). | |
234 | * Other mode bits of the created directory depend on the operating system. For Linux, see below. | |
235 | * | |
236 | * @function module:fs.mkdir | |
237 | * @param {String} path | |
238 | * @param {String|Number=} mode | |
239 | * @return {Promise} | |
240 | */ | |
241 | "mkdir", | |
242 | /** | |
243 | * Reads the contents of a directory. | |
244 | * Promise's result where files is an array of the names of the files in the directory excluding '.' and '..'. | |
245 | * | |
246 | * @function module:fs.readdir | |
247 | * @param {String} path | |
248 | * @return {Promise} | |
249 | */ | |
250 | "readdir", | |
251 | /** | |
252 | * No arguments other than a possible exception are given to the completion callback. | |
253 | * | |
254 | * @function module:fs.close | |
255 | * @param {String} path | |
256 | * @return {Promise} | |
257 | */ | |
258 | "close", | |
259 | /** | |
260 | * No arguments other than a possible exception are given to the completion callback. | |
261 | * | |
262 | * 'r' - Open file for reading. An exception occurs if the file does not exist. | |
263 | * 'r+' - Open file for reading and writing. An exception occurs if the file does not exist. | |
264 | * 'rs' - Open file for reading in synchronous mode. | |
265 | * Instructs the operating system to bypass the local file system cache. | |
266 | * This is primarily useful for opening files on NFS mounts | |
267 | * as it allows you to skip the potentially stale local cache. | |
268 | * It has a very real impact on I/O performance so don't use this flag unless you need it. | |
269 | * 'rs+' - Open file for reading and writing, telling the OS to open it synchronously. | |
270 | * See notes for 'rs' about using this with caution. | |
271 | * 'w' - Open file for writing. The file is created (if it does not exist) or truncated (if it exists). | |
272 | * 'wx' - Like 'w' but fails if path exists. | |
273 | * 'w+' - Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists). | |
274 | * 'wx+' - Like 'w+' but fails if path exists. | |
275 | * 'a' - Open file for appending. The file is created if it does not exist. | |
276 | * 'ax' - Like 'a' but fails if path exists. | |
277 | * 'a+' - Open file for reading and appending. The file is created if it does not exist. | |
278 | * 'ax+' - Like 'a+' but fails if path exists. | |
279 | * | |
280 | * mode sets the file mode (permission and sticky bits), but only if the file was created. | |
281 | * It defaults to 0666, readable and writeable. | |
282 | * | |
283 | * The exclusive flag 'x' (O_EXCL flag in open(2)) ensures that path is newly created. | |
284 | * On POSIX systems, path is considered to exist even if it is a symlink to a non-existent file. | |
285 | * The exclusive flag may or may not work with network file systems. | |
286 | * | |
287 | * On Linux, positional writes don't work when the file is opened in append mode. | |
288 | * The kernel ignores the position argument and always appends the data to the end of the file. | |
289 | * | |
290 | * @function module:fs.close | |
291 | * @param {String} path | |
292 | * @param {String} flags | |
293 | * @param {String|Number=} mode | |
294 | * @return {Promise} | |
295 | */ | |
296 | "open", | |
297 | /** | |
298 | * Change file last access and modification times | |
299 | * | |
300 | * The utime() system call changes the access and modification times of the inode specified by filename | |
301 | * to the actime and modtime fields of times respectively. | |
302 | * | |
303 | * If times is NULL, then the access and modification times of the file are set to the current time. | |
304 | * | |
305 | * Changing timestamps is permitted when: either the process has appropriate privileges, | |
306 | * or the effective user ID equals the user ID of the file, | |
307 | * or times is NULL and the process has write permission for the file. | |
308 | * | |
309 | * @function module:fs.utimes | |
310 | * @param {String} path | |
311 | * @param {String} atime | |
312 | * @param {String} mtime | |
313 | * @return {Promise} | |
314 | */ | |
315 | "utimes", | |
316 | /** | |
317 | * Change file last access and modification times | |
318 | * | |
319 | * The utime() system call changes the access and modification times of the inode specified by filename | |
320 | * to the actime and modtime fields of times respectively. | |
321 | * | |
322 | * If times is NULL, then the access and modification times of the file are set to the current time. | |
323 | * | |
324 | * Changing timestamps is permitted when: either the process has appropriate privileges, | |
325 | * or the effective user ID equals the user ID of the file, | |
326 | * or times is NULL and the process has write permission for the file. | |
327 | * | |
328 | * @function module:fs.futimes | |
329 | * @param {FileDescriptor} fd file descriptor | |
330 | * @param {String} atime | |
331 | * @param {String} mtime | |
332 | * @return {Promise} | |
333 | */ | |
334 | "futimes", | |
335 | /** | |
336 | * Synchronize a file's in-core state with storage device | |
337 | * | |
338 | * @function module:fs.fsync | |
339 | * @param {FileDescriptor} fd file descriptor | |
340 | * @return {Promise} | |
341 | */ | |
342 | "fsync", | |
343 | /** | |
344 | * Write buffer to the file specified by fd. | |
345 | * | |
346 | * @function module:fs.write | |
347 | * @param {FileDescriptor} fd file descriptor | |
348 | * @param {*} buffer | |
349 | * @param {Number=} offset | |
350 | * @param {Number=} length | |
351 | * @param {Number=} position | |
352 | * @return {Promise} | |
353 | */ | |
354 | "write", | |
355 | /** | |
356 | * Read data from the file specified by fd. | |
357 | * | |
358 | * @function module:fs.read | |
359 | * @param {FileDescriptor} fd file descriptor | |
360 | * @param {*} buffer | |
361 | * @param {Number=} offset | |
362 | * @param {Number=} length | |
363 | * @param {Number=} position | |
364 | * @return {Promise} | |
365 | */ | |
366 | "read", | |
367 | /** | |
368 | * Reads the entire contents of a file. | |
369 | * | |
370 | * options: | |
371 | * encoding String | Null default = null | |
372 | * flag String default = 'r' | |
373 | * | |
374 | * | |
375 | * If no encoding is specified, then the raw buffer is returned. | |
376 | * | |
377 | * @example | |
378 | * fs.readFile('/etc/passwd').then(function(data) { | |
379 | * console.log(data); | |
380 | * }); | |
381 | * | |
382 | * @function module:fs.readFile | |
383 | * @param {String} filename | |
384 | * @param {Object} options | |
385 | * @return {Promise} | |
386 | */ | |
387 | "readFile", | |
388 | /** | |
389 | * Writes data to a file, replacing the file if it already exists. data can be a string or a buffer. | |
390 | * | |
391 | * options: | |
392 | * encoding String | Null default = 'utf8' | |
393 | * mode Number default = 438 (aka 0666 in Octal) | |
394 | * flag String default = 'w' | |
395 | * | |
396 | * | |
397 | * If no encoding is specified, then the raw buffer is returned. | |
398 | * | |
399 | * @example | |
400 | * fs.writeFile('message.txt', 'Hello Node').then(function() { | |
401 | * console.log('Ok !'); | |
402 | * }); | |
403 | * | |
404 | * @function module:fs.writeFile | |
405 | * @param {String} filename | |
406 | * @param {String|Buffer} data | |
407 | * @param {Object=} options | |
408 | * @return {Promise} | |
409 | */ | |
410 | "writeFile", | |
411 | /** | |
412 | * Appends data to a file, creating the file if it not yet exists. data can be a string or a buffer. | |
413 | * | |
414 | * options: | |
415 | * encoding String | Null default = 'utf8' | |
416 | * mode Number default = 438 (aka 0666 in Octal) | |
417 | * flag String default = 'a' | |
418 | * | |
419 | * | |
420 | * If no encoding is specified, then the raw buffer is returned. | |
421 | * | |
422 | * @example | |
423 | * fs.appendFile('message.txt', 'Hello Node').then(function() { | |
424 | * console.log('Ok !'); | |
425 | * }); | |
426 | * | |
427 | * @function module:fs.appendFile | |
428 | * @param {String} filename | |
429 | * @param {String|Buffer} data | |
430 | * @param {Object=} options | |
431 | * @return {Promise} | |
432 | */ | |
433 | "appendFile", | |
434 | /** | |
435 | * Test whether or not the given path exists by checking with the file system. | |
436 | * The promise result is either true or false. | |
437 | * | |
438 | * @example | |
439 | * fs.exists('/etc/passwd', 'Hello Node').then(function(exists) { | |
440 | * console.log(exists ? "it's there" : "no passwd!"); | |
441 | * }); | |
442 | * | |
443 | * @function module:fs.exists | |
444 | * @param {String} path | |
445 | * @return {Promise} | |
446 | */ | |
447 | "exists"].forEach(function (name) { | |
448 | 31 | var fsFn = fs[name]; |
449 | 31 | exports[name] = function () { |
450 | 57 | var args = arguments; |
451 | 57 | return new Promise(function (resolve, reject) { |
452 | 57 | Array.prototype.push.call(args, function (err) { |
453 | if (err) { | |
454 | 1 | return reject(err); |
455 | } | |
456 | 56 | var result = Array.prototype.slice.call(arguments, 1); |
457 | if (result.length === 1) { | |
458 | 46 | result = result[0]; |
459 | } | |
460 | 56 | resolve(result); |
461 | }); | |
462 | 57 | fsFn.apply(fs, args); |
463 | }); | |
464 | }; | |
465 | }); | |
466 | ||
467 | // Copy sync and other functions | |
468 | 1 | Object.keys(fs).forEach(function (name) { |
469 | if (!exports[name]) { | |
470 | 47 | exports[name] = fs[name]; |
471 | } | |
472 | }); | |
473 | ||
474 | /** | |
475 | * Read a file and parse its content as JSON | |
476 | * | |
477 | * @param {String} path | |
478 | * @return {Promise} | |
479 | */ | |
480 | 1 | var readJsonFile = function readJsonFile() { |
481 | 3 | return exports.readFile.apply(exports, arguments).then(JSON.parse); |
482 | }; | |
483 | ||
484 | 1 | exports.readJsonFile = readJsonFile; |
485 | /** | |
486 | * Read a file and parse its content as JSON synchronously | |
487 | * | |
488 | * @param {String} path | |
489 | * @return {*} | |
490 | */ | |
491 | 1 | var readJsonFileSync = function readJsonFileSync() { |
492 | 1 | var result = fs.readFileSync.apply(fs, arguments); |
493 | 1 | return result && JSON.parse(result); |
494 | }; | |
495 | ||
496 | 1 | exports.readJsonFileSync = readJsonFileSync; |
497 | 1 | var writeJsonFile = function writeJsonFile() { |
498 | 1 | var args = arguments; |
499 | 1 | args[1] = JSON.stringify(args[1]); |
500 | 1 | return exports.writeFile.apply(exports, args); |
501 | }; | |
502 | ||
503 | 1 | exports.writeJsonFile = writeJsonFile; |
504 | 1 | var writePrettyJsonFile = function writePrettyJsonFile() { |
505 | 1 | var args = arguments; |
506 | 1 | args[1] = JSON.stringify(args[1], null, 4); |
507 | 1 | return exports.writeFile.apply(exports, args); |
508 | }; | |
509 | ||
510 | 1 | exports.writePrettyJsonFile = writePrettyJsonFile; |
511 | 1 | var parseYaml = function parseYaml(content) { |
512 | 3 | return YAML.safeLoad(content.toString()); |
513 | }; | |
514 | ||
515 | 1 | var stringifyYaml = YAML.safeDump; |
516 | ||
517 | /** | |
518 | * Read a file and parse its content as Yaml | |
519 | * | |
520 | * @param {String} path | |
521 | * @return {Promise} | |
522 | */ | |
523 | 1 | var readYamlFile = function readYamlFile() { |
524 | 2 | return exports.readFile.apply(exports, arguments).then(parseYaml); |
525 | }; | |
526 | ||
527 | 1 | exports.readYamlFile = readYamlFile; |
528 | /** | |
529 | * Read a file and parse its content as Yaml synchronously | |
530 | * | |
531 | * @param {String} path | |
532 | * @return {*} | |
533 | */ | |
534 | 1 | var readYamlFileSync = function readYamlFileSync() { |
535 | 1 | var result = fs.readFileSync.apply(fs, arguments); |
536 | 1 | return result && parseYaml(result); |
537 | }; | |
538 | ||
539 | 1 | exports.readYamlFileSync = readYamlFileSync; |
540 | /** | |
541 | * Write in file a yaml content | |
542 | * | |
543 | * @param {String} path | |
544 | * @param {*} content | |
545 | * @param {Object} options | |
546 | * @return {*} | |
547 | */ | |
548 | 1 | var writeYamlFile = function writeYamlFile() { |
549 | 1 | var args = arguments; |
550 | 1 | args[1] = stringifyYaml(args[1]); |
551 | 1 | return exports.writeFile.apply(exports, args); |
552 | }; | |
553 | ||
554 | 1 | exports.writeYamlFile = writeYamlFile; |
555 | /** | |
556 | * Recursively read a directory. | |
557 | * callback is called for each files | |
558 | * Return a Promise when all files are read. | |
559 | * | |
560 | * @param {String} dir | |
561 | * @param {Object} options | |
562 | * @param {Function} callback | |
563 | * @return {Promise} | |
564 | */ | |
565 | 1 | var readRecursiveDirectory = function readRecursiveDirectory(dir, options, callback) { |
566 | 4 | options = Object.assign({ |
567 | recursive: true, | |
568 | directories: false }, options); | |
569 | 4 | return exports.readdir(dir).then(function (files) { |
570 | 4 | return promisesUtils.forEach(files, function (file) { |
571 | 36 | var path = dir + "/" + file; |
572 | 36 | return exports.stat(path).then(function (stat) { |
573 | if (stat && stat.isDirectory()) { | |
574 | if (options.directories) { | |
575 | 1 | return callback({ dirname: file, path: path, basedir: dir, stat: stat }); |
576 | } | |
577 | if (options.recursive) { | |
578 | 1 | return readRecursiveDirectory(path, options, callback); |
579 | } | |
580 | } else { | |
581 | 33 | return callback({ filename: file, path: path, basedir: dir, stat: stat }); |
582 | } | |
583 | }); | |
584 | }); | |
585 | }); | |
586 | }; | |
587 | 1 | exports.readRecursiveDirectory = readRecursiveDirectory; |
588 | //# sourceMappingURL=fs.js.map |
Line | Hits | Source |
---|---|---|
1 | 1 | "use strict"; |
2 | ||
3 | 1 | Object.defineProperty(exports, "__esModule", { |
4 | value: true | |
5 | }); | |
6 | /** | |
7 | * @module generators | |
8 | */ | |
9 | ||
10 | /** | |
11 | * Generate a random code with the specified size | |
12 | * | |
13 | * @param {Number} size | |
14 | * @param {String=} characters | |
15 | * @return {Map} | |
16 | */ | |
17 | 1 | var randomCode = function randomCode(size, characters) { |
18 | 1 | characters = (characters || "abcdefghijklmnopqrstuvwxyz0123456789").split("").sort(function () { |
19 | 118 | return 0.5 - Math.random(); |
20 | }); // shuffle | |
21 | ||
22 | 1 | var finalWord = "", |
23 | lastChar = "", | |
24 | charBeforeLast = ""; | |
25 | 1 | var i = 0, |
26 | length = characters.length, | |
27 | ch; | |
28 | while (i++ < size) { | |
29 | do { | |
30 | 12 | ch = characters[Math.floor(Math.random() * length)]; |
31 | } while (ch === lastChar || ch === charBeforeLast); | |
32 | 12 | charBeforeLast = lastChar; |
33 | 12 | finalWord += lastChar = ch; |
34 | } | |
35 | 1 | return finalWord; |
36 | }; | |
37 | 1 | exports.randomCode = randomCode; |
38 | //# sourceMappingURL=generators.js.map |
Line | Hits | Source |
---|---|---|
1 | 1 | "use strict"; |
2 | ||
3 | 1 | Object.defineProperty(exports, "__esModule", { |
4 | value: true | |
5 | }); | |
6 | /** | |
7 | * Utils | |
8 | * | |
9 | * @module utils | |
10 | */ | |
11 | ||
12 | /** | |
13 | * shortcut for parseInt(value, 10) | |
14 | * | |
15 | * @param {String} value | |
16 | * @return {Number} parsed int | |
17 | */ | |
18 | 1 | var toInt = function toInt(value) { |
19 | 1 | return parseInt(value, 10); |
20 | }; | |
21 | ||
22 | 1 | exports.toInt = toInt; |
23 | /* IS */ | |
24 | ||
25 | /** | |
26 | * Test is a variable is a string | |
27 | * @param {*} value | |
28 | * @return {Boolean} | |
29 | */ | |
30 | 1 | var isString = function isString(value) { |
31 | 18 | return typeof value === "string"; |
32 | }; | |
33 | ||
34 | 1 | exports.isString = isString; |
35 | /** | |
36 | * Test is a variable is an object | |
37 | * @param {*} value | |
38 | * @return {Boolean} | |
39 | */ | |
40 | 1 | var isObject = function isObject(value) { |
41 | 17 | return typeof value === "object"; |
42 | }; | |
43 | ||
44 | 1 | exports.isObject = isObject; |
45 | /** | |
46 | * Test is a variable is a function | |
47 | * @param {*} value | |
48 | * @return {Boolean} | |
49 | */ | |
50 | 1 | var isFunction = function isFunction(value) { |
51 | 2 | return typeof value === "function"; |
52 | }; | |
53 | ||
54 | 1 | exports.isFunction = isFunction; |
55 | /** | |
56 | * Test is a variable is a number | |
57 | * @param {*} value | |
58 | * @return {Boolean} | |
59 | */ | |
60 | 1 | var isNumber = function isNumber(value) { |
61 | 2 | return typeof value === "number"; |
62 | }; | |
63 | ||
64 | 1 | exports.isNumber = isNumber; |
65 | /** | |
66 | * Test is a variable is a integer number | |
67 | * @param {*} value | |
68 | * @return {Boolean} | |
69 | */ | |
70 | 1 | var isInteger = function isInteger(value) { |
71 | 2 | return Number.isInteger(value); |
72 | }; | |
73 | ||
74 | 1 | exports.isInteger = isInteger; |
75 | /** | |
76 | * Test is a variable is an array | |
77 | * | |
78 | * @function | |
79 | * @param {*} arg | |
80 | * @return {Boolean} | |
81 | */ | |
82 | 1 | var isArray = Array.isArray; |
83 | ||
84 | 1 | exports.isArray = isArray; |
85 | /* HTML */ | |
86 | ||
87 | /** | |
88 | * Escape an html string | |
89 | * | |
90 | * @param {String} html string to be escaped | |
91 | * @return {String} escaped string | |
92 | */ | |
93 | 1 | var escape = function escape(html) { |
94 | 1 | return String(html).replace(/&(?!\w+;)/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """); |
95 | }; | |
96 | ||
97 | 1 | exports.escape = escape; |
98 | /** | |
99 | * Escape an url for html | |
100 | * | |
101 | * @param {String} url | |
102 | * @return {String} escaped url | |
103 | */ | |
104 | 1 | var escapeUrl = function escapeUrl(html) { |
105 | 1 | return html.replace(/&/g, "&"); |
106 | }; | |
107 | ||
108 | 1 | exports.escapeUrl = escapeUrl; |
109 | /** | |
110 | * Escape a string for regexp | |
111 | * | |
112 | * @param {String} string | |
113 | * @return {String} escaped string | |
114 | */ | |
115 | 1 | var regexpEscape = function regexpEscape(string) { |
116 | 4 | return string.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1"); |
117 | }; | |
118 | ||
119 | 1 | exports.regexpEscape = regexpEscape; |
120 | /** | |
121 | * Shortcut for Object.defineProperty | |
122 | * | |
123 | * @param {Object} target | |
124 | * @param {String} property name of the property | |
125 | * @param {*} value value | |
126 | * @param {Object=} options: writable default true, configurable default true, enumerable default false | |
127 | * @return {Object} target | |
128 | */ | |
129 | 1 | var defineProperty = function defineProperty(target, property, value, options) { |
130 | 1 | Object.defineProperty(target, property, { |
131 | value: value, | |
132 | writable: (options && options.writable) !== false, | |
133 | configurable: (options && options.configurable) !== false, | |
134 | enumerable: !!(options && options.enumerable) | |
135 | }); | |
136 | 1 | return target; |
137 | }; | |
138 | ||
139 | 1 | exports.defineProperty = defineProperty; |
140 | /** | |
141 | * Shortcut for Object.defineProperty | |
142 | * | |
143 | * @param {Object} target | |
144 | * @param {String} property name of the property | |
145 | * @param {*} value value | |
146 | * @param {Object=} options: enumerable default false | |
147 | * @return {Object} target | |
148 | */ | |
149 | 1 | var defineConstant = function defineConstant(target, property, value, options) { |
150 | 2 | Object.defineProperty(target, property, { |
151 | value: value, | |
152 | writable: false, | |
153 | configurable: false, | |
154 | enumerable: !!(options && options.enumerable) | |
155 | }); | |
156 | 1 | return target; |
157 | }; | |
158 | ||
159 | 1 | exports.defineConstant = defineConstant; |
160 | /** | |
161 | * Shortcut for Object.defineProperty | |
162 | * | |
163 | * @param {Object} target | |
164 | * @param {String} property name of the property | |
165 | * @param {Function} getter getter function | |
166 | * @param {Object=} options: configurable default true, enumerable default false | |
167 | * @return {Object} target | |
168 | */ | |
169 | 1 | var defineGetter = function defineGetter(target, property, getter, options) { |
170 | 1 | Object.defineProperty(target, property, { |
171 | get: getter, | |
172 | configurable: (options && options.configurable) !== false, | |
173 | enumerable: !!(options && options.enumerable) | |
174 | }); | |
175 | 1 | return target; |
176 | }; | |
177 | ||
178 | 1 | exports.defineGetter = defineGetter; |
179 | /** | |
180 | * Shortcut for Object.defineProperty | |
181 | * | |
182 | * @param {Object} target | |
183 | * @param {String} property name of the property | |
184 | * @param {Function} setter setter function | |
185 | * @param {Object=} options: configurable default true, enumerable default false | |
186 | * @return {Object} target | |
187 | */ | |
188 | 1 | var defineSetter = function defineSetter(target, property, setter, options) { |
189 | 1 | Object.defineProperty(target, property, { |
190 | set: setter, | |
191 | configurable: (options && options.configurable) !== false, | |
192 | enumerable: !!(options && options.enumerable) | |
193 | }); | |
194 | 1 | return target; |
195 | }; | |
196 | ||
197 | 1 | exports.defineSetter = defineSetter; |
198 | /** | |
199 | * Shortcut for Object.defineProperties | |
200 | * | |
201 | * @param {Object} target | |
202 | * @param {Object} properties | |
203 | * @param {Object=} options: writable default true, configurable default true, enumerable default false | |
204 | * @return {Object} target | |
205 | */ | |
206 | 1 | var defineProperties = function defineProperties(target, properties, options) { |
207 | if (!properties) { | |
208 | 1 | return target; |
209 | } | |
210 | 1 | options = { |
211 | writable: (options && options.writable) !== false, | |
212 | configurable: (options && options.configurable) !== false, | |
213 | enumerable: !!(options && options.enumerable) | |
214 | }; | |
215 | 1 | Object.keys(properties).forEach(function (key) { |
216 | 2 | Object.defineProperty(target, key, { |
217 | value: properties[key], | |
218 | writable: options.writable, | |
219 | configurable: options.configurable, | |
220 | enumerable: options.enumerable | |
221 | }); | |
222 | }); | |
223 | 1 | return target; |
224 | }; | |
225 | ||
226 | 1 | exports.defineProperties = defineProperties; |
227 | /** | |
228 | * The entries() method returns a new Iterator that contains the key/value pairs for each index in the Object|Array. | |
229 | * | |
230 | * @function module:utils.entries | |
231 | * @param {Object|Array|Map} arg | |
232 | * @param {Function} callback | |
233 | * @param {*=} thisArg | |
234 | * @return {Iterator} | |
235 | */ | |
236 | ||
237 | /** | |
238 | * The filter() method creates a new Object|Array with all elements | |
239 | * that pass the test implemented by the provided function. | |
240 | * | |
241 | * @function module:utils.filter | |
242 | * @param {Object|Array} arg | |
243 | * @param {Function} callback | |
244 | * @param {*=} thisArg | |
245 | * @return {Object|Array} | |
246 | */ | |
247 | ||
248 | /** | |
249 | * The find() method returns a value in the Object|Array | |
250 | * if an element in the Object|Array satisfies the provided testing function. | |
251 | * | |
252 | * @function module:utils.find | |
253 | * @param {Object|Array} arg | |
254 | * @param {Function} callback | |
255 | * @param {*=} thisArg | |
256 | * @return {*} | |
257 | */ | |
258 | ||
259 | /** | |
260 | * The findIndex() method returns a value in the Object|Array | |
261 | * if an element in the Object|Array satisfies the provided testing function. | |
262 | * | |
263 | * @function module:utils.findIndex | |
264 | * @param {Object|Array} arg | |
265 | * @param {Function} callback | |
266 | * @param {*=} thisArg | |
267 | * @return {number|string|undefined} | |
268 | */ | |
269 | ||
270 | /** | |
271 | * The forEach() method executes a provided function once per Object|Array element. | |
272 | * | |
273 | * @function module:utils.forEach | |
274 | * @param {Object|Array|Map} arg | |
275 | * @param {Function} callback | |
276 | * @param {*=} thisArg | |
277 | * @return {number|string|undefined} | |
278 | */ | |
279 | ||
280 | /** | |
281 | * The join() method joins all elements of an Object|Array into a string. | |
282 | * | |
283 | * @function module:utils.join | |
284 | * @param {Object|Array} arg | |
285 | * @param {String} separator | |
286 | * @return {String} | |
287 | */ | |
288 | ||
289 | /** | |
290 | * The map() method creates a new Object|Array with the results | |
291 | * of calling a provided function on every element in this Object|Array. | |
292 | * | |
293 | * @function module:utils.map | |
294 | * @param {Object|Array} arg | |
295 | * @param {Function} callback | |
296 | * @param {*=} thisArg | |
297 | * @return {Object|Array} | |
298 | */ | |
299 | ||
300 | /** | |
301 | * The reduce() method applies a function against an accumulator | |
302 | * and each value of the Object|Array (from left-to-right) has to reduce it to a single value. | |
303 | * | |
304 | * @function module:utils.reduce | |
305 | * @param {Object|Array} arg | |
306 | * @param {Function} callback callback(previousValue, currentKey, currentValue, objectOrArray) | |
307 | * @param {*=} initialValue | |
308 | * @return {*} | |
309 | */ | |
310 | ||
311 | // TODO http://usejsdoc.org/tags-callback.html | |
312 | /** | |
313 | * The reduceRight() method applies a function against an accumulator | |
314 | * and each value of the Object|Array (from right-to-left) has to reduce it to a single value. | |
315 | * | |
316 | * @function module:utils.reduceRight | |
317 | * @param {Object|Array} arg | |
318 | * @param {Function} callback callback(previousValue, currentKey, currentValue, objectOrArray) | |
319 | * @param {*=} initialValue | |
320 | * @return {*} | |
321 | */ | |
322 | ||
323 | /** | |
324 | * The some() method tests whether some element in the Object|Array | |
325 | * passes the test implemented by the provided function. | |
326 | * | |
327 | * @function module:utils.some | |
328 | * @param {Object|Array} arg | |
329 | * @param {Function} callback | |
330 | * @param {*=} thisArg | |
331 | * @return {*} | |
332 | */ | |
333 | ||
334 | 1 | "entries filter find findIndex forEach join map reduce reduceRight some".split(" ").forEach(function (methodName) { |
335 | 10 | exports[methodName] = function (arrayOrObject) { |
336 | 33 | var args = Array.prototype.slice.call(arguments, 1); |
337 | 33 | var method = arrayOrObject[methodName]; |
338 | if (!method) { | |
339 | 16 | method = exports.object[methodName]; |
340 | 16 | args.unshift(arrayOrObject); |
341 | } | |
342 | 33 | return method.apply(arrayOrObject, args); |
343 | }; | |
344 | }); | |
345 | ||
346 | /** | |
347 | * The mapToArray() method creates a new array with the results | |
348 | * of calling a provided function on every element in this Object|Array. | |
349 | * | |
350 | * @function module:utils.mapToArray | |
351 | * @param {Object|Array|Map} arg | |
352 | * @param {Function} callback | |
353 | * @param {*=} thisArg | |
354 | * @return {Array} | |
355 | */ | |
356 | ||
357 | 1 | var mapToArray = function mapToArray(arrayOrObject, callback) { |
358 | if (Array.isArray(arrayOrObject)) { | |
359 | 1 | return arrayOrObject.map(callback); |
360 | } | |
361 | 2 | var array = []; |
362 | 2 | exports.forEach(arrayOrObject, function (value, index) { |
363 | 4 | array.push(callback(value, index)); |
364 | }); | |
365 | 2 | return array; |
366 | }; | |
367 | ||
368 | 1 | exports.mapToArray = mapToArray; |
369 | /** | |
370 | * Access to the array module | |
371 | * | |
372 | * @constant module:utils.array | |
373 | * @type module:array | |
374 | */ | |
375 | 1 | var array = require("./array"); |
376 | ||
377 | 1 | exports.array = array; |
378 | /** | |
379 | * Access to the object module | |
380 | * | |
381 | * @constant module:utils.object | |
382 | * @type module:object | |
383 | */ | |
384 | 1 | var object = require("./object"); |
385 | ||
386 | 1 | exports.object = object; |
387 | /** | |
388 | * Access to the string module | |
389 | * | |
390 | * @constant module:utils.string | |
391 | * @type module:string | |
392 | */ | |
393 | 1 | var string = require("./string/string"); |
394 | ||
395 | 1 | exports.string = string; |
396 | /** | |
397 | * Access to the promises module | |
398 | * | |
399 | * @constant module:utils.promises | |
400 | * @type module:promises | |
401 | */ | |
402 | 1 | var promises = require("./promises"); |
403 | 1 | exports.promises = promises; |
404 | //# sourceMappingURL=index.js.map |
Line | Hits | Source |
---|---|---|
1 | 1 | "use strict"; |
2 | ||
3 | 1 | Object.defineProperty(exports, "__esModule", { |
4 | value: true | |
5 | }); | |
6 | /** | |
7 | * @module object | |
8 | */ | |
9 | ||
10 | /** | |
11 | * Transforms an object into a Map | |
12 | * | |
13 | * @param {Object|Map} object | |
14 | * @return {Map} | |
15 | */ | |
16 | 1 | var toMap = function toMap(object) { |
17 | if (object instanceof Map) { | |
18 | 1 | return object; |
19 | } | |
20 | 1 | var map = new Map(); |
21 | for (var key in object) { | |
22 | 2 | map.set(key, object[key]); |
23 | } | |
24 | 1 | return map; |
25 | }; | |
26 | ||
27 | 1 | exports.toMap = toMap; |
28 | /** | |
29 | * Same as Object.assign, but doesn't override the value if !== undefined | |
30 | * | |
31 | * @param {Object} target | |
32 | * @param {Object} object | |
33 | * @return {Object} target | |
34 | */ | |
35 | 1 | var union = function union(target) { |
36 | 1 | Array.from(arguments).slice(1).forEach(function (object) { |
37 | if (object) { | |
38 | for (var key in object) { | |
39 | if (target[key] === undefined) { | |
40 | 2 | target[key] = object[key]; |
41 | } | |
42 | } | |
43 | } | |
44 | }); | |
45 | 1 | return target; |
46 | }; | |
47 | ||
48 | 1 | exports.union = union; |
49 | /** | |
50 | * Clones an object | |
51 | * | |
52 | * @param {Object} object | |
53 | * @return {Object} target | |
54 | */ | |
55 | 1 | var clone = function clone(object) { |
56 | 1 | return Object.assign({}, object); |
57 | }; | |
58 | ||
59 | 1 | exports.clone = clone; |
60 | /** | |
61 | * Filter an object by the keys | |
62 | * | |
63 | * @param {Object} object | |
64 | * @param {Array} keys | |
65 | * @return {Object} | |
66 | */ | |
67 | 1 | var filterKeys = function filterKeys(object, keys) { |
68 | 2 | var filteredObject = {}; |
69 | 2 | keys.forEach(function (k) { |
70 | 4 | filteredObject[k] = object[k]; |
71 | }); | |
72 | 2 | return filteredObject; |
73 | }; | |
74 | ||
75 | 1 | exports.filterKeys = filterKeys; |
76 | /** | |
77 | * Same a map + join | |
78 | * | |
79 | * @param {Object} object | |
80 | * @param {Array} keys | |
81 | * @return {Object} | |
82 | */ | |
83 | 1 | var mapJoin = function mapJoin(object, separator, callback) { |
84 | if (typeof separator === "function") { | |
85 | 1 | callback = separator; |
86 | 1 | separator = ""; |
87 | } | |
88 | 2 | object = exports.map(object, callback); |
89 | 2 | return exports.join(object, separator); |
90 | }; | |
91 | ||
92 | 1 | exports.mapJoin = mapJoin; |
93 | // Array-like functions | |
94 | 1 | var _commonObjectArray = function _commonObjectArray(propertyName, object, callback, thisArg) { |
95 | if (!thisArg) { | |
96 | 11 | thisArg = object; |
97 | } | |
98 | /*#if DEV*/ | |
99 | if (object[propertyName]) { | |
100 | 0 | throw new Error("call " + propertyName + " !"); |
101 | } | |
102 | /*#/if*/ | |
103 | 12 | return Object.keys(object)[propertyName](function (key) { |
104 | 31 | return callback.call(thisArg, object[key], key); |
105 | }); | |
106 | }; | |
107 | ||
108 | /** | |
109 | * The entries() method returns a new Iterator that contains the key/value pairs for each index in the Object. | |
110 | * | |
111 | * @param {Object|Map} object | |
112 | * @return {Iterator} | |
113 | */ | |
114 | 1 | var entries = function entries(object) { |
115 | 1 | var keys = Object.keys(object), |
116 | i = 0; | |
117 | 1 | return Object.freeze({ |
118 | next: function next() { | |
119 | if (i >= keys.length) { | |
120 | 1 | return Object.freeze({ done: true }); |
121 | } | |
122 | 2 | return Object.freeze({ value: [keys[i], object[keys[i++]]], done: false }); |
123 | } | |
124 | }); | |
125 | }; | |
126 | ||
127 | 1 | exports.entries = entries; |
128 | /** | |
129 | * The filter() method creates a new object with all elements that pass the test implemented by the provided function. | |
130 | * | |
131 | * @param {Object} object | |
132 | * @param {Function} callback | |
133 | * @param {*=} thisArg | |
134 | * @return {Object} | |
135 | */ | |
136 | 1 | var filter = function filter(object, callback, thisArg) { |
137 | 1 | var keys = _commonObjectArray.call(null, "filter", object, callback, thisArg); |
138 | 1 | return exports.filterKeys(object, keys); |
139 | }; | |
140 | ||
141 | 1 | exports.filter = filter; |
142 | /** | |
143 | * The find() method returns a value in the object, | |
144 | * if an element in the object satisfies the provided testing function. Otherwise undefined is returned. | |
145 | * | |
146 | * @param {Object} object | |
147 | * @param {Function} callback | |
148 | * @param {*=} thisArg | |
149 | * @return {*} | |
150 | */ | |
151 | 1 | var find = function find(object, callback, thisArg) { |
152 | 1 | var key = exports.findIndex.apply(null, arguments); |
153 | 1 | return key && object[key]; |
154 | }; | |
155 | ||
156 | 1 | exports.find = find; |
157 | /** | |
158 | * The findIndex() method returns an key in the object, | |
159 | * if an element in the object satisfies the provided testing function. Otherwise undefined is returned. | |
160 | * | |
161 | * @function | |
162 | * @param {Object} object | |
163 | * @param {Function} callback | |
164 | * @param {*=} thisArg | |
165 | * @return {String|undefined} | |
166 | */ | |
167 | 1 | var findIndex = _commonObjectArray.bind(null, "find"); |
168 | ||
169 | 1 | exports.findIndex = findIndex; |
170 | /** | |
171 | * The forEach() method executes a provided function once per object element. | |
172 | * | |
173 | * @function | |
174 | * @param {Object|Map} object | |
175 | * @param {Function} callback | |
176 | * @param {*} thisArg | |
177 | * @return {*} | |
178 | */ | |
179 | 1 | var forEach = _commonObjectArray.bind(null, "forEach"); |
180 | ||
181 | 1 | exports.forEach = forEach; |
182 | /** | |
183 | * The join() method joins all elements of an object into a string. | |
184 | * | |
185 | * @param {Object} object | |
186 | * @param {String} separator | |
187 | * @return {String} | |
188 | */ | |
189 | 1 | var join = function join(object, separator) { |
190 | 3 | return Object.keys(object).map(function (key) { |
191 | 7 | return object[key]; |
192 | }).join(separator); | |
193 | }; | |
194 | ||
195 | 1 | exports.join = join; |
196 | /** | |
197 | * The map() method creates a new object with the results of calling a provided function on every element in this object. | |
198 | * | |
199 | * @param {Object} object | |
200 | * @param {Function} callback | |
201 | * @param {*=} thisArg | |
202 | * @return {*} | |
203 | */ | |
204 | 1 | var map = function map(object, callback, thisArg) { |
205 | 5 | var mappedObject = {}; |
206 | 5 | exports.forEach(object, function (value, key) { |
207 | 13 | mappedObject[key] = callback.apply(this, arguments); |
208 | }); | |
209 | 5 | return mappedObject; |
210 | }; | |
211 | ||
212 | 1 | exports.map = map; |
213 | /** | |
214 | * The reduce() method applies a function against an accumulator | |
215 | * and each value of the object (from left-to-right) has to reduce it to a single value. | |
216 | * | |
217 | * @param {Object} object | |
218 | * @param {Function} callback | |
219 | * @param {*=} initialValue | |
220 | * @return {*} | |
221 | */ | |
222 | 1 | var reduce = function reduce(object, callback, initialValue) { |
223 | 2 | return Object.keys(object).reduce(function (previousValue, currentValue, index, array) { |
224 | 6 | return callback(previousValue, object[currentValue], currentValue, object); |
225 | }, initialValue); | |
226 | }; | |
227 | ||
228 | 1 | exports.reduce = reduce; |
229 | /** | |
230 | * The reduceRight() method applies a function against an accumulator | |
231 | * and each value of the object (from right-to-left) as to reduce it to a single value. | |
232 | * | |
233 | * @param {Object} object | |
234 | * @param {Function} callback | |
235 | * @param {*=} initialValue | |
236 | * @return {*} | |
237 | */ | |
238 | 1 | var reduceRight = function reduceRight(object, callback, initialValue) { |
239 | 2 | return Object.keys(object).reduceRight(function (previousValue, currentValue, index, array) { |
240 | 6 | return callback(previousValue, object[currentValue], currentValue, object); |
241 | }, initialValue); | |
242 | }; | |
243 | ||
244 | 1 | exports.reduceRight = reduceRight; |
245 | /** | |
246 | * The some() method tests whether some element in the object passes the test implemented by the provided function. | |
247 | * | |
248 | * @function | |
249 | * @param {Object} object | |
250 | * @param {Function} callback | |
251 | * @param {*=} thisArg | |
252 | * @return {Boolean} | |
253 | */ | |
254 | 1 | var some = _commonObjectArray.bind(null, "some"); |
255 | 1 | exports.some = some; |
256 | //# sourceMappingURL=object.js.map |
Line | Hits | Source |
---|---|---|
1 | 1 | "use strict"; |
2 | ||
3 | 1 | Object.defineProperty(exports, "__esModule", { |
4 | value: true | |
5 | }); | |
6 | /* jshint freeze: false */ | |
7 | ||
8 | 1 | var S = require("./index"); |
9 | ||
10 | if (!String.prototype.contains) { | |
11 | 1 | String.prototype.contains = function () { |
12 | 61 | return String.prototype.indexOf.apply(this, arguments) !== -1; |
13 | }; | |
14 | } | |
15 | ||
16 | /** | |
17 | * Php utils | |
18 | * | |
19 | * @module php | |
20 | */ | |
21 | ||
22 | /** | |
23 | * Export string into a php string in string | |
24 | * | |
25 | * @param {String} str | |
26 | * @return {String} | |
27 | */ | |
28 | 1 | var exportString = function exportString(str) { |
29 | if (str.contains("\n") || str.contains("\r") || str.contains("\t") || str.contains("\u000b") || str.contains("\f")) { | |
30 | 2 | return "\"" + str.replace(/\\/g, "\\\\").replace(/\n/g, "\n").replace(/\r/g, "\r").replace(/\t/g, "\t").replace(/\v/g, "\u000b").replace(/\f/g, "\f").replace(/\$/, "$") + "\""; |
31 | } | |
32 | if (!str.contains("'")) { | |
33 | 6 | return "'" + str + "'"; |
34 | } | |
35 | if (!str.contains("\"")) { | |
36 | 2 | return "\"" + str.replace(/\$/g, "$") + "\""; |
37 | } | |
38 | 1 | return "'" + str.replace(/\'/g, "\\'") + "'"; |
39 | }; | |
40 | ||
41 | 1 | exports.exportString = exportString; |
42 | 1 | var _exportCodeVar = function _exportCodeVar(v) { |
43 | if (S.isString(v)) { | |
44 | 8 | return exportString(v); |
45 | } | |
46 | if (v === undefined || v === null) { | |
47 | 1 | return "null"; |
48 | } | |
49 | if (v === true) { | |
50 | 1 | return "true"; |
51 | } | |
52 | if (v === false) { | |
53 | 1 | return "false"; |
54 | } | |
55 | 5 | return v; // numeric |
56 | }; | |
57 | ||
58 | 1 | exports._exportCodeVar = _exportCodeVar; |
59 | 1 | var _exportCode = (function (_exportCode2) { |
60 | 1 | var _exportCodeWrapper = function _exportCode(_x, _x2) { |
61 | 16 | return _exportCode2.apply(this, arguments); |
62 | }; | |
63 | ||
64 | 1 | _exportCodeWrapper.toString = function () { |
65 | 0 | return _exportCode2.toString(); |
66 | }; | |
67 | ||
68 | 1 | return _exportCodeWrapper; |
69 | })(function (v, start) { | |
70 | if (v === null || !S.isObject(v)) { | |
71 | 13 | return _exportCodeVar(v); |
72 | } | |
73 | ||
74 | 3 | var content = "array("; |
75 | if (S.isArray(v)) { | |
76 | for (var i = 0, l = v.length; i < l; i++) { | |
77 | 5 | content += _exportCode(v[i]) + ","; |
78 | } | |
79 | } else { | |
80 | for (var k in v) { | |
81 | 3 | content += _exportCodeVar(k) + "=>" + _exportCode(v[k]) + ","; |
82 | } | |
83 | } | |
84 | if (content) { | |
85 | 3 | content = content.replace(/,+$/, ""); |
86 | } | |
87 | 3 | content += start ? ")" : "),"; |
88 | 3 | return content; |
89 | }); | |
90 | ||
91 | 1 | exports._exportCode = _exportCode; |
92 | /** | |
93 | * Export javascript object into a php code in string | |
94 | * | |
95 | * @param {any} v | |
96 | * @param {bool} ifEmptyArray | |
97 | * @return {String} | |
98 | */ | |
99 | 1 | var exportCode = function exportCode(v, ifEmptyArray) { |
100 | 8 | var content = _exportCode(v, true); |
101 | if (ifEmptyArray !== undefined && (content === "false" || content === "array()")) { | |
102 | 0 | return ifEmptyArray; |
103 | } | |
104 | 8 | return content; |
105 | }; | |
106 | 1 | exports.exportCode = exportCode; |
107 | //# sourceMappingURL=php.js.map |
Line | Hits | Source |
---|---|---|
1 | 1 | "use strict"; |
2 | ||
3 | 1 | Object.defineProperty(exports, "__esModule", { |
4 | value: true | |
5 | }); | |
6 | 1 | var S = require("./index"); |
7 | ||
8 | 1 | var createdPromise; |
9 | ||
10 | /** | |
11 | * @module promises | |
12 | */ | |
13 | ||
14 | /** | |
15 | * Creates a callback that resolve or reject a promise | |
16 | * according to the default callback convention in node: (err, result) | |
17 | * | |
18 | * @param {Function} resolve resolve function of the promise | |
19 | * @param {Function} reject reject function of the promise | |
20 | * @return {Function} | |
21 | */ | |
22 | 1 | var resolveFromCallback = function resolveFromCallback(resolve, reject) { |
23 | 4 | return function (err, result) { |
24 | if (err) { | |
25 | 1 | return reject(err); |
26 | } | |
27 | 3 | resolve(result); |
28 | }; | |
29 | }; | |
30 | ||
31 | 1 | exports.resolveFromCallback = resolveFromCallback; |
32 | /** | |
33 | * Returns a promise | |
34 | * | |
35 | * The first argument is a callback with a done function | |
36 | * | |
37 | * @example | |
38 | * S.promiseCallback((done) => { | |
39 | * fs.readFile('./myFile.txt', done); | |
40 | * }).then((txtContentBuffer) => { | |
41 | * console.log(txtContentBuffer.toString()); | |
42 | * }); | |
43 | * | |
44 | * @param {Function} callback callback((done) => {}) | |
45 | * @return {Promise} | |
46 | */ | |
47 | 1 | var promiseCallback = function promiseCallback(callback) { |
48 | 1 | var resolveCallback, rejectCallback; |
49 | 1 | var createdPromise = new Promise(function (resolve, reject) { |
50 | 1 | resolveCallback = resolve; |
51 | 1 | rejectCallback = reject; |
52 | }); | |
53 | 1 | var doneCallback = resolveFromCallback(resolveCallback, rejectCallback); |
54 | 1 | callback(doneCallback); |
55 | 1 | return createdPromise; |
56 | }; | |
57 | ||
58 | 1 | exports.promiseCallback = promiseCallback; |
59 | /** | |
60 | * Returns an array with two values : the promise and the callback to call | |
61 | * | |
62 | * @deprecated Prefer use of S.promiseCallback() | |
63 | * | |
64 | * @example | |
65 | * var [promise, doneCallback] = promises.creator(); | |
66 | * fs.readFile('./myFile.txt', doneCallback); | |
67 | * promise.then((txtContentBuffer) => { | |
68 | * console.log(txtContentBuffer.toString()); | |
69 | * }); | |
70 | * | |
71 | * @return {Array} | |
72 | */ | |
73 | 1 | var creator = function creator() { |
74 | 1 | var resolveCallback, rejectCallback; |
75 | 1 | var createdPromise = new Promise(function (resolve, reject) { |
76 | 1 | resolveCallback = resolve; |
77 | 1 | rejectCallback = reject; |
78 | }); | |
79 | 1 | var doneCallback = resolveFromCallback(resolveCallback, rejectCallback); |
80 | 1 | return [createdPromise, doneCallback]; |
81 | }; | |
82 | ||
83 | 1 | exports.creator = creator; |
84 | /** | |
85 | * Returns a callback that resolve or reject the created promise that you can get with {promises} | |
86 | * | |
87 | * @deprecated Prefer use of S.promiseCallback() | |
88 | * | |
89 | * @return {Function} callback(err, result) | |
90 | */ | |
91 | 1 | var done = function done() { |
92 | 2 | var resolveCallback, rejectCallback; |
93 | 2 | createdPromise = new Promise(function (resolve, reject) { |
94 | 2 | resolveCallback = resolve; |
95 | 2 | rejectCallback = reject; |
96 | }); | |
97 | 2 | return resolveFromCallback(resolveCallback, rejectCallback); |
98 | }; | |
99 | ||
100 | 1 | exports.done = done; |
101 | /** | |
102 | * Returns the Promise created by the previously called method done() | |
103 | * | |
104 | * @deprecated Prefer use of S.promiseCallback() | |
105 | * | |
106 | * @example | |
107 | * promises.promise(callback(promises.done())); | |
108 | * | |
109 | * @return {Promise} | |
110 | */ | |
111 | 1 | var promise = function promise() { |
112 | if (!createdPromise) { | |
113 | 1 | throw new Error("No promise in stack, done() should be called before"); |
114 | } | |
115 | 2 | var p = createdPromise; |
116 | 2 | createdPromise = undefined; |
117 | 2 | return p; |
118 | }; | |
119 | ||
120 | 1 | exports.promise = promise; |
121 | /** | |
122 | * Execute promises in parallel | |
123 | * | |
124 | * @param {Array|Object|Map|Set} iterable an iterable with .map method (like Array), or an key/value object | |
125 | * @param {Function} callback callback(value, index) called for each values | |
126 | * @return {Promise} | |
127 | */ | |
128 | 1 | var forEach = function forEach(iterable, callback) { |
129 | if (Array.isArray(iterable)) { | |
130 | 8 | return Promise.all(iterable.map(callback)); |
131 | } | |
132 | 1 | var keys = [], |
133 | values = []; | |
134 | 1 | S.forEach(S.map(iterable, callback), function (value, index) { |
135 | 3 | keys.push(index); |
136 | 3 | values.push(value); |
137 | }); | |
138 | ||
139 | 1 | return Promise.all(values).then(function (results) { |
140 | 1 | return S.map(iterable, function (value, index) { |
141 | 3 | return results[keys.indexOf(index)]; |
142 | }); | |
143 | }); | |
144 | }; | |
145 | ||
146 | 1 | exports.forEach = forEach; |
147 | /** | |
148 | * Execute promises in series | |
149 | * | |
150 | * @param {Array|Object} iterable an iterable with .map method (like Array), or an key/value object | |
151 | * @param {Function} callback callback(value, index) called for each values | |
152 | * @return {Promise} | |
153 | */ | |
154 | 1 | var forEachSeries = function forEachSeries(iterable, callback) { |
155 | 2 | return new Promise(function (resolve, reject) { |
156 | 2 | var entriesIterator = S.entries(iterable); |
157 | 2 | var results = new iterable.constructor(); |
158 | 2 | (function next() { |
159 | 6 | var current = entriesIterator.next(); |
160 | if (current.done) { | |
161 | 2 | return resolve(results); |
162 | } | |
163 | 4 | var key = current.value[0], |
164 | value = current.value[1]; | |
165 | 4 | var result = callback(value, key); |
166 | if (result && typeof result.then === "function") { | |
167 | 3 | result.then(function (result) { |
168 | 3 | results[key] = result; |
169 | 3 | next(); |
170 | })["catch"](reject); | |
171 | } else { | |
172 | 1 | results[key] = result; |
173 | 1 | setImmediate(next); |
174 | } | |
175 | })(); | |
176 | }); | |
177 | }; | |
178 | ||
179 | 1 | exports.forEachSeries = forEachSeries; |
180 | /** | |
181 | * Execute the second callback wile the first callback is true | |
182 | * | |
183 | * @param {Function} iterable an iterable with .map method (like Array), or an key/value object | |
184 | * @param {Function} callback callback(value, index) called for each values | |
185 | * @return {Promise} | |
186 | */ | |
187 | 1 | var whileTrue = function whileTrue(conditionCallback, callback) { |
188 | 1 | return new Promise(function (resolve, reject) { |
189 | 1 | (function next() { |
190 | if (!conditionCallback()) { | |
191 | 1 | return resolve(); |
192 | } | |
193 | 3 | var result = callback(); |
194 | if (result && typeof result.then === "function") { | |
195 | 1 | result.then(function () { |
196 | 1 | setImmediate(next); |
197 | })["catch"](reject); | |
198 | } else { | |
199 | 2 | setImmediate(next); |
200 | } | |
201 | })(); | |
202 | }); | |
203 | }; | |
204 | ||
205 | 1 | exports.whileTrue = whileTrue; |
206 | /** | |
207 | * Returns a promise | |
208 | * | |
209 | * The first argument is a callback with a done function | |
210 | * | |
211 | * @example | |
212 | * S.promiseCallback((done) => { | |
213 | * fs.readFile('./myFile.txt', done); | |
214 | * }).then((txtContentBuffer) => { | |
215 | * console.log(txtContentBuffer.toString()); | |
216 | * }); | |
217 | * | |
218 | * @function module:utils.promiseCallback | |
219 | * @param {Function} callback callback((done) => {}) | |
220 | * @return {Promise} | |
221 | */ | |
222 | 1 | S.promiseCallback = promiseCallback; |
223 | //# sourceMappingURL=promises.js.map |
Line | Hits | Source |
---|---|---|
1 | 1 | "use strict"; |
2 | ||
3 | 1 | Object.defineProperty(exports, "__esModule", { |
4 | value: true | |
5 | }); | |
6 | 1 | var crypto = require("crypto"); |
7 | ||
8 | /** | |
9 | * Security | |
10 | * Only for NodeJS | |
11 | * | |
12 | * @module security | |
13 | */ | |
14 | ||
15 | /** | |
16 | * Create an md5 hash | |
17 | * | |
18 | * @param {Buffer|String} data | |
19 | * @return {String} base64 string | |
20 | */ | |
21 | 1 | var md5 = function md5(data) { |
22 | 1 | var shasum = crypto.createHash("md5"); |
23 | 1 | shasum.update(data); |
24 | 1 | return shasum.digest("base64"); |
25 | }; | |
26 | ||
27 | 1 | exports.md5 = md5; |
28 | /** | |
29 | * Create an sha1 hash | |
30 | * | |
31 | * @param {Buffer|String} data | |
32 | * @return {String} base64 string | |
33 | */ | |
34 | 1 | var sha1 = function sha1(data) { |
35 | 1 | var shasum = crypto.createHash("sha1"); |
36 | 1 | shasum.update(data); |
37 | 1 | return shasum.digest("base64"); |
38 | }; | |
39 | ||
40 | 1 | exports.sha1 = sha1; |
41 | /** | |
42 | * Create an sha512 hash | |
43 | * | |
44 | * @param {Buffer|String} data | |
45 | * @return {String} base64 string | |
46 | */ | |
47 | 1 | var sha512 = function sha512(data) { |
48 | 1 | var shasum = crypto.createHash("sha512"); |
49 | 1 | shasum.update(data); |
50 | 1 | return shasum.digest("base64"); |
51 | }; | |
52 | 1 | exports.sha512 = sha512; |
53 | //# sourceMappingURL=security.js.map |
Line | Hits | Source |
---|---|---|
1 | 1 | "use strict"; |
2 | ||
3 | 1 | Object.defineProperty(exports, "__esModule", { |
4 | value: true | |
5 | }); | |
6 | /** | |
7 | * @module string | |
8 | */ | |
9 | 1 | var exports = require("./string"); |
10 | 1 | module.exports = exports; |
11 | ||
12 | /* http://phpjs.org/functions/strip_tags:535 */ | |
13 | /** | |
14 | * Strip html tags from a string, trying to keep newlines consistent | |
15 | * @param {String} s | |
16 | * @param {String} allowed list of allowed tags, separated by space | |
17 | * @return {String} | |
18 | */ | |
19 | 1 | var stripTags = function stripTags(s, allowed) { |
20 | 6 | allowed = (allowed || "").toLowerCase().split(" "); |
21 | 6 | var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi; |
22 | 6 | var commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi; |
23 | /* http://www.tutorialchip.com/tutorials/html5-block-level-elements-complete-list/ */ | |
24 | 6 | var blockTags = ("article aside blockquote br dd div dl dt embed fieldset figcaption figure footer form" + " h1 h2 h3 h4 h5 h6 header hgroup hr li menu nav ol output p pre" + " section table tbody textarea tfoot th thead tr ul").split(" "); |
25 | 6 | return s.replace(commentsAndPhpTags, "").replace(tags, function ($0, tag) { |
26 | 16 | tag = tag.toLowerCase(); |
27 | 16 | return allowed.indexOf(tag) !== -1 ? $0 : blockTags.indexOf(tag) !== -1 ? "\n" : ""; |
28 | }).replace(/\s*\n+\s*\n*/g, "\n").trim(); | |
29 | }; | |
30 | ||
31 | 1 | exports.stripTags = stripTags; |
32 | /** | |
33 | * Transform new lines (\r?\n) to br | |
34 | * | |
35 | * @param {String} string | |
36 | * @return {String} | |
37 | */ | |
38 | 1 | var nl2br = function nl2br(string) { |
39 | 1 | return string.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, "$1<br>$2"); |
40 | }; | |
41 | ||
42 | 1 | exports.nl2br = nl2br; |
43 | /** | |
44 | * Transform <br> to new lines | |
45 | * | |
46 | * @param {String} string | |
47 | * @return {String} | |
48 | */ | |
49 | 1 | var br2nl = function br2nl(string) { |
50 | 1 | return string.replace(/<br\s*\/?>/g, "\n"); |
51 | }; | |
52 | 1 | exports.br2nl = br2nl; |
53 | //# sourceMappingURL=../string/html.js.map |
Line | Hits | Source |
---|---|---|
1 | 1 | "use strict"; |
2 | ||
3 | 1 | Object.defineProperty(exports, "__esModule", { |
4 | value: true | |
5 | }); | |
6 | /** | |
7 | * @module string | |
8 | */ | |
9 | 1 | var utils = require("../index"); |
10 | 1 | var exports = require("./string"); |
11 | 1 | module.exports = exports; |
12 | ||
13 | /** | |
14 | * Transliterate a string | |
15 | * | |
16 | * @param {String} string | |
17 | * @return {String} | |
18 | */ | |
19 | 1 | var translit = function translit(string) { |
20 | 4 | return [[/æ|ǽ/g, "ae"], [/œ/g, "oe"], [/Œ/g, "OE"], [/Ä|À|Á|Â|Ã|Ä|Å|Ǻ|Ā|Ă|Ą|Ǎ/g, "A"], [/ä|à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª/g, "a"], [/Ç|Ć|Ĉ|Ċ|Č/g, "C"], [/ç|ć|ĉ|ċ|č/g, "c"], [/Ð|Ď|Đ/g, "D"], [/ð|ď|đ/g, "d"], [/È|É|Ê|Ë|Ē|Ĕ|Ė|Ę|Ě|€/g, "E"], [/è|é|ê|ë|ē|ĕ|ė|ę|ě/g, "e"], [/Ĝ|Ğ|Ġ|Ģ/g, "G"], [/ĝ|ğ|ġ|ģ/g, "g"], [/Ĥ|Ħ/g, "H"], [/ĥ|ħ/g, "h"], [/Ì|Í|Î|Ï|Ĩ|Ī|Ĭ|Ǐ|Į|İ/g, "I"], [/ì|í|î|ï|ĩ|ī|ĭ|ǐ|į|ı/g, "i"], [/Ĵ/g, "J"], [/ĵ/g, "j"], [/Ķ/g, "K"], [/ķ/g, "k"], [/Ĺ|Ļ|Ľ|Ŀ|Ł/g, "L"], [/ĺ|ļ|ľ|ŀ|ł/g, "l"], [/Ñ|Ń|Ņ|Ň/g, "N"], [/ñ|ń|ņ|ň|ʼn/g, "n"], [/Ö|Ò|Ó|Ô|Õ|Ō|Ŏ|Ǒ|Ő|Ơ|Ø|Ǿ/g, "O"], [/ö|ò|ó|ô|õ|ō|ŏ|ǒ|ő|ơ|ø|ǿ|º|°/g, "o"], [/Ŕ|Ŗ|Ř/g, "R"], [/ŕ|ŗ|ř/g, "r"], [/Ś|Ŝ|Ş|Š/g, "S"], [/ś|ŝ|ş|š|ſ/g, "s"], [/Ţ|Ť|Ŧ/g, "T"], [/ţ|ť|ŧ/g, "t"], [/Ü|Ù|Ú|Û|Ũ|Ū|Ŭ|Ů|Ű|Ų|Ư|Ǔ|Ǖ|Ǘ|Ǚ|Ǜ/g, "U"], [/ü|ù|ú|û|ũ|ū|ŭ|ů|ű|ų|ư|ǔ|ǖ|ǘ|ǚ|ǜ/g, "u"], [/Ý|Ÿ|Ŷ/g, "Y"], [/ý|ÿ|ŷ/g, "y"], [/Ŵ/g, "W"], [/ŵ/g, "w"], [/Ź|Ż|Ž/g, "Z"], [/ź|ż|ž/g, "z"], [/Æ|Ǽ/g, "AE"], [/ß/g, "ss"], [/IJ/g, "IJ"], [/ij/g, "ij"], [/ƒ/g, "f"], [/&/g, "et"], [/þ/g, "th"], [/Þ/g, "TH"]].reduce(function (string, v) { |
21 | 196 | return string.replace(v[0], v[1]); |
22 | }, string); | |
23 | }; | |
24 | ||
25 | 1 | exports.translit = translit; |
26 | /** | |
27 | * Normalize a string | |
28 | * | |
29 | * @param {String} string | |
30 | * @return {String} | |
31 | */ | |
32 | 1 | var normalize = function normalize(string) { |
33 | 1 | return translit(string).replace(/[ \-\'\"\_\(\)\[\]\{\}\#\~\&\*\,\.\;\:\!\?\/\\\\|\`<\>\+]+/g, " ").trim().toLowerCase(); |
34 | }; | |
35 | ||
36 | 1 | exports.normalize = normalize; |
37 | /** | |
38 | * Slugify a string | |
39 | * | |
40 | * @param {String} string | |
41 | * @return {String} | |
42 | */ | |
43 | 1 | var slugify = function slugify(string) { |
44 | 2 | var replacement = arguments[1] === undefined ? "-" : arguments[1]; |
45 | ||
46 | 2 | return translit(string.trim()).replace(/([^\d\.])\.+([^\d\.]|$)/g, "$1 $2").replace(/[^a-zA-Z\d\.]/g, " ") // \w is a-z and _ |
47 | .trim().replace(/\s+/g, replacement).replace(new RegExp("^" + utils.regexpEscape(replacement) + "+|" + utils.regexpEscape(replacement) + "+$"), ""); | |
48 | }; | |
49 | 1 | exports.slugify = slugify; |
50 | //# sourceMappingURL=../string/normalize.js.map |
Line | Hits | Source |
---|---|---|
1 | 1 | "use strict"; |
2 | ||
3 | 1 | Object.defineProperty(exports, "__esModule", { |
4 | value: true | |
5 | }); | |
6 | /** | |
7 | * @module string | |
8 | */ | |
9 | ||
10 | /** | |
11 | * Make a string's first character lowercase | |
12 | * | |
13 | * @param {String} string | |
14 | * @return {String} | |
15 | */ | |
16 | 1 | var lcFirst = function lcFirst(string) { |
17 | 5 | return string.charAt(0).toLowerCase() + string.substr(1); |
18 | }; | |
19 | ||
20 | 1 | exports.lcFirst = lcFirst; |
21 | /** | |
22 | * Make a string's first character uppercase | |
23 | * | |
24 | * @param {String} string | |
25 | * @return {String} | |
26 | */ | |
27 | 1 | var ucFirst = function ucFirst(string) { |
28 | 5 | return string.charAt(0).toUpperCase() + string.substr(1); |
29 | }; | |
30 | ||
31 | 1 | exports.ucFirst = ucFirst; |
32 | /** | |
33 | * Test if a string is empty | |
34 | * | |
35 | * @param {String} string | |
36 | * @return {Boolean} | |
37 | */ | |
38 | 1 | var isEmpty = function isEmpty(string) { |
39 | 4 | return string.trim() === ""; |
40 | }; | |
41 | ||
42 | 1 | exports.isEmpty = isEmpty; |
43 | /** | |
44 | * Format a string using %s | |
45 | * | |
46 | * @param {String} string | |
47 | * @param {...String} args | |
48 | * @return {String} | |
49 | */ | |
50 | 1 | var format = function format(string) { |
51 | 2 | return exports.vformat(string, Array.prototype.slice.call(arguments, 1)); |
52 | }; | |
53 | ||
54 | 1 | exports.format = format; |
55 | /** | |
56 | * Format a string using %s | |
57 | * | |
58 | * @param {String} string | |
59 | * @param {string[]} args | |
60 | * @return {String} | |
61 | */ | |
62 | 1 | var vformat = function vformat(string, args) { |
63 | 4 | var i = 0; |
64 | 4 | return string.replace(/%s/g, function (m) { |
65 | 8 | return args[i++] || ""; |
66 | }); | |
67 | }; | |
68 | 1 | exports.vformat = vformat; |
69 | //# sourceMappingURL=../string/string.js.map |