2018-12-26 08:45:53 +01:00
|
|
|
|
var helper = (function() {
|
|
|
|
|
|
2019-01-03 23:30:27 +01:00
|
|
|
|
var e = function(selector) {
|
2018-12-26 08:45:53 +01:00
|
|
|
|
return document.querySelector(selector);
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-03 23:30:27 +01:00
|
|
|
|
var eA = function(selector) {
|
2018-12-26 08:45:53 +01:00
|
|
|
|
return document.querySelectorAll(selector);
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-03 23:30:27 +01:00
|
|
|
|
var toggleClass = function(element, theClassName) {
|
2018-12-26 08:45:53 +01:00
|
|
|
|
element.classList.toggle(theClassName);
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-03 23:30:27 +01:00
|
|
|
|
var addClass = function(element, theClassName) {
|
2018-12-26 08:45:53 +01:00
|
|
|
|
element.classList.add(theClassName);
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-03 23:30:27 +01:00
|
|
|
|
var removeClass = function(element, theClassName) {
|
2018-12-26 08:45:53 +01:00
|
|
|
|
element.classList.remove(theClassName);
|
|
|
|
|
};
|
|
|
|
|
|
2019-05-11 09:33:19 +02:00
|
|
|
|
var getClosest = function(element, selector) {
|
|
|
|
|
var firstChar = selector.charAt(0);
|
|
|
|
|
// Get closest match
|
|
|
|
|
for (; element && element !== document; element = element.parentNode) {
|
|
|
|
|
// If selector is a class
|
|
|
|
|
if (firstChar === ".") {
|
|
|
|
|
if (element.classList.contains(selector.substr(1))) {
|
|
|
|
|
return element;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
// If selector is an ID
|
|
|
|
|
if (firstChar === "#") {
|
|
|
|
|
if (element.id === selector.substr(1)) {
|
|
|
|
|
return element;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
// If selector is a data attribute
|
|
|
|
|
if (firstChar === "[") {
|
|
|
|
|
if (element.hasAttribute(selector.substr(1, selector.length - 2))) {
|
|
|
|
|
return element;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
// If selector is a tag
|
|
|
|
|
if (element.tagName.toLowerCase() === selector) {
|
|
|
|
|
return element;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-03 23:30:27 +01:00
|
|
|
|
var getDateTime = function() {
|
2018-12-26 08:45:53 +01:00
|
|
|
|
var dateStamp = new Date();
|
|
|
|
|
var object = {
|
|
|
|
|
// string: dateStamp.constructor(),
|
|
|
|
|
// time: dateStamp.getTime()
|
|
|
|
|
date: dateStamp.getDate(),
|
|
|
|
|
day: dateStamp.getDay(),
|
|
|
|
|
year: dateStamp.getFullYear(),
|
|
|
|
|
hours: dateStamp.getHours(),
|
|
|
|
|
milliseconds: dateStamp.getMilliseconds(),
|
|
|
|
|
minutes: dateStamp.getMinutes(),
|
|
|
|
|
month: dateStamp.getMonth(),
|
|
|
|
|
seconds: dateStamp.getSeconds()
|
|
|
|
|
}
|
|
|
|
|
return object;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var sortObject = function(object, key) {
|
|
|
|
|
object.sort(function(a, b) {
|
2019-05-11 09:33:19 +02:00
|
|
|
|
var textA = getObject({
|
|
|
|
|
object: a,
|
|
|
|
|
path: key
|
|
|
|
|
});
|
2019-01-03 23:30:27 +01:00
|
|
|
|
if (typeof textA == "string") {
|
|
|
|
|
textA = textA.toLowerCase();
|
|
|
|
|
};
|
2019-05-11 09:33:19 +02:00
|
|
|
|
var textB = getObject({
|
|
|
|
|
object: b,
|
|
|
|
|
path: key
|
|
|
|
|
});
|
2019-01-03 23:30:27 +01:00
|
|
|
|
if (typeof textB == "string") {
|
|
|
|
|
textB = textB.toLowerCase();
|
|
|
|
|
};
|
2018-12-26 08:45:53 +01:00
|
|
|
|
if (textA < textB) {
|
|
|
|
|
return -1;
|
|
|
|
|
} else if (textA > textB) {
|
|
|
|
|
return 1;
|
|
|
|
|
} else {
|
|
|
|
|
return 0;
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
return object;
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-06 16:47:08 +01:00
|
|
|
|
var applyOptions = function(options, override) {
|
|
|
|
|
if (options && override) {
|
|
|
|
|
if (override) {
|
|
|
|
|
for (var key in override) {
|
|
|
|
|
if (key in options) {
|
|
|
|
|
options[key] = override[key];
|
2018-12-26 08:45:53 +01:00
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
2019-01-06 16:47:08 +01:00
|
|
|
|
return options;
|
2018-12-26 08:45:53 +01:00
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-14 19:50:48 +01:00
|
|
|
|
var hslToRgb = function(hslObject) {
|
|
|
|
|
if (hslObject == undefined) {
|
|
|
|
|
return null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var chroma = (1 - Math.abs((2 * hslObject.l) - 1)) * hslObject.s;
|
|
|
|
|
var huePrime = hslObject.h / 60;
|
|
|
|
|
var secondComponent = chroma * (1 - Math.abs((huePrime % 2) - 1));
|
|
|
|
|
|
|
|
|
|
huePrime = Math.floor(huePrime);
|
|
|
|
|
var red;
|
|
|
|
|
var green;
|
|
|
|
|
var blue;
|
|
|
|
|
|
2019-01-15 10:44:59 +01:00
|
|
|
|
if (huePrime === 0 || huePrime === 6) {
|
2019-01-14 19:50:48 +01:00
|
|
|
|
red = chroma;
|
|
|
|
|
green = secondComponent;
|
|
|
|
|
blue = 0;
|
|
|
|
|
} else if (huePrime === 1) {
|
|
|
|
|
red = secondComponent;
|
|
|
|
|
green = chroma;
|
|
|
|
|
blue = 0;
|
|
|
|
|
} else if (huePrime === 2) {
|
|
|
|
|
red = 0;
|
|
|
|
|
green = chroma;
|
|
|
|
|
blue = secondComponent;
|
|
|
|
|
} else if (huePrime === 3) {
|
|
|
|
|
red = 0;
|
|
|
|
|
green = secondComponent;
|
|
|
|
|
blue = chroma;
|
|
|
|
|
} else if (huePrime === 4) {
|
|
|
|
|
red = secondComponent;
|
|
|
|
|
green = 0;
|
|
|
|
|
blue = chroma;
|
|
|
|
|
} else if (huePrime === 5) {
|
|
|
|
|
red = chroma;
|
|
|
|
|
green = 0;
|
|
|
|
|
blue = secondComponent;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var lightnessAdjustment = hslObject.l - (chroma / 2);
|
|
|
|
|
red += lightnessAdjustment;
|
|
|
|
|
green += lightnessAdjustment;
|
|
|
|
|
blue += lightnessAdjustment;
|
|
|
|
|
|
|
|
|
|
var result = {
|
|
|
|
|
r: Math.round(red * 255),
|
|
|
|
|
g: Math.round(green * 255),
|
|
|
|
|
b: Math.round(blue * 255)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-03 23:30:27 +01:00
|
|
|
|
var hexToRgb = function(hex) {
|
2019-01-14 19:50:48 +01:00
|
|
|
|
if (hex == undefined) {
|
|
|
|
|
return null;
|
|
|
|
|
};
|
2018-12-26 08:45:53 +01:00
|
|
|
|
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
|
|
|
|
|
if (result) {
|
|
|
|
|
result = {
|
|
|
|
|
r: parseInt(result[1], 16),
|
|
|
|
|
g: parseInt(result[2], 16),
|
|
|
|
|
b: parseInt(result[3], 16)
|
|
|
|
|
};
|
2019-01-14 19:50:48 +01:00
|
|
|
|
};
|
2018-12-26 08:45:53 +01:00
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-03 23:30:27 +01:00
|
|
|
|
var rgbToHex = function(rgbObject) {
|
2019-01-14 19:50:48 +01:00
|
|
|
|
if (rgbObject == undefined) {
|
|
|
|
|
return null;
|
|
|
|
|
};
|
2018-12-26 08:45:53 +01:00
|
|
|
|
var componentToHex = function(hexPart) {
|
|
|
|
|
hexPart = hexPart.toString(16);
|
|
|
|
|
if (hexPart.length == 1) {
|
|
|
|
|
hexPart = "0" + hexPart
|
|
|
|
|
};
|
|
|
|
|
return hexPart;
|
|
|
|
|
};
|
|
|
|
|
var result = "#" + componentToHex(rgbObject.r) + componentToHex(rgbObject.g) + componentToHex(rgbObject.b);
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-03 23:30:27 +01:00
|
|
|
|
var makeNode = function(override) {
|
|
|
|
|
var options = {
|
|
|
|
|
tag: null,
|
|
|
|
|
text: null,
|
|
|
|
|
attr: null
|
|
|
|
|
};
|
|
|
|
|
if (override) {
|
|
|
|
|
options = helper.applyOptions(options, override);
|
|
|
|
|
};
|
|
|
|
|
var element = document.createElement(options.tag);
|
|
|
|
|
if (options.text != null) {
|
|
|
|
|
element.textContent = options.text;
|
|
|
|
|
};
|
|
|
|
|
if (options.attr != null) {
|
|
|
|
|
options.attr.forEach(function(arrayItem, index) {
|
|
|
|
|
if ("key" in arrayItem && "value" in arrayItem) {
|
|
|
|
|
element.setAttribute(arrayItem.key, arrayItem.value);
|
|
|
|
|
} else if ("key" in arrayItem) {
|
|
|
|
|
element.setAttribute(arrayItem.key, "");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
return element;
|
|
|
|
|
};
|
|
|
|
|
|
2019-05-11 09:33:19 +02:00
|
|
|
|
var node = function(string) {
|
|
|
|
|
// set element
|
|
|
|
|
var tag;
|
|
|
|
|
if (string.indexOf("|") > 0) {
|
|
|
|
|
tag = string.slice(0, string.indexOf("|"));
|
|
|
|
|
} else {
|
|
|
|
|
tag = string;
|
|
|
|
|
};
|
|
|
|
|
var text = false;
|
|
|
|
|
if (tag.indexOf(":") > 0) {
|
|
|
|
|
var pair = tag.split(":");
|
|
|
|
|
tag = pair[0];
|
|
|
|
|
text = pair[1];
|
|
|
|
|
};
|
|
|
|
|
var element = document.createElement(tag);
|
|
|
|
|
if (text && text != "") {
|
|
|
|
|
element.textContent = text;
|
|
|
|
|
};
|
|
|
|
|
var attributes = string.slice(string.indexOf("|") + 1, string.length).split(",");
|
|
|
|
|
// set attributes
|
|
|
|
|
if (string.indexOf("|") > 0 && string.indexOf("|") < string.length - 1) {
|
|
|
|
|
attributes.forEach(function(arrayItem, index) {
|
|
|
|
|
if (arrayItem.indexOf(":") > 0) {
|
|
|
|
|
// if key and value
|
|
|
|
|
var pair = arrayItem.substring(0, arrayItem.indexOf(":")) + "," + arrayItem.substring((arrayItem.indexOf(":") + 1), arrayItem.length);
|
|
|
|
|
pair = pair.split(",");
|
|
|
|
|
attributes[index] = {
|
|
|
|
|
key: pair[0],
|
|
|
|
|
value: pair[1]
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
// if key only
|
|
|
|
|
attributes[index] = {
|
|
|
|
|
key: arrayItem,
|
|
|
|
|
value: undefined
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
attributes.forEach(function(arrayItem, index) {
|
|
|
|
|
if (("key" in arrayItem && arrayItem.key != undefined) && ("value" in arrayItem && arrayItem.value != undefined)) {
|
|
|
|
|
element.setAttribute(arrayItem.key, arrayItem.value);
|
|
|
|
|
} else if ("key" in arrayItem && arrayItem.key != undefined) {
|
|
|
|
|
element.setAttribute(arrayItem.key, "");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
return element;
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-03 23:30:27 +01:00
|
|
|
|
function _makeAddress(path) {
|
|
|
|
|
var array;
|
|
|
|
|
if (path.indexOf("[") != -1 && path.indexOf("]") != -1) {
|
|
|
|
|
array = path.split(".").join(",").split("[").join(",").split("]").join(",").split(",");
|
|
|
|
|
for (var i = 0; i < array.length; i++) {
|
|
|
|
|
if (array[i] == "") {
|
|
|
|
|
array.splice(i, 1);
|
|
|
|
|
};
|
|
|
|
|
if (!isNaN(parseInt(array[i], 10))) {
|
|
|
|
|
array[i] = parseInt(array[i], 10);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
array = path.split(".");
|
|
|
|
|
};
|
|
|
|
|
return array;
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-06 16:47:08 +01:00
|
|
|
|
function setObject(override) {
|
|
|
|
|
var options = {
|
2019-01-03 23:30:27 +01:00
|
|
|
|
path: null,
|
|
|
|
|
object: null,
|
|
|
|
|
newValue: null
|
|
|
|
|
};
|
2019-01-06 16:47:08 +01:00
|
|
|
|
if (override) {
|
|
|
|
|
var options = applyOptions(options, override);
|
2019-01-03 23:30:27 +01:00
|
|
|
|
};
|
2019-01-06 16:47:08 +01:00
|
|
|
|
var address = _makeAddress(options.path);
|
2019-01-03 23:30:27 +01:00
|
|
|
|
var _setData = function() {
|
|
|
|
|
while (address.length > 1) {
|
|
|
|
|
// shift off and store the first key
|
|
|
|
|
var currentKey = address.shift();
|
|
|
|
|
// if the key is not found make a new object
|
2019-01-06 16:47:08 +01:00
|
|
|
|
if (!(currentKey in options.object)) {
|
2019-01-03 23:30:27 +01:00
|
|
|
|
// make an empty object in the current object level
|
|
|
|
|
if (isNaN(currentKey)) {
|
2019-01-06 16:47:08 +01:00
|
|
|
|
options.object[currentKey] = {};
|
2019-01-03 23:30:27 +01:00
|
|
|
|
} else {
|
2019-01-06 16:47:08 +01:00
|
|
|
|
options.object[currentKey] = [];
|
2019-01-03 23:30:27 +01:00
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
// drill down the object with the first key
|
2019-01-06 16:47:08 +01:00
|
|
|
|
options.object = options.object[currentKey];
|
2019-01-03 23:30:27 +01:00
|
|
|
|
};
|
|
|
|
|
var finalKey = address.shift();
|
2019-01-06 16:47:08 +01:00
|
|
|
|
options.object[finalKey] = options.newValue;
|
2019-01-03 23:30:27 +01:00
|
|
|
|
};
|
2019-01-06 16:47:08 +01:00
|
|
|
|
if (options.object != null && options.path != null && options.newValue != null) {
|
2019-01-03 23:30:27 +01:00
|
|
|
|
_setData();
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-06 16:47:08 +01:00
|
|
|
|
function getObject(override) {
|
|
|
|
|
var options = {
|
2019-01-03 23:30:27 +01:00
|
|
|
|
object: null,
|
|
|
|
|
path: null
|
|
|
|
|
};
|
2019-01-06 16:47:08 +01:00
|
|
|
|
if (override) {
|
|
|
|
|
var options = applyOptions(options, override);
|
2019-01-03 23:30:27 +01:00
|
|
|
|
};
|
2019-01-06 16:47:08 +01:00
|
|
|
|
var address = _makeAddress(options.path);
|
2019-01-03 23:30:27 +01:00
|
|
|
|
var _getData = function() {
|
|
|
|
|
while (address.length > 1) {
|
|
|
|
|
// shift off and store the first key
|
|
|
|
|
var currentKey = address.shift();
|
|
|
|
|
// if the key is not found make a new object
|
2019-01-06 16:47:08 +01:00
|
|
|
|
if (!(currentKey in options.object)) {
|
2019-01-03 23:30:27 +01:00
|
|
|
|
// make an empty object in the current object level
|
|
|
|
|
if (isNaN(currentKey)) {
|
2019-01-06 16:47:08 +01:00
|
|
|
|
options.object[currentKey] = {};
|
2019-01-03 23:30:27 +01:00
|
|
|
|
} else {
|
2019-01-06 16:47:08 +01:00
|
|
|
|
options.object[currentKey] = [];
|
2019-01-03 23:30:27 +01:00
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
// drill down the object with the first key
|
2019-01-06 16:47:08 +01:00
|
|
|
|
options.object = options.object[currentKey];
|
2019-01-03 23:30:27 +01:00
|
|
|
|
};
|
|
|
|
|
var finalKey = address.shift();
|
2019-01-06 16:47:08 +01:00
|
|
|
|
if (!(finalKey in options.object)) {
|
2019-01-03 23:30:27 +01:00
|
|
|
|
return "";
|
|
|
|
|
} else {
|
2019-01-06 16:47:08 +01:00
|
|
|
|
return options.object[finalKey];
|
2019-01-03 23:30:27 +01:00
|
|
|
|
};
|
|
|
|
|
};
|
2019-01-06 16:47:08 +01:00
|
|
|
|
if (options.object != null && options.path != null) {
|
2019-01-03 23:30:27 +01:00
|
|
|
|
return _getData();
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function makeObject(string) {
|
|
|
|
|
var _stringOrBooleanOrNumber = function(stringToTest) {
|
|
|
|
|
if (stringToTest == "true") {
|
|
|
|
|
return true;
|
|
|
|
|
} else if (stringToTest == "false") {
|
|
|
|
|
return false;
|
|
|
|
|
} else if (stringToTest.indexOf("#") != -1) {
|
|
|
|
|
return stringToTest.substr(1, kevValuePair[1].length);
|
|
|
|
|
} else {
|
|
|
|
|
return "\"" + stringToTest + "\"";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
// if argument is a string
|
|
|
|
|
if (typeof string == "string") {
|
|
|
|
|
// start building the object
|
|
|
|
|
var objectString = "{";
|
|
|
|
|
// split the string on comma not followed by a space
|
|
|
|
|
// split on character if not followed by a space
|
|
|
|
|
var items = string.split(/,(?=\S)/);
|
|
|
|
|
// loop over each item
|
|
|
|
|
for (var i = 0; i < items.length; i++) {
|
|
|
|
|
// split each would be object key values pair
|
|
|
|
|
// split on character if not followed by a space
|
|
|
|
|
var kevValuePair = items[i].split(/:(?=\S)/);
|
|
|
|
|
// get the key
|
|
|
|
|
var key = "\"" + kevValuePair[0] + "\"";
|
|
|
|
|
var value;
|
|
|
|
|
// if the value has + with a space after it
|
|
|
|
|
if (/\+(?=\S)/.test(kevValuePair[1])) {
|
|
|
|
|
// remove first + symbol
|
|
|
|
|
kevValuePair[1] = kevValuePair[1].substr(1, kevValuePair[1].length);
|
|
|
|
|
// split the would be values
|
|
|
|
|
// split on character if not followed by a space
|
|
|
|
|
var all_value = kevValuePair[1].split(/\+(?=\S)/);
|
|
|
|
|
// if there are multiple values make an array
|
|
|
|
|
value = "["
|
|
|
|
|
for (var q = 0; q < all_value.length; q++) {
|
|
|
|
|
value += _stringOrBooleanOrNumber(all_value[q]) + ",";
|
|
|
|
|
};
|
|
|
|
|
// remove last comma
|
|
|
|
|
value = value.substr(0, value.length - 1);
|
|
|
|
|
// close array
|
|
|
|
|
value += "]"
|
|
|
|
|
} else {
|
|
|
|
|
value = _stringOrBooleanOrNumber(kevValuePair[1]);
|
|
|
|
|
};
|
|
|
|
|
objectString += key + ":" + value + ",";
|
|
|
|
|
};
|
|
|
|
|
// remove last comma
|
|
|
|
|
objectString = objectString.substr(0, objectString.length - 1);
|
|
|
|
|
// close object
|
|
|
|
|
objectString += "}";
|
|
|
|
|
var object = JSON.parse(objectString);
|
|
|
|
|
return object;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var allEqual = function(array) {
|
|
|
|
|
return array.every(function(arrayItem) {
|
|
|
|
|
return arrayItem === array[0];
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-07 15:42:14 +01:00
|
|
|
|
var randomNumber = function(min, max) {
|
|
|
|
|
return Math.floor(Math.random() * (max - min + 1) + min);
|
|
|
|
|
};
|
|
|
|
|
|
2019-04-26 14:48:37 +02:00
|
|
|
|
var toWords = function(number) {
|
|
|
|
|
var ten = 10;
|
|
|
|
|
var oneHundred = 100;
|
|
|
|
|
var oneThousand = 1000;
|
|
|
|
|
var oneMillion = 1000000;
|
|
|
|
|
var oneBillion = 1000000000; // 1,000,000,000 (9)
|
|
|
|
|
var oneTrillion = 1000000000000; // 1,000,000,000,000 (12)
|
|
|
|
|
var oneQuadrillion = 1000000000000000; // 1,000,000,000,000,000 (15)
|
|
|
|
|
var max = 9007199254740992; // 9,007,199,254,740,992 (15)
|
|
|
|
|
var lessThanTwenty = ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"];
|
|
|
|
|
var tenthsLessThanHundred = ["Zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"];
|
|
|
|
|
var _generateWords = function(number) {
|
|
|
|
|
var remainder, word,
|
|
|
|
|
words = arguments[1];
|
|
|
|
|
// We’re done
|
|
|
|
|
if (number === 0) {
|
|
|
|
|
return !words ? "Zero" : words.join(" ").replace(/,$/, "");
|
|
|
|
|
};
|
|
|
|
|
// First run
|
|
|
|
|
if (!words) {
|
|
|
|
|
words = [];
|
|
|
|
|
};
|
|
|
|
|
// If negative, prepend “minus”
|
|
|
|
|
if (number < 0) {
|
|
|
|
|
words.push("minus");
|
|
|
|
|
number = Math.abs(number);
|
|
|
|
|
};
|
|
|
|
|
if (number < 20) {
|
|
|
|
|
remainder = 0;
|
|
|
|
|
word = lessThanTwenty[number];
|
|
|
|
|
} else if (number < oneHundred) {
|
|
|
|
|
remainder = number % ten;
|
|
|
|
|
word = tenthsLessThanHundred[Math.floor(number / ten)];
|
|
|
|
|
// In case of remainder, we need to handle it here to be able to add the “-”
|
|
|
|
|
if (remainder) {
|
|
|
|
|
word += "-" + lessThanTwenty[remainder];
|
|
|
|
|
remainder = 0;
|
|
|
|
|
};
|
|
|
|
|
} else if (number < oneThousand) {
|
|
|
|
|
remainder = number % oneHundred;
|
|
|
|
|
word = _generateWords(Math.floor(number / oneHundred)) + " Hundred";
|
|
|
|
|
} else if (number < oneMillion) {
|
|
|
|
|
remainder = number % oneThousand;
|
|
|
|
|
word = _generateWords(Math.floor(number / oneThousand)) + " Thousand,";
|
|
|
|
|
} else if (number < oneBillion) {
|
|
|
|
|
remainder = number % oneMillion;
|
|
|
|
|
word = _generateWords(Math.floor(number / oneMillion)) + " Million,";
|
|
|
|
|
} else if (number < oneTrillion) {
|
|
|
|
|
remainder = number % oneBillion;
|
|
|
|
|
word = _generateWords(Math.floor(number / oneBillion)) + " Billion,";
|
|
|
|
|
} else if (number < oneQuadrillion) {
|
|
|
|
|
remainder = number % oneTrillion;
|
|
|
|
|
word = _generateWords(Math.floor(number / oneTrillion)) + " Trillion,";
|
|
|
|
|
} else if (number <= max) {
|
|
|
|
|
remainder = number % oneQuadrillion;
|
|
|
|
|
word = _generateWords(Math.floor(number / oneQuadrillion)) + " Quadrillion,";
|
|
|
|
|
};
|
|
|
|
|
words.push(word);
|
|
|
|
|
return _generateWords(remainder, words);
|
|
|
|
|
};
|
|
|
|
|
var num = parseInt(number, 10);
|
|
|
|
|
return _generateWords(num);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var ordinalWords = function(words) {
|
|
|
|
|
var endsWithDoubleZeroPattern = /(hundred|thousand|(m|b|tr|quadr)illion)$/;
|
|
|
|
|
var endsWithTeenPattern = /teen$/;
|
|
|
|
|
var endsWithYPattern = /y$/;
|
|
|
|
|
var endsWithZeroThroughTwelvePattern = /(Zero|One|Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten|Eleven|Twelve)$/;
|
|
|
|
|
var ordinalLessThanThirteen = {
|
|
|
|
|
Zero: "Zeroth",
|
|
|
|
|
One: "First",
|
|
|
|
|
Two: "Second",
|
|
|
|
|
Three: "Third",
|
|
|
|
|
Four: "Fourth",
|
|
|
|
|
Five: "Fifth",
|
|
|
|
|
Six: "Sixth",
|
|
|
|
|
Seven: "Seventh",
|
|
|
|
|
Eight: "Eighth",
|
|
|
|
|
Nine: "Ninth",
|
|
|
|
|
Ten: "Tenth",
|
|
|
|
|
Eleven: "Eleventh",
|
|
|
|
|
Twelve: "Twelfth"
|
|
|
|
|
};
|
|
|
|
|
var replaceWithOrdinalVariant = function(match, numberWord) {
|
|
|
|
|
return ordinalLessThanThirteen[numberWord];
|
|
|
|
|
};
|
|
|
|
|
// Ends with *00 (100, 1000, etc.) or *teen (13, 14, 15, 16, 17, 18, 19)
|
|
|
|
|
if (endsWithDoubleZeroPattern.test(words) || endsWithTeenPattern.test(words)) {
|
|
|
|
|
return words + "th";
|
|
|
|
|
// Ends with *y (20, 30, 40, 50, 60, 70, 80, 90)
|
|
|
|
|
} else if (endsWithYPattern.test(words)) {
|
|
|
|
|
return words.replace(endsWithYPattern, "ieth");
|
|
|
|
|
// Ends with one through twelve
|
|
|
|
|
} else if (endsWithZeroThroughTwelvePattern.test(words)) {
|
|
|
|
|
return words.replace(endsWithZeroThroughTwelvePattern, replaceWithOrdinalVariant);
|
|
|
|
|
};
|
|
|
|
|
return words;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var ordinalNumber = function(number) {
|
|
|
|
|
var j = number % 10;
|
|
|
|
|
var k = number % 100;
|
|
|
|
|
if (j == 1 && k != 11) {
|
|
|
|
|
return number + "st";
|
|
|
|
|
};
|
|
|
|
|
if (j == 2 && k != 12) {
|
|
|
|
|
return number + "nd";
|
|
|
|
|
};
|
|
|
|
|
if (j == 3 && k != 13) {
|
|
|
|
|
return number + "rd";
|
|
|
|
|
};
|
|
|
|
|
return number + "th";
|
|
|
|
|
};
|
|
|
|
|
|
2018-12-26 08:45:53 +01:00
|
|
|
|
// exposed methods
|
|
|
|
|
return {
|
|
|
|
|
e: e,
|
|
|
|
|
eA: eA,
|
|
|
|
|
toggleClass: toggleClass,
|
|
|
|
|
addClass: addClass,
|
|
|
|
|
removeClass: removeClass,
|
2019-05-11 09:33:19 +02:00
|
|
|
|
getClosest: getClosest,
|
2019-01-03 23:30:27 +01:00
|
|
|
|
allEqual: allEqual,
|
2018-12-26 08:45:53 +01:00
|
|
|
|
getDateTime: getDateTime,
|
|
|
|
|
sortObject: sortObject,
|
|
|
|
|
applyOptions: applyOptions,
|
|
|
|
|
hexToRgb: hexToRgb,
|
2019-01-03 23:30:27 +01:00
|
|
|
|
rgbToHex: rgbToHex,
|
2019-01-14 19:50:48 +01:00
|
|
|
|
hslToRgb: hslToRgb,
|
2019-01-03 23:30:27 +01:00
|
|
|
|
makeNode: makeNode,
|
2019-05-11 09:33:19 +02:00
|
|
|
|
node: node,
|
2019-01-03 23:30:27 +01:00
|
|
|
|
setObject: setObject,
|
|
|
|
|
getObject: getObject,
|
2019-01-07 15:42:14 +01:00
|
|
|
|
makeObject: makeObject,
|
2019-04-26 14:48:37 +02:00
|
|
|
|
randomNumber: randomNumber,
|
|
|
|
|
toWords: toWords,
|
|
|
|
|
ordinalWords: ordinalWords,
|
|
|
|
|
ordinalNumber: ordinalNumber
|
2018-12-26 08:45:53 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
})();
|