was.js

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 36  →  ?path2? @ 37
/tags/1.0.2/dist/was.js
@@ -0,0 +1,321 @@
/*! was.js - v1.0.1 - 2019-08-01
* http://grimore.org
* Copyright (c) 2019 [object Object]; Licensed GPL-3.0 */
/* 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
});
}
 
/*************************************************************************/
/* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
/*************************************************************************/
// Vanilla JavaScript
function wasChunk(a, n) {
if (!a.length) {
return [];
}
return [a.slice(0, n)]
.concat(a.slice(n).wasChunk(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 variable !== 'undefined') {
module.exports.collections = {
product: wasProduct,
stride: wasStride,
chunk: wasChunk,
equals: wasEquals
};
}
 
/* 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 variable !== 'undefined') {
module.exports.formats = {
CSVToArray: wasCSVToArray,
ArrayToCSV: wasArrayToCSV
};
}
 
/* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
/*************************************************************************/
function wasKeyValueToObject(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;
}
 
/*************************************************************************/
/* Node.JS package export. */
/*************************************************************************/
if(typeof variable !== 'undefined') {
module.exports.formats = {
KeyValueToObject: wasKeyValueToObject
};
}
 
/* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
/*************************************************************************/
/* fuss/lambda_calculus/functional_programming/aggregators @ grimore.org */
/*************************************************************************/
// Vanilla ES6 JavaScript
function wasSwitch() {
if(arguments.length - 2 % 2 !== 0) {
throw "Pairs of predicates expected for cases";
}
(Array.isArray(arguments[0]) ? arguments[0] : [ arguments[0] ]).forEach((s) => {
var m = false;
for(var i = 2; i < arguments.length; i += 2) {
if(!arguments[i](s)) {
continue;
}
if(!arguments[i + 1](s)) {
continue;
}
m = true;
}
if(!m) {
arguments[1](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 variable !== '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 variable !== '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 variable !== 'undefined') {
module.exports.physics = {
HexToRGB: wasHexToRGB,
RGBToHex: wasRGBToHex
};
}
/tags/1.0.2/dist/was.min.js
@@ -0,0 +1,5 @@
/*! was.js - v1.0.1 - 2019-08-01
* http://grimore.org
* Copyright (c) 2019 [object Object]; Licensed GPL-3.0 */
 
function wasProduct(e,r){for(var t=Math.max(e.length,r.length),n={},a=0;a<t;++a)n[e[a]]=r[a];return n}function wasStride(e,r){return e.filter(function(e,t){return t%r==0})}function wasChunk(e,r){return e.length?[e.slice(0,r)].concat(e.slice(r).wasChunk(r)):[]}function wasEquals(e,r){if(!r)return!1;if(e.length!==r.length)return!1;for(var t=0,n=e.length;t<n;t++)if(e[t]instanceof Array&&r[t]instanceof Array){if(!e[t].equals(r[t]))return!1}else if(e[t]!==r[t])return!1;return!0}function wasCSVToArray(e){var r=[],t=[],n="";do{var a=e.charAt(0);if(e=e.slice(1,e.length),","!==a)if('"'!==a||e.charAt(0)!==a)if('"'!==a)n+=a;else{if(t[t.length-1]!==a){t.push(a);continue}t.pop()}else n+=a,e=e.slice(1,e.length);else{if('"'!==t[t.length-1]){r.push(n),n="";continue}n+=a}}while(""!==e);return r.push(n),r}function wasArrayToCSV(e){for(var r=[],t=0;t<e.length;++t){var n=e[t].toString().replace('"','""');/"\s,\r\n/.test(n)?r[t]='"'+n+'"':r[t]=n}return r.join()}function wasKeyValueToObject(e){var r={};return e.reduce(function(e,r,t){return e[t=Math.floor(t/2)]||(e[t]=[]),e[t].push(r),e},[]).forEach(function(e,t,n){r[e[0]]=e[1]},r),r}function wasSwitch(){if(arguments.length-0!=0)throw"Pairs of predicates expected for cases";(Array.isArray(arguments[0])?arguments[0]:[arguments[0]]).forEach(e=>{for(var r=!1,t=2;t<arguments.length;t+=2)arguments[t](e)&&arguments[t+1](e)&&(r=!0);r||arguments[1](e)})}function wasMapValueToRange(e,r,t,n,a){return n+(a-n)*(e-r)/(t-r)}function wasHexToRGB(e){e=e.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i,function(e,r,t,n){return r+r+t+t+n+n});var r=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(e);return r?{r:parseInt(r[1],16),g:parseInt(r[2],16),b:parseInt(r[3],16)}:null}function wasRGBToHex(e,r,t){return"#"+((1<<24)+(e<<16)+(r<<8)+t).toString(16).slice(1)}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,r){return wasChunk(this,r)}),"function"==typeof jQuery&&$.extend({chunk:wasChunk}),Array.prototype.equals||(Array.prototype.equals=function(e,r){return wasEquals(this,r)}),"function"==typeof jQuery&&$.extend({equals:wasEquals}),"undefined"!=typeof variable&&(module.exports.collections={product:wasProduct,stride:wasStride,chunk:wasChunk,equals:wasEquals}),"undefined"!=typeof variable&&(module.exports.formats={CSVToArray:wasCSVToArray,ArrayToCSV:wasArrayToCSV}),"undefined"!=typeof variable&&(module.exports.formats={KeyValueToObject:wasKeyValueToObject}),Array.prototype.switch||(Array.prototype.switch=function(){wasSwitch(this,arguments[0],arguments.slice(1))}),"function"==typeof jQuery&&$.extend({switch:wasSwitch}),"undefined"!=typeof variable&&(module.exports.lambda={switch:wasSwitch}),"undefined"!=typeof variable&&(module.exports.mathematics={MapValueToRange:wasMapValueToRange}),"undefined"!=typeof variable&&(module.exports.physics={HexToRGB:wasHexToRGB,RGBToHex:wasRGBToHex});