Skip to content

Commit 3cac6f0

Browse files
committed
feat: Gate getAll filtering on filterInvalid option
1 parent 94bec5e commit 3cac6f0

File tree

2 files changed

+80
-23
lines changed

2 files changed

+80
-23
lines changed

src/__tests__/utils.test.ts

Lines changed: 67 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
PropertyType,
1010
getDefaultValues,
1111
ObjectIdConstructorParameter,
12+
GetIdsOptions,
1213
} from '../utils';
1314

1415
describe('utils', () => {
@@ -379,44 +380,78 @@ describe('utils', () => {
379380
});
380381

381382
describe('getIds', () => {
382-
// prettier-ignore
383-
test.each<[string, readonly ObjectIdConstructorParameter[], readonly ObjectId[]]>([
383+
test.each<
384+
[
385+
string,
386+
{
387+
input: readonly ObjectIdConstructorParameter[];
388+
options?: GetIdsOptions;
389+
expected: readonly ObjectId[];
390+
},
391+
]
392+
>([
384393
[
385394
'strings',
386-
['123456789012345678900001', '123456789012345678900002'],
387-
[new ObjectId('123456789012345678900001'), new ObjectId('123456789012345678900002')],
395+
{
396+
input: ['123456789012345678900001', '123456789012345678900002'],
397+
expected: [
398+
new ObjectId('123456789012345678900001'),
399+
new ObjectId('123456789012345678900002'),
400+
],
401+
},
388402
],
389403
[
390-
'objectIds',
391-
[new ObjectId('123456789012345678900099'), new ObjectId('123456789012345678900022')],
392-
[new ObjectId('123456789012345678900099'), new ObjectId('123456789012345678900022')],
404+
'ObjectIds',
405+
{
406+
input: [
407+
new ObjectId('123456789012345678900099'),
408+
new ObjectId('123456789012345678900022'),
409+
],
410+
expected: [
411+
new ObjectId('123456789012345678900099'),
412+
new ObjectId('123456789012345678900022'),
413+
],
414+
},
393415
],
394416
[
395-
'Uint8Array',
396-
[
397-
new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]),
398-
new Uint8Array([13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24])
399-
],
400-
[new ObjectId('0102030405060708090a0b0c'), new ObjectId('0d0e0f101112131415161718')],
417+
'Uint8Arrays',
418+
{
419+
input: [
420+
new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]),
421+
new Uint8Array([13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]),
422+
],
423+
expected: [
424+
new ObjectId('0102030405060708090a0b0c'),
425+
new ObjectId('0d0e0f101112131415161718'),
426+
],
427+
},
401428
],
402429
[
403430
'mixed',
404-
['123456789012345678900014', new ObjectId('123456789012345678900088')],
405-
[new ObjectId('123456789012345678900014'), new ObjectId('123456789012345678900088')],
431+
{
432+
input: ['123456789012345678900014', new ObjectId('123456789012345678900088')],
433+
expected: [
434+
new ObjectId('123456789012345678900014'),
435+
new ObjectId('123456789012345678900088'),
436+
],
437+
},
406438
],
407439
[
408-
'invalid',
409-
['123', '123456789012345678900021'],
410-
[new ObjectId('123456789012345678900021')]
440+
'invalid values, when filterInvalid is true',
441+
{
442+
input: ['123', '123456789012345678900021'],
443+
options: { filterInvalid: true },
444+
expected: [new ObjectId('123456789012345678900021')],
445+
},
411446
],
412-
])('%s', (_name, input, expected) => {
447+
])('should return ObjectIds from %s', (_name, { input, options, expected }) => {
413448
expect.assertions(4);
414449

415450
// Given
416451
expect(expected.length).toBeLessThanOrEqual(input.length);
417452

418453
// When
419-
const actual = getIds(input);
454+
const actual = getIds(input, options);
420455

421456
// Then
422457
expect(actual).toEqual(expected);
@@ -429,5 +464,17 @@ describe('utils', () => {
429464
);
430465
expect(isEveryHexEquivalent).toBeTruthy();
431466
});
467+
468+
test('should throw on invalid values, when filterInvalid is unspecified', () => {
469+
expect.assertions(1);
470+
471+
// Given
472+
const input = ['123', '123456789012345678900021'];
473+
474+
// When/Then
475+
expect(() => getIds(input)).toThrowErrorMatchingInlineSnapshot(
476+
'"input must be a 24 character hex string, 12 byte Uint8Array, or an integer"'
477+
);
478+
});
432479
});
433480
});

src/utils.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,23 @@ export type RequireAtLeastOne<TObj, Keys extends keyof TObj = keyof TObj> = {
195195
Pick<TObj, Exclude<keyof TObj, Keys>>;
196196

197197
export type ObjectIdConstructorParameter = ConstructorParameters<typeof ObjectId>[0];
198-
export function getIds(ids: Iterable<ObjectIdConstructorParameter>): ObjectId[] {
198+
export interface GetIdsOptions {
199+
filterInvalid?: boolean;
200+
}
201+
export function getIds(
202+
ids: Iterable<ObjectIdConstructorParameter>,
203+
options?: GetIdsOptions
204+
): ObjectId[] {
199205
return Array.from(ids).flatMap((id) => {
200206
try {
201207
return new ObjectId(id);
202-
} catch {
203-
return [];
208+
} catch (error) {
209+
if (!options?.filterInvalid) {
210+
throw error;
211+
}
204212
}
213+
214+
return [];
205215
});
206216
}
207217

0 commit comments

Comments
 (0)