From 942d9d89ba75792b86b6beda6f79ac0327d37f23 Mon Sep 17 00:00:00 2001 From: pyoor Date: Mon, 27 Aug 2018 18:57:27 -0400 Subject: [PATCH 01/16] Set default seed value --- lib/random/random.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/random/random.js b/lib/random/random.js index ec87107..99356dc 100644 --- a/lib/random/random.js +++ b/lib/random/random.js @@ -8,13 +8,9 @@ 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 + * @param {number} seed - Value to initialize MersenneTwister */ - static init (seed) { - if (seed === null || seed === undefined) { - random.seed = new Date().getTime() - } - + static init (seed = new Date().getTime()) { random.twister = new MersenneTwister() random.twister.seed(random.seed) } From ba33374c6539db6533dd7ac4deb86c240e900063 Mon Sep 17 00:00:00 2001 From: pyoor Date: Mon, 27 Aug 2018 18:57:39 -0400 Subject: [PATCH 02/16] Set default limit value --- lib/random/random.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/random/random.js b/lib/random/random.js index 99356dc..f75c243 100644 --- a/lib/random/random.js +++ b/lib/random/random.js @@ -12,22 +12,18 @@ class random { */ static init (seed = new Date().getTime()) { random.twister = new MersenneTwister() - random.twister.seed(random.seed) + random.twister.seed(seed) } /** * Returns an integer in [0, limit) (uniform distribution) * @param {number} limit */ - static number (limit) { + static number (limit = 0xffffffff) { 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 From a0c59d400cf00177b7296744ecf87c314dc30482 Mon Sep 17 00:00:00 2001 From: pyoor Date: Mon, 27 Aug 2018 18:59:17 -0400 Subject: [PATCH 03/16] Fix bug in offset and remove randListIndex --- lib/utils/script.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/utils/script.js b/lib/utils/script.js index 5a9ff80..67d95dc 100644 --- a/lib/utils/script.js +++ b/lib/utils/script.js @@ -80,11 +80,7 @@ class script extends utils { } static offset (s) { - return `(${s} % ${random.range(1, 3)})` - } - - static randListIndex (objName) { - return `${random.number()} % ${o.pick(objName)}.length` + return `(${random.number()} % ${s})` } /** From a74b09d2c151ac08e3271275876227b5b8e1de01 Mon Sep 17 00:00:00 2001 From: pyoor Date: Mon, 27 Aug 2018 19:00:00 -0400 Subject: [PATCH 04/16] Wrap all runner output in try/catch --- lib/utils/script.js | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/lib/utils/script.js b/lib/utils/script.js index 67d95dc..f955c6d 100644 --- a/lib/utils/script.js +++ b/lib/utils/script.js @@ -92,20 +92,18 @@ class script extends utils { cmds = (Array.isArray(cmds)) ? cmds : [cmds] cmds = cmds.filter((i) => i !== undefined) if (cmds.length) { - if (random.chance(50)) { - // Wrap each command in try/catch for use in setInterval, setTimeout, repeater - switch (random.number(3)) { - case 0: - return `setInterval(function () { ${utils.script.safely(cmds)} }, ${random.range(100, 400)} )` - case 1: - return `setTimeout(function () { ${utils.script.safely(cmds)} }, ${random.range(100, 400)} )` - case 2: - let n = random.number(random.number(30)) - return `for (let i = 0; i < ${n}; i++) { ${utils.script.safely(cmds)} }` - } + // Wrap each command in try/catch for use in setInterval, setTimeout, repeater + switch (random.number(50)) { + case 0: + return script.safely(`setInterval(function () { ${script.safely(cmds)} }, ${random.range(100, 400)} )`) + case 1: + return script.safely(`setTimeout(function () { ${script.safely(cmds)} }, ${random.range(100, 400)} )`) + case 2: + let n = random.number(random.number(30)) + return script.safely(`for (let i = 0; i < ${n}; i++) { ${script.safely(cmds)} }`) + default: + return script.safely(cmds) } - - return utils.script.safely(cmds) } } From bde62e4d2908830285c42f6f8ae6c3009da09b10 Mon Sep 17 00:00:00 2001 From: pyoor Date: Mon, 27 Aug 2018 19:26:57 -0400 Subject: [PATCH 05/16] Remove postinstall hook --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index a4e3f0b..d1e4f0d 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,6 @@ "build": "webpack -p", "watch": "webpack --watch", "precommit": "yarn lint", - "postinstall": "yarn build", "release": "np" }, "standard": { From 3b5be216cafef6cda2cbdda52cdf0922c3582b56 Mon Sep 17 00:00:00 2001 From: pyoor Date: Mon, 27 Aug 2018 19:27:17 -0400 Subject: [PATCH 06/16] Change precommit hook to prepush --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d1e4f0d..c35849d 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "docs": "esdoc", "build": "webpack -p", "watch": "webpack --watch", - "precommit": "yarn lint", + "prepush": "yarn lint", "release": "np" }, "standard": { From d721eb5b586ec3d206a79de96c5c9590f481051c Mon Sep 17 00:00:00 2001 From: pyoor Date: Mon, 27 Aug 2018 19:39:19 -0400 Subject: [PATCH 07/16] Try to fix utils.script.runner again --- lib/utils/script.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/utils/script.js b/lib/utils/script.js index f955c6d..e711b2b 100644 --- a/lib/utils/script.js +++ b/lib/utils/script.js @@ -86,7 +86,7 @@ class script extends utils { /** * Wrap command(s) in setInterval, setTimeout, loop or run directly * @param {string|string[]} cmds - Command(s) to be executed - * @returns {string} + * @returns {array} */ static runner (cmds) { cmds = (Array.isArray(cmds)) ? cmds : [cmds] @@ -95,14 +95,13 @@ class script extends utils { // Wrap each command in try/catch for use in setInterval, setTimeout, repeater switch (random.number(50)) { case 0: - return script.safely(`setInterval(function () { ${script.safely(cmds)} }, ${random.range(100, 400)} )`) + return [`setInterval(function () { ${script.safely(cmds)} }, ${random.range(100, 400)} )`] case 1: - return script.safely(`setTimeout(function () { ${script.safely(cmds)} }, ${random.range(100, 400)} )`) + return [`setTimeout(function () { ${script.safely(cmds)} }, ${random.range(100, 400)} )`] case 2: - let n = random.number(random.number(30)) - return script.safely(`for (let i = 0; i < ${n}; i++) { ${script.safely(cmds)} }`) + return [`for (let i = 0; i < ${random.number(random.number(30))}; i++) { ${script.safely(cmds)} }`] default: - return script.safely(cmds) + return cmds } } } From aaa01050e2448fc7015555e621ce23fb67f57334 Mon Sep 17 00:00:00 2001 From: pyoor Date: Wed, 29 Aug 2018 17:46:30 -0400 Subject: [PATCH 08/16] Remove unnecessary methods --- lib/utils/common.js | 40 ---------------------------------------- 1 file changed, 40 deletions(-) diff --git a/lib/utils/common.js b/lib/utils/common.js index d35f2ef..7cb9d70 100644 --- a/lib/utils/common.js +++ b/lib/utils/common.js @@ -5,46 +5,6 @@ const jsesc = require('jsesc') const utils = require('../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 * @param s - String to be quoted From ac7f4b3580f8cf68655cce55590737414f71bfe8 Mon Sep 17 00:00:00 2001 From: pyoor Date: Wed, 29 Aug 2018 17:46:46 -0400 Subject: [PATCH 09/16] Add unquote method --- lib/utils/common.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/utils/common.js b/lib/utils/common.js index 7cb9d70..a04cbe7 100644 --- a/lib/utils/common.js +++ b/lib/utils/common.js @@ -24,6 +24,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 * https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_Unicode_Problem From 2f873744761677cfc8e898a1355e71c9b1be17b7 Mon Sep 17 00:00:00 2001 From: pyoor Date: Wed, 29 Aug 2018 17:51:20 -0400 Subject: [PATCH 10/16] Add HTML encode/decode methods --- lib/utils/common.js | 19 +++++++++++++++++++ package.json | 1 + 2 files changed, 20 insertions(+) diff --git a/lib/utils/common.js b/lib/utils/common.js index a04cbe7..2e7b0cb 100644 --- a/lib/utils/common.js +++ b/lib/utils/common.js @@ -1,6 +1,7 @@ /* 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 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +const htmlentities = require('htmlentities') const jsesc = require('jsesc') const utils = require('../utils') @@ -72,6 +73,24 @@ class common extends utils { } } + /** + * Escape special characters using HTML entities + * @param {string} str + * @returns {string} + */ + static htmlEncode (str) { + return htmlentities.encode(str) + } + + /** + * Remove HTML entities from string + * @param {string} str + * @returns {string} + */ + static htmlDencode (str) { + return htmlentities.decode(str) + } + /** * Remove duplicate items from a list * @param {Array} list diff --git a/package.json b/package.json index c35849d..1258fbd 100644 --- a/package.json +++ b/package.json @@ -79,6 +79,7 @@ "webpack-cli": "^3.1.0" }, "dependencies": { + "htmlentities": "^1.0.0", "jsesc": "^2.5.1" } } From 52417f6723d7ca7d71ea22513781d6a3116ba4d0 Mon Sep 17 00:00:00 2001 From: pyoor Date: Wed, 29 Aug 2018 18:05:55 -0400 Subject: [PATCH 11/16] Use format string --- lib/utils/common.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils/common.js b/lib/utils/common.js index 2e7b0cb..3b9d1bc 100644 --- a/lib/utils/common.js +++ b/lib/utils/common.js @@ -49,7 +49,7 @@ class common extends utils { return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function toSolidBytes (match, p1) { // noinspection JSCheckFunctionSignatures - return String.fromCharCode('0x' + p1) + return String.fromCharCode(`0x${p1}`) }) ) } else { From 75369656edf467305931481749cde3aca53a7e6f Mon Sep 17 00:00:00 2001 From: pyoor Date: Wed, 29 Aug 2018 18:06:10 -0400 Subject: [PATCH 12/16] Refactor htmlEscape/unescape methods --- lib/utils/common.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/utils/common.js b/lib/utils/common.js index 3b9d1bc..88665d0 100644 --- a/lib/utils/common.js +++ b/lib/utils/common.js @@ -78,7 +78,7 @@ class common extends utils { * @param {string} str * @returns {string} */ - static htmlEncode (str) { + static htmlEscape (str) { return htmlentities.encode(str) } @@ -87,7 +87,7 @@ class common extends utils { * @param {string} str * @returns {string} */ - static htmlDencode (str) { + static htmlUnescape (str) { return htmlentities.decode(str) } From 813b6b2c84a5da570c178dd827853f4c35326317 Mon Sep 17 00:00:00 2001 From: pyoor Date: Wed, 29 Aug 2018 18:06:27 -0400 Subject: [PATCH 13/16] Simplify uniqueList --- lib/utils/common.js | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/lib/utils/common.js b/lib/utils/common.js index 88665d0..3b5f3a5 100644 --- a/lib/utils/common.js +++ b/lib/utils/common.js @@ -97,15 +97,7 @@ class common extends utils { * @returns {Array} */ static uniqueList (list) { - let tmp = {} - 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 + return list.filter((v, i, a) => a.indexOf(v) === i) } /** From 4fda7284c6f8eb4d40d7a7adbf5e42097346a696 Mon Sep 17 00:00:00 2001 From: pyoor Date: Thu, 30 Aug 2018 09:56:43 -0400 Subject: [PATCH 14/16] Replace htmlentities with newer html-entities --- lib/utils/common.js | 6 +++--- package.json | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/utils/common.js b/lib/utils/common.js index 3b5f3a5..3e26da8 100644 --- a/lib/utils/common.js +++ b/lib/utils/common.js @@ -1,7 +1,7 @@ /* 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 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -const htmlentities = require('htmlentities') +const entities = new (require('html-entities').XmlEntities)() const jsesc = require('jsesc') const utils = require('../utils') @@ -79,7 +79,7 @@ class common extends utils { * @returns {string} */ static htmlEscape (str) { - return htmlentities.encode(str) + return entities.encode(str) } /** @@ -88,7 +88,7 @@ class common extends utils { * @returns {string} */ static htmlUnescape (str) { - return htmlentities.decode(str) + return entities.decode(str) } /** diff --git a/package.json b/package.json index 1258fbd..c4e4b6d 100644 --- a/package.json +++ b/package.json @@ -79,7 +79,7 @@ "webpack-cli": "^3.1.0" }, "dependencies": { - "htmlentities": "^1.0.0", + "html-entities": "^1.2.1", "jsesc": "^2.5.1" } } From d09037b37cc7932c51f5cfb6bae98b435217b896 Mon Sep 17 00:00:00 2001 From: pyoor Date: Tue, 4 Sep 2018 13:34:07 -0400 Subject: [PATCH 15/16] Set seed to null by default --- lib/random/random.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/random/random.js b/lib/random/random.js index 2844fdf..7ae584d 100644 --- a/lib/random/random.js +++ b/lib/random/random.js @@ -8,9 +8,12 @@ const logger = require('../logging') class random { /** * Must be called before any other methods can be called to initialize MersenneTwister - * @param {number} seed - Value to initialize MersenneTwister + * @param {?number} seed - Value to initialize MersenneTwister */ - static init (seed = new Date().getTime()) { + static init (seed = null) { + if (seed === null) { + seed = new Date().getTime() + } random.twister = new MersenneTwister() random.twister.seed(seed) } From 7fdbfa84b5b2071527daa6aa40ed91b851a69f5a Mon Sep 17 00:00:00 2001 From: pyoor Date: Tue, 4 Sep 2018 13:34:20 -0400 Subject: [PATCH 16/16] Refactor choose and weighted --- lib/random/random.js | 44 ++++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/lib/random/random.js b/lib/random/random.js index 7ae584d..85794f2 100644 --- a/lib/random/random.js +++ b/lib/random/random.js @@ -144,43 +144,35 @@ class random { 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]) { - if (flat === true) { - return list[i][1] - } else { - return random.pick([list[i][1]]) - } + const expanded = [] + list.forEach(([weight, value]) => { + while (weight--) { + expanded.push(value) } - n = n - list[i][0] - } + }) 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. - * @param {*} wa + * Return a flattened list of weighted values + * [{w: 1, v: 'foo'}, {w: 1, v: 'bar'}] + * @param {Array} list + * @param {Array} */ - static weighted (wa) { - let a = [] - for (let i = 0; i < wa.length; ++i) { - for (let j = 0; j < wa[i].w; ++j) { - a.push(wa[i].v) + static weighted (list) { + const expanded = [] + list.forEach((item) => { + while (item.w--) { + expanded.push(item.v) } - } + }) - return a + return expanded } static use (obj) {