-
Notifications
You must be signed in to change notification settings - Fork 1
/
zero.ts
52 lines (51 loc) · 1.35 KB
/
zero.ts
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
import { MusicXMLError } from '../../MusicXMLError';
import { DescriptorChild } from '../schema';
import * as util from '../util';
export const zero = <T extends DescriptorChild>(child: T): any => {
if (util.isString(child)) {
return child;
}
if (util.isNumber(child)) {
return child;
}
if (util.isDescriptor(child)) {
switch (child.type) {
case 'string':
return '';
case 'regex':
return child.zero;
case 'int':
return Number.isFinite(child.min) ? child.min : 0;
case 'float':
return Number.isFinite(child.min) ? child.min : 0;
case 'constant':
return child.value;
case 'date':
return new Date(1970, 0, 1, 0, 0, 0, 0);
case 'choices':
return zero(child.choices[0]);
case 'optional':
return null;
case 'label':
return zero(child.value);
case 'required':
return zero(child.value);
case 'zeroOrMore':
return [];
case 'oneOrMore':
return [zero(child.value)];
case 'not':
return zero(child.include);
}
}
if (util.isXMLElementCtor(child)) {
return new child();
}
if (util.isFunction(child)) {
return zero(child());
}
if (util.isArray(child)) {
return child.map((c) => zero(c));
}
throw new MusicXMLError('cannot compute zero value', { child });
};