forked from evanx/redexutil
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathArrays.js
46 lines (38 loc) · 1001 Bytes
/
Arrays.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
// Copyright (c) 2015, Evan Summers (twitter.com/evanxsummers)
// ISC license, see http://github.com/evanx/redexutil/LICENSE
const logger = Loggers.create(__filename, 'info');
export function length(array, defaultValue) {
if (!array) {
return defaultValue;
}
return array.length;
}
export function reverse(array) {
return lodash(array).slice(0).reverse().value();
}
export function pushIf(array, value, condition) {
if (condition || value) {
array.push(value);
}
}
export async function mapAsync(array, fn) {
return Promise.all(array.map(async (item) => {
try {
let result = await fn();
logger.debug('mapAsync', result);
return result;
} catch (err) {
logger.debug('mapAsync', err);
throw err;
}
}));
}
export function joinColon(array) {
return array.join(':');
}
export function joinDash(array) {
return array.join('-');
}
export function joinSpace(array) {
return array.join(' ');
}