-
Notifications
You must be signed in to change notification settings - Fork 7
/
getSortedImports.js
84 lines (76 loc) · 3 KB
/
getSortedImports.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
const sortArr = arr => arr.sort((a,b) => b[1] - a[1]);
const arrToObj = arr => arr.reduce((acc, cur) => {
acc[cur[0]] = cur[1];
return acc;
}, {});
const getImportCategory = importPath => {
const categoriesRegex = /\b(?:react-icons|utilities|layouts|react-charts|react-topology|react-table|react-code-editor|react-console|react-user-feedback|react-catalog-view-extension|react-log-viewer|react-virtualized-extension|extensions|quickstarts|react-styles|react-tokens)\b/gi;
const matches = importPath.match(categoriesRegex);
const category = matches ? matches[0] : 'components';
return category;
}
const sortImportsByCount = importsObj => {
const entriesArr = Object.entries(importsObj);
const sortedEntriesArr = sortArr(entriesArr);
const sortedEntriesObj = arrToObj(sortedEntriesArr);
return sortedEntriesObj;
}
// Build object with imports grouped by category
const buildCategorizedImports = obj => {
let categorizedImports = {};
const importsArr = Object.entries(obj);
importsArr.map(([importPath, importedItemsObj]) => {
// get category based on import path & add field to categorizedImports if needed
const category = getImportCategory(importPath);
if (!categorizedImports[category]) {
categorizedImports[category] = {};
}
// loop through importedItems & add to categorizedImports
const importedItemsArr = Object.entries(importedItemsObj);
importedItemsArr.map(([importName, importCount]) => {
categorizedImports[category][importName] = categorizedImports[category][importName]
? categorizedImports[category][importName] += importCount
: importCount;
});
});
return categorizedImports;
}
// Sort categorized imports by quantity
const sortCategorizedImports = categorizedImportsObj => {
let sortedCategorizedImports = {};
const categorizedImportsArr = Object.entries(categorizedImportsObj);
categorizedImportsArr.map(([category, importedItemsObj]) => {
const sortedCategory = sortImportsByCount(importedItemsObj);
sortedCategorizedImports[category] = sortedCategory;
});
return sortedCategorizedImports;
}
const getSortedImports = (importsObj) => {
const importCategories = buildCategorizedImports(importsObj);
const sortedCategories = sortCategorizedImports(importCategories);
return sortedCategories;
}
const sortUsageSection = (usageObj) => Object
.entries(usageObj)
.sort((
[_pfItemName1, pfItemData1],
[_pfItemName2, pfItemData2]
) => pfItemData2.product_count - pfItemData1.product_count)
.reduce((acc, [curPfItemName, curPfItemData]) => {
acc[curPfItemName] = curPfItemData;
return acc;
}, {});
// Sort productUsage sections by product_count
const getSortedUsage = (productUsageObj) => {
const sortedResult = {};
console.log('GET SORTED USAGE');
Object.entries(productUsageObj).map(([sectionName, sectionData]) => {
const sortedSection = sortUsageSection(sectionData);
sortedResult[sectionName] = sortedSection;
});
return sortedResult;
}
module.exports = {
getSortedImports,
getSortedUsage
}