UNPKG

@betaflight/api

Version:

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

66 lines 2.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sleep = exports.partialWriteFunc = exports.mergeDeep = exports.includeIf = exports.toIdentifier = exports.fromIdentifier = exports.packValues = exports.unpackValues = exports.times = exports.isTupleOf = exports.bitCheck = void 0; const bitCheck = (num, bit) => (num >> bit) % 2 !== 0; exports.bitCheck = bitCheck; const isTupleOf = (list, length) => list.length === length; exports.isTupleOf = isTupleOf; const times = (f, number) => new Array(number).fill(0).map((_, i) => f(i)); exports.times = times; const unpackValues = (mask, schema, { inverted = false } = {}) => schema.filter((_, i) => ((mask >> i) % 2 !== 0) !== inverted); exports.unpackValues = unpackValues; 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); exports.packValues = packValues; const fromIdentifier = (schema, value) => { const index = schema.indexOf(value); return index === -1 ? undefined : index; }; exports.fromIdentifier = fromIdentifier; const toIdentifier = (schema, value) => { var _a; return (_a = schema[value]) !== null && _a !== void 0 ? _a : value; }; exports.toIdentifier = toIdentifier; const includeIf = (statement, value) => { if (statement) { if (Array.isArray(value)) { return value; } return [value]; } return []; }; exports.includeIf = includeIf; 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 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] = (0, exports.mergeDeep)(target[key], sourceNoNull[key]); } } else { Object.assign(output, { [key]: sourceNoNull[key], }); } }); return output; } return target; }; exports.mergeDeep = mergeDeep; const partialWriteFunc = (readFunc, writeFunc) => async (port, config) => writeFunc(port, (0, exports.mergeDeep)(await readFunc(port), config)); exports.partialWriteFunc = partialWriteFunc; const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); exports.sleep = sleep; //# sourceMappingURL=utils.js.map