Skip to content

Instantly share code, notes, and snippets.

View sethdavis512's full-sized avatar
🤖

Seth Davis sethdavis512

🤖
View GitHub Profile
function increment(input) { return input + 1;}
function decrement(input) { return input - 1; }
function double(input) { return input * 2; }
function halve(input) { return input / 2; }
var initial_value = 1;
var pipeline = [
increment,
increment,
@mixin transform($property) {
-webkit-transform: $property;
-ms-transform: $property;
transform: $property;
}
.box { @include transform(rotate(30deg)); }
/* ES5, using Bluebird */
var isMomHappy = true;
// Promise
var willIGetNewPhone = new Promise(
function (resolve, reject) {
if (isMomHappy) {
var phone = {
brand: 'Samsung',
color: 'black'
@sethdavis512
sethdavis512 / diffObjects.js
Created October 11, 2018 17:01
Lodash Diff Objects
import { transform, isEqual, isObject } from 'lodash';
/**
* Deep diff between two object, using lodash
* @param {Object} object Object compared
* @param {Object} base Object to compare with
* @return {Object} Return a new object who represent the diff
*/
function difference(object, base) {
return transform(object, (result, value, key) => {
const pause = (duration) => new Promise(res => setTimeout(res, duration));
const retry = (retries, fn) =>
fn().catch(err => retries > 1 ? retry(retries - 1, fn) : Promise.reject(err));
const backoff = (retries, fn, delay = 500) =>
fn().catch(err => retries > 1
? pause(delay).then(() => backoff(retries - 1, fn, delay * 2))
: Promise.reject(err));
@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: []
const flattenFilePath = (pathPart, directoryOrFileContents) => {
return Object.entries(directoryOrFileContents).reduce((flattenedFilePaths, [key, value]) => {
if (typeof value === "object") {
Object.assign(flattenedFilePaths, flattenFilePath(`${pathPart}/${key}`, value))
} else {
flattenedFilePaths[`${pathPart}/${key}`] = value
}
return flattenedFilePaths
}, {})
// 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.
}, {});
}
{
"Stateless Functional Component": {
"prefix": "stateless",
"body": [
"import React from 'react';",
"import PropTypes from 'prop-types';",
"",
"const ${TM_FILENAME_BASE/(.*)\\..+$/$1/} = () => {",
" return <div className=\"\"></div>;",
"}",
// Arrow
const buildMockArray = <T>(num: number, content: T): T[] => Array(num).fill(null).map(() => content);
// Traditional
function buildMockArray<T>(num: number, content: T): T[] {
return Array(num).fill(null).map(() => content);
}