was.js

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 25  →  ?path2? @ 26
/trunk/dist/was.js
@@ -3,69 +3,63 @@
* 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);
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) {
var a = this;
return $.map(
new Array(Math.max(this.length, a.length)),
function(e, i) {
var o = {};
o[a[i]] = b[i];
return o;
});
};
Array.prototype.product = wasProduct;
}
$.extend({
product: function(a, b) {
return $.map(
new Array(Math.max(this.length, a.length)),
function(e, i) {
var o = {};
o[a[i]] = b[i];
return o;
});
}
});
// jQuery
if(typeof jQuery === 'function') {
$.extend({
product: wasProduct
});
}
 
/*************************************************************************/
/* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
/*************************************************************************/
function wasStride(s) {
return this.filter(function(e, i) {
return i % s === 0;
});
}
if (!Array.prototype.stride) {
Array.prototype.stride = function(s) {
return this.filter(function(e, i) {
return i % s === 0;
});
};
Array.prototype.stride = wasStride;
}
$.extend({
stride: function(a, s) {
return a.filter(function(e, i) {
return i % s === 0;
});
}
});
// jQuery
if(typeof jQuery === 'function') {
$.extend({
stride: wasStride
});
}
 
/*************************************************************************/
/* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
/*************************************************************************/
// Vanilla JavaScript
function wasChunk(n) {
if (!this.length) {
return [];
}
return [this.slice(0, n)]
.concat(this.slice(n).wasChunk(n));
}
if (!Array.prototype.chunk) {
Array.prototype.chunk = function(n) {
if (!this.length) {
return [];
}
return [this.slice(0, n)]
.concat(this.slice(n).chunk(n));
};
Array.prototype.chunk = wasChunk;
}
$.extend({
chunk: function(a, n) {
if (!a.length) {
return [];
}
return [a.slice(0, n)]
.concat(a.slice(n).chunk(n));
}
});
// jQuery
if(typeof jQuery === 'function') {
$.extend({
chunk: wasChunk
});
}
 
/*************************************************************************/
/* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
@@ -72,63 +66,53 @@
/*************************************************************************/
/*stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript*/
/*************************************************************************/
if (!Array.prototype.equals) {
// attach the .equals method to Array's prototype to call it on any array
Array.prototype.equals = function(a) {
// if the other array is a falsy value, return
if (!a) {
return false;
}
// Vanilla JavaScript
function wasEquals(a) {
// if the other array is a falsy value, return
if (!a) {
return false;
}
 
// compare lengths - can save a lot of time
if (this.length !== a.length) {
return false;
}
// compare lengths - can save a lot of time
if (this.length !== a.length) {
return false;
}
 
for (var i = 0, l = this.length; i < l; i++) {
// Check if we have nested arrays
if (this[i] instanceof Array && a[i] instanceof Array) {
// recurse into the nested arrays
if (!this[i].equals(a[i])) {
return false;
}
} else if (this[i] !== a[i]) {
// Warning - two different object instances will never be equal: {x:20} != {x:20}
for (var i = 0, l = this.length; i < l; i++) {
// Check if we have nested arrays
if (this[i] instanceof Array && a[i] instanceof Array) {
// recurse into the nested arrays
if (!this[i].equals(a[i])) {
return false;
}
} else if (this[i] !== a[i]) {
// Warning - two different object instances will never be equal: {x:20} != {x:20}
return false;
}
return true;
};
}
return true;
}
if (!Array.prototype.equals) {
// attach the .equals method to Array's prototype to call it on any array
Array.prototype.equals = wasEquals;
}
// jQuery
if(typeof jQuery === 'function') {
$.extend({
equals: wasEquals
});
}
 
$.extend({
equals: function(a) {
// if the other array is a falsy value, return
if (!a) {
return false;
}
/*************************************************************************/
/* Node.JS package export. */
/*************************************************************************/
module.exports.collections.arrays = {
product: wasProduct,
stride: wasStride,
chunk: wasChunk,
equals: wasEquals
};
 
// compare lengths - can save a lot of time
if (this.length !== a.length) {
return false;
}
 
for (var i = 0, l = this.length; i < l; i++) {
// Check if we have nested arrays
if (this[i] instanceof Array && a[i] instanceof Array) {
// recurse into the nested arrays
if (!this[i].equals(a[i])) {
return false;
}
} else if (this[i] !== a[i]) {
// Warning - two different object instances will never be equal: {x:20} != {x:20}
return false;
}
}
return true;
}
});
 
/* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */
/*************************************************************************/
function wasCSVToArray(csv) {
@@ -185,9 +169,16 @@
return csv.join();
}
 
/*************************************************************************/
/* Node.JS package export. */
/*************************************************************************/
module.exports.formats.csv = {
CSVToArray: wasCSVToArray,
ArrayToCSV: wasArrayToCSV
};
/* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
/*************************************************************************/
function wasKeyValueObjectify(a) {
function wasKeyValueToObject(a) {
var o = {};
a.reduce(function(a, c, i) {
i = Math.floor(i / 2);
@@ -202,10 +193,17 @@
return o;
}
 
/*************************************************************************/
/* Node.JS package export. */
/*************************************************************************/
module.exports.formats.kvp = {
KeyValueToObject: wasKeyValueToObject
};
/* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
/*************************************************************************/
/* fuss/lambda_calculus/functional_programming/aggregators @ grimore.org */
/*************************************************************************/
// Vanilla ES6 JavaScript
function wasSwitch(q, d, ...c) {
if(c.length % 2 !== 0) {
throw "Pairs of predicates expected for cases";
@@ -228,8 +226,6 @@
}
});
}
 
// Vanilla ES6 JavaScript
if (!Array.prototype.switch) {
Array.prototype.switch = wasSwitch;
}
@@ -239,8 +235,11 @@
switch: wasSwitch
});
}
// Node.JS
module.exports = {
 
/*************************************************************************/
/* Node.JS package export. */
/*************************************************************************/
module.exports.lambda.aggregators = {
switch: wasSwitch
};
 
@@ -252,6 +251,13 @@
);
}
 
/*************************************************************************/
/* Node.JS package export. */
/*************************************************************************/
module.exports.formats.kvp = {
MapValueToRange: wasMapValueToRange
};
 
/* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */
/*************************************************************************/
function wasHexToRGB(hex) {
@@ -282,3 +288,11 @@
b
).toString(16).slice(1);
}
 
/*************************************************************************/
/* Node.JS package export. */
/*************************************************************************/
module.exports.formats.kvp = {
HexToRGB: wasHexToRGB,
RGBToHex: wasRGBToHex
};
/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 wasCSVToArray(t){var r=[],e=[],n="";do{var i=t.charAt(0);if(t=t.slice(1,t.length),","!==i)if('"'!==i||t.charAt(0)!==i)if('"'!==i)n+=i;else{if(e[e.length-1]!==i){e.push(i);continue}e.pop()}else n+=i,t=t.slice(1,t.length);else{if('"'!==e[e.length-1]){r.push(n),n="";continue}n+=i}}while(""!==t);return r.push(n),r}function wasArrayToCSV(t){for(var r=[],e=0;e<t.length;++e){var n=t[e].toString().replace('"','""');/"\s,\r\n/.test(n)?r[e]='"'+n+'"':r[e]=n}return r.join()}function wasKeyValueObjectify(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,n){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 n=!1,i=0;i<e.length;i+=2)e[i](t)&&e[i+1](t)&&(n=!0);n||r(t)})}function wasMapValueToRange(t,r,e,n,i){return n+(i-n)*(t-r)/(e-r)}function wasHexToRGB(t){t=t.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i,function(t,r,e,n){return r+r+e+e+n+n});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=function(t){var r=this;return $.map(new Array(Math.max(this.length,r.length)),function(e,n){var i={};return i[r[n]]=t[n],i})}),$.extend({product:function(t,r){return $.map(new Array(Math.max(this.length,t.length)),function(e,n){var i={};return i[t[n]]=r[n],i})}}),Array.prototype.stride||(Array.prototype.stride=function(t){return this.filter(function(r,e){return e%t==0})}),$.extend({stride:function(t,r){return t.filter(function(t,e){return e%r==0})}}),Array.prototype.chunk||(Array.prototype.chunk=function(t){return this.length?[this.slice(0,t)].concat(this.slice(t).chunk(t)):[]}),$.extend({chunk:function(t,r){return t.length?[t.slice(0,r)].concat(t.slice(r).chunk(r)):[]}}),Array.prototype.equals||(Array.prototype.equals=function(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}),$.extend({equals:function(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}}),Array.prototype.switch||(Array.prototype.switch=wasSwitch),"function"==typeof jQuery&&$.extend({switch:wasSwitch}),module.exports={switch:wasSwitch};
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.arrays={product:wasProduct,stride:wasStride,chunk:wasChunk,equals:wasEquals},module.exports.formats.csv={CSVToArray:wasCSVToArray,ArrayToCSV:wasArrayToCSV},module.exports.formats.kvp={KeyValueToObject:wasKeyValueToObject},Array.prototype.switch||(Array.prototype.switch=wasSwitch),"function"==typeof jQuery&&$.extend({switch:wasSwitch}),module.exports.lambda.aggregators={switch:wasSwitch},module.exports.formats.kvp={MapValueToRange:wasMapValueToRange},module.exports.formats.kvp={HexToRGB:wasHexToRGB,RGBToHex:wasRGBToHex};
/trunk/lib/collections/arrays/arrays.js
@@ -105,7 +105,7 @@
/* Node.JS package export. */
/*************************************************************************/
module.exports.collections.arrays = {
stride: wasProduct,
product: wasProduct,
stride: wasStride,
chunk: wasChunk,
equals: wasEquals