24 lines
528 B
JavaScript
24 lines
528 B
JavaScript
|
'use strict';
|
||
|
|
||
|
const moment = require('moment');
|
||
|
|
||
|
class IdGenerator {
|
||
|
constructor() {
|
||
|
this._lastUsedTimestamp = null;
|
||
|
this._counter = 0;
|
||
|
}
|
||
|
|
||
|
nextId() {
|
||
|
const currentTimestamp = moment().valueOf();
|
||
|
if (currentTimestamp === this._lastUsedTimestamp) {
|
||
|
return currentTimestamp.toString(36) + '.' + (++this._counter).toString(36);
|
||
|
} else {
|
||
|
this._counter = 0;
|
||
|
this._lastUsedTimestamp = currentTimestamp;
|
||
|
return currentTimestamp.toString(36);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = IdGenerator;
|