was.js – Blame information for rev 25

Subversion Repositories:
Rev:
Rev Author Line No. Line
2 office 1 /*************************************************************************/
2 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
3 /*************************************************************************/
25 office 4 function wasProduct(b) {
5 var a = this;
6 var m = Math.max(this.length, a.length);
7 var o = {};
8 for(var i = 0; i < m; ++i) {
9 o[a[i]] = b[i];
10 }
11 return o;
12 }
2 office 13 if (!Array.prototype.product) {
25 office 14 Array.prototype.product = wasProduct;
2 office 15 }
25 office 16 // jQuery
17 if(typeof jQuery === 'function') {
18 $.extend({
19 product: wasProduct
20 });
21 }
2 office 22  
23 /*************************************************************************/
24 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
25 /*************************************************************************/
25 office 26 function wasStride(s) {
27 return this.filter(function(e, i) {
28 return i % s === 0;
29 });
30 }
2 office 31 if (!Array.prototype.stride) {
25 office 32 Array.prototype.stride = wasStride;
2 office 33 }
25 office 34 // jQuery
35 if(typeof jQuery === 'function') {
36 $.extend({
37 stride: wasStride
38 });
39 }
2 office 40  
41 /*************************************************************************/
42 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
43 /*************************************************************************/
25 office 44 // Vanilla JavaScript
45 function wasChunk(n) {
46 if (!this.length) {
47 return [];
48 }
49 return [this.slice(0, n)]
50 .concat(this.slice(n).wasChunk(n));
51 }
2 office 52 if (!Array.prototype.chunk) {
25 office 53 Array.prototype.chunk = wasChunk;
2 office 54 }
25 office 55 // jQuery
56 if(typeof jQuery === 'function') {
57 $.extend({
58 chunk: wasChunk
59 });
60 }
5 office 61  
62 /*************************************************************************/
63 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
64 /*************************************************************************/
65 /*stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript*/
66 /*************************************************************************/
25 office 67 // Vanilla JavaScript
68 function wasEquals(a) {
69 // if the other array is a falsy value, return
70 if (!a) {
71 return false;
72 }
5 office 73  
25 office 74 // compare lengths - can save a lot of time
75 if (this.length !== a.length) {
76 return false;
77 }
5 office 78  
25 office 79 for (var i = 0, l = this.length; i < l; i++) {
80 // Check if we have nested arrays
81 if (this[i] instanceof Array && a[i] instanceof Array) {
82 // recurse into the nested arrays
83 if (!this[i].equals(a[i])) {
5 office 84 return false;
85 }
25 office 86 } else if (this[i] !== a[i]) {
87 // Warning - two different object instances will never be equal: {x:20} != {x:20}
88 return false;
5 office 89 }
25 office 90 }
91 return true;
5 office 92 }
25 office 93 if (!Array.prototype.equals) {
94 // attach the .equals method to Array's prototype to call it on any array
95 Array.prototype.equals = wasEquals;
96 }
97 // jQuery
98 if(typeof jQuery === 'function') {
99 $.extend({
100 equals: wasEquals
101 });
102 }
5 office 103  
25 office 104 /*************************************************************************/
105 /* Node.JS package export. */
106 /*************************************************************************/
107 module.exports.collections.arrays = {
108 stride: wasProduct,
109 stride: wasStride,
110 chunk: wasChunk,
111 equals: wasEquals
112 };