Skip to content

Instantly share code, notes, and snippets.

View dylansmith's full-sized avatar

Dylan Smith dylansmith

View GitHub Profile
@dylansmith
dylansmith / number-to-string-wtf.js
Created August 31, 2018 15:58
number-to-string-wtf.js
console.time('Number.toString');
for (var i=0; i < 1000000; i++) {
Number.toString(i);
}
console.timeEnd('Number.toString');
console.time('i.toString');
for (var i=0; i < 1000000; i++) {
i.toString();
}
@dylansmith
dylansmith / date-mock.js
Created April 3, 2018 15:03
Native JS datetime mocking
function mockTime(val) {
Date = new Proxy(Date, {
construct: function(target, args) {
if (args.length === 0) {
return new target(val);
}
return new target(...args);
}
});
}
@dylansmith
dylansmith / unkeyBy.js
Last active March 27, 2018 14:19
unkeyBy (inverse of _.keyBy)
// using map - fastest!
const unkeyBy = (obj, prop) => {
const res = [];
Object.keys(obj).map((x, i) => res[i] = { ...obj[x], [prop]: x });
return res;
};
// adds ungrouping
const ungroupBy = (obj, prop) => {
const res = [];
@dylansmith
dylansmith / package.json
Created June 5, 2017 11:13
jest config for TypeScript
{
"jest": {
"testEnvironment": "node",
"transform": {
"^.+\\.ts$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "test/.*\\.(ts)$",
"modulePaths": ["<rootDir>/src"],
"moduleFileExtensions": [
"ts",
@dylansmith
dylansmith / package.json
Last active January 11, 2016 12:42
Run `npm install` after git pull if package.json was changed
{
"devDependencies": {
"ghooks": "latest"
},
"config": {
"ghooks": {
"post-merge": "./hooks/post-merge.sh"
}
}
}
@dylansmith
dylansmith / recursive_json_key_transform.js
Created January 11, 2016 09:25
Recursively transform JSON keys
var fs = require('fs')
var _ = require('lodash')
var humps = require('humps')
var input = require('./input.json')
var outputFile = './output.json'
function processObj(val) {
if (_.isArray(val)) {
return val.map(item => processObj(item))
}
@dylansmith
dylansmith / actions.js
Created December 11, 2015 14:25
Minimal-boilerplate redux actionCreator pattern, using redux-actions
import { createAction } from 'redux-actions'
export const SOME_ACTION = 'SOME_ACTION'
export const ANOTHER_ACTION = 'ANOTHER_ACTION'
const actionCreators = {
someAction: () => createAction(SOME_ACTION/*, ...*/),
anotherAction: () => createAction(ANOTHER_ACTION/*, ...*/)
}
@dylansmith
dylansmith / gist:1e398ed1ab92bc0d250e
Created October 27, 2015 08:07
Get directory of current bash script
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
@dylansmith
dylansmith / ec2ssh.sh
Last active October 27, 2015 06:11
ec2 ssh access by "name" tag and instance number, with optional command to pass
#!/bin/bash
# USAGE:
# ./ec2ssh.sh <tag:name> <instance_num> [<remote_command>]
if [ $# -lt 2 ]
then
echo "Usage: `basename $0` <tag:name> <instance_num>"
exit 1
fi
@dylansmith
dylansmith / create_aws_creds.sh
Created October 15, 2015 11:18
Create .aws/credentials from environment vars
#!/bin/bash
mkdir -p ~/.aws
echo -e "[default]\naws_access_key_id=$AWS_ACCESS_KEY_ID\naws_secret_access_key=$AWS_SECRET_ACCESS_KEY" > ~/.aws/credentials