Skip to content

Commit eac44b3

Browse files
authored
👔 Added Formatting and Linting (#31)
1 parent b9a4835 commit eac44b3

File tree

13 files changed

+2254
-70
lines changed

13 files changed

+2254
-70
lines changed

.eslintrc.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": ["prettier","airbnb"],
3+
"plugins": ["prettier"],
4+
"rules": {
5+
"prettier/prettier": ["error"]
6+
},
7+
"env": {
8+
"browser": true,
9+
"node": true
10+
}
11+
}

.npmignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
.all-contributorsrc
22
.github
33
CONTRIBUTING.md
4-
docs
4+
docs
5+
.eslintrc.json
6+
.prettierrc

.prettierrc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"printWidth": 100,
3+
"tabWidth": 2,
4+
"singleQuote": true,
5+
"trailingComma": "all",
6+
"bracketSpacing": true,
7+
"jsxBracketSameLine": true,
8+
"parser": "flow",
9+
"semi": true,
10+
"useTabs": false,
11+
"quoteProps": "as-needed",
12+
"jsxSingleQuote": true,
13+
"arrowParens": "always"
14+
}

hooks/useDarkMode.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
1-
import { useState, useEffect } from "react";
1+
import { useState, useEffect } from 'react';
2+
3+
/**
4+
*
5+
* Returns true if globally preferred theme is dark and false if not.
6+
* @returns {boolean} Is dark mode globally preferred.
7+
*/
8+
const getGlobalPreference = () => window.matchMedia('(prefers-color-scheme: dark)').matches;
29

310
/**
411
*
512
* Returns true if user prefers dark mode and false if not.
613
* @returns {boolean} Does user prefers dark mode.
714
*/
8-
const _getUserPreferredMode = () => {
9-
//If localStorage is available user preference is stored, return the stored value.
15+
const getUserPreferredMode = () => {
16+
// If localStorage is available user preference is stored, return the stored value.
1017
if (localStorage) {
11-
const localPreference = localStorage.getItem("dark-mode");
18+
const localPreference = localStorage.getItem('dark-mode');
1219
if (localPreference) {
13-
return JSON.parse(localStorage.getItem("dark-mode"));
20+
return JSON.parse(localStorage.getItem('dark-mode'));
1421
}
22+
// Else return the global dark-mode preference.
23+
return getGlobalPreference();
1524
}
16-
//Else return the global dark-mode preference.
17-
else {
18-
return _getGlobalPreference();
19-
}
25+
// If localStorage does not exist, return the global dark-mode preference.
26+
return getGlobalPreference();
2027
};
2128

22-
/**
23-
*
24-
* Returns true if globally preferred theme is dark and false if not.
25-
* @returns {boolean} Is dark mode globally preferred.
26-
*/
27-
const _getGlobalPreference = () => window.matchMedia("(prefers-color-scheme: dark)").matches;
28-
2929
/**
3030
*
3131
* Custom hook which let's you toggle dark-mode by adding a class to the body.
32-
*
32+
*
3333
* @param {string} className Class name which is to be added to body when dark mode.
3434
* @returns {Array} Array containing a boolean isDarkMode and a darkModeToggle function.
3535
*/
3636
const useDarkMode = (className) => {
37-
const [isDarkMode, setIsDarkMode] = useState(_getUserPreferredMode());
37+
const [isDarkMode, setIsDarkMode] = useState(getUserPreferredMode());
3838

3939
useEffect(() => {
4040
if (isDarkMode) {
@@ -45,7 +45,7 @@ const useDarkMode = (className) => {
4545
}, [isDarkMode]);
4646

4747
const toggleDarkMode = () => {
48-
localStorage.setItem("dark-mode", !isDarkMode);
48+
localStorage.setItem('dark-mode', !isDarkMode);
4949
setIsDarkMode(!isDarkMode);
5050
};
5151

hooks/useDebounce.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { useCallback } from "react";
1+
import { useCallback } from 'react';
22

33
/**
44
* Debounce function generator.
55
* @param {function} inputFunction Function which is to be modified.
66
* @param {number} delay The time delay in milliseconds.
77
* @returns {function} The modified function.
88
*/
9-
function _debounce(inputFunction, delay) {
9+
function debounce(inputFunction, delay) {
1010
let timeout;
11-
return function (...args) {
11+
return (...args) => {
1212
const context = this;
1313
if (timeout) clearTimeout(timeout);
1414
timeout = setTimeout(() => {
@@ -26,6 +26,6 @@ function _debounce(inputFunction, delay) {
2626
* @param {number} delay The time delay in milliseconds.
2727
* @returns {function} The modified function.
2828
*/
29-
const useDebounce = (inputFunction, delay) => useCallback(_debounce(inputFunction, delay), []);
29+
const useDebounce = (inputFunction, delay) => useCallback(debounce(inputFunction, delay), []);
3030

3131
export default useDebounce;

hooks/useGeoLocation.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect } from "react";
1+
import { useState, useEffect } from 'react';
22

33
/**
44
* @typedef {Object} GeoLocation
@@ -13,7 +13,7 @@ import { useState, useEffect } from "react";
1313
const useGeoLocation = () => {
1414
const [geoLocation, setGeoLocation] = useState({});
1515

16-
//Only called on ComponentDidMount
16+
// Only called on ComponentDidMount
1717
useEffect(() => {
1818
// Checking if device supports geolocation
1919
if (navigator.geolocation) {
@@ -28,7 +28,7 @@ const useGeoLocation = () => {
2828
setGeoLocation(userPosition);
2929
});
3030
} else {
31-
throw new Error("The browser does not support geolocation");
31+
throw new Error('The browser does not support geolocation');
3232
}
3333
}, []);
3434

hooks/useLocalStorage.js

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
1-
import { useState } from "react";
1+
import { useState } from 'react';
2+
3+
/**
4+
* A function which fetches the value corresponding to the key from localStorage.
5+
* If the item doesn't exist returns the input value.
6+
*
7+
* @param {*} value
8+
* @param {string} key Key for the localStorage.
9+
* @returns {*} The value fetched from localStorage.
10+
*/
11+
const getItem = (value, key) => {
12+
try {
13+
const item = localStorage.getItem(key);
14+
return item ? JSON.parse(item) : value;
15+
} catch (error) {
16+
// If something went wrong returns the value itself.
17+
return value;
18+
}
19+
};
220

321
/**
422
* Custom useState hook which saves the state value in localStorage
@@ -8,41 +26,22 @@ import { useState } from "react";
826
* @returns {Array} Array containing stateful value and updater function.
927
*/
1028
const useLocalStorage = (initialValue, key) => {
11-
const [storedValue, setStoredValue] = useState(_getItem(initialValue, key));
29+
const [storedValue, setStoredValue] = useState(getItem(initialValue, key));
1230

1331
const setValue = (value) => {
14-
if (typeof localStorage !== "undefined") {
15-
//If value passed is a function, evaluating the function.
32+
if (typeof localStorage !== 'undefined') {
33+
// If value passed is a function, evaluating the function.
1634
const valueToStore = value instanceof Function ? value(storedValue) : value;
1735

18-
//Setting state and saving the value to localStorage.
36+
// Setting state and saving the value to localStorage.
1937
setStoredValue(valueToStore);
2038
localStorage.setItem(key, JSON.stringify(valueToStore));
2139
} else {
22-
console.error("localStorage does not exist");
40+
throw new Error('localStorage does not exist');
2341
}
2442
};
2543

2644
return [storedValue, setValue];
2745
};
2846

29-
/**
30-
* A function which fetches the value corresponding to the key from localStorage.
31-
* If the item doesn't exist returns the input value.
32-
*
33-
* @param {*} value
34-
* @param {string} key Key for the localStorage.
35-
* @returns {*} The value fetched from localStorage.
36-
*/
37-
const _getItem = (value, key) => {
38-
try {
39-
const item = localStorage.getItem(key);
40-
return item ? JSON.parse(item) : value;
41-
} catch (error) {
42-
//If something went wrong returns the value itself.
43-
console.error(error);
44-
return value;
45-
}
46-
};
47-
4847
export default useLocalStorage;

hooks/useMousePosition.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect } from "react";
1+
import { useState, useEffect } from 'react';
22

33
/**
44
* @typedef {Object} MousePosition
@@ -15,7 +15,7 @@ const useMousePosition = () => {
1515
const [coordinates, setCoordinates] = useState({});
1616

1717
useEffect(() => {
18-
//Event listener to track the mouse location.
18+
// Event listener to track the mouse location.
1919
const eventHandler = (e) => {
2020
const mousePosition = {
2121
x: e.clientX,
@@ -24,10 +24,10 @@ const useMousePosition = () => {
2424

2525
setCoordinates(mousePosition);
2626
};
27-
window.addEventListener("mousemove", eventHandler);
27+
window.addEventListener('mousemove', eventHandler);
2828

2929
return () => {
30-
window.removeEventListener("mousemove", eventHandler);
30+
window.removeEventListener('mousemove', eventHandler);
3131
};
3232
}, []);
3333

hooks/useStack.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState } from "react";
1+
import { useState } from 'react';
22

33
/**
44
* Custom hook for creating and managing Stack.
@@ -28,11 +28,12 @@ const useStack = (initialValue) => {
2828
*/
2929
const pop = () => {
3030
if (stack.length > 0) {
31-
let newStack = [...stack];
31+
const newStack = [...stack];
3232
const poppedValue = newStack.pop();
3333
setStack(newStack);
3434
return poppedValue;
35-
} else return undefined;
35+
}
36+
return undefined;
3637
};
3738

3839
return [stack, push, pop];

hooks/useToggle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState } from "react";
1+
import { useState } from 'react';
22

33
/**
44
*

0 commit comments

Comments
 (0)