1 | |||
2 | 1 | var patternByCategorySlug = require('../util/filter-patterns.js'); |
|
3 | |||
4 | 1 | module.exports = function ( ) { |
|
5 | 40 | var patterns = this.data.patterns; |
|
6 | |||
7 | // categories data |
||
8 | 40 | var categories = []; |
|
9 | 40 | for (var pattern in patterns) { |
|
0 ignored issues
–
show
|
|||
10 | var patterncategories = |
||
11 | !Array.isArray(patterns[pattern].pattern.categories) ? |
||
12 | [patterns[pattern].pattern.categories] : |
||
13 | patterns[pattern].pattern.categories; |
||
14 | for (var i in patterncategories) { |
||
0 ignored issues
–
show
A for in loop automatically includes the property of any prototype object, consider checking the key using
hasOwnProperty .
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: var someObject;
for (var key in someObject) {
if ( ! someObject.hasOwnProperty(key)) {
continue; // Skip keys from the prototype.
}
doSomethingWith(key);
}
Loading history...
|
|||
15 | var cat = { |
||
16 | name: String(patterncategories[i]), |
||
17 | slug: String(patterncategories[i]) |
||
18 | }; |
||
19 | 2 | if ( !isCatInList( categories, cat, 'slug' ) ) { |
|
20 | var catPatterns = patternByCategorySlug(cat.slug, patterns); |
||
21 | cat.patterns = catPatterns; |
||
22 | cat.patternsCount = (function (b) { |
||
23 | var c = 0; for (var a in b) { c++; }; return c; |
||
0 ignored issues
–
show
A for in loop automatically includes the property of any prototype object, consider checking the key using
hasOwnProperty .
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: var someObject;
for (var key in someObject) {
if ( ! someObject.hasOwnProperty(key)) {
continue; // Skip keys from the prototype.
}
doSomethingWith(key);
}
Loading history...
|
|||
24 | })(catPatterns); |
||
25 | categories.push(cat); |
||
26 | } |
||
27 | } |
||
28 | } |
||
29 | |||
30 | 40 | categories = sortByKey(categories, 'slug'); |
|
31 | 40 | this.data.categories = categories; |
|
32 | 40 | return (categories); |
|
33 | } |
||
34 | |||
35 | function isCatInList ( arr, obj, key ) { |
||
36 | for (var idx in arr) { |
||
0 ignored issues
–
show
A for in loop automatically includes the property of any prototype object, consider checking the key using
hasOwnProperty .
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: var someObject;
for (var key in someObject) {
if ( ! someObject.hasOwnProperty(key)) {
continue; // Skip keys from the prototype.
}
doSomethingWith(key);
}
Loading history...
|
|||
37 | 5 | if ( arr[idx][key] && obj[key] && (arr[idx][key] == obj[key]) ) { |
|
38 | return (true); |
||
39 | } |
||
40 | } |
||
41 | return false; |
||
42 | } |
||
43 | function sortByKey(array, key) { |
||
44 | 40 | return array.sort(function(a, b) { |
|
45 | var x = a[key]; var y = b[key]; |
||
46 | 4 | return ((x < y) ? -1 : ((x > y) ? 1 : 0)); |
|
47 | }); |
||
48 | } |
||
49 |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: