Fix various modules using this

This commit is contained in:
Christoph Diehl 2018-04-07 00:15:55 +02:00
parent c9e1987eb1
commit fc53bca173
No known key found for this signature in database
GPG Key ID: 799CE5B68FEF404A
10 changed files with 80 additions and 71 deletions

View File

@ -8,9 +8,9 @@ const random = require('../random')
class colors extends make { class colors extends make {
static any () { static any () {
return random.pick([ return random.pick([
this.rgb, colors.rgb,
this.hsl, colors.hsl,
this.keyword colors.keyword
]) ])
} }

View File

@ -58,11 +58,11 @@ class command extends make {
} }
static name () { static name () {
return random.item(Object.keys(this.data())) return random.item(Object.keys(command.data()))
} }
static value (value) { static value (value) {
return random.pick(this.data()[name]) return random.pick(command.data()[name])
} }
} }

View File

@ -43,9 +43,9 @@ class files extends make {
static any () { static any () {
return random.pick([ return random.pick([
this.image, files.image,
this.video, files.video,
this.audio files.audio
]) ])
} }
} }

View File

@ -56,9 +56,9 @@ class font extends make {
} }
static family () { static family () {
let s = random.pick(this.familyName) let s = random.pick(font.familyName)
if (random.chance(8)) { if (random.chance(8)) {
s += ', ' + random.pick(this.genericFamily) s += ', ' + random.pick(font.genericFamily)
} }
return s return s
} }
@ -86,20 +86,20 @@ class font extends make {
static font () { static font () {
let s = '' let s = ''
if (random.chance(4)) { if (random.chance(4)) {
s += random.pick(this.style) + ' ' s += random.pick(font.style) + ' '
} }
if (random.chance(4)) { if (random.chance(4)) {
s += random.pick(this.variant) + ' ' s += random.pick(font.variant) + ' '
} }
if (random.chance(4)) { if (random.chance(4)) {
s += random.pick(this.weight) + ' ' s += random.pick(font.weight) + ' '
} }
if (random.chance(4)) { if (random.chance(4)) {
s += make.number.any() + '/' s += make.number.any() + '/'
} }
s += this.size() s += font.size()
s += ' ' s += ' '
s += this.family() s += font.family()
return s return s
} }
} }

View File

@ -56,7 +56,7 @@ class network extends make {
' ', ' ',
random.pick([make.number.any]), random.pick([make.number.any]),
' ', ' ',
random.pick([this.goodHostnames]), random.pick([network.goodHostnames]),
' ', ' ',
random.pick([56187, make.number.any]), random.pick([56187, make.number.any]),
' ', ' ',
@ -69,7 +69,7 @@ class network extends make {
' ', ' ',
random.pick(['raddr']), random.pick(['raddr']),
' ', ' ',
random.pick([this.goodHostnames]), random.pick([network.goodHostnames]),
' ', ' ',
random.pick(['rport']), random.pick(['rport']),
random.use([utils.block.block([' ', make.number.any])]) random.use([utils.block.block([' ', make.number.any])])
@ -95,12 +95,12 @@ class network extends make {
// https://tools.ietf.org/html/rfc7065#section-3.1 // https://tools.ietf.org/html/rfc7065#section-3.1
return utils.block.block([ return utils.block.block([
// scheme // scheme
random.pick(this.PeerConnectionProtocols), random.pick(network.PeerConnectionProtocols),
':', ':',
// turn-host // turn-host
random.pick([ random.pick([
this.any, network.any,
this.hostname network.hostname
]), ]),
// turn-port // turn-port
random.use([utils.block.block([':', make.number.any])]), random.use([utils.block.block([':', make.number.any])]),
@ -172,10 +172,10 @@ class network extends make {
static hostname () { static hostname () {
return random.pick([ return random.pick([
this.randomIPv4, network.randomIPv4,
this.randomIPv6, network.randomIPv6,
this.goodHostnames, network.goodHostnames,
this.badHostnames network.badHostnames
]) ])
} }

View File

@ -219,17 +219,17 @@ class text extends make {
} }
static quotedString () { static quotedString () {
return utils.common.quote(this.any()) return utils.common.quote(text.any())
} }
static chars () { static chars () {
return random.pick([ return random.pick([
this.controlChar, text.controlChar,
this.token, text.token,
this.assignmentOperator, text.assignmentOperator,
this.arithmeticOperator, text.arithmeticOperator,
String.fromCharCode(this.layoutCharCodes()), String.fromCharCode(text.layoutCharCodes()),
String.fromCharCode(this.bidiCharCodes()) String.fromCharCode(text.bidiCharCodes())
]) ])
} }

View File

@ -78,27 +78,27 @@ class typed extends make {
static any () { static any () {
return random.choose([ return random.choose([
[1, [this.byte, this.octet]], [1, [typed.byte, typed.octet]],
[1, [this.short, this.unsignedShort]], [1, [typed.short, typed.unsignedShort]],
[1, [this.long, this.unsignedLong]], [1, [typed.long, typed.unsignedLong]],
[1, [this.float, this.unrestrictedFloat]], [1, [typed.float, typed.unrestrictedFloat]],
[1, [this.double, this.unrestrictedDouble]], [1, [typed.double, typed.unrestrictedDouble]],
[1, [make.number.range, make.number.tiny]] [1, [make.number.range, make.number.tiny]]
]) ])
} }
static arrayBuffer (byteLength = null) { static arrayBuffer (byteLength = null) {
let length = (byteLength !== null) ? byteLength : this.unsignedShort() let length = (byteLength !== null) ? byteLength : typed.unsignedShort()
return 'new ArrayBuffer(' + length + ')' return 'new ArrayBuffer(' + length + ')'
} }
static dataView (byteLength = null) { static dataView (byteLength = null) {
let length = (byteLength !== null) ? byteLength : this.unsignedShort() let length = (byteLength !== null) ? byteLength : typed.unsignedShort()
return 'new DataView(' + this.arrayBuffer(length) + ')' return 'new DataView(' + typed.arrayBuffer(length) + ')'
} }
static typedArray (byteLength = null) { static typedArray (byteLength = null) {
let length = (byteLength !== null) ? byteLength : this.unsignedShort() let length = (byteLength !== null) ? byteLength : typed.unsignedShort()
let arrType = random.item([ let arrType = random.item([
'Int8', 'Uint8', 'Uint8Clamped', 'Int8', 'Uint8', 'Uint8Clamped',
'Int16', 'Uint16', 'Int16', 'Uint16',
@ -119,11 +119,11 @@ class typed extends make {
static bufferSource () { static bufferSource () {
switch (random.number(4)) { switch (random.number(4)) {
case 0: case 0:
return this.arrayBuffer() return typed.arrayBuffer()
case 1: case 1:
return this.dataView() return typed.dataView()
default: default:
return this.typedArray() return typed.typedArray()
} }
} }
} }

View File

@ -44,9 +44,9 @@ class uri extends make {
static any () { static any () {
return random.choose([ return random.choose([
[1, this.problematic], [1, uri.problematic],
[10, this.standard], [10, uri.standard],
[10, this.namespace], [10, uri.namespace],
[10, make.files.any] [10, make.files.any]
]) ])
} }

View File

@ -41,9 +41,9 @@ class webgl extends make {
} }
static WebGLFormat () { static WebGLFormat () {
let internalformat = random.item(Object.keys(this.internalFormat)) let internalformat = random.item(Object.keys(webgl.internalFormat))
let format = this.internalFormat[internalformat].format let format = webgl.internalFormat[internalformat].format
let type = random.item(this.internalFormat[internalformat].type) let type = random.item(webgl.internalFormat[internalformat].type)
return [internalformat, format, type] return [internalformat, format, type]
} }

View File

@ -12,10 +12,10 @@ class random {
*/ */
static init (seed) { static init (seed) {
if (seed === null || seed === undefined) { if (seed === null || seed === undefined) {
this.seed = new Date().getTime() random.seed = new Date().getTime()
} }
this.twister = new MersenneTwister() random.twister = new MersenneTwister()
this.twister.seed(this.seed) random.twister.seed(random.seed)
} }
/** /**
@ -23,6 +23,9 @@ class random {
* @param limit * @param limit
*/ */
static number (limit) { static number (limit) {
if (!random.twister) {
throw new Error('random.init must be called first.')
}
if (limit === null || limit === undefined) { if (limit === null || limit === undefined) {
limit = 0xffffffff limit = 0xffffffff
} }
@ -30,7 +33,7 @@ class random {
let y = (x * limit) >>> 0 let y = (x * limit) >>> 0
let r let r
do { do {
r = this.twister.int32() r = random.twister.int32()
} while (y && r >= y) // eslint-disable-line no-unmodified-loop-condition } while (y && r >= y) // eslint-disable-line no-unmodified-loop-condition
return (r / x) >>> 0 return (r / x) >>> 0
} }
@ -39,7 +42,10 @@ class random {
* Returns a float in [0, 1). Uniform distribution. * Returns a float in [0, 1). Uniform distribution.
*/ */
static float () { static float () {
return this.twister.real2() if (!random.twister) {
throw new Error('random.init must be called first.')
}
return random.twister.real2()
} }
/** /**
@ -48,11 +54,14 @@ class random {
* @param limit * @param limit
*/ */
static range (start, limit) { static range (start, limit) {
if (!random.twister) {
throw new Error('random.init must be called first.')
}
if (isNaN(start) || isNaN(limit)) { if (isNaN(start) || isNaN(limit)) {
logger.traceback() logger.traceback()
throw new TypeError(`random.range() received non-number type: (${start}, ${limit})`) throw new TypeError(`random.range() received non-number type: (${start}, ${limit})`)
} }
return this.number(limit - start + 1) + start return random.number(limit - start + 1) + start
} }
/** /**
@ -60,7 +69,7 @@ class random {
* @param {*} limit * @param {*} limit
*/ */
static ludOneTo (limit) { static ludOneTo (limit) {
return Math.exp(this.float() * Math.log(limit)) return Math.exp(random.float() * Math.log(limit))
} }
static item (list) { static item (list) {
@ -68,7 +77,7 @@ class random {
logger.traceback() logger.traceback()
throw new TypeError(`random.item() received invalid object: (${list})`) throw new TypeError(`random.item() received invalid object: (${list})`)
} }
return list[this.number(list.length)] return list[random.number(list.length)]
} }
/** /**
@ -80,14 +89,14 @@ class random {
for (let i in obj) { for (let i in obj) {
list.push(i) list.push(i)
} }
return this.item(list) return random.item(list)
} }
/** /**
* Return a random Boolean value. * Return a random Boolean value.
*/ */
static bool () { static bool () {
return this.item([true, false]) return random.item([true, false])
} }
static pick (obj) { static pick (obj) {
@ -95,7 +104,7 @@ class random {
return obj() return obj()
} }
if (Array.isArray(obj)) { if (Array.isArray(obj)) {
return this.pick(this.item(obj)) return random.pick(random.item(obj))
} }
return obj return obj
} }
@ -108,7 +117,7 @@ class random {
logger.traceback() logger.traceback()
throw new TypeError(`random.chance() received non-number type: (${limit})`) throw new TypeError(`random.chance() received non-number type: (${limit})`)
} }
return this.number(limit) === 1 return random.number(limit) === 1
} }
static choose (list, flat) { static choose (list, flat) {
@ -120,13 +129,13 @@ class random {
for (let i = 0; i < list.length; i++) { for (let i = 0; i < list.length; i++) {
total += list[i][0] total += list[i][0]
} }
let n = this.number(total) let n = random.number(total)
for (let i = 0; i < list.length; i++) { for (let i = 0; i < list.length; i++) {
if (n < list[i][0]) { if (n < list[i][0]) {
if (flat === true) { if (flat === true) {
return list[i][1] return list[i][1]
} else { } else {
return this.pick([list[i][1]]) return random.pick([list[i][1]])
} }
} }
n = n - list[i][0] n = n - list[i][0]
@ -134,7 +143,7 @@ class random {
if (flat === true) { if (flat === true) {
return list[0][1] return list[0][1]
} }
return this.pick([list[0][1]]) return random.pick([list[0][1]])
} }
/** /**
@ -152,13 +161,13 @@ class random {
} }
static use (obj) { static use (obj) {
return this.bool() ? obj : '' return random.bool() ? obj : ''
} }
static shuffle (arr) { static shuffle (arr) {
let i = arr.length let i = arr.length
while (i--) { while (i--) {
let p = this.number(i + 1) let p = random.number(i + 1)
let t = arr[i] let t = arr[i]
arr[i] = arr[p] arr[i] = arr[p]
arr[p] = t arr[p] = t
@ -167,7 +176,7 @@ class random {
static shuffled (arr) { static shuffled (arr) {
let newArray = arr.slice() let newArray = arr.slice()
this.shuffle(newArray) random.shuffle(newArray)
return newArray return newArray
} }
@ -177,11 +186,11 @@ class random {
throw new TypeError(`random.subset() received non-array type: (${list})`) throw new TypeError(`random.subset() received non-array type: (${list})`)
} }
if (typeof limit !== 'number') { if (typeof limit !== 'number') {
limit = this.number(list.length + 1) limit = random.number(list.length + 1)
} }
let result = [] let result = []
for (let i = 0; i < limit; i++) { for (let i = 0; i < limit; i++) {
result.push(this.pick(list)) result.push(random.pick(list))
} }
return result return result
} }
@ -193,7 +202,7 @@ class random {
static pop (arr) { static pop (arr) {
let i, obj let i, obj
i = this.number(arr.length) i = random.number(arr.length)
obj = arr[i] obj = arr[i]
arr.splice(i, 1) arr.splice(i, 1)
@ -201,7 +210,7 @@ class random {
} }
static hex (len) { static hex (len) {
return this.number(Math.pow(2, len * 4)).toString(16) return random.number(Math.pow(2, len * 4)).toString(16)
} }
} }