mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-11 02:13:48 +00:00
Refactor/socket controller (#212)
* refactor(socket): dictionary cleanup * refactor(socket): detangle socket from EventTimer * refactor(socket): poll object * refactor(socket): wip, extract data responsibilities to provider * fix: issue with onAir * refactor: create data provider and validation utils * refactor: remove deprecated endpoint * refactor: detangle and validate ontime controller * refactor: detangle and validate events controller * refactor: validate routers * refactor: handle post failure in modals
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
import { isEmptyObject, mergeObject, removeUndefined } from '../parserUtils.js';
|
||||
|
||||
describe('isEmptyObject()', () => {
|
||||
test('finds an empty object', () => {
|
||||
const isEmpty = isEmptyObject({});
|
||||
expect(isEmpty).toBe(true);
|
||||
});
|
||||
test('throws on other types', () => {
|
||||
expect(() => isEmptyObject(12)).toThrow();
|
||||
});
|
||||
test('resolves an object with methods', () => {
|
||||
const isEmpty = isEmptyObject({ test: 'yes' });
|
||||
expect(isEmpty).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('mergeObject()', () => {
|
||||
test('it suppresses undefined keys', () => {
|
||||
const a = {
|
||||
first: 'yes',
|
||||
second: 'yes',
|
||||
};
|
||||
const b = {
|
||||
first: undefined,
|
||||
second: 'no',
|
||||
};
|
||||
const merged = mergeObject(a, b);
|
||||
expect(merged).toStrictEqual({
|
||||
first: 'yes',
|
||||
second: 'no',
|
||||
});
|
||||
});
|
||||
test('it handles falsy values', () => {
|
||||
const a = {
|
||||
first: 'yes',
|
||||
second: 'yes',
|
||||
third: 'yes',
|
||||
};
|
||||
const b = {
|
||||
first: 0,
|
||||
second: null,
|
||||
third: '',
|
||||
};
|
||||
const merged = mergeObject(a, b);
|
||||
expect(merged).toStrictEqual({
|
||||
first: 0,
|
||||
second: null,
|
||||
third: '',
|
||||
});
|
||||
});
|
||||
test.skip('it only merges fields of the first object', () => {
|
||||
const a = {
|
||||
first: 'yes',
|
||||
second: 'yes',
|
||||
third: 'yes',
|
||||
};
|
||||
const b = {
|
||||
first: 0,
|
||||
second: null,
|
||||
third: '',
|
||||
forth: 'not-this',
|
||||
};
|
||||
const merged = mergeObject(a, b);
|
||||
expect(merged).toStrictEqual({
|
||||
first: 0,
|
||||
second: null,
|
||||
third: '',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('removeUndefined()', () => {
|
||||
test('it removes undefined keys from object', () => {
|
||||
const obj = {
|
||||
first: 'yes',
|
||||
second: undefined,
|
||||
third: 'yes',
|
||||
};
|
||||
expect(removeUndefined(obj)).toStrictEqual({
|
||||
first: 'yes',
|
||||
third: 'yes',
|
||||
});
|
||||
});
|
||||
test('it handles falsy values object', () => {
|
||||
const obj = {
|
||||
first: '',
|
||||
second: 0,
|
||||
third: 'null',
|
||||
};
|
||||
expect(removeUndefined(obj)).toStrictEqual(obj);
|
||||
});
|
||||
});
|
||||
@@ -1,19 +1,13 @@
|
||||
const adjective = [
|
||||
'abandoned',
|
||||
'able',
|
||||
'absolute',
|
||||
'adorable',
|
||||
'adventurous',
|
||||
'academic',
|
||||
'acceptable',
|
||||
'acclaimed',
|
||||
'accomplished',
|
||||
'accurate',
|
||||
'aching',
|
||||
'acidic',
|
||||
'acrobatic',
|
||||
'active',
|
||||
'actual',
|
||||
'adaptable',
|
||||
'adorable',
|
||||
'adventurous',
|
||||
'adept',
|
||||
'admirable',
|
||||
'admired',
|
||||
@@ -23,16 +17,11 @@ const adjective = [
|
||||
'advanced',
|
||||
'afraid',
|
||||
'affectionate',
|
||||
'aged',
|
||||
'aggravating',
|
||||
'aggressive',
|
||||
'agile',
|
||||
'agitated',
|
||||
'agonizing',
|
||||
'agreeable',
|
||||
'ajar',
|
||||
'alarmed',
|
||||
'alarming',
|
||||
'alert',
|
||||
'alienated',
|
||||
'alive',
|
||||
@@ -52,13 +41,11 @@ const adjective = [
|
||||
'annual',
|
||||
'another',
|
||||
'antique',
|
||||
'anxious',
|
||||
'any',
|
||||
'apprehensive',
|
||||
'appropriate',
|
||||
'apt',
|
||||
'arctic',
|
||||
'arid',
|
||||
'aromatic',
|
||||
'artistic',
|
||||
'ashamed',
|
||||
@@ -73,18 +60,12 @@ const adjective = [
|
||||
'authorized',
|
||||
'automatic',
|
||||
'avaricious',
|
||||
'average',
|
||||
'aware',
|
||||
'awesome',
|
||||
'awful',
|
||||
'awkward',
|
||||
'babyish',
|
||||
'bad',
|
||||
'back',
|
||||
'baggy',
|
||||
'bare',
|
||||
'barren',
|
||||
'basic',
|
||||
'beautiful',
|
||||
'belated',
|
||||
'beloved',
|
||||
@@ -112,7 +93,6 @@ const adjective = [
|
||||
'boiling',
|
||||
'bold',
|
||||
'bony',
|
||||
'boring',
|
||||
'bossy',
|
||||
'both',
|
||||
'bouncy',
|
||||
@@ -127,12 +107,10 @@ const adjective = [
|
||||
'broken',
|
||||
'bronze',
|
||||
'brown',
|
||||
'bruised',
|
||||
'bubbly',
|
||||
'bulky',
|
||||
'bumpy',
|
||||
'buoyant',
|
||||
'burdensome',
|
||||
'burly',
|
||||
'bustling',
|
||||
'busy',
|
||||
@@ -195,7 +173,6 @@ const adjective = [
|
||||
'cooperative',
|
||||
'coordinated',
|
||||
'corny',
|
||||
'corrupt',
|
||||
'costly',
|
||||
'courageous',
|
||||
'courteous',
|
||||
@@ -203,8 +180,6 @@ const adjective = [
|
||||
'crazy',
|
||||
'creamy',
|
||||
'creative',
|
||||
'creepy',
|
||||
'criminal',
|
||||
'crisp',
|
||||
'critical',
|
||||
'crooked',
|
||||
@@ -219,7 +194,6 @@ const adjective = [
|
||||
'curvy',
|
||||
'cute',
|
||||
'cylindrical',
|
||||
'damaged',
|
||||
'damp',
|
||||
'dangerous',
|
||||
'dapper',
|
||||
@@ -236,10 +210,8 @@ const adjective = [
|
||||
'decimal',
|
||||
'decisive',
|
||||
'deep',
|
||||
'defenseless',
|
||||
'defensive',
|
||||
'defiant',
|
||||
'deficient',
|
||||
'definite',
|
||||
'definitive',
|
||||
'delayed',
|
||||
@@ -249,7 +221,6 @@ const adjective = [
|
||||
'delirious',
|
||||
'demanding',
|
||||
'dense',
|
||||
'dental',
|
||||
'dependable',
|
||||
'dependent',
|
||||
'descriptive',
|
||||
@@ -258,29 +229,19 @@ const adjective = [
|
||||
'determined',
|
||||
'devoted',
|
||||
'different',
|
||||
'difficult',
|
||||
'digital',
|
||||
'diligent',
|
||||
'dim',
|
||||
'dimpled',
|
||||
'dimwitted',
|
||||
'direct',
|
||||
'disastrous',
|
||||
'discrete',
|
||||
'disfigured',
|
||||
'disgusting',
|
||||
'disloyal',
|
||||
'dismal',
|
||||
'distant',
|
||||
'downright',
|
||||
'dreary',
|
||||
'dirty',
|
||||
'disguised',
|
||||
'dishonest',
|
||||
'dismal',
|
||||
'distant',
|
||||
'distinct',
|
||||
'distorted',
|
||||
'dizzy',
|
||||
'dopey',
|
||||
'doting',
|
||||
@@ -293,7 +254,6 @@ const adjective = [
|
||||
'droopy',
|
||||
'dry',
|
||||
'dual',
|
||||
'dull',
|
||||
'dutiful',
|
||||
'each',
|
||||
'eager',
|
||||
@@ -474,20 +434,16 @@ const adjective = [
|
||||
'great',
|
||||
'greedy',
|
||||
'green',
|
||||
'gregarious',
|
||||
'grim',
|
||||
'grimy',
|
||||
'gripping',
|
||||
'grizzled',
|
||||
'gross',
|
||||
'grotesque',
|
||||
'grouchy',
|
||||
'grounded',
|
||||
'growing',
|
||||
'growling',
|
||||
'grown',
|
||||
'grubby',
|
||||
'gruesome',
|
||||
'grumpy',
|
||||
'guilty',
|
||||
'gullible',
|
||||
@@ -506,7 +462,6 @@ const adjective = [
|
||||
'harmonious',
|
||||
'harsh',
|
||||
'hasty',
|
||||
'hateful',
|
||||
'haunting',
|
||||
'healthy',
|
||||
'heartfelt',
|
||||
@@ -528,16 +483,13 @@ const adjective = [
|
||||
'honorable',
|
||||
'honored',
|
||||
'hopeful',
|
||||
'horrible',
|
||||
'hospitable',
|
||||
'hot',
|
||||
'huge',
|
||||
'humble',
|
||||
'humiliating',
|
||||
'humming',
|
||||
'humongous',
|
||||
'hungry',
|
||||
'hurtful',
|
||||
'husky',
|
||||
'icky',
|
||||
'icy',
|
||||
@@ -548,10 +500,7 @@ const adjective = [
|
||||
'idiotic',
|
||||
'idolized',
|
||||
'ignorant',
|
||||
'ill',
|
||||
'illegal',
|
||||
'ill-fated',
|
||||
'ill-informed',
|
||||
'illiterate',
|
||||
'illustrious',
|
||||
'imaginary',
|
||||
@@ -566,15 +515,11 @@ const adjective = [
|
||||
'imperfect',
|
||||
'imperturbable',
|
||||
'impish',
|
||||
'impolite',
|
||||
'important',
|
||||
'impossible',
|
||||
'impractical',
|
||||
'impressionable',
|
||||
'impressive',
|
||||
'improbable',
|
||||
'impure',
|
||||
'inborn',
|
||||
'incomparable',
|
||||
'incompatible',
|
||||
'incomplete',
|
||||
@@ -592,7 +537,6 @@ const adjective = [
|
||||
'innocent',
|
||||
'insecure',
|
||||
'insidious',
|
||||
'insignificant',
|
||||
'insistent',
|
||||
'instructive',
|
||||
'insubstantial',
|
||||
@@ -639,7 +583,6 @@ const adjective = [
|
||||
'known',
|
||||
'kooky',
|
||||
'kosher',
|
||||
'lame',
|
||||
'lanky',
|
||||
'large',
|
||||
'last',
|
||||
@@ -722,9 +665,7 @@ const adjective = [
|
||||
'miniature',
|
||||
'minor',
|
||||
'minty',
|
||||
'miserable',
|
||||
'miserly',
|
||||
'misguided',
|
||||
'misty',
|
||||
'mixed',
|
||||
'modern',
|
||||
@@ -777,18 +718,15 @@ const adjective = [
|
||||
'noteworthy',
|
||||
'novel',
|
||||
'noxious',
|
||||
'numb',
|
||||
'nutritious',
|
||||
'nutty',
|
||||
'obedient',
|
||||
'obese',
|
||||
'oblong',
|
||||
'oily',
|
||||
'oblong',
|
||||
'obvious',
|
||||
'occasional',
|
||||
'odd',
|
||||
'oddball',
|
||||
'offbeat',
|
||||
'offensive',
|
||||
'official',
|
||||
@@ -836,7 +774,6 @@ const adjective = [
|
||||
'personal',
|
||||
'pertinent',
|
||||
'pesky',
|
||||
'pessimistic',
|
||||
'petty',
|
||||
'phony',
|
||||
'physical',
|
||||
@@ -858,7 +795,6 @@ const adjective = [
|
||||
'pointed',
|
||||
'pointless',
|
||||
'poised',
|
||||
'poor',
|
||||
'popular',
|
||||
'portly',
|
||||
'posh',
|
||||
@@ -903,7 +839,6 @@ const adjective = [
|
||||
'quarterly',
|
||||
'queasy',
|
||||
'querulous',
|
||||
'questionable',
|
||||
'quick',
|
||||
'quick-witted',
|
||||
'quiet',
|
||||
@@ -931,7 +866,6 @@ const adjective = [
|
||||
'reliable',
|
||||
'relieved',
|
||||
'remarkable',
|
||||
'remorseful',
|
||||
'remote',
|
||||
'repentant',
|
||||
'required',
|
||||
@@ -949,7 +883,6 @@ const adjective = [
|
||||
'robust',
|
||||
'rosy',
|
||||
'rotating',
|
||||
'rotten',
|
||||
'rough',
|
||||
'round',
|
||||
'rowdy',
|
||||
@@ -963,7 +896,6 @@ const adjective = [
|
||||
'rusty',
|
||||
'sad',
|
||||
'safe',
|
||||
'salty',
|
||||
'same',
|
||||
'sandy',
|
||||
'sane',
|
||||
@@ -998,7 +930,6 @@ const adjective = [
|
||||
'shadowy',
|
||||
'shady',
|
||||
'shallow',
|
||||
'shameful',
|
||||
'shameless',
|
||||
'sharp',
|
||||
'shimmering',
|
||||
@@ -1011,7 +942,6 @@ const adjective = [
|
||||
'showy',
|
||||
'shrill',
|
||||
'shy',
|
||||
'sick',
|
||||
'silent',
|
||||
'silky',
|
||||
'silly',
|
||||
@@ -1027,7 +957,6 @@ const adjective = [
|
||||
'sleepy',
|
||||
'slight',
|
||||
'slim',
|
||||
'slimy',
|
||||
'slippery',
|
||||
'slow',
|
||||
'slushy',
|
||||
@@ -1136,7 +1065,6 @@ const adjective = [
|
||||
'tender',
|
||||
'tense',
|
||||
'tepid',
|
||||
'terrible',
|
||||
'terrific',
|
||||
'testy',
|
||||
'thankful',
|
||||
@@ -1183,7 +1111,6 @@ const adjective = [
|
||||
'tubby',
|
||||
'turbulent',
|
||||
'twin',
|
||||
'ugly',
|
||||
'ultimate',
|
||||
'unacceptable',
|
||||
'unaware',
|
||||
@@ -1198,14 +1125,12 @@ const adjective = [
|
||||
'unfolded',
|
||||
'unfortunate',
|
||||
'unhappy',
|
||||
'unhealthy',
|
||||
'uniform',
|
||||
'unimportant',
|
||||
'unique',
|
||||
'united',
|
||||
'unkempt',
|
||||
'unknown',
|
||||
'unlawful',
|
||||
'unlined',
|
||||
'unlucky',
|
||||
'unnatural',
|
||||
@@ -1235,7 +1160,6 @@ const adjective = [
|
||||
'usable',
|
||||
'used',
|
||||
'useful',
|
||||
'useless',
|
||||
'utilized',
|
||||
'utter',
|
||||
'vacant',
|
||||
@@ -1271,7 +1195,6 @@ const adjective = [
|
||||
'warmhearted',
|
||||
'warped',
|
||||
'wary',
|
||||
'wasteful',
|
||||
'watchful',
|
||||
'waterlogged',
|
||||
'watery',
|
||||
@@ -1611,7 +1534,6 @@ const object = [
|
||||
'classroom',
|
||||
'delivery',
|
||||
'device',
|
||||
'difficulty',
|
||||
'drama',
|
||||
'election',
|
||||
'engine',
|
||||
@@ -1625,7 +1547,6 @@ const object = [
|
||||
'suggestion',
|
||||
'tension',
|
||||
'variation',
|
||||
'anxiety',
|
||||
'atmosphere',
|
||||
'awareness',
|
||||
'bread',
|
||||
@@ -1761,7 +1682,6 @@ const object = [
|
||||
'drawer',
|
||||
'establishment',
|
||||
'examination',
|
||||
'garbage',
|
||||
'grocery',
|
||||
'honey',
|
||||
'impression',
|
||||
@@ -1886,7 +1806,6 @@ const object = [
|
||||
'group',
|
||||
'risk',
|
||||
'word',
|
||||
'fat',
|
||||
'force',
|
||||
'key',
|
||||
'light',
|
||||
@@ -1934,7 +1853,6 @@ const object = [
|
||||
'coast',
|
||||
'action',
|
||||
'age',
|
||||
'bad',
|
||||
'boat',
|
||||
'record',
|
||||
'result',
|
||||
@@ -2223,7 +2141,6 @@ const object = [
|
||||
'pleasure',
|
||||
'score',
|
||||
'screw',
|
||||
'sex',
|
||||
'shop',
|
||||
'shower',
|
||||
'suit',
|
||||
@@ -2416,7 +2333,6 @@ const object = [
|
||||
'counter',
|
||||
'flower',
|
||||
'grandfather',
|
||||
'harm',
|
||||
'knee',
|
||||
'lawyer',
|
||||
'leather',
|
||||
@@ -2747,7 +2663,6 @@ const object = [
|
||||
'crazy',
|
||||
'escape',
|
||||
'gather',
|
||||
'hate',
|
||||
'prior',
|
||||
'repair',
|
||||
'rough',
|
||||
@@ -2757,19 +2672,15 @@ const object = [
|
||||
'strike',
|
||||
'employ',
|
||||
'external',
|
||||
'hurt',
|
||||
'illegal',
|
||||
'laugh',
|
||||
'lay',
|
||||
'mobile',
|
||||
'nasty',
|
||||
'ordinary',
|
||||
'respond',
|
||||
'royal',
|
||||
'senior',
|
||||
'split',
|
||||
'strain',
|
||||
'struggle',
|
||||
'swim',
|
||||
'train',
|
||||
'upper',
|
||||
@@ -2808,7 +2719,6 @@ const object = [
|
||||
'prompt',
|
||||
'quiet',
|
||||
'refuse',
|
||||
'regret',
|
||||
'reveal',
|
||||
'rush',
|
||||
'shake',
|
||||
@@ -2848,9 +2758,6 @@ const object = [
|
||||
'wake',
|
||||
'wrap',
|
||||
'yesterday',
|
||||
'Thomas',
|
||||
'Tom',
|
||||
'Lieuwe',
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
@@ -49,3 +49,39 @@ export const validateFile = (file) => {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @description Verifies if object is empty
|
||||
* @param {object} obj
|
||||
*/
|
||||
export const isEmptyObject = (obj) => {
|
||||
if (typeof obj === 'object' && obj !== null && !Array.isArray(obj)) {
|
||||
return Object.keys(obj).length === 0;
|
||||
}
|
||||
throw new Error('Variable is not an object');
|
||||
};
|
||||
|
||||
/**
|
||||
* @description Merges two objects, suppressing undefined keys
|
||||
* @param {object} a
|
||||
* @param {object} b
|
||||
*/
|
||||
export const mergeObject = (a, b) => {
|
||||
const merged = {};
|
||||
Object.keys({ ...a, ...b }).map((key) => {
|
||||
merged[key] = typeof b[key] === 'undefined' ? a[key] : b[key];
|
||||
});
|
||||
return merged;
|
||||
};
|
||||
|
||||
/**
|
||||
* @description Removes undefined
|
||||
* @param {object} obj
|
||||
*/
|
||||
export const removeUndefined = (obj) => {
|
||||
const patched = {};
|
||||
Object.keys({ ...obj })
|
||||
.filter((key) => typeof obj[key] !== 'undefined')
|
||||
.map((key) => (patched[key] = obj[key]));
|
||||
return patched;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
import { isEmptyObject } from './parserUtils.js';
|
||||
|
||||
/**
|
||||
* @description initial checks for an empty of malformed request object
|
||||
* @param obj
|
||||
* @param res
|
||||
*/
|
||||
export const failEmptyObjects = (obj, res) => {
|
||||
let failed = false;
|
||||
try {
|
||||
if (isEmptyObject(obj)) {
|
||||
res.status(400).send('No object found in request');
|
||||
failed = true;
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(400).send(error);
|
||||
failed = true;
|
||||
}
|
||||
return failed;
|
||||
};
|
||||
|
||||
/**
|
||||
* @description initial checks for an empty of malformed request object
|
||||
* @param obj
|
||||
* @param res
|
||||
*/
|
||||
export const failIsNotArray = (obj, res) => {
|
||||
let failed = false;
|
||||
try {
|
||||
if (!Array.isArray(obj)) {
|
||||
res.status(400).send('No array found in request');
|
||||
failed = true;
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(400).send(error);
|
||||
failed = true;
|
||||
}
|
||||
return failed;
|
||||
};
|
||||
Reference in New Issue
Block a user