This repository was archived by the owner on May 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
134 lines (121 loc) · 3.69 KB
/
index.js
File metadata and controls
134 lines (121 loc) · 3.69 KB
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
/*
_ ____ _
/ \ _ __ _ _ / ___|___ __| | ___
/ _ \ | '_ \| | | | | | / _ \ / _` |/ _ \
/ ___ \| | | | |_| | | |__| (_) | (_| | __/
/_/ \_\_| |_|\__, | \____\___/ \__,_|\___|
|___/
*/
/**
* @description A bare minimum set of utilities for working with collections
* @constructor
*/
function Collection() {}
/**
* @description Iterate over each element in a collection.
*
* @param {object|array} collection The collection to iterate over can be either an array or an object
* @param {callback(index, element)} callback is executed for each element in the collection
*
* @tutorial collection.each
*/
Collection.prototype.each = function(collection, callback) {
(Array.isArray(collection) ? eachOfArray : eachOfObject)(collection, callback);
};
/**
* @description Return a new collection being a filtered subset of elements from a collection
*
* @param {object|array} collection The collection to iterate over can be either an array or an object
* @param {callback(element)} callback Predicate callback is executed for each element in the collection and must
* return true if the element will be included in the filter
* @param {Object|Array} [empty=empty array] You can provide an empty array or object to be populated by the filter procedure. By
* default the returned collection is an array however you can populate properties on an object by specifying an
* object as the empty.
*
* @tutorial collection.filter
*
* @returns {object|array} collection
*/
Collection.prototype.filter = function(collection, callback, empty) {
var result = empty || [];
this.each(collection, function(index, item) {
if (callback(item)) {
(Array.isArray(result) ? collectArray : collectObject)(index, item, result);
}
});
return result;
};
/**
* @description Find the first item satisfied by predicate callback in the provided collection
*
* @param {object|array} collection The collection to iterate over can be either an array or an object
* @param {callback(element)} callback Predicate callback is executed for each element in the collection and must
* return true if the element will be included in the filter
*
* @tutorial collection.find
*
* @returns {* | null} returns the found element or null if no element wa found
*/
Collection.prototype.find = function(collection, callback) {
var found = null;
this.each(collection, function(index, item) {
if (callback(item)) {
found = item;
return false;
}
});
return found;
};
/**
* Adds the value to the collection, key is always ignored
*
* @param {Integer} key
* @param {Object} value
* @param {Array} collection
* @private
*/
function collectArray(key, value, collection) {
collection.push(value);
}
/**
* Adds the specified key value pair to the collection
*
* @param {String} key
* @param {Object} value
* @param {Object} collection
* @private
*/
function collectObject(key, value, collection) {
collection[key] = value;
}
/**
* for each item in object execute callback
*
* @param {Array} object
* @param {function} callback
* @private
*/
function eachOfArray(object, callback) {
var index = 0;
for (; index < object.length; index++) {
if (callback.call(object[index], index, object[index]) === false) {
break;
}
}
}
/**
* for each property on object execute callback
*
* @param {Object} object
* @param {function} callback
* @private
*/
function eachOfObject(object, callback) {
var index;
for (index in object) {
if (callback.call(object[index], index, object[index]) === false) {
break;
}
}
}
module.exports = new Collection();