was.js

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 29  →  ?path2? @ 30
/trunk/dist/was.js
@@ -3,9 +3,8 @@
* Copyright (c) 2017 Wizardry and Steamworks <office@grimore.org>; Licensed GPL-3.0 */
/* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
/*************************************************************************/
function wasProduct(b) {
var a = this;
var m = Math.max(this.length, a.length);
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];
@@ -13,7 +12,9 @@
return o;
}
if (!Array.prototype.product) {
Array.prototype.product = wasProduct;
Array.prototype.product = function(b) {
return wasProduct(this, b);
};
}
// jQuery
if(typeof jQuery === 'function') {
@@ -25,13 +26,15 @@
/*************************************************************************/
/* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
/*************************************************************************/
function wasStride(s) {
return this.filter(function(e, i) {
function wasStride(a, s) {
return a.filter(function(e, i) {
return i % s === 0;
});
}
if (!Array.prototype.stride) {
Array.prototype.stride = wasStride;
Array.prototype.stride = function(s) {
return wasStride(this, s);
};
}
// jQuery
if(typeof jQuery === 'function') {
@@ -44,15 +47,17 @@
/* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
/*************************************************************************/
// Vanilla JavaScript
function wasChunk(n) {
if (!this.length) {
function wasChunk(a, n) {
if (!a.length) {
return [];
}
return [this.slice(0, n)]
.concat(this.slice(n).wasChunk(n));
return [a.slice(0, n)]
.concat(a.slice(n).wasChunk(n));
}
if (!Array.prototype.chunk) {
Array.prototype.chunk = wasChunk;
Array.prototype.chunk = function(a, n) {
return wasChunk(this, n);
};
}
// jQuery
if(typeof jQuery === 'function') {
@@ -67,25 +72,25 @@
/*stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript*/
/*************************************************************************/
// Vanilla JavaScript
function wasEquals(a) {
function wasEquals(a, b) {
// if the other array is a falsy value, return
if (!a) {
if (!b) {
return false;
}
 
// compare lengths - can save a lot of time
if (this.length !== a.length) {
if (a.length !== b.length) {
return false;
}
 
for (var i = 0, l = this.length; i < l; i++) {
for (var i = 0, l = a.length; i < l; i++) {
// Check if we have nested arrays
if (this[i] instanceof Array && a[i] instanceof Array) {
if (a[i] instanceof Array && b[i] instanceof Array) {
// recurse into the nested arrays
if (!this[i].equals(a[i])) {
if (!a[i].equals(b[i])) {
return false;
}
} else if (this[i] !== a[i]) {
} else if (a[i] !== b[i]) {
// Warning - two different object instances will never be equal: {x:20} != {x:20}
return false;
}
@@ -94,7 +99,9 @@
}
if (!Array.prototype.equals) {
// attach the .equals method to Array's prototype to call it on any array
Array.prototype.equals = wasEquals;
Array.prototype.equals = function(a, b) {
return wasEquals(this, b);
};
}
// jQuery
if(typeof jQuery === 'function') {
@@ -209,7 +216,7 @@
throw "Pairs of predicates expected for cases";
}
(Array.isArray(this) ? this : [ this ]).forEach((s) => {
(Array.isArray(q) ? q : [ q ]).forEach((s) => {
var m = false;
for(var i = 0; i < c.length; i += 2) {
if(!c[i](s)) {
@@ -227,7 +234,9 @@
});
}
if (!Array.prototype.switch) {
Array.prototype.switch = wasSwitch;
Array.prototype.switch = function(d, ...c) {
wasSwitch(this, d, ...c);
};
}
// jQuery
if(typeof jQuery === 'function') {
/trunk/dist/was.min.js
@@ -2,4 +2,4 @@
* http://grimore.org
* Copyright (c) 2017 Wizardry and Steamworks <office@grimore.org>; Licensed GPL-3.0 */
 
function wasProduct(t){for(var r=Math.max(this.length,this.length),e={},a=0;a<r;++a)e[this[a]]=t[a];return e}function wasStride(t){return this.filter(function(r,e){return e%t==0})}function wasChunk(t){return this.length?[this.slice(0,t)].concat(this.slice(t).wasChunk(t)):[]}function wasEquals(t){if(!t)return!1;if(this.length!==t.length)return!1;for(var r=0,e=this.length;r<e;r++)if(this[r]instanceof Array&&t[r]instanceof Array){if(!this[r].equals(t[r]))return!1}else if(this[r]!==t[r])return!1;return!0}function wasCSVToArray(t){var r=[],e=[],a="";do{var o=t.charAt(0);if(t=t.slice(1,t.length),","!==o)if('"'!==o||t.charAt(0)!==o)if('"'!==o)a+=o;else{if(e[e.length-1]!==o){e.push(o);continue}e.pop()}else a+=o,t=t.slice(1,t.length);else{if('"'!==e[e.length-1]){r.push(a),a="";continue}a+=o}}while(""!==t);return r.push(a),r}function wasArrayToCSV(t){for(var r=[],e=0;e<t.length;++e){var a=t[e].toString().replace('"','""');/"\s,\r\n/.test(a)?r[e]='"'+a+'"':r[e]=a}return r.join()}function wasKeyValueToObject(t){var r={};return t.reduce(function(t,r,e){return e=Math.floor(e/2),t[e]||(t[e]=[]),t[e].push(r),t},[]).forEach(function(t,e,a){r[t[0]]=t[1]},r),r}function wasSwitch(t,r,...e){if(e.length%2!=0)throw"Pairs of predicates expected for cases";(Array.isArray(this)?this:[this]).forEach(t=>{for(var a=!1,o=0;o<e.length;o+=2)e[o](t)&&e[o+1](t)&&(a=!0);a||r(t)})}function wasMapValueToRange(t,r,e,a,o){return a+(o-a)*(t-r)/(e-r)}function wasHexToRGB(t){t=t.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i,function(t,r,e,a){return r+r+e+e+a+a});var r=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(t);return r?{r:parseInt(r[1],16),g:parseInt(r[2],16),b:parseInt(r[3],16)}:null}function wasRGBToHex(t,r,e){return"#"+((1<<24)+(t<<16)+(r<<8)+e).toString(16).slice(1)}Array.prototype.product||(Array.prototype.product=wasProduct),"function"==typeof jQuery&&$.extend({product:wasProduct}),Array.prototype.stride||(Array.prototype.stride=wasStride),"function"==typeof jQuery&&$.extend({stride:wasStride}),Array.prototype.chunk||(Array.prototype.chunk=wasChunk),"function"==typeof jQuery&&$.extend({chunk:wasChunk}),Array.prototype.equals||(Array.prototype.equals=wasEquals),"function"==typeof jQuery&&$.extend({equals:wasEquals}),module.exports.collections={product:wasProduct,stride:wasStride,chunk:wasChunk,equals:wasEquals},module.exports.formats={CSVToArray:wasCSVToArray,ArrayToCSV:wasArrayToCSV},module.exports.formats={KeyValueToObject:wasKeyValueToObject},Array.prototype.switch||(Array.prototype.switch=wasSwitch),"function"==typeof jQuery&&$.extend({switch:wasSwitch}),module.exports.lambda={switch:wasSwitch},module.exports.mathematics={MapValueToRange:wasMapValueToRange},module.exports.physics={HexToRGB:wasHexToRGB,RGBToHex:wasRGBToHex};
function wasProduct(r,t){for(var e=Math.max(r.length,t.length),n={},a=0;a<e;++a)n[r[a]]=t[a];return n}function wasStride(r,t){return r.filter(function(r,e){return e%t==0})}function wasChunk(r,t){return r.length?[r.slice(0,t)].concat(r.slice(t).wasChunk(t)):[]}function wasEquals(r,t){if(!t)return!1;if(r.length!==t.length)return!1;for(var e=0,n=r.length;e<n;e++)if(r[e]instanceof Array&&t[e]instanceof Array){if(!r[e].equals(t[e]))return!1}else if(r[e]!==t[e])return!1;return!0}function wasCSVToArray(r){var t=[],e=[],n="";do{var a=r.charAt(0);if(r=r.slice(1,r.length),","!==a)if('"'!==a||r.charAt(0)!==a)if('"'!==a)n+=a;else{if(e[e.length-1]!==a){e.push(a);continue}e.pop()}else n+=a,r=r.slice(1,r.length);else{if('"'!==e[e.length-1]){t.push(n),n="";continue}n+=a}}while(""!==r);return t.push(n),t}function wasArrayToCSV(r){for(var t=[],e=0;e<r.length;++e){var n=r[e].toString().replace('"','""');/"\s,\r\n/.test(n)?t[e]='"'+n+'"':t[e]=n}return t.join()}function wasKeyValueToObject(r){var t={};return r.reduce(function(r,t,e){return e=Math.floor(e/2),r[e]||(r[e]=[]),r[e].push(t),r},[]).forEach(function(r,e,n){t[r[0]]=r[1]},t),t}function wasSwitch(r,t,...e){if(e.length%2!=0)throw"Pairs of predicates expected for cases";(Array.isArray(r)?r:[r]).forEach(r=>{for(var n=!1,a=0;a<e.length;a+=2)e[a](r)&&e[a+1](r)&&(n=!0);n||t(r)})}function wasMapValueToRange(r,t,e,n,a){return n+(a-n)*(r-t)/(e-t)}function wasHexToRGB(r){r=r.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i,function(r,t,e,n){return t+t+e+e+n+n});var t=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(r);return t?{r:parseInt(t[1],16),g:parseInt(t[2],16),b:parseInt(t[3],16)}:null}function wasRGBToHex(r,t,e){return"#"+((1<<24)+(r<<16)+(t<<8)+e).toString(16).slice(1)}Array.prototype.product||(Array.prototype.product=function(r){return wasProduct(this,r)}),"function"==typeof jQuery&&$.extend({product:wasProduct}),Array.prototype.stride||(Array.prototype.stride=function(r){return wasStride(this,r)}),"function"==typeof jQuery&&$.extend({stride:wasStride}),Array.prototype.chunk||(Array.prototype.chunk=function(r,t){return wasChunk(this,t)}),"function"==typeof jQuery&&$.extend({chunk:wasChunk}),Array.prototype.equals||(Array.prototype.equals=function(r,t){return wasEquals(this,t)}),"function"==typeof jQuery&&$.extend({equals:wasEquals}),module.exports.collections={product:wasProduct,stride:wasStride,chunk:wasChunk,equals:wasEquals},module.exports.formats={CSVToArray:wasCSVToArray,ArrayToCSV:wasArrayToCSV},module.exports.formats={KeyValueToObject:wasKeyValueToObject},Array.prototype.switch||(Array.prototype.switch=function(r,...t){wasSwitch(this,r,...t)}),"function"==typeof jQuery&&$.extend({switch:wasSwitch}),module.exports.lambda={switch:wasSwitch},module.exports.mathematics={MapValueToRange:wasMapValueToRange},module.exports.physics={HexToRGB:wasHexToRGB,RGBToHex:wasRGBToHex};