@uboness/homebridge-unifi-access
Version:
Homebridge Unifi Access Plugin
62 lines • 2.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Detachables = exports.spliceFirstMatch = exports.cleanArrayAsync = exports.isNumber = exports.isBoolean = exports.isString = exports.isUndefined = exports.isNil = void 0;
const isNil = (value) => value === null || value === undefined;
exports.isNil = isNil;
const isUndefined = (value) => value === undefined;
exports.isUndefined = isUndefined;
const isString = (value) => !(0, exports.isNil)(value) && typeof value === 'string';
exports.isString = isString;
const isBoolean = (value) => !(0, exports.isNil)(value) && typeof value === 'boolean';
exports.isBoolean = isBoolean;
const isNumber = (value) => !(0, exports.isNil)(value) && typeof value === 'number';
exports.isNumber = isNumber;
const cleanArrayAsync = async (array, cb) => {
while (array.length > 0) {
const value = array.pop();
if (value) {
await cb(value);
}
}
};
exports.cleanArrayAsync = cleanArrayAsync;
const spliceFirstMatch = (array, predicate) => {
const index = array.findIndex(predicate);
if (index < 0) {
return;
}
const removed = array[index];
array.splice(index, 1);
return removed;
};
exports.spliceFirstMatch = spliceFirstMatch;
class Detachables {
constructor(detachables = []) {
this.detachables = detachables;
}
add(detachable) {
const _detachable = {
detach: () => {
detachable.detach();
(0, exports.spliceFirstMatch)(this.detachables, d => d === _detachable);
}
};
this.detachables.push(_detachable);
return _detachable;
}
remove(detachable) {
(0, exports.spliceFirstMatch)(this.detachables, d => d === detachable);
}
detach() {
var _a;
while (this.detachables.length > 0) {
try {
(_a = this.detachables.pop()) === null || _a === void 0 ? void 0 : _a.detach();
}
catch (e) {
}
}
}
}
exports.Detachables = Detachables;
//# sourceMappingURL=common.js.map