Resolved conflicts

This commit is contained in:
n1474335 2017-12-27 12:50:29 +00:00
commit e2161ec934
25 changed files with 811 additions and 208 deletions

View file

@ -0,0 +1,252 @@
import Utils from "../Utils.js";
/**
* Math operations on numbers.
*
* @author bwhitn [brian.m.whitney@outlook.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*
* @namespace
*/
const Arithmetic = {
/**
* @constant
* @default
*/
DELIM_OPTIONS: ["Line feed", "Space", "Comma", "Semi-colon", "Colon", "CRLF"],
/**
* Splits a string based on a delimiter and calculates the sum of numbers.
*
* @param {string} input
* @param {Object[]} args
* @returns {number}
*/
runSum: function(input, args) {
const val = Arithmetic._sum(Arithmetic._createNumArray(input, args[0]));
return typeof(val) === "number" ? val : NaN;
},
/**
* Splits a string based on a delimiter and subtracts all the numbers.
*
* @param {string} input
* @param {Object[]} args
* @returns {number}
*/
runSub: function(input, args) {
let val = Arithmetic._sub(Arithmetic._createNumArray(input, args[0]));
return typeof(val) === "number" ? val : NaN;
},
/**
* Splits a string based on a delimiter and multiplies the numbers.
*
* @param {string} input
* @param {Object[]} args
* @returns {number}
*/
runMulti: function(input, args) {
let val = Arithmetic._multi(Arithmetic._createNumArray(input, args[0]));
return typeof(val) === "number" ? val : NaN;
},
/**
* Splits a string based on a delimiter and divides the numbers.
*
* @param {string} input
* @param {Object[]} args
* @returns {number}
*/
runDiv: function(input, args) {
let val = Arithmetic._div(Arithmetic._createNumArray(input, args[0]));
return typeof(val) === "number" ? val : NaN;
},
/**
* Splits a string based on a delimiter and computes the mean (average).
*
* @param {string} input
* @param {Object[]} args
* @returns {number}
*/
runMean: function(input, args) {
let val = Arithmetic._mean(Arithmetic._createNumArray(input, args[0]));
return typeof(val) === "number" ? val : NaN;
},
/**
* Splits a string based on a delimiter and finds the median.
*
* @param {string} input
* @param {Object[]} args
* @returns {number}
*/
runMedian: function(input, args) {
let val = Arithmetic._median(Arithmetic._createNumArray(input, args[0]));
return typeof(val) === "number" ? val : NaN;
},
/**
* splits a string based on a delimiter and computes the standard deviation.
*
* @param {string} input
* @param {Object[]} args
* @returns {number}
*/
runStdDev: function(input, args) {
let val = Arithmetic._stdDev(Arithmetic._createNumArray(input, args[0]));
return typeof(val) === "number" ? val : NaN;
},
/**
* Converts a string array to a number array.
*
* @private
* @param {string[]} input
* @param {string} delim
* @returns {number[]}
*/
_createNumArray: function(input, delim) {
delim = Utils.charRep[delim || "Space"];
let splitNumbers = input.split(delim),
numbers = [],
num;
for (let i = 0; i < splitNumbers.length; i++) {
if (splitNumbers[i].indexOf(".") >= 0) {
num = parseFloat(splitNumbers[i].trim());
} else {
num = parseInt(splitNumbers[i].trim(), 0);
}
if (!isNaN(num)) {
numbers.push(num);
}
}
return numbers;
},
/**
* Adds an array of numbers and returns the value.
*
* @private
* @param {number[]} data
* @returns {number}
*/
_sum: function(data) {
if (data.length > 0) {
return data.reduce((acc, curr) => acc + curr);
}
},
/**
* Subtracts an array of numbers and returns the value.
*
* @private
* @param {number[]} data
* @returns {number}
*/
_sub: function(data) {
if (data.length > 0) {
return data.reduce((acc, curr) => acc - curr);
}
},
/**
* Multiplies an array of numbers and returns the value.
*
* @private
* @param {number[]} data
* @returns {number}
*/
_multi: function(data) {
if (data.length > 0) {
return data.reduce((acc, curr) => acc * curr);
}
},
/**
* Divides an array of numbers and returns the value.
*
* @private
* @param {number[]} data
* @returns {number}
*/
_div: function(data) {
if (data.length > 0) {
return data.reduce((acc, curr) => acc / curr);
}
},
/**
* Computes mean of a number array and returns the value.
*
* @private
* @param {number[]} data
* @returns {number}
*/
_mean: function(data) {
if (data.length > 0) {
return Arithmetic._sum(data) / data.length;
}
},
/**
* Computes median of a number array and returns the value.
*
* @private
* @param {number[]} data
* @returns {number}
*/
_median: function (data) {
if ((data.length % 2) === 0) {
let first, second;
data.sort(function(a, b){
return a - b;
});
first = data[Math.floor(data.length / 2)];
second = data[Math.floor(data.length / 2) - 1];
return Arithmetic._mean([first, second]);
} else {
return data[Math.floor(data.length / 2)];
}
},
/**
* Computes standard deviation of a number array and returns the value.
*
* @private
* @param {number[]} data
* @returns {number}
*/
_stdDev: function (data) {
if (data.length > 0) {
let avg = Arithmetic._mean(data);
let devSum = 0;
for (let i = 0; i < data.length; i++) {
devSum += (data[i] - avg) ** 2;
}
return Math.sqrt(devSum / data.length);
}
},
};
export default Arithmetic;

View file

@ -186,7 +186,7 @@ const ByteRepr = {
// 0x and \x are added to the beginning if they are selected, so increment the positions accordingly
if (delim === "0x" || delim === "\\x") {
pos[0].start += 2;
pos[0].end += 2;
pos[0].end += 2;
}
return pos;
},

View file

@ -2,9 +2,10 @@ import {camelCase, kebabCase, snakeCase} from "lodash";
import Utils from "../Utils.js";
import vkbeautify from "vkbeautify";
import {DOMParser as dom} from "xmldom";
import {DOMParser} from "xmldom";
import xpath from "xpath";
import jpath from "jsonpath";
import nwmatcher from "nwmatcher";
import prettyPrintOne from "imports-loader?window=>global!exports-loader?prettyPrintOne!google-code-prettify/bin/prettify.min.js";
@ -336,7 +337,7 @@ const Code = {
let doc;
try {
doc = new dom().parseFromString(input);
doc = new DOMParser().parseFromString(input, "application/xml");
} catch (err) {
return "Invalid input XML.";
}
@ -423,7 +424,7 @@ const Code = {
let query = args[0],
delimiter = args[1],
parser = new DOMParser(),
html,
dom,
result;
if (!query.length || !input.length) {
@ -431,32 +432,32 @@ const Code = {
}
try {
html = parser.parseFromString(input, "text/html");
dom = parser.parseFromString(input);
} catch (err) {
return "Invalid input HTML.";
}
try {
result = html.querySelectorAll(query);
const matcher = nwmatcher({document: dom});
result = matcher.select(query, dom);
} catch (err) {
return "Invalid CSS Selector. Details:\n" + err.message;
}
const nodeToString = function(node) {
return node.toString();
/* xmldom does not return the outerHTML value.
switch (node.nodeType) {
case Node.ELEMENT_NODE: return node.outerHTML;
case Node.ATTRIBUTE_NODE: return node.value;
case Node.COMMENT_NODE: return node.data;
case Node.TEXT_NODE: return node.wholeText;
case Node.DOCUMENT_NODE: return node.outerHTML;
case node.ELEMENT_NODE: return node.outerHTML;
case node.ATTRIBUTE_NODE: return node.value;
case node.TEXT_NODE: return node.wholeText;
case node.COMMENT_NODE: return node.data;
case node.DOCUMENT_NODE: return node.outerHTML;
default: throw new Error("Unknown Node Type: " + node.nodeType);
}
}*/
};
return Array.apply(null, Array(result.length))
.map(function(_, i) {
return result[i];
})
return result
.map(nodeToString)
.join(delimiter);
},

View file

@ -135,6 +135,31 @@ const Entropy = {
},
/**
* Chi Square operation.
*
* @param {byteArray} data
* @param {Object[]} args
* @returns {number}
*/
runChiSq: function(input, args) {
let distArray = new Array(256).fill(0),
total = 0;
for (let i = 0; i < input.length; i++) {
distArray[input[i]]++;
}
for (let i = 0; i < distArray.length; i++) {
if (distArray[i] > 0) {
total += Math.pow(distArray[i] - input.length / 256, 2) / (input.length / 256);
}
}
return total;
},
/**
* Calculates the Shannon entropy for a given chunk of data.
*

View file

@ -1,5 +1,5 @@
/**
* Microsoft operations.
* Microsoft operations.
*
* @author bmwhitn [brian.m.whitney@outlook.com]
* @copyright Crown Copyright 2017

View file

@ -26,9 +26,14 @@ const NetBIOS = {
let output = [],
offset = args[0];
for (let i = 0; i < input.length; i++) {
output.push((input[i] >> 4) + offset);
output.push((input[i] & 0xf) + offset);
if (input.length <= 16) {
let len = input.length;
input.length = 16;
input.fill(32, len, 16);
for (let i = 0; i < input.length; i++) {
output.push((input[i] >> 4) + offset);
output.push((input[i] & 0xf) + offset);
}
}
return output;
@ -46,9 +51,15 @@ const NetBIOS = {
let output = [],
offset = args[0];
for (let i = 0; i < input.length; i += 2) {
output.push(((input[i] - offset) << 4) |
((input[i + 1] - offset) & 0xf));
if (input.length <= 32 && (input.length % 2) === 0) {
for (let i = 0; i < input.length; i += 2) {
output.push((((input[i] & 0xff) - offset) << 4) |
(((input[i + 1] & 0xff) - offset) & 0xf));
}
for (let i = output.length - 1; i > 0; i--) {
if (output[i] === 32) output.splice(i, i);
else break;
}
}
return output;

View file

@ -14,16 +14,72 @@ const Numberwang = {
* @returns {string}
*/
run: function(input, args) {
if (!input) return "Let's play Wangernumb!";
const match = input.match(/\d+/);
if (match) {
return match[0] + "! That's Numberwang!";
let output;
if (!input) {
output = "Let's play Wangernumb!";
} else {
// That's a bad miss!
return "Sorry, that's not Numberwang. Let's rotate the board!";
const match = input.match(/(f0rty-s1x|shinty-six|filth-hundred and neeb|-?√?\d+(\.\d+)?i?([a-z]?)%?)/i);
if (match) {
if (match[3]) output = match[0] + "! That's AlphaNumericWang!";
else output = match[0] + "! That's Numberwang!";
} else {
// That's a bad miss!
output = "Sorry, that's not Numberwang. Let's rotate the board!";
}
}
const rand = Math.floor(Math.random() * Numberwang._didYouKnow.length);
return output + "\n\nDid you know: " + Numberwang._didYouKnow[rand];
},
/**
* Taken from http://numberwang.wikia.com/wiki/Numberwang_Wikia
*
* @private
* @constant
*/
_didYouKnow: [
"Numberwang, contrary to popular belief, is a fruit and not a vegetable.",
"Robert Webb once got WordWang while presenting an episode of Numberwang.",
"The 6705th digit of pi is Numberwang.",
"Numberwang was invented on a Sevenday.",
"Contrary to popular belief, Albert Einstein always got good grades in Numberwang at school. He once scored ^4$ on a test.",
"680 asteroids have been named after Numberwang.",
"Archimedes is most famous for proclaiming \"That's Numberwang!\" during an epiphany about water displacement he had while taking a bath.",
"Numberwang Day is celebrated in Japan on every day of the year apart from June 6.",
"Biologists recently discovered Numberwang within a strand of human DNA.",
"Numbernot is a special type of non-Numberwang number. It is divisible by 3 and the letter \"y\".",
"Julie once got 612.04 Numberwangs in a single episode of Emmerdale.",
"In India, it is traditional to shout out \"Numberwang!\" instead of checkmate during games of chess.",
"There is a rule on Countdown which states that if you get Numberwang in the numbers round, you automatically win. It has only ever been invoked twice.",
"\"Numberwang\" was the third-most common baby name for a brief period in 1722.",
"\"The Lion King\" was loosely based on Numberwang.",
"\"A Numberwang a day keeps the doctor away\" is how Donny Cosy, the oldest man in the world, explained how he was in such good health at the age of 136.",
"The \"number lock\" button on a keyboard is based on the popular round of the same name in \"Numberwang\".",
"Cambridge became the first university to offer a course in Numberwang in 1567.",
"Schrödinger's Numberwang is a number that has been confusing dentists for centuries.",
"\"Harry Potter and the Numberwang of Numberwang\" was rejected by publishers -41 times before it became a bestseller.",
"\"Numberwang\" is the longest-running British game show in history; it has aired 226 seasons, each containing 19 episodes, which makes a grand total of 132 episodes.",
"The triple Numberwang bonus was discovered by archaeologist Thomas Jefferson in Somerset.",
"Numberwang is illegal in parts of Czechoslovakia.",
"Numberwang was discovered in India in the 12th century.",
"Numberwang has the chemical formula Zn4SO2(HgEs)3.",
"The first pack of cards ever created featured two \"Numberwang\" cards instead of jokers.",
"Julius Caesar was killed by an overdose of Numberwang.",
"The most Numberwang musical note is G#.",
"In 1934, the forty-third Google Doodle promoted the upcoming television show \"Numberwang on Ice\".",
"A recent psychology study found that toddlers were 17% faster at identifying numbers which were Numberwang.",
"There are 700 ways to commit a foul in the television show \"Numberwang\". All 700 of these fouls were committed by Julie in one single episode in 1473.",
"Astronomers suspect God is Numberwang.",
"Numberwang is the official beverage of Canada.",
"In the pilot episode of \"The Price is Right\", if a contestant got the value of an item exactly right they were told \"That's Numberwang!\" and immediately won ₹5.7032.",
"The first person to get three Numberwangs in a row was Madonna.",
"\"Numberwang\" has the code U+46402 in Unicode.",
"The musical note \"Numberwang\" is between D# and E♮.",
"Numberwang was first played on the moon in 1834.",
],
};
export default Numberwang;

View file

@ -1,6 +1,7 @@
import otp from "otp";
import Base64 from "./Base64.js";
/**
* One-Time Password operations.
*

View file

@ -1,3 +1,6 @@
import crypto from "crypto";
/**
* UUID operations.
*
@ -17,25 +20,17 @@ const UUID = {
* @returns {string}
*/
runGenerateV4: function(input, args) {
if (window && typeof(window.crypto) !== "undefined" && typeof(window.crypto.getRandomValues) !== "undefined") {
let buf = new Uint32Array(4),
i = 0;
window.crypto.getRandomValues(buf);
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
let r = (buf[i >> 3] >> ((i % 8) * 4)) & 0xf,
v = c === "x" ? r : (r & 0x3 | 0x8);
i++;
return v.toString(16);
});
} else {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
let r = Math.random() * 16 | 0,
v = c === "x" ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
},
const buf = new Uint32Array(4).map(() => {
return crypto.randomBytes(4).readUInt32BE(0, true);
});
let i = 0;
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
let r = (buf[i >> 3] >> ((i % 8) * 4)) & 0xf,
v = c === "x" ? r : (r & 0x3 | 0x8);
i++;
return v.toString(16);
});
}
};
export default UUID;