Merge pull request #1 from MozillaSecurity/pyoor-dev

Minor improvements
This commit is contained in:
pyoor 2018-07-26 19:45:17 -04:00 committed by GitHub
commit b2a73e5130
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 98 additions and 38 deletions

View file

@ -141,11 +141,13 @@ class network extends make {
} }
static dtmf () { static dtmf () {
return random.subset([ let count = make.number.range()
'*', '#', const values = []
'A', 'B', 'C', 'D', while (count--) {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9' values.push(random.item(['*', '#', 'A', 'B', 'C', 'D', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']))
], make.number.range()).join('') }
return values.join('')
} }
static goodHostnames () { static goodHostnames () {

View file

@ -72,7 +72,7 @@ class webgl extends make {
static parseUniforms (shader, group = 1) { static parseUniforms (shader, group = 1) {
/* Todo: Parse their individual data types into categories. */ /* Todo: Parse their individual data types into categories. */
return webgl.match(shader, /uniform .+? (\w+)(?=[\[;])/gm, group) /* eslint-disable-line no-useless-escape */ return webgl.match(shader, /uniform .+? (\w+)(?=[\[;])/gm, group) // eslint-disable-line no-useless-escape
} }
static parseAttributes (shader, group = 1) { static parseAttributes (shader, group = 1) {
@ -90,7 +90,7 @@ class webgl extends make {
static parseFragDatav3 (shader, group = 1) { static parseFragDatav3 (shader, group = 1) {
// #version 300 // #version 300
return webgl.match(shader, /out .+? (\w+)(?=[\[;])/gm, group) /* eslint-disable-line no-useless-escape */ return webgl.match(shader, /out .+? (\w+)(?=[\[;])/gm, group) // eslint-disable-line no-useless-escape
} }
static parseFrag (shader, group = 1) { static parseFrag (shader, group = 1) {
@ -102,7 +102,12 @@ class webgl extends make {
} }
static randomBitmask () { static randomBitmask () {
return parseInt((random.subset([1, 0], 8).join(''))) const values = []
for (let i = 0; i < 8; i++) {
values.push(random.item([1, 0]))
}
return parseInt(values.join(''))
} }
static randomBufferTarget (isWebGL2) { static randomBufferTarget (isWebGL2) {

View file

@ -7,28 +7,31 @@ const {logger} = require('../logging')
class random { class random {
/** /**
* Must be called before any other methods can be called to initialize MersenneTwister. * Must be called before any other methods can be called to initialize MersenneTwister
* @param {number|null|undefined} seed Value to initialize MersenneTwister. * @param {number|null|undefined} seed - Value to initialize MersenneTwister
*/ */
static init (seed) { static init (seed) {
if (seed === null || seed === undefined) { if (seed === null || seed === undefined) {
random.seed = new Date().getTime() random.seed = new Date().getTime()
} }
random.twister = new MersenneTwister() random.twister = new MersenneTwister()
random.twister.seed(random.seed) random.twister.seed(random.seed)
} }
/** /**
* Returns an integer in [0, limit). Uniform distribution. * Returns an integer in [0, limit) (uniform distribution)
* @param limit * @param {number} limit
*/ */
static number (limit) { static number (limit) {
if (!random.twister) { if (!random.twister) {
throw new Error('random.init must be called first.') throw new Error('random.init must be called first.')
} }
if (limit === null || limit === undefined) { if (limit === null || limit === undefined) {
limit = 0xffffffff limit = 0xffffffff
} }
let x = (0x100000000 / limit) >>> 0 let x = (0x100000000 / limit) >>> 0
let y = (x * limit) >>> 0 let y = (x * limit) >>> 0
let r let r
@ -39,96 +42,123 @@ class random {
} }
/** /**
* Returns a float in [0, 1). Uniform distribution. * Returns a float in [0, 1) (uniform distribution)
*/ */
static float () { static float () {
if (!random.twister) { if (!random.twister) {
throw new Error('random.init must be called first.') throw new Error('random.init must be called first.')
} }
return random.twister.real2() return random.twister.real2()
} }
/** /**
* Returns an integer in [start, limit]. Uniform distribution. * Returns an integer in [start, limit) (uniform distribution)
* @param start * @param {number} start
* @param limit * @param {number} limit
*/ */
static range (start, limit) { static range (start, limit) {
if (!random.twister) { if (!random.twister) {
throw new Error('random.init must be called first.') 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 random.number(limit - start + 1) + start return random.number(limit - start + 1) + start
} }
/** /**
* Returns a float in [1, limit]. The logarithm has uniform distribution. * Returns a float in [1, limit). The logarithm has uniform distribution.
* @param {*} limit * @param {number} limit
*/ */
static ludOneTo (limit) { static ludOneTo (limit) {
return Math.exp(random.float() * Math.log(limit)) return Math.exp(random.float() * Math.log(limit))
} }
/**
* Returns a random index from a list
* @param {Array} list
* @returns list[n]
*/
static item (list) { static item (list) {
if (list === undefined || typeof list === 'string' || list.length === undefined) { if (!Array.isArray(list)) {
logger.traceback() logger.traceback()
throw new TypeError(`random.item() received invalid object: (${list})`) throw new TypeError(`random.item() received invalid object: (${list})`)
} }
return list[random.number(list.length)] return list[random.number(list.length)]
} }
/** /**
* Returns a random key of a provided object. * Returns a random key of a provided object
* @param {*} obj * @param {Object} obj
*/ */
static key (obj) { static key (obj) {
let list = [] return random.item(Object.keys(obj))
for (let i in obj) {
list.push(i)
}
return random.item(list)
} }
/** /**
* Return a random Boolean value. * Return a random Boolean value
*/ */
static bool () { static bool () {
return random.item([true, false]) return random.item([true, false])
} }
/**
* Recursively iterate over array until non-array item identified
* If item is a function, evaluate it with no args
* @param {*} obj
* @returns {*}
*/
static pick (obj) { static pick (obj) {
if (!(Array.isArray(obj) || typeof obj === 'function')) {
logger.traceback()
throw new TypeError(`random.pick() received invalid object: (${obj})`)
}
if (typeof obj === 'function') { if (typeof obj === 'function') {
return obj() return obj()
} } else if (Array.isArray(obj)) {
if (Array.isArray(obj)) {
return random.pick(random.item(obj)) return random.pick(random.item(obj))
} }
return obj return obj
} }
static chance (limit) { /**
if (limit === null || limit === undefined) { * Returns a boolean result based on limit
limit = 2 * @param limit
} * @returns {boolean}
*/
static chance (limit = 2) {
if (isNaN(limit)) { if (isNaN(limit)) {
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 random.number(limit) === 1 return random.number(limit) === 1
} }
static choose (list, flat) { /**
* Return an item from an array of arrays where the first index in each sub-array denotes the weight
* @param {Array} list - Array of arrays
* @param {Boolean} flat - Indicates whether we should iterate over the arrays recursively
* @returns {*}
*/
static choose (list, flat = false) {
if (!(Array.isArray(list))) { if (!(Array.isArray(list))) {
logger.traceback() logger.traceback()
throw new TypeError(`random.choose() received non-array type: (${list})`) throw new TypeError(`random.choose() received non-array type: (${list})`)
} }
let total = 0 let total = 0
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 = random.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]) {
@ -140,9 +170,11 @@ class random {
} }
n = n - list[i][0] n = n - list[i][0]
} }
if (flat === true) {
if (flat) {
return list[0][1] return list[0][1]
} }
return random.pick([list[0][1]]) return random.pick([list[0][1]])
} }
@ -157,6 +189,7 @@ class random {
a.push(wa[i].v) a.push(wa[i].v)
} }
} }
return a return a
} }
@ -164,6 +197,10 @@ class random {
return random.bool() ? obj : '' return random.bool() ? obj : ''
} }
/**
* Returns arr shuffled
* @param arr
*/
static shuffle (arr) { static shuffle (arr) {
let i = arr.length let i = arr.length
while (i--) { while (i--) {
@ -174,24 +211,40 @@ class random {
} }
} }
/**
* Returns a shuffled copy of arr
* @param arr
* @returns {*}
*/
static shuffled (arr) { static shuffled (arr) {
let newArray = arr.slice() let newArray = arr.slice()
random.shuffle(newArray) random.shuffle(newArray)
return newArray return newArray
} }
/**
* Select an array containing a subset of 'list'
* @param list
* @param limit
* @returns {Array}
*/
static subset (list, limit) { static subset (list, limit) {
if (!(Array.isArray(list))) { if (!(Array.isArray(list))) {
logger.traceback() logger.traceback()
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 = random.number(list.length + 1) limit = random.number(list.length + 1)
} }
let result = []
for (let i = 0; i < limit; i++) { // Deepclone list
result.push(random.pick(list)) const temp = JSON.parse(JSON.stringify(list))
const result = []
while (limit--) {
result.push(random.pop(temp))
} }
return result return result
} }