UNPKG

node-appwrite

Version:

Appwrite is an open-source self-hosted backend server that abstract and simplify complex and repetitive development tasks behind a very simple REST API

33 lines (26 loc) 929 B
class ID { // Generate an hex ID based on timestamp // Recreated from https://www.php.net/manual/en/function.uniqid.php static #hexTimestamp = () => { const now = new Date(); const sec = Math.floor(now.getTime() / 1000); const msec = now.getMilliseconds(); // Convert to hexadecimal const hexTimestamp = sec.toString(16) + msec.toString(16).padStart(5, '0'); return hexTimestamp; } // Generate a unique ID with padding to have a longer ID static unique = (padding = 7) => { const baseId = ID.#hexTimestamp(); let randomPadding = ''; for (let i = 0; i < padding; i++) { const randomHexDigit = Math.floor(Math.random() * 16).toString(16); randomPadding += randomHexDigit; } return baseId + randomPadding; } static custom = (id) => { return id } } module.exports = ID;