-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.js
149 lines (127 loc) · 2.71 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
'use strict';
const typeOf = require('kind-of');
exports.isEmpty = val => {
if (typeof val === 'function') {
return false;
}
if (exports.isObject(val)) {
val = Object.keys(val);
}
if (Array.isArray(val)) {
return val.length === 0;
}
if (typeof val === 'undefined') {
return true;
}
if (val === 0) {
return false;
}
if (val == null) {
return true;
}
};
exports.omit = (obj, keys = []) => {
let res = {};
for (let key of Object.keys(obj)) {
if (!keys.includes(key)) {
res[key] = obj[key];
}
}
return res;
};
/**
* Get the "title" from a markdown link
*/
exports.getTitle = str => {
if (/^\[[^\]]+\]\(/.test(str)) {
let m = /^\[([^\]]+)\]/.exec(str);
if (m) return m[1];
}
return str;
};
exports.isString = str => str !== '' && typeof str === 'string';
exports.isNumber = num => {
if (typeof num === 'number') {
return num - num === 0;
}
if (typeof num === 'string' && num.trim() !== '') {
return Number.isFinite ? Number.isFinite(+num) : isFinite(+num);
}
return false;
};
exports.isObject = val => typeOf(val) === 'object';
exports.isPlainObject = val => {
return exports.isObject(val) && val.constructor === Object;
};
/**
* Returns true if the given `val` is an object.
*
* ```js
* utils.isObject('foo');
* //=> false
*
* utils.isObject({});
* //=> true
* ```
* @param {any} `val`
* @return {Boolean}
* @api public
*/
exports.isObject = val => {
return typeOf(val) === 'object';
};
/**
* Stringify HTML tag attributes from the given `object`.
*
* @param {Object} `object` Object of attributes as key-value pairs.
* @return {String} Stringified attributes, e.g. `foo="bar"`
* @api public
*/
exports.toAttributes = obj => {
return Object.keys(obj).map(key => `${key}="${obj[key]}"`).join(' ');
};
exports.toCodeBlock = (str, lang) => {
if (typeof str !== 'string') {
throw new TypeError('expected a string.');
}
let code = '';
code += '```' + (typeof lang === 'string' ? lang : '');
code += '\n';
code += str;
code += '\n';
code += '```';
return code;
};
/**
* Generate a random number
*
* @param {Number} `min`
* @param {Number} `max`
* @return {Number}
*/
exports.random = (min, max) => {
return min + Math.floor(Math.random() * (max - min + 1));
};
exports.flatten = arr => {
const flat = (a, res) => {
let len = a.length;
let i = -1;
while (len--) {
let cur = a[++i];
if (Array.isArray(cur)) {
flat(cur, res);
} else {
res.push(cur);
}
}
return res;
};
return flat(arr, []);
};
exports.union = (...args) => {
return [...new Set(exports.flatten(args))];
};
/**
* Expose `exports` modules
*/
module.exports = exports;