/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 |
}; |