-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
145 lines (133 loc) · 2.96 KB
/
Copy pathutil.js
File metadata and controls
145 lines (133 loc) · 2.96 KB
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
'use strict';
const fs = require('fs');
function nonEmptyCb(s) {
return (s.length > 0) ? s : undefined;
}
function integerCb(s) {
var r;
if (s.match(/^(0|-?[1-9][0-9]*)$/)) {
if (Number.isSafeInteger(r = Number.parseInt(s))) {
return r;
}
}
return undefined;
}
function integerListCb(s) {
var r = [];
if (s.replace(/^\s*/, '').replace(/\s$/, '').replace(/\s*,\s*/g, ',').split(',').some(function(n) {
var x;
if (n.match(/^(0|-?[1-9][0-9]*)$/)) {
if (Number.isSafeInteger(x = Number.parseInt(n))) {
r.push(x);
return false;
}
}
return true;
})) {
return undefined;
}
return (r.length > 0) ? r : undefined;
}
function ipv4(s) {
var m, i = integerWithLimitsCbFactory(0, 255), a, b, c, d;
if (m = s.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)) {
a = i(m[1]);
b = i(m[2]);
c = i(m[3]);
d = i(m[4]);
if ((a !== undefined) && (b !== undefined) && (c !== undefined) && (d !== undefined)) {
return a.toString() + '.' + b.toString() + '.' + c.toString() + '.' + d.toString();
}
}
return undefined;
}
function integerWithLimitsCbFactory(min, max) {
return function(s) {
var r = integerCb(s);
if (r === undefined) {
return undefined;
}
if ((min !== undefined) && (r < min)) {
return undefined;
}
if ((max !== undefined) && (r > max)) {
return undefined;
}
return r;
};
}
function allowListCbFactory(allowedValues) {
if (! (Array.isArray(allowedValues))) {
allowedValues = [ allowedValues ];
}
allowedValues = allowedValues.filter(function(x) { return ((typeof(x) === 'string') || (x instanceof RegExp)); });
return function(v) {
if (typeof(v) !== 'string') {
return undefined;
}
return ((allowedValues.some(function(x) {
if (x instanceof RegExp) {
return (v.match(x)) ? true : false;
}
return (x === v);
})) ? v : undefined);
}
}
function existingFileNameCb(s) {
try {
if (! fs.statSync(s).isFile()) {
throw new Error('Invalid file');
}
} catch (e) {
s = undefined;
}
return s;
}
function fileContentsStringCb(s) {
try {
if (! fs.statSync(s).isFile()) {
throw new Error('Invalid file');
}
s = fs.readFileSync(s, { encoding: 'utf8', flag: 'r' } );
if (! s) {
throw new Error('File read error');
}
} catch (e) {
s = undefined;
}
return s;
}
function jsonObject(s) {
try {
s = JSON.parse(s);
if (! (s && (typeof(s) === 'object'))) {
throw new Error('JSON value is not an object');
}
} catch (e) {
s = undefined;
}
return s;
}
function jsonArray(s) {
try {
s = JSON.parse(s);
if (! Array.isArray(s)) {
throw new Error('JSON value is not an array');
}
} catch (e) {
s = undefined;
}
return s;
}
module.exports = {
nonEmptyCb: nonEmptyCb,
integerCb: integerCb,
integerListCb: integerListCb,
integerWithLimitsCbFactory: integerWithLimitsCbFactory,
allowListCbFactory: allowListCbFactory,
existingFileNameCb: existingFileNameCb,
fileContentsStringCb: fileContentsStringCb,
ipv4: ipv4,
jsonObject: jsonObject,
jsonArray: jsonArray
};