was.js

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 1  →  ?path2? @ HEAD
File deleted
/trunk/dist/formats/csv/csv.js
File deleted
/trunk/dist/collections/arrays/arrays.js
/trunk/dist/was.js
@@ -0,0 +1,360 @@
/*! was.js - v1.0.10 - 2021-10-31
* http://grimore.org
* Copyright (c) 2021 [object Object]; Licensed GPL-3.0 */
/* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
/*************************************************************************/
function wasArrayToObject(a) {
var o = {};
a.reduce(function(a, c, i) {
i = Math.floor(i / 2);
if (!a[i]) {
a[i] = [];
}
a[i].push(c);
return a;
}, []).forEach(function(c, i, a) {
o[c[0]] = c[1];
}, o);
return o;
}
if (!Array.prototype.toObject) {
Array.prototype.toObject = function() {
return wasArrayToObject(this);
};
}
// jQuery
if(typeof jQuery === 'function') {
$.extend({
toObject: wasArrayToObject
});
}
 
 
/*************************************************************************/
/* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
/*************************************************************************/
function wasProduct(a, b) {
var m = Math.max(a.length, b.length);
var o = {};
for(var i = 0; i < m; ++i) {
o[a[i]] = b[i];
}
return o;
}
if (!Array.prototype.product) {
Array.prototype.product = function(b) {
return wasProduct(this, b);
};
}
// jQuery
if(typeof jQuery === 'function') {
$.extend({
product: wasProduct
});
}
 
/*************************************************************************/
/* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
/*************************************************************************/
function wasStride(a, s) {
return a.filter(function(e, i) {
return i % s === 0;
});
}
if (!Array.prototype.stride) {
Array.prototype.stride = function(s) {
return wasStride(this, s);
};
}
// jQuery
if(typeof jQuery === 'function') {
$.extend({
stride: wasStride
});
}
 
// Vanilla JavaScript
function wasChunk(a, n) {
return Array.from(new Array(Math.ceil(a.length/n)), (_,i)=>a.slice(i*n,i*n+n));
}
if (!Array.prototype.chunk) {
Array.prototype.chunk = function(a, n) {
return wasChunk(this, n);
};
}
// jQuery
if(typeof jQuery === 'function') {
$.extend({
chunk: wasChunk
});
}
 
/*************************************************************************/
/* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
/*************************************************************************/
/*stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript*/
/*************************************************************************/
// Vanilla JavaScript
function wasEquals(a, b) {
// if the other array is a falsy value, return
if (!b) {
return false;
}
 
// compare lengths - can save a lot of time
if (a.length !== b.length) {
return false;
}
 
for (var i = 0, l = a.length; i < l; i++) {
// Check if we have nested arrays
if (a[i] instanceof Array && b[i] instanceof Array) {
// recurse into the nested arrays
if (!a[i].equals(b[i])) {
return false;
}
} else if (a[i] !== b[i]) {
// Warning - two different object instances will never be equal: {x:20} != {x:20}
return false;
}
}
return true;
}
if (!Array.prototype.equals) {
// attach the .equals method to Array's prototype to call it on any array
Array.prototype.equals = function(a, b) {
return wasEquals(this, b);
};
}
// jQuery
if(typeof jQuery === 'function') {
$.extend({
equals: wasEquals
});
}
 
/*************************************************************************/
/* Node.JS package export. */
/*************************************************************************/
if(typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports.collections = {
product: wasProduct,
stride: wasStride,
chunk: wasChunk,
equals: wasEquals,
toObject: wasArrayToObject
};
}
 
/* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */
/*************************************************************************/
function wasCSVToArray(csv) {
var l = [];
var s = [];
var m = "";
do {
var a = csv.charAt(0);
csv = csv.slice(1, csv.length);
if(a === ",") {
if(s[s.length-1] !== '"') {
l.push(m);
m = "";
continue;
}
m += a;
continue;
}
if(a === '"' && csv.charAt(0) === a) {
m += a;
csv = csv.slice(1, csv.length);
continue;
}
if(a === '"') {
if(s[s.length-1] !== a) {
s.push(a);
continue;
}
s.pop();
continue;
}
m += a;
} while(csv !== "");
l.push(m);
return l;
}
 
/*************************************************************************/
/* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */
/*************************************************************************/
function wasArrayToCSV(a) {
var csv = [];
for(var i=0; i<a.length; ++i) {
var cell = a[i].toString().replace('"', '""');
if(/"\s,\r\n/.test(cell)) {
csv[i] = '"' + cell + '"';
continue;
}
csv[i] = cell;
}
return csv.join();
}
 
/* Node.JS package export. */
/*************************************************************************/
if(typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
/* global wasCSVToArray, wasArrayToCSV, wasKeyValueToObject, wasKeyValueGet, wasKeyValueSet */
module.exports.formats = {
csv: {
CSVToArray: wasCSVToArray,
ArrayToCSV: wasArrayToCSV
},
kvp: {
KeyValueToObject: wasKeyValueToObject,
KeyValueGet: wasKeyValueGet,
KeyValueSet: wasKeyValueSet
}
};
}
 
/* Copyright (C) 2020 Wizardry and Steamworks - License: GNU GPLv3 */
/*************************************************************************/
function wasKeyValueToObject(data) {
var result = {};
data.split('&').forEach((kvp) => {
var kv = kvp.split('=')
.filter((kv) =>
kv[0] !== 'undefined' && kv[1] !== 'undefined');
result[kv[0]] = kv[1];
 
});
return result;
}
 
/*************************************************************************/
/* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
/*************************************************************************/
function wasKeyValueSet(k, v, data) {
return data.split('&')
.map(c => c.split('=')[0] === k ? `${k}=${v}` : c)
.concat(`${k}=${v}`)
.filter((e,i,s) => s.indexOf(e) === i)
.join('&');
}
 
 
/*************************************************************************/
/* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
/*************************************************************************/
function wasKeyValueGet(k, data) {
return data.split('&').reduce((a, c) => {
var s = c.split('=');
return s[0] === k ? s[1] : a;
}, '');
}
/* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
/*************************************************************************/
/* fuss/lambda_calculus/functional_programming/aggregators @ grimore.org */
/*************************************************************************/
function wasSwitch(q, d, ...c) {
if(c.length % 2 !== 0) {
throw "Pairs of predicates expected for cases";
}
(Array.isArray(q) ? q : [ q ]).forEach((s) => {
var m = false;
for(var i = 0; i < c.length; i += 2) {
if(!c[i](s)) {
continue;
}
if(!c[i + 1](s)) {
continue;
}
m = true;
}
if(!m) {
d(s);
}
});
}
if (!Array.prototype.switch) {
Array.prototype.switch = function() {
wasSwitch(this, arguments[0], arguments.slice(1));
};
}
// jQuery
if(typeof jQuery === 'function') {
$.extend({
switch: wasSwitch
});
}
 
/*************************************************************************/
/* Node.JS package export. */
/*************************************************************************/
if(typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports.lambda = {
switch: wasSwitch
};
}
 
/* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */
/*************************************************************************/
function wasMapValueToRange(value, xMin, xMax, yMin, yMax) {
return yMin + (
( yMax - yMin ) * ( value - xMin ) / ( xMax - xMin )
);
}
 
/*************************************************************************/
/* Node.JS package export. */
/*************************************************************************/
if(typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports.mathematics = {
MapValueToRange: wasMapValueToRange
};
}
 
/* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */
/*************************************************************************/
function wasHexToRGB(hex) {
var shortRegEx = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(
shortRegEx,
function(m, r, g, b) {
return r + r + g + g + b + b;
}
);
 
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
 
/*************************************************************************/
/* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */
/*************************************************************************/
function wasRGBToHex(r, g, b) {
return "#" + (
(1 << 24) +
(r << 16) +
(g << 8) +
b
).toString(16).slice(1);
}
 
/*************************************************************************/
/* Node.JS package export. */
/*************************************************************************/
if(typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports.physics = {
HexToRGB: wasHexToRGB,
RGBToHex: wasRGBToHex
};
}
/trunk/dist/was.min.js
@@ -0,0 +1,5 @@
/*! was.js - v1.0.10 - 2021-10-31
* http://grimore.org
* Copyright (c) 2021 [object Object]; Licensed GPL-3.0 */
 
function wasArrayToObject(e){var t={};return e.reduce(function(e,t,r){return e[r=Math.floor(r/2)]||(e[r]=[]),e[r].push(t),e},[]).forEach(function(e,r,o){t[e[0]]=e[1]},t),t}function wasProduct(e,t){for(var r=Math.max(e.length,t.length),o={},n=0;n<r;++n)o[e[n]]=t[n];return o}function wasStride(e,t){return e.filter(function(e,r){return r%t==0})}function wasChunk(e,t){return Array.from(new Array(Math.ceil(e.length/t)),(r,o)=>e.slice(o*t,o*t+t))}function wasEquals(e,t){if(!t)return!1;if(e.length!==t.length)return!1;for(var r=0,o=e.length;r<o;r++)if(e[r]instanceof Array&&t[r]instanceof Array){if(!e[r].equals(t[r]))return!1}else if(e[r]!==t[r])return!1;return!0}function wasCSVToArray(e){var t=[],r=[],o="";do{var n=e.charAt(0);if(e=e.slice(1,e.length),","!==n)if('"'!==n||e.charAt(0)!==n)if('"'!==n)o+=n;else{if(r[r.length-1]!==n){r.push(n);continue}r.pop()}else o+=n,e=e.slice(1,e.length);else{if('"'!==r[r.length-1]){t.push(o),o="";continue}o+=n}}while(""!==e);return t.push(o),t}function wasArrayToCSV(e){for(var t=[],r=0;r<e.length;++r){var o=e[r].toString().replace('"','""');/"\s,\r\n/.test(o)?t[r]='"'+o+'"':t[r]=o}return t.join()}function wasKeyValueToObject(e){var t={};return e.split("&").forEach(e=>{var r=e.split("=").filter(e=>"undefined"!==e[0]&&"undefined"!==e[1]);t[r[0]]=r[1]}),t}function wasKeyValueSet(e,t,r){return r.split("&").map(r=>r.split("=")[0]===e?`${e}=${t}`:r).concat(`${e}=${t}`).filter((e,t,r)=>r.indexOf(e)===t).join("&")}function wasKeyValueGet(e,t){return t.split("&").reduce((t,r)=>{var o=r.split("=");return o[0]===e?o[1]:t},"")}function wasSwitch(e,t,...r){if(r.length%2!=0)throw"Pairs of predicates expected for cases";(Array.isArray(e)?e:[e]).forEach(e=>{for(var o=!1,n=0;n<r.length;n+=2)r[n](e)&&r[n+1](e)&&(o=!0);o||t(e)})}function wasMapValueToRange(e,t,r,o,n){return o+(n-o)*(e-t)/(r-t)}function wasHexToRGB(e){e=e.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i,function(e,t,r,o){return t+t+r+r+o+o});var t=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(e);return t?{r:parseInt(t[1],16),g:parseInt(t[2],16),b:parseInt(t[3],16)}:null}function wasRGBToHex(e,t,r){return"#"+((1<<24)+(e<<16)+(t<<8)+r).toString(16).slice(1)}Array.prototype.toObject||(Array.prototype.toObject=function(){return wasArrayToObject(this)}),"function"==typeof jQuery&&$.extend({toObject:wasArrayToObject}),Array.prototype.product||(Array.prototype.product=function(e){return wasProduct(this,e)}),"function"==typeof jQuery&&$.extend({product:wasProduct}),Array.prototype.stride||(Array.prototype.stride=function(e){return wasStride(this,e)}),"function"==typeof jQuery&&$.extend({stride:wasStride}),Array.prototype.chunk||(Array.prototype.chunk=function(e,t){return wasChunk(this,t)}),"function"==typeof jQuery&&$.extend({chunk:wasChunk}),Array.prototype.equals||(Array.prototype.equals=function(e,t){return wasEquals(this,t)}),"function"==typeof jQuery&&$.extend({equals:wasEquals}),"undefined"!=typeof module&&void 0!==module.exports&&(module.exports.collections={product:wasProduct,stride:wasStride,chunk:wasChunk,equals:wasEquals,toObject:wasArrayToObject}),"undefined"!=typeof module&&void 0!==module.exports&&(module.exports.formats={csv:{CSVToArray:wasCSVToArray,ArrayToCSV:wasArrayToCSV},kvp:{KeyValueToObject:wasKeyValueToObject,KeyValueGet:wasKeyValueGet,KeyValueSet:wasKeyValueSet}}),Array.prototype.switch||(Array.prototype.switch=function(){wasSwitch(this,arguments[0],arguments.slice(1))}),"function"==typeof jQuery&&$.extend({switch:wasSwitch}),"undefined"!=typeof module&&void 0!==module.exports&&(module.exports.lambda={switch:wasSwitch}),"undefined"!=typeof module&&void 0!==module.exports&&(module.exports.mathematics={MapValueToRange:wasMapValueToRange}),"undefined"!=typeof module&&void 0!==module.exports&&(module.exports.physics={HexToRGB:wasHexToRGB,RGBToHex:wasRGBToHex});