-
Notifications
You must be signed in to change notification settings - Fork 79
/
job-utils.js
129 lines (110 loc) · 3.5 KB
/
job-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
const humanInterval = require('human-interval');
const isSANB = require('is-string-and-not-blank');
const later = require('@breejs/later');
const ms = require('ms');
/**
* Naively checks if passed value is of later.js schedule format (https://breejs.github.io/later/schedules.html)
*
* @param {*} value to check for schedule format
* @returns {boolean}
*/
const isSchedule = (value) => {
return typeof value === 'object' && Array.isArray(value.schedules);
};
/**
* Extracts job name from job definition
*
* @param {string | Object | Function} job definition
* @returns {string}
*/
const getName = (job) => {
if (isSANB(job)) return job;
if (typeof job === 'object' && isSANB(job.name)) return job.name;
if (typeof job === 'function' && isSANB(job.name)) return job.name;
};
/**
* Parses provided value into millisecond
*
* @param {string} _value
*/
const getHumanToMs = (_value) => {
const value = humanInterval(_value);
if (Number.isNaN(value)) return ms(_value);
return value;
};
/**
* Parses schedule value into "later" schedule object or milliseconds
*
* @param {boolean | string | number | Object} value
* @returns {number | boolean | Object}
*/
const parseValue = (value) => {
const originalValue = value;
if (value === false) return value;
if (isSchedule(value)) return value;
if (isSANB(value)) {
const schedule = later.schedule(later.parse.text(value));
if (schedule.isValid()) return later.parse.text(value);
value = getHumanToMs(value);
if (value === 0) {
// There is a bug in the human-interval library that causes some invalid
// strings to be parsed as valid, returning 0 as output (instead of NaN).
// Since the user is using a String to define the interval, it is most
// likely that he/she is not trying to set it to 0ms.
// Hence, this must be an error.
throw new Error(
`Value "${originalValue}" is not a String parseable by \`later.parse.text\` (see <https://breejs.github.io/later/parsers.html#text> for examples)`
);
}
}
if (!Number.isFinite(value) || value < 0)
throw new Error(
`Value "${originalValue}" must be a finite number >= 0 or a String parseable by \`later.parse.text\` (see <https://breejs.github.io/later/parsers.html#text> for examples)`
);
return value;
};
/**
* Processes job objects extracting their names
* Can conditionaly skip records by their index
*
* @param {any[]} jobs
* @param {number} excludeIndex
* @returns {string[]} job names
*/
const getJobNames = (jobs, excludeIndex) => {
const names = [];
for (const [i, job] of jobs.entries()) {
if (i === excludeIndex) continue;
const name = getName(job);
if (name) names.push(name);
}
return names;
};
/**
* Processes job name to generate a partial path for the job
* Allows for resiliancy when the path extensions are either
* provided or not on both default and accepted extensions
*
* @param {string} name
* @param {number} acceptedExtensions
* @param {string} defaultExtension
* @returns {string} job path
*/
const getJobPath = (name, acceptedExtensions, defaultExtension) => {
const extFindArray = acceptedExtensions.map((ext) => {
return ext.startsWith('.') ? ext : `.${ext}`;
});
const hasExt = extFindArray.find((ext) => name.endsWith(ext));
if (hasExt) return name;
return defaultExtension.startsWith('.')
? `${name}${defaultExtension}`
: `${name}.${defaultExtension}`;
};
module.exports = {
getHumanToMs,
getJobNames,
getJobPath,
getName,
isSchedule,
parseValue
};