@communecter/cocolight-api-client 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,53 @@
1
+ const MongoID = {};
2
+
3
+ MongoID._looksLikeObjectID = str => str.length === 24 && /^[0-9a-f]*$/.test(str);
4
+
5
+ MongoID.ObjectID = class ObjectID {
6
+ constructor (hexString) {
7
+ //random-based impl of Mongo ObjectID
8
+ if (hexString) {
9
+ hexString = hexString.toLowerCase();
10
+ if (!MongoID._looksLikeObjectID(hexString)) {
11
+ throw new Error("Invalid hexadecimal string for creating an ObjectID");
12
+ }
13
+ // meant to work with _.isEqual(), which relies on structural equality
14
+ this._str = hexString;
15
+ }
16
+ }
17
+
18
+ equals(other) {
19
+ return other instanceof MongoID.ObjectID &&
20
+ this.valueOf() === other.valueOf();
21
+ }
22
+
23
+ toString() {
24
+ return `ObjectID("${this._str}")`;
25
+ }
26
+
27
+ clone() {
28
+ return new MongoID.ObjectID(this._str);
29
+ }
30
+
31
+ typeName() {
32
+ return "oid";
33
+ }
34
+
35
+ getTimestamp() {
36
+ return Number.parseInt(this._str.substr(0, 8), 16);
37
+ }
38
+
39
+ valueOf() {
40
+ return this._str;
41
+ }
42
+
43
+ toJSONValue() {
44
+ return this.valueOf();
45
+ }
46
+
47
+ toHexString() {
48
+ return this.valueOf();
49
+ }
50
+
51
+ };
52
+
53
+ export default MongoID;