Skip to content

Commit

Permalink
Merge 4f895f7 into 63db6b8
Browse files Browse the repository at this point in the history
  • Loading branch information
irokhes committed Feb 9, 2020
2 parents 63db6b8 + 4f895f7 commit 44b8cb4
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
7 changes: 7 additions & 0 deletions chance.js
Expand Up @@ -7294,6 +7294,13 @@
return this.pick(this.get("emotions"));
};

Chance.prototype.mongo_objectid = function() {
const timestamp = (new Date().getTime() / 1000 | 0).toString(16);
return timestamp + 'xxxxxxxxxxxxxxxx'.replace(/[x]/g, function() {
return (Math.random() * 16 | 0).toString(16);
}).toLowerCase();
};

// -- End Miscellaneous --

Chance.prototype.mersenne_twister = function (seed) {
Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Expand Up @@ -123,5 +123,6 @@
* [tv](miscellaneous/tv.md)
* [unique](miscellaneous/unique.md)
* [weighted](miscellaneous/weighted.md)
* [mongo_objectid](miscellaneous/mongo_objectid.md)

<script type="text/javascript" src="chance.js"></script>
11 changes: 9 additions & 2 deletions docs/chance.js
Expand Up @@ -2492,7 +2492,7 @@
};

var data = {



firstNames: {
Expand Down Expand Up @@ -6848,7 +6848,7 @@
"Joy",
"Surprise",
"Anger",
"Sadness",
"Sadness",
"Fear"
],
};
Expand Down Expand Up @@ -7034,6 +7034,13 @@
return this.pick(this.get("emotions"));
};

Chance.prototype.mongo_objectid = function() {
const timestamp = (new Date().getTime() / 1000 | 0).toString(16);
return timestamp + 'xxxxxxxxxxxxxxxx'.replace(/[x]/g, function() {
return (Math.random() * 16 | 0).toString(16);
}).toLowerCase();
}

// -- End Miscellaneous --

Chance.prototype.mersenne_twister = function (seed) {
Expand Down
13 changes: 13 additions & 0 deletions docs/miscellaneous/mongo_objectid.md
@@ -0,0 +1,13 @@
# mongo_objectid

```js
// usage
chance.mongo_objectid()
```

Return a random mongo object ID string.

```js
chance.mongo_objectid();
=> '507f191e810c19729de860ea'
```
10 changes: 10 additions & 0 deletions test/test.misc.js
Expand Up @@ -296,3 +296,13 @@ test('tv() works as expected', t => {
t.true(/^[KW][A-Z][A-Z][A-Z]/.test(tv))
})
})

// chance.mongo_objectid()
test('mongo_objectid() returns a proper objectId', t => {
_.times(1000, () => {
const objectId = chance.mongo_objectid();
const hexTimestamp = objectId.substring(0,8);
t.true(_.isDate(new Date(parseInt(hexTimestamp, 16) * 1000)));
t.is(objectId.length, 24);
})
})

0 comments on commit 44b8cb4

Please sign in to comment.