Skip to content

Instantly share code, notes, and snippets.

View sethdavis512's full-sized avatar
🤖

Seth Davis sethdavis512

🤖
View GitHub Profile
@sethdavis512
sethdavis512 / replace-char.js
Last active August 4, 2022 06:33 — forked from alisterlf/gist:3490957
Remove Accents
function RemoveAccents(strAccents) {
var strAccents = strAccents.split('');
var strAccentsOut = new Array();
var strAccentsLen = strAccents.length;
var accents = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž';
var accentsOut = "AAAAAAaaaaaaOOOOOOOooooooEEEEeeeeeCcDIIIIiiiiUUUUuuuuNnSsYyyZz";
for (var y = 0; y < strAccentsLen; y++) {
if (accents.indexOf(strAccents[y]) != -1) {
strAccentsOut[y] = accentsOut.substr(accents.indexOf(strAccents[y]), 1);
} else
@sethdavis512
sethdavis512 / write-directories.js
Last active August 4, 2022 06:32
Use object to build directory structure.
const fs = require('fs');
// Paths functions from
// https://lowrey.me/getting-all-paths-of-an-javascript-object/
function getPaths(root) {
let paths = [];
let nodes = [{
obj: root,
path: []
// From https://stackoverflow.com/questions/286141/remove-blank-attributes-from-an-object-in-javascript
const removeEmpty = (obj) => {
return Object.keys(obj)
.filter(k => obj[k] !== null && obj[k] !== undefined && obj[k] !== '')
.reduce((newObj, k) => {
return typeof obj[k] === 'object' ?
Object.assign(newObj, {[k]: removeEmpty(obj[k])}) : // Recurse.
Object.assign(newObj, {[k]: obj[k]}); // Copy value.
}, {});
}
export const composePromise = (...fns) => initialValue =>
fns.reduceRight((res, fn) => Promise.resolve(res).then(fn), initialValue);
// Individual Promises
const saveAppFee = () => saveFees(a, b, c);
const saveLnpFee = () => saveFees(x, y, z);
// Composed Promises
const saveAppAndLnp = composePromise(saveAppFee, saveLnpFee);
const saveApp = composePromise(saveAppFee);
function toDollar(n) {
const numWithCommas = n.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return `$${numWithCommas}`;
}
const keyBy = (arr, objKey) => arr.reduce(
(acc, cur) => (
{ ...acc, [cur[objKey]]: cur }
), {});
const groupBy = (array, groupKey) => {
return array.reduce((acc, cur) => {
if (acc[cur[groupKey]]) {
acc[cur[groupKey]].push(cur);
} else {
acc[cur[groupKey]] = [cur];
}
return acc;
}, {});
};
@sethdavis512
sethdavis512 / filterWithParams.js
Last active August 4, 2022 06:29
Filter array based on object key value pairs.
const valuesMatch = (sourceObj, matchObj) => {
let matches = true;
Object.keys(matchObj).forEach(matchKey => {
if (sourceObj[matchKey] !== matchObj[matchKey]) {
matches = false;
}
});
return matches;
};
const scream = str => str.toUpperCase()
const exclaim = str => `${str}!`
const repeat = str => `${str} ${str}`
const string = 'Egghead.io is awesome'
// Nested
const result2 = repeat(exclaim(scream(string)))
// EGGHEAD.IO IS AWESOME! EGGHEAD.IO IS AWESOME!
const toConstant = (str: string): string => str.replace(/\s+/g, '_').toUpperCase();