Skip to content

Commit 4356d3d

Browse files
author
Ovidiu Barabula
committed
feat(util): add helper to check if object has specific keys
1 parent c0261f4 commit 4356d3d

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

src/util/utility-functions.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
required,
1515
retry,
1616
sleep,
17+
hasAllKeys,
1718
} from './utility-functions';
1819

1920
describe('Utility Functions', () => {
@@ -345,4 +346,42 @@ describe('Utility Functions', () => {
345346
assert.throws(() => required(1), ERRORS.REQUIRED_NEEDS_MESSAGE);
346347
});
347348
});
349+
350+
351+
describe('hasAllKeys()', () => {
352+
const subject = {
353+
keyA: 'valueA',
354+
keyB: 'valueB',
355+
keyC: 'valueC',
356+
};
357+
358+
it('returns true if object contains all keys', () => {
359+
expect(hasAllKeys(subject, 'keyA', 'keyB', 'keyC')).to.be.true;
360+
});
361+
362+
363+
it('returns false if object doesn\'t contain all keys', () => {
364+
expect(hasAllKeys(subject, 'keyA', 'keyB', 'keyC', 'keyD')).to.be.false;
365+
});
366+
367+
368+
it('throws if called without parameters', () => {
369+
assert.throws(() => hasAllKeys(), ERRORS.HAS_ALL_KEYS_NEEDS_OBJECT);
370+
});
371+
372+
373+
it('throws if first parameter is not an object', () => {
374+
assert.throws(() => hasAllKeys(false), ERRORS.HAS_ALL_KEYS_NEEDS_OBJECT);
375+
});
376+
377+
378+
it('throws if rest params are not passed', () => {
379+
assert.throws(() => hasAllKeys(subject), ERRORS.HAS_ALL_KEYS_NEEDS_KEYS_ARRAY);
380+
});
381+
382+
383+
it('throws if rest params are not strings', () => {
384+
assert.throws(() => hasAllKeys(subject, 'keyA', 1, true), ERRORS.HAS_ALL_KEYS_NEEDS_KEYS_ARRAY);
385+
});
386+
});
348387
});

src/util/utility-functions.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ export const ERRORS = {
3636

3737
// required()
3838
REQUIRED_NEEDS_MESSAGE: 'required() requires first argument <message> of type \'string\'',
39+
40+
// hasAllKeys()
41+
HAS_ALL_KEYS_NEEDS_KEYS_ARRAY: 'hasAllKeys() requires rest argument(s) <key> of type \'string\'',
42+
HAS_ALL_KEYS_NEEDS_OBJECT: 'hasAllKeys() requires first argument <object> of type \'object\'',
3943
};
4044

4145

@@ -265,3 +269,22 @@ export function required<T>(message: string): T {
265269

266270
throw new Error(message);
267271
}
272+
273+
274+
/**
275+
* Check if passed object as all the keys in the keys array
276+
* @param object Object to be tested
277+
* @param keys Array of keys
278+
*/
279+
export function hasAllKeys(
280+
object: ({[key: string]: any}) = required(ERRORS.HAS_ALL_KEYS_NEEDS_OBJECT),
281+
...keys: string[],
282+
): boolean {
283+
if (typeof object !== 'object') {
284+
return required(ERRORS.HAS_ALL_KEYS_NEEDS_OBJECT);
285+
} else if (keys.length === 0 || !keys.every(item => typeof item === 'string')) {
286+
return required(ERRORS.HAS_ALL_KEYS_NEEDS_KEYS_ARRAY);
287+
}
288+
289+
return keys.every(key => hasNested(object, key));
290+
}

types/util/utility-functions.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export declare const ERRORS: {
1515
RETRY_NEEDS_OPTIONS_TO_BE_OBJECT: string;
1616
RETRY_RETRIES_CANNOT_BE_ZERO: string;
1717
REQUIRED_NEEDS_MESSAGE: string;
18+
HAS_ALL_KEYS_NEEDS_KEYS_ARRAY: string;
19+
HAS_ALL_KEYS_NEEDS_OBJECT: string;
1820
};
1921
/**
2022
* Check object for path
@@ -64,3 +66,11 @@ export declare function retry(fn: AnyFunction, options?: {
6466
* @param message Custom error message
6567
*/
6668
export declare function required<T>(message: string): T;
69+
/**
70+
* Check if passed object as all the keys in the keys array
71+
* @param object Object to be tested
72+
* @param keys Array of keys
73+
*/
74+
export declare function hasAllKeys(object?: ({
75+
[key: string]: any;
76+
}), ...keys: string[]): boolean;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy