-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrafity.arrays.js
executable file
·151 lines (136 loc) · 3.64 KB
/
crafity.arrays.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*jslint bitwise: true, unparam: true, maxerr: 50, white: true */
/*globals window*/
/*!
* crafity.arrays
* Copyright(c) 2011 Crafity
* Copyright(c) 2011 Bart Riemens
* Copyright(c) 2011 Galina Slavova
* MIT Licensed
*/
(function (crafity) {
"use strict";
if (!crafity.common) {
throw new Error("Missing dependency 'crafity.common'");
}
var common = crafity.common
, arrays = (crafity.arrays = {});
/**
* Convert an object into an array
* @param obj The object to convert
* @returns The object as an array
*/
arrays.toArray = function (obj) {
common.arg({ name: 'obj', value: obj, required: true });
//return Array.prototype.slice.call(obj, 0);
return Array.apply(null, obj);
};
/**
* Add an object to an array
* @param array The array to add to
* @param obj The object to add
* @returns The array
*/
arrays.add = function (array, obj) {
common.arg({ name: 'array', value: array, required: true, type: Array });
array.push(obj);
return array;
};
/**
* Check if an object is in an array
* @param array
* @param obj
* @returns a boolean value
*/
arrays.contains = function (array, obj) {
common.arg({ name: 'array', value: array, required: true, type: Array });
return !!~array.indexOf(obj);
};
/**
* Check if an array does not contain an object
* @param array The array
* @param obj The object
* @return a boolean value
*/
arrays.contains.not = function (array, obj) {
return !arrays.contains.apply(this, arrays.toArray(arguments));
};
/**
* Return an array, containing the common members of both input arrays.
* Example arrays.intersect([1,2,3], [3,4,5]) returns [3]
* @returns {Array} an array
*/
arrays.intersect = function (first, second) {
if (arguments.length === 0) {
return [].slice();
}
var a1 = first
, a2 = null
, a = []
, n = 1
, l, l2
, i, j;
while (n < arguments.length) {
a2 = arguments[n];
l = a1.length;
l2 = a2.length;
for (i = 0; i < l; i++) {
for (j = 0; j < l2; j++) {
if (a1[i] === a2[j] && a.indexOf(a1[i]) === -1) {
a.push(a1[i]);
}
}
}
n++;
}
return a;
};
/**
* Group values in an array.
* @param {Array} array The array to group
* @param {Function} fn (optional) A grouping function
* @return {Object} A object with grouped key/values
*
* @example A Simple group by example: groupBy([1, 2, 3, 2, 4, 4, 3, 5]).
* Result: { '1': [ 1 ],'2': [ 2, 2 ],'3': [ 3, 3 ],'4': [ 4, 4 ],'5': [ 5 ] }
*
* @example A simple group by and return length example: groupBy([1, 2, 3, 2, 4, 4, 3, 5],
* function (array) {
* return array.length;
* }));
* Result: { '1': 1, '2': 2, '3': 2, '4': 2, '5': 1 }
*
* @example A simple group by and sum example: groupBy([1, 2, 3, 2, 4, 4, 3, 5],
* function (array) {
* return array.reduce(function (seed, value) { return seed + value }, 0);
* }));
* Result: { '1': 1, '2': 4, '3': 6, '4': 8, '5': 5 }
*
*/
arrays.groupBy = function (array, fn) {
var result = {};
array.forEach(function (item) {
result[item] = result[item] || [];
result[item].push(item);
});
if (fn) {
Object.keys(result).forEach(function (key) {
result[key] = fn(result[key]);
});
}
return result;
};
/**
* Get all the unique values in an Array
* @param {Array} array The array to get the unique values from;
* @return {Array} A new array containing all the unique values
*/
arrays.distinct = function (array) {
var result = [];
array.forEach(function (value) {
if (arrays.contains.not(result, value)) {
result.push(value);
}
});
return result;
};
}(window.crafity = window.crafity || {}));