Files
web/src/common.js

44 lines
1.3 KiB
JavaScript

export function mixin(child, ...parents) {
// child is modified directly, assigning fields from parents that are missing in child. parents fields are
// assigned by parents order, highest priority first
for( const parent of parents ) {
for ( const key in parent) {
if (parent.hasOwnProperty(key) && !child.hasOwnProperty(key) && parent[key] !== undefined) {
child[key] = parent[key];
}
}
}
return child;
}
export function prototype(parent, child) {
const result = Object.create(parent);
Object.assign(result, child)
return result
}
export function invokeCallback(cbs, prop, ...args) {
if (prop in cbs) cbs[prop].call(cbs, ...args)
}
export function invokeCallbacks(callbacks, prop, ...args) {
if (!callbacks) return
callbacks.forEach((cb)=>invokeCallback(cb, prop, ...args))
}
export function encodeIEE754(value) {
const buffer = new ArrayBuffer(4);
const view = new DataView(buffer);
view.setFloat32(0, value, false /* big endian */);
return view.getUint32(0, false);
}
export function decodeIEE754(value) {
const buffer = new ArrayBuffer(4);
const view = new DataView(buffer);
view.setUint32(0, value, false)
return view.getFloat32(0, false)
}