Skip to content

Commit

Permalink
feat: Remove GetAllOptions and filterInvalid
Browse files Browse the repository at this point in the history
Per feedback on #979
  • Loading branch information
duncanbeevers committed Dec 13, 2024
1 parent 3cac6f0 commit b98bfd6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 28 deletions.
19 changes: 2 additions & 17 deletions src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
PropertyType,
getDefaultValues,
ObjectIdConstructorParameter,
GetIdsOptions,
} from '../utils';

describe('utils', () => {
Expand Down Expand Up @@ -385,7 +384,6 @@ describe('utils', () => {
string,
{
input: readonly ObjectIdConstructorParameter[];
options?: GetIdsOptions;
expected: readonly ObjectId[];
},
]
Expand Down Expand Up @@ -440,18 +438,17 @@ describe('utils', () => {
'invalid values, when filterInvalid is true',
{
input: ['123', '123456789012345678900021'],
options: { filterInvalid: true },
expected: [new ObjectId('123456789012345678900021')],
},
],
])('should return ObjectIds from %s', (_name, { input, options, expected }) => {
])('should return ObjectIds from %s', (_name, { input, expected }) => {
expect.assertions(4);

// Given
expect(expected.length).toBeLessThanOrEqual(input.length);

// When
const actual = getIds(input, options);
const actual = getIds(input);

// Then
expect(actual).toEqual(expected);
Expand All @@ -464,17 +461,5 @@ describe('utils', () => {
);
expect(isEveryHexEquivalent).toBeTruthy();
});

test('should throw on invalid values, when filterInvalid is unspecified', () => {
expect.assertions(1);

// Given
const input = ['123', '123456789012345678900021'];

// When/Then
expect(() => getIds(input)).toThrowErrorMatchingInlineSnapshot(
'"input must be a 24 character hex string, 12 byte Uint8Array, or an integer"'
);
});
});
});
14 changes: 3 additions & 11 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,20 +195,12 @@ export type RequireAtLeastOne<TObj, Keys extends keyof TObj = keyof TObj> = {
Pick<TObj, Exclude<keyof TObj, Keys>>;

export type ObjectIdConstructorParameter = ConstructorParameters<typeof ObjectId>[0];
export interface GetIdsOptions {
filterInvalid?: boolean;
}
export function getIds(
ids: Iterable<ObjectIdConstructorParameter>,
options?: GetIdsOptions
): ObjectId[] {
export function getIds(ids: Iterable<ObjectIdConstructorParameter>): ObjectId[] {
return Array.from(ids).flatMap((id) => {
try {
return new ObjectId(id);
} catch (error) {
if (!options?.filterInvalid) {
throw error;
}
} catch {
// Intentionally empty
}

return [];
Expand Down

0 comments on commit b98bfd6

Please sign in to comment.