Skip to content

Instantly share code, notes, and snippets.

Avatar

acanimal acanimal

View GitHub Profile
@acanimal
acanimal / open.sh
Created Dec 26, 2020
Open new instance of Google Chrome with different user data dir
View open.sh
open -na "Google Chrome" --args --user-data-dir=$SOME_FOLDER
@acanimal
acanimal / query.js
Created Dec 24, 2020
GraphQL query to get github user stats
View query.js
import { Octokit } from "@octokit/rest";
// Go to github settings and create a token. Add permissions for user/email
const auth_token = 'YOUR TOKEN';
const octokit = new Octokit({
auth: auth_token,
});
const response = await octokit.graphql(`
@acanimal
acanimal / cra-sb.sh
Created Aug 12, 2018
create-react-app with storybook
View cra-sb.sh
$ npx create-react-app $PROJECT_NAME
$ cd $PROJECT_NAME
$ npm i -g @storybook/cli
$ getstorybook
$ yarn run storybook
@acanimal
acanimal / getDomainName.js
Last active Aug 17, 2018 — forked from tahirm/getDomainName.js
Get complete domain name with protocol and port if available. #js #urlFrom http://stackoverflow.com/questions/6941533/javascript-get-protocol-domain-and-port-from-url
View getDomainName.js
const domain = location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : '');
@acanimal
acanimal / nest.js
Created Mar 10, 2018
Accessing nested object properties
View nest.js
// From: https://codeburst.io/accessing-nested-objects-in-javascript-c2ed249fe576
const getNestedObject = (nestedObj, pathArr) => {
return pathArr.reduce((obj, key) =>
(obj && obj[key] !== 'undefined') ? obj[key] : null, nestedObj);
}
@acanimal
acanimal / model-user.js
Last active Feb 17, 2020 — forked from lucasscariot/model-user.js
Composite Primary Key in Sequelize
View model-user.js
/*
* Migration
*/
module.exports = {
up: function (queryInterface, Sequelize) {
return queryInterface.createTable('JobSkills', {
experience: {
type: Sequelize.INTEGER,
allowNull: false
},
@acanimal
acanimal / asyncMiddleware.js
Created Dec 6, 2017
Helper to use async middlewares
View asyncMiddleware.js
const asyncHandler = fn =>
(req, res, next) => {
Promise.resolve(fn(req, res, next))
.catch(next);
};
// Usage
express.get('/', asyncHandler(async (req, res, next) => {
const bar = await foo.findAll();
res.send(bar)
@acanimal
acanimal / withReduxProvider.js
Created Mar 24, 2017
High Order Component to wrap a component with redux Provider
View withReduxProvider.js
import React, { Component } from 'react';
import { Provider } from 'react-redux';
import initialState from './store/initialState';
import createStore from './store/createStore';
const store = createStore(initialState);
const getDisplayName = WrappedComponent => WrappedComponent.displayName || WrappedComponent.name || 'WrappedComponent';
/**
* HOC to wrap a component within redux provider to allow access the store.
@acanimal
acanimal / hoa.js
Created Jan 18, 2017
Higher Order Actions
View hoa.js
const higherOrderAction = (func) => (...args) => (dispatch, ...props) => {
// Do whatever you need.
// Call the wrapped action.
const call = func(undefined, ...args);
// Check if the call returns a function, which means it is an async action
if (call && call.constructor && call.call && call.apply) {
return call(dispatch, ...props);
}
@acanimal
acanimal / error_class_es6.js
Created Jan 10, 2017
Custom error in ES6
View error_class_es6.js
class ExtendableError extends Error {
constructor(message) {
super(message);
this.name = this.constructor.name;
this.message = message;
if (typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(this, this.constructor);
} else {
this.stack = (new Error(message)).stack;
}