Skip to content

Commit 139011a

Browse files
committed
db.id(undefined) returns new id instead of null
1 parent 31f56cd commit 139011a

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

lib/_types.js

Whitespace-only changes.

lib/util.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,21 @@ module.exports = {
7171
forceArray: function(value) {
7272
return this.isArray(value)? value : [value]
7373
},
74+
75+
/**
76+
* @typedef {string} HexString
77+
* @description A hex string of 24 characters
78+
*/
7479

7580
/**
7681
* Casts to ObjectId
77-
* @param {string|ObjectId} str - string = hex string
78-
* @return {ObjectId}
82+
* @param {HexString|ObjectId} [value] - empty string = new ObjectId('')
7983
*/
80-
id: function(str) {
81-
if (str == null) return new ObjectId()
82-
return typeof str === 'string' ? ObjectId.createFromHexString(str) : str
84+
id: function(value) {
85+
if (value === undefined) return new ObjectId()
86+
else if (typeof value === 'string') return ObjectId.createFromHexString(value)
87+
else if (value instanceof ObjectId) return value
88+
else throw new Error(`Monastery: invalid value "${value}" passed to id(value?: HexString|ObjectId).`)
8389
},
8490

8591
inArray: (array, key, value) => {

0 commit comments

Comments
 (0)