bia-framework
Version:
Bia Framework to work with angular 2
76 lines • 3.25 kB
JavaScript
"use strict";
var ObjectId = (function () {
function ObjectId() {
var _this = this;
this.timestamp = Math.floor(new Date().valueOf() / 1000);
this.increment = Math.floor(Math.random() * (16777216));
this.pid = Math.floor(Math.random() * (65536));
this.machine = Math.floor(Math.random() * (16777216));
this.setMachine = function () {
var mongoMachineId = parseInt(localStorage['mongoMachineId']);
if (mongoMachineId >= 0 && mongoMachineId <= 16777215) {
_this.machine = Math.floor(localStorage['mongoMachineId']);
}
localStorage['mongoMachineId'] = _this.machine;
};
this.getDate = function () {
return new Date(_this.timestamp * 1000);
};
this.toArray = function () {
var strOid = _this.toString();
var array = [];
var i;
for (i = 0; i < 12; i++) {
array[i] = parseInt(strOid.slice(i * 2, i * 2 + 2), 16);
}
return array;
};
this.toString = function () {
if (_this.timestamp === undefined
|| _this.machine === undefined
|| _this.pid === undefined
|| _this.increment === undefined) {
return 'Invalid ObjectId';
}
var timestamp = _this.timestamp.toString(16);
var machine = _this.machine.toString(16);
var pid = _this.pid.toString(16);
var increment = _this.increment.toString(16);
return '00000000'.substr(0, 8 - timestamp.length) + timestamp +
'000000'.substr(0, 6 - machine.length) + machine +
'0000'.substr(0, 4 - pid.length) + pid +
'000000'.substr(0, 6 - increment.length) + increment;
};
this.setMachine();
if (typeof (arguments[0]) == 'object') {
this.timestamp = arguments[0].timestamp;
this.machine = arguments[0].machine;
this.pid = arguments[0].pid;
this.increment = arguments[0].increment;
}
else if (typeof (arguments[0]) == 'string' && arguments[0].length == 24) {
this.timestamp = Number('0x' + arguments[0].substr(0, 8)),
this.machine = Number('0x' + arguments[0].substr(8, 6)),
this.pid = Number('0x' + arguments[0].substr(14, 4)),
this.increment = Number('0x' + arguments[0].substr(18, 6));
}
else if (arguments.length == 4 && arguments[0] != null) {
this.timestamp = arguments[0];
this.machine = arguments[1];
this.pid = arguments[2];
this.increment = arguments[3];
}
else {
this.timestamp = Math.floor(new Date().valueOf() / 1000);
this.machine = this.machine;
this.pid = this.pid;
this.increment = this.increment++;
if (this.increment > 0xffffff) {
this.increment = 0;
}
}
}
return ObjectId;
}());
exports.ObjectId = ObjectId;
//# sourceMappingURL=ObjectId.js.map