UNPKG

@tucmc/hazel

Version:
169 lines (168 loc) 4.74 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DMap = void 0; const crypto = __importStar(require("crypto")); class DMap { content; constructor(content) { if (Array.isArray(content)) { const rec = {}; content.forEach((s) => { let k; let v; if (Array.isArray(s)) { ; [k, v] = s; } else { k = Object.keys(s)[0]; v = Object.values(s)[0]; } rec[k] = v; }); this.content = rec; } else { this.content = { ...content }; } } size() { return this.keys().length; } keys() { return Object.keys(this.content); } values() { return Object.values(this.content); } iterable() { return this.keys().map((k) => [k, this.content[k]]); } map(c) { return this.keys().map((k, i) => c(k, this.content[k], i, { [k]: this.content[k] })); } sort(c) { return new DMap(this.iterable().sort(c)); } iterateSync(c) { this.keys().forEach((k, i) => { c(k, this.content[k], i, { [k]: this.content[k] }); }); } async iterate(c) { let i = 0; for (const [k, v] of this.iterable()) { await c(k, v, i, { [k]: this.content[k] }); i += 1; } } hasKey(c) { return this.keys().includes(c); } filter(matcher) { return new DMap(this.iterable().filter(([k, v]) => matcher(k, v))); } groupBy(keyLocator) { const grouped = {}; this.iterateSync((k, v, _, obj) => { const gKey = keyLocator(v); if (gKey in grouped) { grouped[gKey].push(obj); return; } grouped[gKey] = [obj]; }); return new DMap(grouped); } findKeys(matcher) { const matchedKey = []; this.keys().forEach((k) => { if (matcher(this.content[k])) matchedKey.push(k); }); return matchedKey; } findValues(matcher) { const matchedVal = []; this.values().forEach((v) => { if (matcher(v)) matchedVal.push(v); }); return matchedVal; } hasValue(c) { if (typeof c === 'object') { return (this.findValues((a) => Object.entries(a) .sort() .toString() === Object.entries(c) .sort() .toString()).length > 0); } return this.values().includes(c); } keyMatch(comparable) { const match = []; this.keys().forEach((k) => { if (comparable.includes(k)) match.push(k); }); return match; } keyDiff(comparable) { const diff = []; this.keys().forEach((k) => { if (!comparable.includes(k)) diff.push(k); }); return diff; } get(key) { return this.content[key]; } set(key, value) { this.content[key] = value; return; } insert(value) { const uuid = crypto.randomUUID(); const key = `temp-${uuid}`; this.set(key, value); return key; } getRecord() { return this.content; } isLive() { return false; } remove(key) { const del = this.content[key]; delete this.content[key]; return del; } } exports.DMap = DMap;