cdkey

Generate random strings by template.

Usage no npm install needed!

<script type="module">
  import cdkey from 'https://cdn.skypack.dev/cdkey';
</script>

README

node-cdkey

Build Status Coverage Status

Generate random string by template.

Installation

npm i cdkey

Basic Usage

Browser

<script src="https://unpkg.com/cdkey/dist/cdkey.min.js"></script>
<script>
  console.log(cdkey.default()) // eC8q-8ERg-fTZa-Vh2o
</script>

Node.js

import cdkey from 'cdkey' // esm

const cdkey = require('cdkey/lib').default // cjs

console.log(cdkey()) // eC8q-8ERg-fTZa-Vh2o

Advenced Usage

Simple amount

Generate string by default template (diablo style).

cdkey([Number amount])
cdkey() // eC8q-8ERg-fTZa-Vh2o
cdkey(2) // [ 'kcsi-V5xR-1xv8-zq7q', 'cumh-jYVn-5vL9-mwLM' ]

Template

Generate string by custom template.

cdkey(String template, [Number amount], [Object syntax])
syntax chars for random exclude non-readable chars
0 [0-9] [01]
A [A-Z] [OI]
a [a-z] [l]
X [0-9] + [A-Z] [01OI]
x [0-9] + [a-z] [01l]
? [0-9] + [A-Z] + [a-z] [01OIl]
cdkey('XXXX') // 7F3K
cdkey('????', 2) // [ 'cUwc', 'n9zu' ]

cdkey('AAAA', { A: 'AB' }) // ABBA
cdkey('cccc', 2, { c: 'ABC' }) // [ 'BCAA', 'ACAB' ]

Options

Generate string by custom or builtin options.

cdkey(Object options, [Number amount, [String template | Number length]])
  • The 2ed argument would override options.amount
  • The 3rd argument would override options.template or options.length, depand on style.
attribute type default
options.char string
options.length number
options.template string
options.syntax object
options.amount number 1

char + length (custom)

cdkey({
  char: 'abc',
  length: 4
})
// acab

cdkey({
  char: 'abc',
  length: 4,
  amount: 2
}, 1, 3)
// aca

template + syntax (custom)

cdkey({
  template: 'aaaa',
  syntax: {
    a: '012'
  },
  amount: 2
})
// [ "1220", "2001" ]

cdkey({
  template: '0000',
  syntax: {
    a: '012'
  },
  amount: 5
}, 2, 'aaaa')
// [ "1220", "2001" ]

char + length (builtin options)

key char length
ALPHANUMERIC [0-9 a-z A-Z] 32
ALPHABETIC [a-z A-Z] 32
NUMBER, NUMERIC [0-9] 32
UPPER [A-Z] 32
LOWER [a-z] 32
HEX [0-9 A-F] 32
import { cdkey, NUMBER } from 'cdkey'

cdkey(NUMBER) // 22030189956236488846744098007707
cdkey(NUMBER, 2, 8) // [ '05250373', '42852368' ]

template + syntax (builtin options)

key template
DEFAULT '????-????-????-????'
DIABLO 'XXXX-XXXX-XXXX-XXXX'
import { cdkey, DIABLO } from 'cdkey'

cdkey(DIABLO, 2) // [ '2F2L-HJTG-P4L6-QBTZ', 'F1XM-K9JZ-ED9L-EPL9' ]

Fluent (chain methods)

Generate string by custom options and chain methods.

import { create } from 'cdkey'

create()
  .char(String chars)
  .length(Number length)
  .template(String template)
  .syntax(Object syntax)
  .amount(Number amount)
  .gen()
create()
  .char('012')
  .length(8)
  .gen()
// 01201002

create()
  .template('AAAA')
  .syntax({ A: 'ABC' })
  .amount(2)
  .gen()
// [ 'BCAA', 'ACAB' ]

Helper methods

import { syntax } from 'cdkey'
syntax() // To get default syntax object.
// {
//   '0': '23456789',
//   'A': 'ABCDEFGHJKLMNPQRSTUVWXYZ',
//   'a': 'abcdefghijkmnopqrstuvwxyz',
//   'X': '23456789ABCDEFGHJKLMNPQRSTUVWXYZ',
//   'x': '23456789abcdefghijkmnopqrstuvwxyz',
//   '?': '23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
// }

TODO

  • escape string in template.
  • command line support.
  • browser support.

License

MIT