Skip to content

Commit

Permalink
👔 Added Formatting and Linting (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
aromalanil authored Oct 4, 2020
1 parent b9a4835 commit eac44b3
Show file tree
Hide file tree
Showing 13 changed files with 2,254 additions and 70 deletions.
11 changes: 11 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": ["prettier","airbnb"],
"plugins": ["prettier"],
"rules": {
"prettier/prettier": ["error"]
},
"env": {
"browser": true,
"node": true
}
}
4 changes: 3 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.all-contributorsrc
.github
CONTRIBUTING.md
docs
docs
.eslintrc.json
.prettierrc
14 changes: 14 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"printWidth": 100,
"tabWidth": 2,
"singleQuote": true,
"trailingComma": "all",
"bracketSpacing": true,
"jsxBracketSameLine": true,
"parser": "flow",
"semi": true,
"useTabs": false,
"quoteProps": "as-needed",
"jsxSingleQuote": true,
"arrowParens": "always"
}
38 changes: 19 additions & 19 deletions hooks/useDarkMode.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
import { useState, useEffect } from "react";
import { useState, useEffect } from 'react';

/**
*
* Returns true if globally preferred theme is dark and false if not.
* @returns {boolean} Is dark mode globally preferred.
*/
const getGlobalPreference = () => window.matchMedia('(prefers-color-scheme: dark)').matches;

/**
*
* Returns true if user prefers dark mode and false if not.
* @returns {boolean} Does user prefers dark mode.
*/
const _getUserPreferredMode = () => {
//If localStorage is available user preference is stored, return the stored value.
const getUserPreferredMode = () => {
// If localStorage is available user preference is stored, return the stored value.
if (localStorage) {
const localPreference = localStorage.getItem("dark-mode");
const localPreference = localStorage.getItem('dark-mode');
if (localPreference) {
return JSON.parse(localStorage.getItem("dark-mode"));
return JSON.parse(localStorage.getItem('dark-mode'));
}
// Else return the global dark-mode preference.
return getGlobalPreference();
}
//Else return the global dark-mode preference.
else {
return _getGlobalPreference();
}
// If localStorage does not exist, return the global dark-mode preference.
return getGlobalPreference();
};

/**
*
* Returns true if globally preferred theme is dark and false if not.
* @returns {boolean} Is dark mode globally preferred.
*/
const _getGlobalPreference = () => window.matchMedia("(prefers-color-scheme: dark)").matches;

/**
*
* Custom hook which let's you toggle dark-mode by adding a class to the body.
*
*
* @param {string} className Class name which is to be added to body when dark mode.
* @returns {Array} Array containing a boolean isDarkMode and a darkModeToggle function.
*/
const useDarkMode = (className) => {
const [isDarkMode, setIsDarkMode] = useState(_getUserPreferredMode());
const [isDarkMode, setIsDarkMode] = useState(getUserPreferredMode());

useEffect(() => {
if (isDarkMode) {
Expand All @@ -45,7 +45,7 @@ const useDarkMode = (className) => {
}, [isDarkMode]);

const toggleDarkMode = () => {
localStorage.setItem("dark-mode", !isDarkMode);
localStorage.setItem('dark-mode', !isDarkMode);
setIsDarkMode(!isDarkMode);
};

Expand Down
8 changes: 4 additions & 4 deletions hooks/useDebounce.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { useCallback } from "react";
import { useCallback } from 'react';

/**
* Debounce function generator.
* @param {function} inputFunction Function which is to be modified.
* @param {number} delay The time delay in milliseconds.
* @returns {function} The modified function.
*/
function _debounce(inputFunction, delay) {
function debounce(inputFunction, delay) {
let timeout;
return function (...args) {
return (...args) => {
const context = this;
if (timeout) clearTimeout(timeout);
timeout = setTimeout(() => {
Expand All @@ -26,6 +26,6 @@ function _debounce(inputFunction, delay) {
* @param {number} delay The time delay in milliseconds.
* @returns {function} The modified function.
*/
const useDebounce = (inputFunction, delay) => useCallback(_debounce(inputFunction, delay), []);
const useDebounce = (inputFunction, delay) => useCallback(debounce(inputFunction, delay), []);

export default useDebounce;
6 changes: 3 additions & 3 deletions hooks/useGeoLocation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect } from "react";
import { useState, useEffect } from 'react';

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

//Only called on ComponentDidMount
// Only called on ComponentDidMount
useEffect(() => {
// Checking if device supports geolocation
if (navigator.geolocation) {
Expand All @@ -28,7 +28,7 @@ const useGeoLocation = () => {
setGeoLocation(userPosition);
});
} else {
throw new Error("The browser does not support geolocation");
throw new Error('The browser does not support geolocation');
}
}, []);

Expand Down
49 changes: 24 additions & 25 deletions hooks/useLocalStorage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
import { useState } from "react";
import { useState } from 'react';

/**
* A function which fetches the value corresponding to the key from localStorage.
* If the item doesn't exist returns the input value.
*
* @param {*} value
* @param {string} key Key for the localStorage.
* @returns {*} The value fetched from localStorage.
*/
const getItem = (value, key) => {
try {
const item = localStorage.getItem(key);
return item ? JSON.parse(item) : value;
} catch (error) {
// If something went wrong returns the value itself.
return value;
}
};

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

const setValue = (value) => {
if (typeof localStorage !== "undefined") {
//If value passed is a function, evaluating the function.
if (typeof localStorage !== 'undefined') {
// If value passed is a function, evaluating the function.
const valueToStore = value instanceof Function ? value(storedValue) : value;

//Setting state and saving the value to localStorage.
// Setting state and saving the value to localStorage.
setStoredValue(valueToStore);
localStorage.setItem(key, JSON.stringify(valueToStore));
} else {
console.error("localStorage does not exist");
throw new Error('localStorage does not exist');
}
};

return [storedValue, setValue];
};

/**
* A function which fetches the value corresponding to the key from localStorage.
* If the item doesn't exist returns the input value.
*
* @param {*} value
* @param {string} key Key for the localStorage.
* @returns {*} The value fetched from localStorage.
*/
const _getItem = (value, key) => {
try {
const item = localStorage.getItem(key);
return item ? JSON.parse(item) : value;
} catch (error) {
//If something went wrong returns the value itself.
console.error(error);
return value;
}
};

export default useLocalStorage;
8 changes: 4 additions & 4 deletions hooks/useMousePosition.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect } from "react";
import { useState, useEffect } from 'react';

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

useEffect(() => {
//Event listener to track the mouse location.
// Event listener to track the mouse location.
const eventHandler = (e) => {
const mousePosition = {
x: e.clientX,
Expand All @@ -24,10 +24,10 @@ const useMousePosition = () => {

setCoordinates(mousePosition);
};
window.addEventListener("mousemove", eventHandler);
window.addEventListener('mousemove', eventHandler);

return () => {
window.removeEventListener("mousemove", eventHandler);
window.removeEventListener('mousemove', eventHandler);
};
}, []);

Expand Down
7 changes: 4 additions & 3 deletions hooks/useStack.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from "react";
import { useState } from 'react';

/**
* Custom hook for creating and managing Stack.
Expand Down Expand Up @@ -28,11 +28,12 @@ const useStack = (initialValue) => {
*/
const pop = () => {
if (stack.length > 0) {
let newStack = [...stack];
const newStack = [...stack];
const poppedValue = newStack.pop();
setStack(newStack);
return poppedValue;
} else return undefined;
}
return undefined;
};

return [stack, push, pop];
Expand Down
2 changes: 1 addition & 1 deletion hooks/useToggle.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from "react";
import { useState } from 'react';

/**
*
Expand Down
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export { default as useState } from "./hooks/useStack";
export { default as useToggle } from "./hooks/useToggle";
export { default as useDarkMode } from "./hooks/useDarkMode";
export { default as useDebounce } from "./hooks/useDebounce";
export { default as useGeoLocation } from "./hooks/useGeoLocation";
export { default as useLocalStorage } from "./hooks/useLocalStorage";
export { default as useMousePosition } from "./hooks/useMousePosition";
export { default as useState } from './hooks/useStack';
export { default as useToggle } from './hooks/useToggle';
export { default as useDarkMode } from './hooks/useDarkMode';
export { default as useDebounce } from './hooks/useDebounce';
export { default as useGeoLocation } from './hooks/useGeoLocation';
export { default as useLocalStorage } from './hooks/useLocalStorage';
export { default as useMousePosition } from './hooks/useMousePosition';
Loading

0 comments on commit eac44b3

Please sign in to comment.