Skip to content

Commit b00d711

Browse files
committed
feat: Accept all ObjectId constructor types in getIds
1 parent 6cfb1e0 commit b00d711

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/__tests__/utils.test.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ import { describe, expect, test } from '@jest/globals';
22
import { ObjectId } from 'mongodb';
33
import { expectType } from 'ts-expect';
44
import { DefaultsOption } from '../schema';
5-
import { NestedPaths, ProjectionType, getIds, PropertyType, getDefaultValues } from '../utils';
5+
import {
6+
NestedPaths,
7+
ProjectionType,
8+
getIds,
9+
PropertyType,
10+
getDefaultValues,
11+
ObjectIdConstructorParameter,
12+
} from '../utils';
613

714
describe('utils', () => {
815
interface TestDocument {
@@ -372,7 +379,8 @@ describe('utils', () => {
372379
});
373380

374381
describe('getIds', () => {
375-
test.each<[string, readonly (ObjectId | string)[], readonly ObjectId[]]>([
382+
// prettier-ignore
383+
test.each<[string, readonly ObjectIdConstructorParameter[], readonly ObjectId[]]>([
376384
[
377385
'strings',
378386
['123456789012345678900001', '123456789012345678900002'],
@@ -383,6 +391,14 @@ describe('utils', () => {
383391
[new ObjectId('123456789012345678900099'), new ObjectId('123456789012345678900022')],
384392
[new ObjectId('123456789012345678900099'), new ObjectId('123456789012345678900022')],
385393
],
394+
[
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')],
401+
],
386402
[
387403
'mixed',
388404
['123456789012345678900014', new ObjectId('123456789012345678900088')],

src/utils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,9 @@ export type RequireAtLeastOne<TObj, Keys extends keyof TObj = keyof TObj> = {
194194
}[Keys] &
195195
Pick<TObj, Exclude<keyof TObj, Keys>>;
196196

197-
export function getIds(ids: Set<string> | readonly (ObjectId | string)[]): ObjectId[] {
198-
return [...ids].map((id) => new ObjectId(id));
197+
export type ObjectIdConstructorParameter = ConstructorParameters<typeof ObjectId>[0];
198+
export function getIds(ids: Iterable<ObjectIdConstructorParameter>): ObjectId[] {
199+
return Array.from(ids).map((id) => new ObjectId(id));
199200
}
200201

201202
/**

0 commit comments

Comments
 (0)