short-objectid
Version:
The idea of this project is make your details route easier 🥰 ..
47 lines (46 loc) • 1.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
class ShortObjectIdCache {
constructor() {
this.cacheState = [];
}
static getInstance() {
if (!ShortObjectIdCache.instance) {
ShortObjectIdCache.instance = new ShortObjectIdCache();
}
return ShortObjectIdCache.instance;
}
getCacheState() {
return this.cacheState;
}
subscribe(newData) {
let ExistInCacheState = this.getCacheState().filter((state) => state.oid === newData.oid).length !== 0;
if (ExistInCacheState) {
return;
}
this.cacheState = [...this.cacheState, newData];
}
getFullObjectId(shortObjectId) {
// catch promise warn with express and mongoose ..
if (isNaN(parseInt(shortObjectId.toString(), 10))) {
return;
}
const result = this.getCacheState().filter((state, index) => state.shortObjectId == shortObjectId);
if (result.length !== 1) {
throw new Error(`Your ShortObjectId < ${shortObjectId} > Not Found ..`);
}
return result[0].oid;
}
getShortObjectId(fullObjectId) {
// catch promise warn with express and mongoose ..
if (typeof fullObjectId == 'undefined') {
return;
}
const result = this.getCacheState().filter(state => state.oid == fullObjectId);
if (result.length !== 1) {
throw new Error(`Your ObjectId < ${fullObjectId} > Not Found ..`);
}
return result[0].shortObjectId;
}
}
exports.default = ShortObjectIdCache;