Skip to content

Commit a6933dc

Browse files
authored
fix: restore insist (#759)
1 parent d384f3f commit a6933dc

3 files changed

Lines changed: 43 additions & 0 deletions

File tree

test/utilities/integrity.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { should } from 'chai';
2+
import { insist } from '../../tools/integrity';
3+
4+
should();
5+
6+
describe('integrity', () => {
7+
it('should throw TypeError when required property is missing', () => {
8+
(() => insist({}, 'a')).should.throw(TypeError);
9+
(() => insist({ a: 1 }, 'a', 'b')).should.throw(TypeError);
10+
});
11+
it('should not throw when all required properties are present', () => {
12+
(() => insist({ a: 1, b: 2 }, 'a', 'b')).should.not.throw();
13+
});
14+
it('should allow falsy values (0, false, \'\') as property values', () => {
15+
(() => insist({ a: 0 }, 'a')).should.not.throw();
16+
(() => insist({ a: false }, 'a')).should.not.throw();
17+
(() => insist({ a: '' }, 'a')).should.not.throw();
18+
});
19+
it('should throw when object is null or undefined', () => {
20+
(() => insist(null as unknown as Record<string, unknown>, 'a')).should.throw();
21+
(() => insist(undefined as unknown as Record<string, unknown>, 'a')).should.throw();
22+
});
23+
it('should not throw when no properties are required', () => {
24+
(() => insist({ a: 0 })).should.not.throw();
25+
});
26+
});

tools/integrity.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @description Insist that the provided data has the required properties.
3+
* @param {Record<string, unknown>} thing to encourage to have data
4+
* @param {...string} args arguments to ensure
5+
*/
6+
export const insist = (thing: Record<string, unknown>, ...args: string[]) => {
7+
if (!thing || !Object.keys(thing).length) {
8+
throw new TypeError('No data provided.');
9+
}
10+
11+
args.forEach((arg) => {
12+
if (!(arg in thing)) {
13+
throw new TypeError(`Missing required property: '${arg}'.`);
14+
}
15+
});
16+
};

tools/utilities.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* istanbul ignore file */
22

3+
export * from './integrity';
34
export * from './timeDate';
45
export * from './translation';

0 commit comments

Comments
 (0)