commit
f4982dc303
4 changed files with 72 additions and 110 deletions
|
@ -8,32 +8,25 @@ 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} seed - Value to initialize MersenneTwister
|
||||||
*/
|
*/
|
||||||
static init (seed) {
|
static init (seed = null) {
|
||||||
if (seed === null || seed === undefined) {
|
if (seed === null) {
|
||||||
random.seed = new Date().getTime()
|
seed = new Date().getTime()
|
||||||
} else {
|
|
||||||
random.seed = seed
|
|
||||||
}
|
}
|
||||||
|
|
||||||
random.twister = new MersenneTwister()
|
random.twister = new MersenneTwister()
|
||||||
random.twister.seed(random.seed)
|
random.twister.seed(seed)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an integer in [0, limit) (uniform distribution)
|
* Returns an integer in [0, limit) (uniform distribution)
|
||||||
* @param {number} limit
|
* @param {number} limit
|
||||||
*/
|
*/
|
||||||
static number (limit) {
|
static number (limit = 0xffffffff) {
|
||||||
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) {
|
|
||||||
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
|
||||||
|
@ -151,43 +144,35 @@ class random {
|
||||||
throw new TypeError(`random.choose() received non-array type: (${list})`)
|
throw new TypeError(`random.choose() received non-array type: (${list})`)
|
||||||
}
|
}
|
||||||
|
|
||||||
let total = 0
|
const expanded = []
|
||||||
for (let i = 0; i < list.length; i++) {
|
list.forEach(([weight, value]) => {
|
||||||
total += list[i][0]
|
while (weight--) {
|
||||||
}
|
expanded.push(value)
|
||||||
|
|
||||||
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 random.pick([list[i][1]])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
n = n - list[i][0]
|
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
if (flat) {
|
if (flat) {
|
||||||
return list[0][1]
|
return random.item(expanded)
|
||||||
}
|
}
|
||||||
|
|
||||||
return random.pick([list[0][1]])
|
return random.pick(expanded)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* More memory-hungry but hopefully faster than random.choose$flat.
|
* Return a flattened list of weighted values
|
||||||
* @param {*} wa
|
* [{w: 1, v: 'foo'}, {w: 1, v: 'bar'}]
|
||||||
|
* @param {Array} list
|
||||||
|
* @param {Array}
|
||||||
*/
|
*/
|
||||||
static weighted (wa) {
|
static weighted (list) {
|
||||||
let a = []
|
const expanded = []
|
||||||
for (let i = 0; i < wa.length; ++i) {
|
list.forEach((item) => {
|
||||||
for (let j = 0; j < wa[i].w; ++j) {
|
while (item.w--) {
|
||||||
a.push(wa[i].v)
|
expanded.push(item.v)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
return a
|
return expanded
|
||||||
}
|
}
|
||||||
|
|
||||||
static use (obj) {
|
static use (obj) {
|
||||||
|
|
|
@ -1,50 +1,11 @@
|
||||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
const entities = new (require('html-entities').XmlEntities)()
|
||||||
const jsesc = require('jsesc')
|
const jsesc = require('jsesc')
|
||||||
const utils = require('../utils')
|
const utils = require('../utils')
|
||||||
|
|
||||||
class common extends utils {
|
class common extends utils {
|
||||||
/**
|
|
||||||
* Return stringified object
|
|
||||||
* @param obj
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
static objToString (obj) {
|
|
||||||
try {
|
|
||||||
return `${obj}`
|
|
||||||
} catch (e) {
|
|
||||||
return `[${e}]`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return enumerable properties recursively
|
|
||||||
* @param obj
|
|
||||||
* @returns {Array}
|
|
||||||
*/
|
|
||||||
static getAllProperties (obj) {
|
|
||||||
let list = []
|
|
||||||
while (obj) {
|
|
||||||
list = list.concat(Object.getOwnPropertyNames(obj))
|
|
||||||
obj = Object.getPrototypeOf(obj)
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return all properties (non-recursive)
|
|
||||||
* @param obj
|
|
||||||
* @returns {Array}
|
|
||||||
*/
|
|
||||||
static getKeysFromHash (obj) {
|
|
||||||
let list = []
|
|
||||||
for (let p in obj) {
|
|
||||||
list.push(p)
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Escape and quote a string
|
* Escape and quote a string
|
||||||
* @param s - String to be quoted
|
* @param s - String to be quoted
|
||||||
|
@ -64,6 +25,19 @@ class common extends utils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove quotes and escape sequence from string
|
||||||
|
* @param {string} s
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
static unquote (s) {
|
||||||
|
return s.replace(/\\'/g, '\'')
|
||||||
|
.replace(/\\"/g, '"')
|
||||||
|
.replace(/\\0/g, '\0')
|
||||||
|
.replace(/\\\\/g, '\\')
|
||||||
|
.replace(/(^['|"])(.*)\1$/gm, '$2')
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unicode safe b64 encoding
|
* Unicode safe b64 encoding
|
||||||
* https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_Unicode_Problem
|
* https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_Unicode_Problem
|
||||||
|
@ -75,7 +49,7 @@ class common extends utils {
|
||||||
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
|
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
|
||||||
function toSolidBytes (match, p1) {
|
function toSolidBytes (match, p1) {
|
||||||
// noinspection JSCheckFunctionSignatures
|
// noinspection JSCheckFunctionSignatures
|
||||||
return String.fromCharCode('0x' + p1)
|
return String.fromCharCode(`0x${p1}`)
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
|
@ -99,21 +73,31 @@ class common extends utils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Escape special characters using HTML entities
|
||||||
|
* @param {string} str
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
static htmlEscape (str) {
|
||||||
|
return entities.encode(str)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove HTML entities from string
|
||||||
|
* @param {string} str
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
static htmlUnescape (str) {
|
||||||
|
return entities.decode(str)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove duplicate items from a list
|
* Remove duplicate items from a list
|
||||||
* @param {Array} list
|
* @param {Array} list
|
||||||
* @returns {Array}
|
* @returns {Array}
|
||||||
*/
|
*/
|
||||||
static uniqueList (list) {
|
static uniqueList (list) {
|
||||||
let tmp = {}
|
return list.filter((v, i, a) => a.indexOf(v) === i)
|
||||||
let r = []
|
|
||||||
for (let i = 0; i < list.length; i++) {
|
|
||||||
tmp[list[i]] = list[i]
|
|
||||||
}
|
|
||||||
for (let i in tmp) {
|
|
||||||
r.push(tmp[i])
|
|
||||||
}
|
|
||||||
return r
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -80,37 +80,30 @@ class script extends utils {
|
||||||
}
|
}
|
||||||
|
|
||||||
static offset (s) {
|
static offset (s) {
|
||||||
return `(${s} % ${random.range(1, 3)})`
|
return `(${random.number()} % ${s})`
|
||||||
}
|
|
||||||
|
|
||||||
static randListIndex (objName) {
|
|
||||||
return `${random.number()} % ${o.pick(objName)}.length`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrap command(s) in setInterval, setTimeout, loop or run directly
|
* Wrap command(s) in setInterval, setTimeout, loop or run directly
|
||||||
* @param {string|string[]} cmds - Command(s) to be executed
|
* @param {string|string[]} cmds - Command(s) to be executed
|
||||||
* @returns {string}
|
* @returns {array}
|
||||||
*/
|
*/
|
||||||
static runner (cmds) {
|
static runner (cmds) {
|
||||||
cmds = (Array.isArray(cmds)) ? cmds : [cmds]
|
cmds = (Array.isArray(cmds)) ? cmds : [cmds]
|
||||||
cmds = cmds.filter((i) => i !== undefined)
|
cmds = cmds.filter((i) => i !== undefined)
|
||||||
if (cmds.length) {
|
if (cmds.length) {
|
||||||
if (random.chance(50)) {
|
|
||||||
// Wrap each command in try/catch for use in setInterval, setTimeout, repeater
|
// Wrap each command in try/catch for use in setInterval, setTimeout, repeater
|
||||||
switch (random.number(3)) {
|
switch (random.number(50)) {
|
||||||
case 0:
|
case 0:
|
||||||
return `setInterval(function () { ${utils.script.safely(cmds)} }, ${random.range(100, 400)} )`
|
return [`setInterval(function () { ${script.safely(cmds)} }, ${random.range(100, 400)} )`]
|
||||||
case 1:
|
case 1:
|
||||||
return `setTimeout(function () { ${utils.script.safely(cmds)} }, ${random.range(100, 400)} )`
|
return [`setTimeout(function () { ${script.safely(cmds)} }, ${random.range(100, 400)} )`]
|
||||||
case 2:
|
case 2:
|
||||||
let n = random.number(random.number(30))
|
return [`for (let i = 0; i < ${random.number(random.number(30))}; i++) { ${script.safely(cmds)} }`]
|
||||||
return `for (let i = 0; i < ${n}; i++) { ${utils.script.safely(cmds)} }`
|
default:
|
||||||
|
return cmds
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return utils.script.safely(cmds)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static safely (obj) {
|
static safely (obj) {
|
||||||
|
|
|
@ -29,8 +29,7 @@
|
||||||
"docs": "esdoc",
|
"docs": "esdoc",
|
||||||
"build": "webpack -p",
|
"build": "webpack -p",
|
||||||
"watch": "webpack --watch",
|
"watch": "webpack --watch",
|
||||||
"precommit": "yarn lint",
|
"prepush": "yarn lint",
|
||||||
"postinstall": "yarn build",
|
|
||||||
"release": "np"
|
"release": "np"
|
||||||
},
|
},
|
||||||
"standard": {
|
"standard": {
|
||||||
|
@ -80,6 +79,7 @@
|
||||||
"webpack-cli": "^3.1.0"
|
"webpack-cli": "^3.1.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"html-entities": "^1.2.1",
|
||||||
"jsesc": "^2.5.1"
|
"jsesc": "^2.5.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue