Skip to content

Commit

Permalink
✨ Add checksum support to the ObjectId class
Browse files Browse the repository at this point in the history
  • Loading branch information
skerit committed Oct 5, 2023
1 parent 2e529c8 commit abe1f2c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* Upgrade all dependencies
* Don't log `.js.map` 404 errors
* Add `Conduit.Socket#is_connected` property
* Add checksum support to the `ObjectId` class

## 1.3.15 (2023-07-03)

Expand Down
17 changes: 15 additions & 2 deletions lib/init/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ JSON.registerUndrier('ObjectID', function undryOI(holder, key, value) {
});

/**
* Make JSON-Dry handle ObjectIDs when drying
* Make JSON-Dry handle ObjectIds when drying
*
* @author Jelle De Loecker <jelle@elevenways.be>
* @since 1.3.16
Expand All @@ -530,7 +530,7 @@ JSON.registerDrier('ObjectId', function dryOI(holder, key, value) {
}, {add_path: false});

/**
* Correctly un-dry ObjectIDs
* Correctly un-dry ObjectIds
*
* @author Jelle De Loecker <jelle@elevenways.be>
* @since 1.3.16
Expand All @@ -540,6 +540,19 @@ JSON.registerUndrier('ObjectId', function undryOI(holder, key, value) {
return alchemy.castObjectId(value);
});

/**
* Monkey-patch checksum support to the ObjectId class
*
* @author Jelle De Loecker <jelle@elevenways.be>
* @since 1.3.16
* @version 1.3.16
*/
if (typeof mongo?.ObjectId == 'function') {
Function.setMethod(mongo.ObjectId, Blast.checksumSymbol, function checksum() {
return this.toString();
});
}

/**
* Create a new ObjectId
*
Expand Down
44 changes: 44 additions & 0 deletions test/09-datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,48 @@ describe('Mongo Datasource', function() {
assert.strictEqual(top_record.Project.name, 'protoblast');
});
});

describe('ObjectId', function() {
it('should be able to be checksummed', function() {

let set = new HashSet();

let a = alchemy.ObjectId(),
b = alchemy.ObjectId(),
c = alchemy.ObjectId();

let old_a = a,
old_b = b,
old_c = c;

assert.strictEqual((a+'').isObjectId(), true);
assert.strictEqual((b+'').isObjectId(), true);
assert.strictEqual((c+'').isObjectId(), true);

set.add(a);
set.add(b);
set.add(c);

assert.notStrictEqual(a+'', b+'');
assert.notStrictEqual(a+'', c+'');

assert.strictEqual(set.size, 3);

assert.strictEqual(set.has(a), true);
assert.strictEqual(set.has(b), true);
assert.strictEqual(set.has(c), true);

a = alchemy.castObjectId(a+'');
b = alchemy.castObjectId(b+'');
c = alchemy.castObjectId(c+'');

assert.strictEqual(set.has(a), true);
assert.strictEqual(set.has(b), true);
assert.strictEqual(set.has(c), true);

assert.notStrictEqual(old_a, a);
assert.notStrictEqual(old_b, b);
assert.notStrictEqual(old_c, c);
});
})
});

0 comments on commit abe1f2c

Please sign in to comment.