2.0.2 • Published 5 years ago

cdkey v2.0.2

Weekly downloads
9
License
MIT
Repository
github
Last release
5 years ago

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])
syntaxchars for randomexclude non-readable chars
00-901
AA-ZOI
aa-zl
X0-9 + A-Z01OI
x0-9 + a-z01l
?0-9 + A-Z + a-z01OIl
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.
attributetypedefault
options.charstring
options.lengthnumber
options.templatestring
options.syntaxobject
options.amountnumber1

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)

keycharlength
ALPHANUMERIC0-9 a-z A-Z32
ALPHABETICa-z A-Z32
NUMBER, NUMERIC0-932
UPPERA-Z32
LOWERa-z32
HEX0-9 A-F32
import { cdkey, NUMBER } from 'cdkey'

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

template + syntax (builtin options)

keytemplate
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

2.0.2

5 years ago

2.0.1

5 years ago

1.0.9

5 years ago

1.0.7

6 years ago

1.0.6

7 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.0.1

8 years ago