1 | /** |
||
2 | * retrieve current list of pattern categories |
||
3 | */ |
||
4 | function getCategories ( ) { |
||
5 | 50 | var patterns = this.listpatterns(); |
|
6 | |||
7 | function isCatInList ( arr, obj, idx ) { |
||
8 | for (var i in arr) { |
||
0 ignored issues
–
show
|
|||
9 | 5 | if ( arr[i][idx] && obj[idx] && (arr[i][idx] == obj[idx]) ) { |
|
10 | return (true); |
||
11 | } |
||
12 | } |
||
13 | return false; |
||
14 | } |
||
15 | function sortByKey(array, key) { |
||
16 | 50 | return array.sort(function(a, b) { |
|
17 | var x = a[key]; var y = b[key]; |
||
18 | 4 | return ((x < y) ? -1 : ((x > y) ? 1 : 0)); |
|
19 | }); |
||
20 | } |
||
21 | |||
22 | // categories data |
||
23 | 50 | var categories = []; |
|
24 | 50 | for (var pattern in patterns) { |
|
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...
|
|||
25 | var patterncategories = []; |
||
26 | 4 | if (patterns[pattern].pattern && patterns[pattern].pattern.categories) { |
|
27 | var patterncategories = |
||
0 ignored issues
–
show
Comprehensibility
Naming
Best Practice
introduced
by
The variable
patterncategories already seems to be declared on line 25 . Consider using another variable name or omitting the var keyword.
This check looks for variables that are declared in multiple lines. There may be several reasons for this. In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs. If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.
Loading history...
|
|||
28 | !Array.isArray(patterns[pattern].pattern.categories) ? |
||
29 | [patterns[pattern].pattern.categories] : |
||
30 | patterns[pattern].pattern.categories; |
||
31 | } |
||
32 | 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...
|
|||
33 | var cat = { |
||
34 | name: String(patterncategories[i]), |
||
35 | slug: String(patterncategories[i]) |
||
36 | }; |
||
37 | 2 | if ( !isCatInList( categories, cat, 'slug' ) ) { |
|
38 | var catPatterns = this.listpatterns(cat.slug); |
||
39 | cat.patterns = catPatterns; |
||
40 | cat.patternsCount = (function (b) { |
||
41 | 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...
|
|||
42 | })(catPatterns); |
||
43 | categories.push(cat); |
||
44 | } |
||
45 | } |
||
46 | } |
||
47 | |||
48 | 50 | categories = sortByKey(categories, 'slug'); |
|
49 | 50 | return (categories); |
|
50 | } |
||
51 | |||
52 | module.exports = getCategories; |
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: