-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelper.js
76 lines (50 loc) · 1.85 KB
/
helper.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
// helper\.js
const puppeteer = require('puppeteer');
// append the item's category to the base url and add it as a property
// used in map()
let addUrlToItem = function(item) {
const baseURL = "https://shop-usa.palaceskateboards.com/collections";
item.url = `${baseURL}/${item.category}`;
return item;
};
// checks if category passed in matches a valid category
let isValidCategory = function(userCategory) {
if(typeof userCategory !== "string") // the category must be a string to be valid
return false;
const validCategories = ["jackets", "shirting", "trousers", "tracksuits",
"sweatshirts", "tops", "t-shirts", "hats",
"footwear", "accessories", "hardware"];
// check if category the user entered is inside the valid list
return validCategories.includes(userCategory);
};
let validateItems = function(rawItems) {
rawItems.forEach(rawItem => {
// validate the category name (it's a string and matches a valid category)
if(!isValidCategory(rawItem.category)) {
console.log("this item's category is malformed:\n");
console.log(rawItem);
}
});
};
/*
* Checks for file name in command line arguments
* If no file specified, it defaults to 'items.js'
* There is no validation of 'items.js' file,
* or if user file was valid
*/
function get_items() {
const numAcceptableCLParams = 3;
const filenameIndex = 2;
let itemsFileName = 'items.js'; // use if nothing was passed through the CL
// command line contained a file name
if (process.argv.length === numAcceptableCLParams)
itemsFileName = process.argv[filenameIndex];
// import items from file that's relative to the current directory
let rawItems = require(`./${itemsFileName}`);
validateItems(rawItems);
let itemsWithUrls = rawItems.map(addUrlToItem); // add the category's url to each item
return itemsWithUrls;
};
module.exports = {
get_items: get_items
};