UNPKG

@leyyo/cache

Version:
231 lines (230 loc) 6.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CacheFormatAbstract = void 0; const config_1 = require("../config"); const util_1 = require("../util"); // noinspection JSUnusedGlobalSymbols class CacheFormatAbstract { constructor(channel) { this.channel = channel; this.prefixes = { key: config_1.PRE_KEY, invalidation: config_1.PRE_INVALIDATOR, alias: config_1.PRE_ALIAS, owner: config_1.PRE_OWNER, field: '', member: '', }; } _trim(ids) { if (ids[0] === null) { ids.splice(0, 1); this._trim(ids); return; } else if (ids[ids.length - 1] === null) { ids.splice(ids.length - 1, 1); this._trim(ids); return; } return; } _toPlain(value) { if (value === undefined || value == null) { return null; } switch (typeof value) { case "string": const str = value.trim(); return str !== '' ? str : null; case "number": return value.toString(10); case "bigint": return String(value); case "boolean": return value ? 'true' : 'false'; default: return null; } } _toPlainList(ids) { if (!Array.isArray(ids) || ids.length < 1) { return []; } return ids .map(id => this._toPlain(id)) .filter(id => id !== null) .filter((id, index, array) => array.indexOf(id) === index); } _toDelimited(value) { if (value === undefined || value == null) { return null; } switch (typeof value) { case "string": const str = value.trim(); return str !== '' ? util_1.cacheUtil.alphaNumeric(str) : null; case "number": return value.toString(10); case "bigint": return String(value); case "boolean": return value ? 'true' : 'false'; default: return null; } } _buildFull(short, idType) { var _a; if (short) { return { short, full: this._fullValue((_a = this.prefixes[idType]) !== null && _a !== void 0 ? _a : '', short) }; } return {}; } _fullId(id, type) { id = this.basic(id); return id ? this._buildFull(id, type) : {}; } _fullIds(ids, type) { // improper array if (!Array.isArray(ids) || ids.length < 1) { return { shorts: [], fulls: [] }; } // empty-valued array const result = ids.map(id => this.basic(id)); if (result.length < 1) { return { shorts: [], fulls: [] }; } // converts to full const fulls = result.map(id => this._buildFull(id, type)); return { shorts: fulls.map(id => id.short), fulls: fulls.map(id => id.full), duplicated: (ids === null || ids === void 0 ? void 0 : ids.length) > fulls.length }; } checkName(property) { if (!property) { return []; } else if (typeof property === 'string') { return [property]; } else if (Array.isArray(property)) { return property.filter(p => p).map(p => String(p)); } return []; } key(key) { return this._fullId(key, 'key'); } keys(keys) { return this._fullIds(keys, 'key'); } basic(id) { if (Array.isArray(id)) { let arr = id; if (arr.length < 1) { return null; } arr = arr.map(item => this._toDelimited(item)); // trim array this._trim(arr); return arr.length > 0 ? arr.join(config_1.DLM_BETWEEN_PARTS) : null; } return this._toDelimited(id); } basics(ids) { if (!Array.isArray(ids) || ids.length < 1) { return []; } return ids .map(id => this.basic(id)) .filter(id => id !== null) .filter((id, index, array) => array.indexOf(id) === index); } alias(alias) { return this._fullId(alias, 'alias'); } aliases(aliases) { return this._fullIds(aliases, 'alias'); } owner(owner) { return this._fullId(owner, 'owner'); } owners(owners) { return this._fullIds(owners, 'owner'); } invalidation(invalidation) { return this._fullId(invalidation, 'invalidation'); } invalidations(invalidations) { return this._fullIds(invalidations, 'owner'); } field(field) { return this._toPlain(field); } fields(fields) { return this._toPlainList(fields); } member(member) { return this._toPlain(member); } members(members) { return this._toPlainList(members); } memberShorts(members) { const arr = this.members(members); return { shorts: arr, duplicated: arr.length < (members === null || members === void 0 ? void 0 : members.length) }; } value(value) { if (value === undefined || value == null) { return null; } switch (typeof value) { case "string": return value.trim(); case "bigint": return value.toString(10); case "number": return value; case "boolean": return value ? 'true' : 'false'; default: try { return JSON.stringify(value); } catch (e) { return null; } } } valueDoc(value) { if (!value || typeof value !== 'object' || Array.isArray(value)) { return null; } const entries = Object.entries(value); if (entries.length < 1) { return null; } const obj = {}; for (const [k, v] of entries) { const field = this.basic(k); if (field) { obj[field] = this.value(v); } } if (Object.entries(obj).length < 1) { return null; } return obj; } // region secure get $back() { return this; } get $secure() { return this; } } exports.CacheFormatAbstract = CacheFormatAbstract;