-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
49 lines (41 loc) · 1.03 KB
/
utils.js
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
'use strict';
function randomString(n) {
let result = '';
let alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
let i = 0;
while (i < n) {
let index = Math.floor(Math.random() * alphabet.length);
result += alphabet[index];
i++;
}
return result;
}
function render(template, placeholders) {
let result = template;
for (let k in placeholders) {
let regex = new RegExp('\\${' + k + '}', 'g');
result = result.replace(regex, placeholders[k]);
}
return result;
}
function query(collection, condition) {
let match = function (target, condition) {
for (let key in condition) {
if (target[key] != condition[key]) {
return false;
}
}
return true;
};
for (let i in collection) {
if (match(collection[i], condition)) {
return collection[i];
}
}
return null;
}
module.exports = {
randomString: randomString,
render: render,
query: query
};