Merge pull request #1 from MozillaSecurity/pyoor-dev
Minor improvements
This commit is contained in:
commit
b2a73e5130
3 changed files with 98 additions and 38 deletions
|
@ -141,11 +141,13 @@ class network extends make {
|
|||
}
|
||||
|
||||
static dtmf () {
|
||||
return random.subset([
|
||||
'*', '#',
|
||||
'A', 'B', 'C', 'D',
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
|
||||
], make.number.range()).join('')
|
||||
let count = make.number.range()
|
||||
const values = []
|
||||
while (count--) {
|
||||
values.push(random.item(['*', '#', 'A', 'B', 'C', 'D', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']))
|
||||
}
|
||||
|
||||
return values.join('')
|
||||
}
|
||||
|
||||
static goodHostnames () {
|
||||
|
|
|
@ -72,7 +72,7 @@ class webgl extends make {
|
|||
|
||||
static parseUniforms (shader, group = 1) {
|
||||
/* 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) {
|
||||
|
@ -90,7 +90,7 @@ class webgl extends make {
|
|||
|
||||
static parseFragDatav3 (shader, group = 1) {
|
||||
// #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) {
|
||||
|
@ -102,7 +102,12 @@ class webgl extends make {
|
|||
}
|
||||
|
||||
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) {
|
||||
|
|
|
@ -7,28 +7,31 @@ const {logger} = require('../logging')
|
|||
|
||||
class random {
|
||||
/**
|
||||
* Must be called before any other methods can be called to initialize MersenneTwister.
|
||||
* @param {number|null|undefined} seed Value 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
|
||||
*/
|
||||
static init (seed) {
|
||||
if (seed === null || seed === undefined) {
|
||||
random.seed = new Date().getTime()
|
||||
}
|
||||
|
||||
random.twister = new MersenneTwister()
|
||||
random.twister.seed(random.seed)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an integer in [0, limit). Uniform distribution.
|
||||
* @param limit
|
||||
* Returns an integer in [0, limit) (uniform distribution)
|
||||
* @param {number} limit
|
||||
*/
|
||||
static number (limit) {
|
||||
if (!random.twister) {
|
||||
throw new Error('random.init must be called first.')
|
||||
}
|
||||
|
||||
if (limit === null || limit === undefined) {
|
||||
limit = 0xffffffff
|
||||
}
|
||||
|
||||
let x = (0x100000000 / limit) >>> 0
|
||||
let y = (x * limit) >>> 0
|
||||
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 () {
|
||||
if (!random.twister) {
|
||||
throw new Error('random.init must be called first.')
|
||||
}
|
||||
|
||||
return random.twister.real2()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an integer in [start, limit]. Uniform distribution.
|
||||
* @param start
|
||||
* @param limit
|
||||
* Returns an integer in [start, limit) (uniform distribution)
|
||||
* @param {number} start
|
||||
* @param {number} 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 random.number(limit - start + 1) + start
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a float in [1, limit]. The logarithm has uniform distribution.
|
||||
* @param {*} limit
|
||||
* Returns a float in [1, limit). The logarithm has uniform distribution.
|
||||
* @param {number} limit
|
||||
*/
|
||||
static ludOneTo (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) {
|
||||
if (list === undefined || typeof list === 'string' || list.length === undefined) {
|
||||
if (!Array.isArray(list)) {
|
||||
logger.traceback()
|
||||
throw new TypeError(`random.item() received invalid object: (${list})`)
|
||||
}
|
||||
|
||||
return list[random.number(list.length)]
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random key of a provided object.
|
||||
* @param {*} obj
|
||||
* Returns a random key of a provided object
|
||||
* @param {Object} obj
|
||||
*/
|
||||
static key (obj) {
|
||||
let list = []
|
||||
for (let i in obj) {
|
||||
list.push(i)
|
||||
}
|
||||
return random.item(list)
|
||||
return random.item(Object.keys(obj))
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a random Boolean value.
|
||||
* Return a random Boolean value
|
||||
*/
|
||||
static bool () {
|
||||
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) {
|
||||
if (!(Array.isArray(obj) || typeof obj === 'function')) {
|
||||
logger.traceback()
|
||||
throw new TypeError(`random.pick() received invalid object: (${obj})`)
|
||||
}
|
||||
|
||||
if (typeof obj === 'function') {
|
||||
return obj()
|
||||
}
|
||||
if (Array.isArray(obj)) {
|
||||
} else if (Array.isArray(obj)) {
|
||||
return random.pick(random.item(obj))
|
||||
}
|
||||
|
||||
return obj
|
||||
}
|
||||
|
||||
static chance (limit) {
|
||||
if (limit === null || limit === undefined) {
|
||||
limit = 2
|
||||
}
|
||||
/**
|
||||
* Returns a boolean result based on limit
|
||||
* @param limit
|
||||
* @returns {boolean}
|
||||
*/
|
||||
static chance (limit = 2) {
|
||||
if (isNaN(limit)) {
|
||||
logger.traceback()
|
||||
throw new TypeError(`random.chance() received non-number type: (${limit})`)
|
||||
}
|
||||
|
||||
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))) {
|
||||
logger.traceback()
|
||||
throw new TypeError(`random.choose() received non-array type: (${list})`)
|
||||
}
|
||||
|
||||
let total = 0
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
total += list[i][0]
|
||||
}
|
||||
|
||||
let n = random.number(total)
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
if (n < list[i][0]) {
|
||||
|
@ -140,9 +170,11 @@ class random {
|
|||
}
|
||||
n = n - list[i][0]
|
||||
}
|
||||
if (flat === true) {
|
||||
|
||||
if (flat) {
|
||||
return list[0][1]
|
||||
}
|
||||
|
||||
return random.pick([list[0][1]])
|
||||
}
|
||||
|
||||
|
@ -157,6 +189,7 @@ class random {
|
|||
a.push(wa[i].v)
|
||||
}
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
|
@ -164,6 +197,10 @@ class random {
|
|||
return random.bool() ? obj : ''
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns arr shuffled
|
||||
* @param arr
|
||||
*/
|
||||
static shuffle (arr) {
|
||||
let i = arr.length
|
||||
while (i--) {
|
||||
|
@ -174,24 +211,40 @@ class random {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a shuffled copy of arr
|
||||
* @param arr
|
||||
* @returns {*}
|
||||
*/
|
||||
static shuffled (arr) {
|
||||
let newArray = arr.slice()
|
||||
random.shuffle(newArray)
|
||||
return newArray
|
||||
}
|
||||
|
||||
/**
|
||||
* Select an array containing a subset of 'list'
|
||||
* @param list
|
||||
* @param limit
|
||||
* @returns {Array}
|
||||
*/
|
||||
static subset (list, limit) {
|
||||
if (!(Array.isArray(list))) {
|
||||
logger.traceback()
|
||||
throw new TypeError(`random.subset() received non-array type: (${list})`)
|
||||
}
|
||||
|
||||
if (typeof limit !== 'number') {
|
||||
limit = random.number(list.length + 1)
|
||||
}
|
||||
let result = []
|
||||
for (let i = 0; i < limit; i++) {
|
||||
result.push(random.pick(list))
|
||||
|
||||
// Deepclone list
|
||||
const temp = JSON.parse(JSON.stringify(list))
|
||||
const result = []
|
||||
while (limit--) {
|
||||
result.push(random.pop(temp))
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue