-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (59 loc) · 1.76 KB
/
index.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
import fetch from 'node-fetch';
// Recommend using node-fetch for those familiar with JS fetch
/**
* Author: Raoul A. Bock.
* Senior Software Engineer
* Deepcatch Namibia Holdings
* Assessment Due Date: 2021-09-16
*/
const ENDPOINT = 'https://nt-cdn.s3.amazonaws.com/colors.json';
/**
* @param name filter for color name
* @param hex filter for color hex code
* @param compName filter for complementary color name
* @param compHex filter for complementary color hex code
* @returns Promise
*/
const fetchColors = async ({ name, hex, compName, compHex }) => {
const res = fetch(ENDPOINT);
const data = await getColors();
let res= [];
name = prep(name);
hex = prep(hex);
compName = prep(compName);
compHex = prep(compHex);
//Filter by top-level keys
const top = searchColors({data:data,name:name,hex:hex});
if (top.length > 0) {
Object.keys(top).forEach(n=>{res.push(top[n]);
});
}
// Deep filter by complementary colors
Object.keys(data).forEach(e=> {
const {comp} = data[e];
const comps = searchColors({data:comp,name:compName,hex:compHex});
if (comps.length > 0){
Object.keys(comps).forEach(x=> {
res.push(data[e]);
});
}
});
/*
* Your code for filtering against datasets comes here.
* Please return a filtered array, or promise from fetch function.
* Run "npm test" to test your results before making your commit.
*/
return res;
};
const searchColors = ({data,name,hex}) => {
return data.filter(k => k.name.toLowerCase().includes(name) || k.hex.toLowerCase().includes(hex));
}
const getColors = async () => {
const response = await fetch(ENDPOINT);
return await response.json();
}
const prep = (str) => {
return (str == undefined) ? undefined:str.toLowerCase();
}
// Leave this here
export default fetchColors;