UNPKG

@betaflight/api

Version:

A high-level API to read data from betaflight flight controllers

52 lines 2.35 kB
export const bitCheck = (num, bit) => (num >> bit) % 2 !== 0; export const isTupleOf = (list, length) => list.length === length; export const times = (f, number) => new Array(number).fill(0).map((_, i) => f(i)); export const unpackValues = (mask, schema, { inverted = false } = {}) => schema.filter((_, i) => ((mask >> i) % 2 !== 0) !== inverted); export const packValues = (values, schema, { inverted = false } = {}) => (inverted ? schema.filter((bit) => !values.includes(bit)) : values.filter((val) => schema.includes(val))).reduce((acc, val) => acc | (1 << schema.indexOf(val)), 0); export const fromIdentifier = (schema, value) => { const index = schema.indexOf(value); return index === -1 ? undefined : index; }; export const toIdentifier = (schema, value) => { var _a; return (_a = schema[value]) !== null && _a !== void 0 ? _a : value; }; export const includeIf = (statement, value) => { if (statement) { if (Array.isArray(value)) { return value; } return [value]; } return []; }; const filterUnset = (object) => Object.fromEntries(Object.entries(object).filter(([, value]) => value !== undefined && value !== null)); const isObject = (item) => !!item && typeof item === "object" && !Array.isArray(item); // this is dirt, oh well export const mergeDeep = (target, source) => { if (isObject(target) && isObject(source)) { const output = { ...target }; const sourceNoNull = filterUnset(source); Object.keys(sourceNoNull).forEach((key) => { if (isObject(sourceNoNull[key])) { if (!(key in target)) { Object.assign(output, { [key]: sourceNoNull[key], }); } else { output[key] = mergeDeep(target[key], sourceNoNull[key]); } } else { Object.assign(output, { [key]: sourceNoNull[key], }); } }); return output; } return target; }; export const partialWriteFunc = (readFunc, writeFunc) => async (port, config) => writeFunc(port, mergeDeep(await readFunc(port), config)); export const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); //# sourceMappingURL=utils.js.map