ergogen/src/assert.js

70 lines
2.3 KiB
JavaScript
Raw Normal View History

2020-07-10 22:40:34 +02:00
const m = require('makerjs')
const u = require('./utils')
2020-06-27 20:13:06 +02:00
const Point = require('./point')
2021-01-01 00:50:04 +01:00
const mathjs = require('mathjs')
const mathnum = exports.mathnum = raw => units => {
return mathjs.evaluate(`${raw}`, units || {})
}
2020-06-27 20:13:06 +02:00
const assert = exports.assert = (exp, msg) => {
if (!exp) {
throw new Error(msg)
}
}
2021-01-01 00:50:04 +01:00
const type = exports.type = val => units => {
2020-06-27 20:13:06 +02:00
if (Array.isArray(val)) return 'array'
if (val === null) return 'null'
2021-01-01 00:50:04 +01:00
try {
const num = mathnum(val)(units)
if (typeof num === 'number') return 'number'
} catch (err) {}
2020-06-27 20:13:06 +02:00
return typeof val
}
2021-01-01 00:50:04 +01:00
const sane = exports.sane = (val, name, _type) => units => {
assert(type(val)(units) == _type, `Field "${name}" should be of type ${_type}!`)
if (_type == 'number') return mathnum(val)(units)
2020-06-27 20:13:06 +02:00
return val
}
2021-05-22 19:06:57 +02:00
const unexpected = exports.unexpected = (obj, name, expected) => {
2021-01-01 00:50:04 +01:00
const sane_obj = sane(obj, name, 'object')()
2020-06-27 20:13:06 +02:00
for (const key of Object.keys(sane_obj)) {
assert(expected.includes(key), `Unexpected key "${key}" within field "${name}"!`)
}
}
2020-09-06 17:15:46 +02:00
const _in = exports.in = (raw, name, arr) => {
2020-06-30 22:29:14 +02:00
assert(arr.includes(raw), `Field "${name}" should be one of [${arr.join(', ')}]!`)
return raw
}
2021-01-01 00:50:04 +01:00
const arr = exports.arr = (raw, name, length, _type, _default) => units => {
assert(type(raw)(units) == 'array', `Field "${name}" should be an array!`)
2020-09-06 17:15:46 +02:00
assert(length == 0 || raw.length == length, `Field "${name}" should be an array of length ${length}!`)
raw = raw.map(val => val || _default)
2021-01-01 00:50:04 +01:00
raw.map(val => assert(type(val)(units) == _type, `Field "${name}" should contain ${_type}s!`))
if (_type == 'number') {
raw = raw.map(val => mathnum(val)(units))
}
2020-06-28 22:35:53 +02:00
return raw
2020-06-27 20:13:06 +02:00
}
2021-01-01 00:50:04 +01:00
const numarr = exports.numarr = (raw, name, length) => units => arr(raw, name, length, 'number', 0)(units)
const strarr = exports.strarr = (raw, name) => arr(raw, name, 0, 'string', '')()
2020-09-06 17:15:46 +02:00
2021-01-01 00:50:04 +01:00
const xy = exports.xy = (raw, name) => units => numarr(raw, name, 2)(units)
2020-06-28 22:35:53 +02:00
2021-01-01 00:50:04 +01:00
const wh = exports.wh = (raw, name) => units => {
2020-06-28 22:35:53 +02:00
if (!Array.isArray(raw)) raw = [raw, raw]
2021-01-01 00:50:04 +01:00
return xy(raw, name)(units)
2020-06-28 22:35:53 +02:00
}
2021-01-01 00:50:04 +01:00
exports.trbl = (raw, name) => units => {
2020-06-28 22:35:53 +02:00
if (!Array.isArray(raw)) raw = [raw, raw, raw, raw]
if (raw.length == 2) raw = [raw[1], raw[0], raw[1], raw[0]]
2021-01-01 00:50:04 +01:00
return numarr(raw, name, 4, 'number', 0)(units)
}