swarms
Version:
The ultimate node.js library for controlling Bitcraze Crazyflie 2.0 drones
72 lines • 2.01 kB
JavaScript
;
/**
* @file Random stuff used throughout the project
*/
Object.defineProperty(exports, "__esModule", { value: true });
const _ = require("lodash");
/**
* Convert number to hex
*/
function toHex(decimal, evenLength = false, xPrefix = false) {
let hex = decimal.toString(16);
if (evenLength && hex.length % 2 !== 0) {
hex = '0' + hex;
}
if (xPrefix) {
hex = '0x' + hex;
}
return hex;
}
exports.toHex = toHex;
/**
* Convert number to binary
*/
function toBinary(decimal, lengthMultipleOfFour = false, gapEveryByte = false, minLength = 0) {
let binary = _.padStart(decimal.toString(2), minLength, '0');
if (lengthMultipleOfFour && binary.length % 4 !== 0) {
binary = _.padStart(binary, Math.ceil(binary.length / 4) * 4, '0');
}
if (gapEveryByte) {
binary = binary.match(/.{1,4}/g).join(' ');
}
return binary;
}
exports.toBinary = toBinary;
/**
* Rounds a number to a certain decimal place.
* Precision 0 by default.
*/
function round(num, precision = 0) {
return _.round(num, precision);
}
exports.round = round;
/**
* Returns a promise that resolves in n milliseconds
*/
function wait(milliseconds) {
return new Promise((resolve, reject) => {
setTimeout(resolve, milliseconds);
});
}
exports.wait = wait;
/**
* Wait until event is emitted
* @TODO Add timeout as well if event is never emitted
*/
function waitUntilEvent(emitter, eventName) {
return () => new Promise((resolve, reject) => {
emitter.once(eventName, (data) => {
resolve(data);
});
});
}
exports.waitUntilEvent = waitUntilEvent;
/**
* Takes a TypeScript enum and returns the actual list of keys.
* Since enums have reverse lookup keys as well, this has to be done when checking for the _actual_ keys.
*/
function properEnumKeys(enumObj) {
return Object.values(enumObj).filter(v => typeof v === 'string');
}
exports.properEnumKeys = properEnumKeys;
//# sourceMappingURL=utils.js.map