-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmongo.js
More file actions
30 lines (28 loc) · 837 Bytes
/
Copy pathmongo.js
File metadata and controls
30 lines (28 loc) · 837 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"use strict";
/**
Created by Complynx on 22.03.2019,
http://complynx.net
<complynx@yandex.ru> Daniel Drizhuk
*/
/**
* Generates Mongo-compliant ID
* @returns {string}
*/
export function generate_id() {
let timestamp = (new Date().getTime() / 1000 | 0).toString(16);
return timestamp + 'xxxxxxxxxxxxxxxx'.replace(/[x]/g, function() {
return (Math.random() * 16 | 0).toString(16);
}).toLowerCase();
}
/**
* extracts string from MongoID
* @param {*} id MongoID
* @returns {string} ID representation
*/
export function id_to_string(id){
if(typeof id === 'string') return id;
if('$oid' in id) return id_to_string(id['$oid']);
if('$id' in id) return id_to_string(id['$id']);
if('$uid' in id) return id_to_string(id['$uid']);
if('_id' in id) return id_to_string(id['_id']);
}