UNPKG

jdb-lite

Version:

A lightweight schema-based JSON database library with validation

33 lines 886 B
/** * Simple ObjectId implementation for unique document IDs */ export class ObjectId { constructor(id) { if (id && ObjectId.isValid(id)) { this._id = id; } else { this._id = this.generateId(); } } generateId() { const timestamp = Math.floor(Date.now() / 1000).toString(16); const randomBytes = Array.from({ length: 16 }, () => Math.floor(Math.random() * 256).toString(16).padStart(2, '0')).join(''); return timestamp + randomBytes.substring(0, 16); } toString() { return this._id; } toHexString() { return this._id; } static isValid(id) { if (typeof id !== 'string') return false; return /^[0-9a-fA-F]{24}$/.test(id); } static generate() { return new ObjectId(); } } //# sourceMappingURL=objectid.js.map