-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathType.js
More file actions
278 lines (230 loc) · 6.69 KB
/
Type.js
File metadata and controls
278 lines (230 loc) · 6.69 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"use strict";
const File = require('phylo');
const path = require('path');
const semver = require('semver');
/**
* @class Type
* This class manages type conversions for a single data type. Instances of this class
* are created by `Type.define`.
*/
class Type {
/**
* Adds a type definition to the registry.
* @param {Type} def The `Type` instance or a config object for one.
*/
static define (def) {
// Type instances are stored on the defs object (for easy iteration) and directly
// on the Type constructor (for easy use). Check constructor to prevent smashing
// one of its static methods.
if (Type[def.name]) {
throw new Error(`Type already defined: "${def.name}"`);
}
if (!def.isType) {
def = new Type(def);
}
Type.defs[def.name] = Type[def.name] = def;
}
static get (name) {
return Type.defs[name] || null;
}
/**
* Returns the `Type` instance appropriate to the given `value`.
* @param {*} value The value to determine.
* @return {Type} The `Type` of the `value`.
*/
static of (value) {
let def = Type.pick(value, def => def.is(value));
if (!def) {
// If the value is not a direct type match, see who can parse it
def = Type.pick(value, def => def.convert(value) !== null);
}
return def;
}
/**
* Picks and returns the uniquely matching `Type` that matches the provided `test`
* function.
* @param {*} [value] A value to describe what is being sought. This is only used for
* the `Error` thrown if multiple matches are found. If this value is `null` or not
* provided, the array of matching names is returned.
* @param {Function} test
* @return {Type/String[]} The uniquely matching `Type` or `null` for no matches. If
* no `value` is provided and multiple matches are found, the array of matching names
* is returned.
*/
static pick (value, test) {
let defs = Type.defs;
let found = null;
let ambiguous;
if (!test) {
test = value;
value = null;
}
for (let s in defs) {
let def = defs[s];
if (test(def, value)) {
if (found) {
(ambiguous || (ambiguous = [found.name])).push(s);
}
found = def;
}
}
if (ambiguous) {
if (value === null) {
return ambiguous;
}
throw new Error(`Ambiguous type for "${value}"; could be: ${ambiguous.join(', ')}`);
}
return found;
}
constructor (config) {
Object.assign(this, config);
}
/**
* Converts the given value to this type of data. If the value cannot be converted,
* `null` is returned.
* @param {*} value The value to convert.
* @return {*} The converted value or `null`.
*/
convert (value) {
return value;
}
/**
* Returns `true` if the `value` is already of this type.
* @param {*} value The value to test.
* @return {Boolean} `true` if the type of `value` is this type, `false` if not.
*/
is (value) {
return this.name === typeof value;
}
}
Type.defs = {};
Type.prototype.isType = true;
class BooleanType extends Type {
constructor () {
super({
anyRe: /^(?:true|false|yes|no|on|off)$/i,
trueRe: /^(?:true|yes|on)$/i,
falseRe: /^(?:false|no|off)$/i,
default: false,
name: 'boolean',
help: 'One of true|false|yes|no|on|off'
});
}
convert (value) {
var r = value;
if (typeof value !== 'boolean') {
r = String(r);
if (this.trueRe.test(r)) {
r = true;
} else if (this.falseRe.test(r)) {
r = false;
} else {
r = null;
}
}
return r;
}
}
class NumberType extends Type {
constructor () {
super({
default: 0,
name: 'number',
re: /^[-+]?[0-9]*\.?[0-9]+(?:e[-+]?[0-9]+)?$/i,
help: 'A numerical value'
});
}
convert (value) {
let me = this;
if (Array.isArray(value)) {
return value.map((i) => me.convert(i));
}
var r = +value; // good start...
if (typeof value !== 'number') {
if (value === false) {
return 0;
}
// parseFloat accepts numbers followed by non-sense...
// Beware of operator + and isNaN...
//
// +'' === 0 (ok...)
// +null === 0 (oh yeah)
// isNaN(null) === false (srsly!)
// isNaN('') === false (now you're just being mean!)
// +' \t ' === 0 (out of here!)
let s = String(value);
if (!this.re.test(s)) {
r = null;
}
}
return r;
}
}
class PathType extends Type {
constructor () {
super({
default: '',
name: 'path',
help: 'A path relative to the location the script is executed at'
});
}
convert (value) {
if (Array.isArray(value)) {
return value.map(value => this.convert(value));
}
value = this.parse(value);
return File.from(value);
}
parse (value) {
if (value == null || value === false || typeof value === 'function') {
return null;
}
return String(value);
}
is () {
return false;
}
}
class StringType extends Type {
constructor () {
super({
default: '',
name: 'string'
});
}
convert (value) {
if (value == null || value === false) {
return null;
}
let me = this;
if (Array.isArray(value)) {
return value.map((i) => me.convert(i));
}
return String(value);
}
}
class SemverType extends Type {
constructor () {
super({
default: '1.0.0',
name: 'semver',
help: 'A SemVer version, parsed by node-semver'
});
}
convert (value) {
let me = this;
if (Array.isArray(value)) {
return value.map((i) => me.convert(i));
}
return semver.parse(value||'');
}
is (value) {
return semver.parse(value) !== null;
}
}
Type.define(new BooleanType());
Type.define(new NumberType());
Type.define(new PathType());
Type.define(new StringType());
Type.define(new SemverType());
module.exports = Type;