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) { |
||
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) { |
|
25 | var patterncategories = []; |
||
26 | 4 | if (patterns[pattern].pattern && patterns[pattern].pattern.categories) { |
|
27 | var patterncategories = |
||
0 ignored issues
–
show
|
|||
28 | !Array.isArray(patterns[pattern].pattern.categories) ? |
||
29 | [patterns[pattern].pattern.categories] : |
||
30 | patterns[pattern].pattern.categories; |
||
31 | } |
||
32 | for (var i in patterncategories) { |
||
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; |
||
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; |
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.