Skip to content

Instantly share code, notes, and snippets.

View toddmantell's full-sized avatar

Todd Mantell toddmantell

View GitHub Profile

Keybase proof

I hereby claim:

  • I am toddmantell on github.
  • I am toddmantell (https://keybase.io/toddmantell) on keybase.
  • I have a public key ASCQlLgPGbwAExM1x8Ed7ppDqp54-Us_-58sRDFrBVDkhQo

To claim this, I am signing this object:

@toddmantell
toddmantell / vscodeSettings.json
Created November 28, 2017 03:16
vscode Settings
{
"editor.wrappingColumn": 0,
"typescript.check.tscVersion": false,
"workbench.colorTheme": "Operator Mono Dark Theme",
"editor.tabSize": 2,
"workbench.iconTheme": "vscode-icons",
"[javascript]": { "editor.tabSize": 2, "editor.insertSpaces": false, "editor.detectIndentation": false }
}
@toddmantell
toddmantell / addLogFunctionArrayPrototype.js
Created November 20, 2017 23:14
Add function to Array.prototype
const numbers = [1,2,3,4,5,6,7,8];
Array.prototype.logValues = function() {
for (i = 0; i < this.length; i++) {
console.log(this[i]);
}
};
numbers.logValues();
@toddmantell
toddmantell / creatingObject.js
Last active November 20, 2017 22:44
Object.create() vs new
/*Object.create() vs new constructor(): constructor will have __proto__ and a "prototype" property, but a regular object will only have __proto__*/
//Object.create() just creates a new object and assigns it the prototype that you provide as the first argument
const person = {
name: 'Name',
age: 100,
sayHello: function () {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old`);
}
};
@toddmantell
toddmantell / destructuredImport.js
Last active June 11, 2018 22:44
transform destructured import to CommonJS
//Should transform: import {Component} from 'react';
//to: const _Component = require('react').Component;
export default function ({types: t}) {
return {
visitor: {
ImportDeclaration(path) {
const {node} = path;
path.replaceWith(
t.variableDeclaration('var',
@toddmantell
toddmantell / importToCommon.js
Last active October 30, 2017 14:41
Import declaration transform to commonJS
//copy this into AST Explorer and run it against a standard import statement, like import React from 'react';
export default function ({types: t}) {
return {
visitor: {
ImportDeclaration(path) {
const {node} = path;
path.replaceWith(
t.variableDeclaration('var', [
t.variableDeclarator(t.identifier(`_${node.specifiers[0].local.name}`),
@toddmantell
toddmantell / gqlTypeTransform.js
Created October 23, 2017 16:03
graphql type transformer
export default function () {
return {
visitor: {
VariableDeclaration(path) {
path.node.declarations[0].id.name = 'TransformedInVariableDeclaration';
const template = path.node.declarations[0].init.quasis[0].value.raw;
console.log(template);
},
TemplateLiteral(path) {
const template = path.node.quasis[0].value.raw;
@toddmantell
toddmantell / gist:17e43d3ec112e2b0cfb772ae6242eb08
Created October 18, 2017 16:09
Using Object.assign to immutably update an array
const baseArray = [1, 2, 3, 4];
const newArr = Object.assign([...baseArray], {2: 5});
console.log(newArr); // [1, 2, 5, 4]
@toddmantell
toddmantell / Ryan'sTernaryAsCleanCode.js
Last active July 10, 2018 04:12
Refactoring Ryan's Nested Ternaries into Clean Functions
// Original logic for reference:
component ? ( // component prop gets first priority, only called if there's a match
match ? React.createElement(component, props) : null
) : render ? ( // render prop is next, only called if there's a match
match ? render(props) : null
) : children ? ( // children come last, always called
typeof children === 'function' ? (
children(props)
) : !Array.isArray(children) || children.length ? ( // Preact defaults to empty children array
React.Children.only(children)