Skip to content

Instantly share code, notes, and snippets.

View psynewave's full-sized avatar

Mark Flavin psynewave

View GitHub Profile
const getDaysBetweenDates = (dateStart, dateEnd) => (new Date(dateEnd) - new Date(dateStart)) / (1000 * 3600 * 24);
getDaysBetweenDates('1/21/2020', '1/21/2021'); // 366 leap year
getDaysBetweenDates('1/21/2019', '1/20/2020'); // 365
getDaysBetweenDates(new Date('08/04/2020'), new Date('08/24/2020'));
getDaysBetweenDates('08/04/2020','08/24/2020');
const take = (arr, n = 1, fromStart = true) => fromStart ? arr.slice(0, n) : n < 0 ? arr.slice(n) : arr.slice(-1 * n);
let fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
take(fruits)
take(fruits, 3)
take(fruits, -3, false)
take(fruits, 3, false)
@psynewave
psynewave / parseJWT.js
Created August 26, 2020 03:09
Decode JWT Client Side
const b64DecodeUnicode = (str) => decodeURIComponent(Array.prototype.map.call(atob(str),(c) => "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2)).join(""));
const parseJWT = (token) => JSON.parse(b64DecodeUnicode(token.split(".")[1].replace("-", "+").replace("_", "/")));
@psynewave
psynewave / unescapeHTML.js
Created August 25, 2020 19:02
Unescape escaped HTML characters.
const unescapeHTML = str =>
str.replace(
/&amp;|&lt;|&gt;|&#39;|&quot;/g,
tag =>
({
'&amp;': '&',
'&lt;': '<',
'&gt;': '>',
'&#39;': "'",
'&quot;': '"'
@psynewave
psynewave / fetchRssConvert2JSON.js
Created August 25, 2020 18:13
Fetch RSS and Convert to JSON
const RSS_URL = `https://cors-anywhere.herokuapp.com/https://archive.org/services/collection-rss.php?collection=podcast_jeff-regan-the-great-detec_426081250`;
const xmlToJson = (xml) => {
let obj = {};
try {
xml.hasChildNodes();
} catch (err) {
xml = new DOMParser().parseFromString(xml, "text/xml");
}
@psynewave
psynewave / checkIfObjectIsEmpty.js
Created August 25, 2020 17:45
Check if an Object is Empty
var checkIfObjectEmpty = obj => Object.keys(obj).length === 0 && obj.constructor === Object
@psynewave
psynewave / PokemonFetch.js
Last active August 25, 2020 17:46
Promise All Example
const apiURL = "https://pokeapi.co/api/v2/type/1";
const pokemonFetch = async () => {
const initial = await fetch(apiURL);
const initialJson = await initial.json();
const detailsData = initialJson.pokemon.map(async i => {
const preFetchData = await fetch(i.pokemon.url);
return preFetchData.json();
})
@psynewave
psynewave / mergeTwoArrays.js
Created July 9, 2020 22:15
One liner for merging arrays with an optional unique flag
const mergeTwoArrays = (a, b, unique = false) => unique != true ? a.concat(b) : [...new Set([...a, ...b])];
@psynewave
psynewave / generateSeriesIntegers.js
Created July 9, 2020 21:59
A one liner for generating a filled array of integers starting with 1
const generateSeriesIntegers = len => [ ...Array(len).keys() ].map( i => i + 1);
@psynewave
psynewave / sumOfSeries.js
Last active July 9, 2020 21:54
A one line function for summing a series of numbers in an array
const sumOfSeries = arr => (arr.length * (parseInt(arr[0], 10) + parseInt(arr[arr.length -1], 10)))/2