diff --git a/lib/make/colors.js b/lib/make/colors.js index 959b575..929a992 100644 --- a/lib/make/colors.js +++ b/lib/make/colors.js @@ -8,9 +8,9 @@ const random = require('../random') class colors extends make { static any () { return random.pick([ - this.rgb, - this.hsl, - this.keyword + colors.rgb, + colors.hsl, + colors.keyword ]) } diff --git a/lib/make/command.js b/lib/make/command.js index 2648747..d6827be 100644 --- a/lib/make/command.js +++ b/lib/make/command.js @@ -58,11 +58,11 @@ class command extends make { } static name () { - return random.item(Object.keys(this.data())) + return random.item(Object.keys(command.data())) } static value (value) { - return random.pick(this.data()[name]) + return random.pick(command.data()[name]) } } diff --git a/lib/make/files.js b/lib/make/files.js index 76aac78..8b80d35 100644 --- a/lib/make/files.js +++ b/lib/make/files.js @@ -43,9 +43,9 @@ class files extends make { static any () { return random.pick([ - this.image, - this.video, - this.audio + files.image, + files.video, + files.audio ]) } } diff --git a/lib/make/fonts.js b/lib/make/fonts.js index 402812d..16ec092 100644 --- a/lib/make/fonts.js +++ b/lib/make/fonts.js @@ -56,9 +56,9 @@ class font extends make { } static family () { - let s = random.pick(this.familyName) + let s = random.pick(font.familyName) if (random.chance(8)) { - s += ', ' + random.pick(this.genericFamily) + s += ', ' + random.pick(font.genericFamily) } return s } @@ -86,20 +86,20 @@ class font extends make { static font () { let s = '' if (random.chance(4)) { - s += random.pick(this.style) + ' ' + s += random.pick(font.style) + ' ' } if (random.chance(4)) { - s += random.pick(this.variant) + ' ' + s += random.pick(font.variant) + ' ' } if (random.chance(4)) { - s += random.pick(this.weight) + ' ' + s += random.pick(font.weight) + ' ' } if (random.chance(4)) { s += make.number.any() + '/' } - s += this.size() + s += font.size() s += ' ' - s += this.family() + s += font.family() return s } } diff --git a/lib/make/network.js b/lib/make/network.js index c7ebe02..a1a16aa 100644 --- a/lib/make/network.js +++ b/lib/make/network.js @@ -56,7 +56,7 @@ class network extends make { ' ', random.pick([make.number.any]), ' ', - random.pick([this.goodHostnames]), + random.pick([network.goodHostnames]), ' ', random.pick([56187, make.number.any]), ' ', @@ -69,7 +69,7 @@ class network extends make { ' ', random.pick(['raddr']), ' ', - random.pick([this.goodHostnames]), + random.pick([network.goodHostnames]), ' ', random.pick(['rport']), 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 return utils.block.block([ // scheme - random.pick(this.PeerConnectionProtocols), + random.pick(network.PeerConnectionProtocols), ':', // turn-host random.pick([ - this.any, - this.hostname + network.any, + network.hostname ]), // turn-port random.use([utils.block.block([':', make.number.any])]), @@ -172,10 +172,10 @@ class network extends make { static hostname () { return random.pick([ - this.randomIPv4, - this.randomIPv6, - this.goodHostnames, - this.badHostnames + network.randomIPv4, + network.randomIPv6, + network.goodHostnames, + network.badHostnames ]) } diff --git a/lib/make/text.js b/lib/make/text.js index 9e0737a..e1ee21b 100644 --- a/lib/make/text.js +++ b/lib/make/text.js @@ -219,17 +219,17 @@ class text extends make { } static quotedString () { - return utils.common.quote(this.any()) + return utils.common.quote(text.any()) } static chars () { return random.pick([ - this.controlChar, - this.token, - this.assignmentOperator, - this.arithmeticOperator, - String.fromCharCode(this.layoutCharCodes()), - String.fromCharCode(this.bidiCharCodes()) + text.controlChar, + text.token, + text.assignmentOperator, + text.arithmeticOperator, + String.fromCharCode(text.layoutCharCodes()), + String.fromCharCode(text.bidiCharCodes()) ]) } diff --git a/lib/make/typed.js b/lib/make/typed.js index 3b8fdb1..5f957a6 100644 --- a/lib/make/typed.js +++ b/lib/make/typed.js @@ -78,27 +78,27 @@ class typed extends make { static any () { return random.choose([ - [1, [this.byte, this.octet]], - [1, [this.short, this.unsignedShort]], - [1, [this.long, this.unsignedLong]], - [1, [this.float, this.unrestrictedFloat]], - [1, [this.double, this.unrestrictedDouble]], + [1, [typed.byte, typed.octet]], + [1, [typed.short, typed.unsignedShort]], + [1, [typed.long, typed.unsignedLong]], + [1, [typed.float, typed.unrestrictedFloat]], + [1, [typed.double, typed.unrestrictedDouble]], [1, [make.number.range, make.number.tiny]] ]) } static arrayBuffer (byteLength = null) { - let length = (byteLength !== null) ? byteLength : this.unsignedShort() + let length = (byteLength !== null) ? byteLength : typed.unsignedShort() return 'new ArrayBuffer(' + length + ')' } static dataView (byteLength = null) { - let length = (byteLength !== null) ? byteLength : this.unsignedShort() - return 'new DataView(' + this.arrayBuffer(length) + ')' + let length = (byteLength !== null) ? byteLength : typed.unsignedShort() + return 'new DataView(' + typed.arrayBuffer(length) + ')' } static typedArray (byteLength = null) { - let length = (byteLength !== null) ? byteLength : this.unsignedShort() + let length = (byteLength !== null) ? byteLength : typed.unsignedShort() let arrType = random.item([ 'Int8', 'Uint8', 'Uint8Clamped', 'Int16', 'Uint16', @@ -119,11 +119,11 @@ class typed extends make { static bufferSource () { switch (random.number(4)) { case 0: - return this.arrayBuffer() + return typed.arrayBuffer() case 1: - return this.dataView() + return typed.dataView() default: - return this.typedArray() + return typed.typedArray() } } } diff --git a/lib/make/uri.js b/lib/make/uri.js index d65f2d9..cfa9fa3 100644 --- a/lib/make/uri.js +++ b/lib/make/uri.js @@ -44,9 +44,9 @@ class uri extends make { static any () { return random.choose([ - [1, this.problematic], - [10, this.standard], - [10, this.namespace], + [1, uri.problematic], + [10, uri.standard], + [10, uri.namespace], [10, make.files.any] ]) } diff --git a/lib/make/webgl.js b/lib/make/webgl.js index 828bbe7..bab4599 100644 --- a/lib/make/webgl.js +++ b/lib/make/webgl.js @@ -41,9 +41,9 @@ class webgl extends make { } static WebGLFormat () { - let internalformat = random.item(Object.keys(this.internalFormat)) - let format = this.internalFormat[internalformat].format - let type = random.item(this.internalFormat[internalformat].type) + let internalformat = random.item(Object.keys(webgl.internalFormat)) + let format = webgl.internalFormat[internalformat].format + let type = random.item(webgl.internalFormat[internalformat].type) return [internalformat, format, type] } diff --git a/lib/random/random.js b/lib/random/random.js index 541891f..63a1f61 100644 --- a/lib/random/random.js +++ b/lib/random/random.js @@ -12,10 +12,10 @@ class random { */ static init (seed) { if (seed === null || seed === undefined) { - this.seed = new Date().getTime() + random.seed = new Date().getTime() } - this.twister = new MersenneTwister() - this.twister.seed(this.seed) + random.twister = new MersenneTwister() + random.twister.seed(random.seed) } /** @@ -23,6 +23,9 @@ class random { * @param limit */ static number (limit) { + if (!random.twister) { + throw new Error('random.init must be called first.') + } if (limit === null || limit === undefined) { limit = 0xffffffff } @@ -30,7 +33,7 @@ class random { let y = (x * limit) >>> 0 let r do { - r = this.twister.int32() + r = random.twister.int32() } while (y && r >= y) // eslint-disable-line no-unmodified-loop-condition return (r / x) >>> 0 } @@ -39,7 +42,10 @@ class random { * Returns a float in [0, 1). Uniform distribution. */ 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 */ static range (start, limit) { + if (!random.twister) { + throw new Error('random.init must be called first.') + } if (isNaN(start) || isNaN(limit)) { logger.traceback() 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 */ static ludOneTo (limit) { - return Math.exp(this.float() * Math.log(limit)) + return Math.exp(random.float() * Math.log(limit)) } static item (list) { @@ -68,7 +77,7 @@ class random { logger.traceback() 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) { list.push(i) } - return this.item(list) + return random.item(list) } /** * Return a random Boolean value. */ static bool () { - return this.item([true, false]) + return random.item([true, false]) } static pick (obj) { @@ -95,7 +104,7 @@ class random { return obj() } if (Array.isArray(obj)) { - return this.pick(this.item(obj)) + return random.pick(random.item(obj)) } return obj } @@ -108,7 +117,7 @@ class random { logger.traceback() throw new TypeError(`random.chance() received non-number type: (${limit})`) } - return this.number(limit) === 1 + return random.number(limit) === 1 } static choose (list, flat) { @@ -120,13 +129,13 @@ class random { for (let i = 0; i < list.length; i++) { total += list[i][0] } - let n = this.number(total) + let n = random.number(total) for (let i = 0; i < list.length; i++) { if (n < list[i][0]) { if (flat === true) { return list[i][1] } else { - return this.pick([list[i][1]]) + return random.pick([list[i][1]]) } } n = n - list[i][0] @@ -134,7 +143,7 @@ class random { if (flat === true) { 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) { - return this.bool() ? obj : '' + return random.bool() ? obj : '' } static shuffle (arr) { let i = arr.length while (i--) { - let p = this.number(i + 1) + let p = random.number(i + 1) let t = arr[i] arr[i] = arr[p] arr[p] = t @@ -167,7 +176,7 @@ class random { static shuffled (arr) { let newArray = arr.slice() - this.shuffle(newArray) + random.shuffle(newArray) return newArray } @@ -177,11 +186,11 @@ class random { throw new TypeError(`random.subset() received non-array type: (${list})`) } if (typeof limit !== 'number') { - limit = this.number(list.length + 1) + limit = random.number(list.length + 1) } let result = [] for (let i = 0; i < limit; i++) { - result.push(this.pick(list)) + result.push(random.pick(list)) } return result } @@ -193,7 +202,7 @@ class random { static pop (arr) { let i, obj - i = this.number(arr.length) + i = random.number(arr.length) obj = arr[i] arr.splice(i, 1) @@ -201,7 +210,7 @@ class random { } static hex (len) { - return this.number(Math.pow(2, len * 4)).toString(16) + return random.number(Math.pow(2, len * 4)).toString(16) } }