-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnativize.js
32 lines (24 loc) · 872 Bytes
/
nativize.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
/*
Given the serialization of some value, convert it to a form the app
expects, as declared in the template's slot declaration.
This might grow to include range checking.
In general, it can produce Validation Errors (TypeErrors for now),
which are different from a template simply not matching.
*/
module.exports = (type, v) => {
if (type === 'string') return v
if (type === 'number' || type === 'integer') {
v = JSON.parse(v)
if (typeof v !== 'number') throw new TypeError('expected number')
if (type === 'integer') {
if (v !== Math.round(v)) throw new TypeError('expected integer')
}
return v
}
if (type === 'boolean') {
if (v.match(/true/i)) return true
if (v.match(/false/i)) return false
throw new TypeError('expected boolean')
}
throw new TypeError('unimplemented type: ' + JSON.stringify(type))
}