ergogen/src/utils.js

136 lines
3.4 KiB
JavaScript
Raw Normal View History

2020-05-31 20:33:23 +02:00
const m = require('makerjs')
2021-01-01 21:46:01 +01:00
exports.deepcopy = value => {
if (value === undefined) return undefined
return JSON.parse(JSON.stringify(value))
}
2020-05-31 20:33:23 +02:00
2021-01-01 00:50:04 +01:00
const deep = exports.deep = (obj, key, val) => {
2020-12-26 15:17:15 +01:00
const levels = key.split('.')
const last = levels.pop()
let step = obj
for (const level of levels) {
step[level] = step[level] || {}
step = step[level]
}
2021-01-01 00:50:04 +01:00
if (val === undefined) return step[last]
2020-12-26 15:17:15 +01:00
step[last] = val
return obj
}
exports.template = (str, vals={}) => {
const regex = /\{\{([^}]*)\}\}/g
let res = str
let shift = 0
for (const match of str.matchAll(regex)) {
const replacement = deep(vals, match[1]) || ''
res = res.substring(0, match.index + shift)
+ replacement
2022-04-16 12:44:55 +02:00
+ res.substring(match.index + shift + match[0].length)
shift += replacement.length - match[0].length
}
return res
}
2020-06-16 22:24:46 +02:00
const eq = exports.eq = (a=[], b=[]) => {
return a[0] === b[0] && a[1] === b[1]
2020-05-31 22:59:51 +02:00
}
const line = exports.line = (a, b) => {
return new m.paths.Line(a, b)
}
exports.circle = (p, r) => {
2020-07-18 22:58:44 +02:00
return {paths: {circle: new m.paths.Circle(p, r)}}
2020-05-31 22:59:51 +02:00
}
2020-07-10 22:40:34 +02:00
exports.rect = (w, h, o=[0, 0]) => {
2020-06-02 22:59:05 +02:00
const res = {
2020-05-31 22:59:51 +02:00
top: line([0, h], [w, h]),
right: line([w, h], [w, 0]),
bottom: line([w, 0], [0, 0]),
left: line([0, 0], [0, h])
2020-06-02 22:59:05 +02:00
}
return m.model.move({paths: res}, o)
2020-05-31 22:59:51 +02:00
}
exports.poly = (arr) => {
let counter = 0
let prev = arr[arr.length - 1]
const res = {
paths: {}
}
for (const p of arr) {
2020-06-06 23:07:14 +02:00
if (eq(prev, p)) continue
2020-05-31 22:59:51 +02:00
res.paths['p' + (++counter)] = line(prev, p)
prev = p
}
return res
2020-06-02 22:59:05 +02:00
}
2020-07-10 22:40:34 +02:00
2022-01-10 13:44:57 +01:00
exports.bbox = (arr) => {
let minx = Infinity
let miny = Infinity
let maxx = -Infinity
let maxy = -Infinity
for (const p of arr) {
minx = Math.min(minx, p[0])
miny = Math.min(miny, p[1])
maxx = Math.max(maxx, p[0])
maxy = Math.max(maxy, p[1])
}
return {low: [minx, miny], high: [maxx, maxy]}
}
2022-01-11 22:40:09 +01:00
const farPoint = exports.farPoint = [1234.1234, 2143.56789]
2020-07-10 22:40:34 +02:00
2022-01-09 22:56:05 +01:00
exports.union = exports.add = (a, b) => {
2020-07-10 22:40:34 +02:00
return m.model.combine(a, b, false, true, false, true, {
farPoint
})
}
exports.subtract = (a, b) => {
return m.model.combine(a, b, false, true, true, false, {
farPoint
})
}
exports.intersect = (a, b) => {
return m.model.combine(a, b, true, false, true, false, {
farPoint
})
}
exports.stack = (a, b) => {
return {
models: {
a, b
}
}
}
const semver = exports.semver = (str, name='') => {
2022-01-23 22:02:05 +01:00
let main = str.split('-')[0]
if (main.startsWith('v')) {
main = main.substring(1)
}
while (main.split('.').length < 3) {
main += '.0'
}
if (/^\d+\.\d+\.\d+$/.test(main)) {
2022-01-23 22:02:05 +01:00
const parts = main.split('.').map(part => parseInt(part, 10))
return {major: parts[0], minor: parts[1], patch: parts[2]}
} else throw new Error(`Invalid semver "${str}" at ${name}!`)
}
const satisfies = exports.satisfies = (current, expected) => {
if (current.major === undefined) current = semver(current)
if (expected.major === undefined) expected = semver(expected)
return current.major === expected.major && (
current.minor > expected.minor || (
current.minor === expected.minor &&
current.patch >= expected.patch
)
)
2020-07-10 22:40:34 +02:00
}