Skip to content

Commit

Permalink
feat(char): Support generating chars within a codepoint range
Browse files Browse the repository at this point in the history
  • Loading branch information
justinvdm committed May 3, 2020
1 parent 784452a commit 534db3e
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 0 deletions.
47 changes: 47 additions & 0 deletions char.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var hash = require('./hash')
var conj = require('./utils/conj')
var defaults = require('./utils/defaults')
var fit = require('./utils/fit')
var fromCodePoint = require('./utils/fromCodePoint')

var DEFAULT_RANGES = [
// ascii lower
[0x61, 0x7a],

// ascii upper
[0x41, 0x5a],

// latin-1 supplement lower
[0xdf, 0xf6],
[0xf8, 0xff],

// latin-1 supplement upper
[0xc0, 0xd6],
[0xd8, 0xde],

// digits
[0x30, 0x39]
]

function char(input, opts) {
opts = opts || 0
var ranges = defaults(opts.ranges, DEFAULT_RANGES)

var id = hash([input, 'char', 0])
var range = ranges[id % ranges.length]

id = hash([id, 'char', 1])
return fromCodePoint(fit(id, range[0], range[1]))
}

char.options = function charOptions(opts) {
var base = this
charFn.options = char.options
return charFn

function charFn(input, overrides) {
return base(input, conj(opts, overrides))
}
}

module.exports = char
13 changes: 13 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ declare const float: Float

export { float }

export interface CharOptions {
ranges: [number, number][]
}

export interface Char {
(input: Input, options?: Partial<Char>): string
options(overrides?: Partial<Char>): this
}

declare const char: Char

export { char }

export interface WordOptions {
capitalize: boolean
minSyllables: number
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ exports.oneOfWeighted = require('./oneOfWeighted')

exports.int = require('./int')
exports.float = require('./float')
exports.char = require('./char')
exports.word = require('./word')
exports.words = require('./words')
exports.sentence = require('./sentence')
Expand Down
1 change: 1 addition & 0 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export { default as oneOfWeighted } from './oneOfWeighted'

export { default as int } from './int'
export { default as float } from './float'
export { default as char } from './char'
export { default as word } from './word'
export { default as words } from './words'
export { default as sentence } from './sentence'
Expand Down

0 comments on commit 534db3e

Please sign in to comment.