UNPKG

stackpress

Version:

Incept is a content management framework.

84 lines (83 loc) 2.4 kB
import crypto from 'node:crypto'; import mustache from 'mustache'; export const generators = [ 'cuid()', 'nano()', 'random()', 'now()' ]; export function camelize(string) { return lowerize(string.trim() .replace(/[^a-zA-Z0-9]/g, '_') .replace(/_{2,}/g, '_') .replace(/^_+|_+$/g, '') .replace(/([-_][a-z0-9])/ig, ($1) => { return $1.toUpperCase() .replace('-', '') .replace('_', ''); })); } export function capitalize(word) { return word.charAt(0).toUpperCase() + word.slice(1); } export function dasherize(string) { return string.trim() .replace(/[^a-zA-Z0-9]/g, '-') .replace(/-{2,}/g, '-') .replace(/^-+|-+$/g, '') .replace(/([a-z])([A-Z0-9])/g, '$1-$2') .toLowerCase(); } export function decrypt(encrypted, seed) { const value = Buffer.from(encrypted, 'hex'); const hash = crypto .createHash('sha256') .update(seed) .digest('base64') .substring(0, 32); const iv = hash.substring(0, 16); const decipher = crypto.createDecipheriv('aes-256-cbc', hash, iv); return Buffer.concat([ decipher.update(value), decipher.final() ]).toString(); } export function encrypt(value, seed) { const hash = crypto .createHash('sha256') .update(seed) .digest('base64') .substring(0, 32); const iv = hash.substring(0, 16); const cipher = crypto.createCipheriv('aes-256-cbc', hash, iv); const encrypted = Buffer.concat([ cipher.update(value), cipher.final() ]); return encrypted.toString('hex'); } export function hash(string) { return crypto .createHash('shake256') .update(string) .digest('hex'); } export function lowerize(word) { return word.charAt(0).toLowerCase() + word.slice(1); } export function snakerize(string) { return string.trim() .replace(/[^a-zA-Z0-9]/g, '_') .replace(/-{2,}/g, '_') .replace(/^_+|_+$/g, '') .replace(/([a-z])([A-Z0-9])/g, '$1_$2') .toLowerCase(); } export function render(template, data = {}) { return mustache.render(template, data); } export function objectToAttributeString(attributes) { return Object.entries(attributes).map(([key, value]) => { return `${key}={${JSON.stringify(value)}}`; }).join(' '); }