UNPKG

@sumor/short-id

Version:

This is a short-id library for Node.js and the browser. You can easily use it to generate a short id from number.

13 lines (12 loc) 272 B
export default (code, rule) => { const chars = rule const radix = chars.length code = String(code) const len = code.length let i = 0 let number = 0 while (i < len) { number += radix ** i++ * chars.indexOf(code.charAt(len - i) || 0) } return number }