Skip to content

Instantly share code, notes, and snippets.

View JamieMason's full-sized avatar

Jamie Mason JamieMason

View GitHub Profile
@JamieMason
JamieMason / is-async-function.js
Created October 23, 2019 10:17
isAsyncFunction
const isAsyncFunction = fn => typeof fn === 'function' && fn.constructor.name === 'AsyncFunction';
@JamieMason
JamieMason / is-promise-like-js
Created October 23, 2019 10:16
isPromiseLike
const isPromiseLike = obj =>
obj !== null &&
(typeof obj === 'object' || typeof obj === 'function') &&
typeof obj.then === 'function';
@JamieMason
JamieMason / to-oxford-comma.md
Last active December 18, 2023 12:33
Format JavaScript Array of Strings to Oxford Comma.

Format JavaScript Array of Strings to Oxford Comma

const toOxfordComma = (array) =>
  array.length === 2
    ? array.join(' and ')
    : array.length > 2
    ? array
        .slice(0, array.length - 1)
 .concat(`and ${array.slice(-1)}`)
@JamieMason
JamieMason / USAGE.md
Created September 9, 2019 10:45
Get Paths To Files In A Git Repository

Get Paths To Files In A Git Repository

Commands

added_in_branch

Files added (not modified or deleted) in our branch.

added_in_git_stage

Files added (not modified or deleted) in git add.

in_git_stage

Files in git add.

@JamieMason
JamieMason / GET_ALL_CHANGES_FROM_GIT_FOR_THE_CURRENT_USER.md
Last active August 29, 2019 13:02
Get all changes from Git for the current User/Developer/Author/Committer

Get all changes from Git for the current Author

  1. Get the Email of the current User/Developer/Author/Committer.
  2. Resolve the Email through the Git Mailmap.
  3. Get all files they have ever created or modified.
  4. Get all files modified in the current branch compared to master.
  5. Some files they've modified could have since been deleted by someone else, so filter out files which no longer exist.

Use Case

@JamieMason
JamieMason / machine.js
Last active August 23, 2019 15:01
Generated by XState Viz: https://xstate.js.org/viz
// Generally Expected Flow (actions and implementation are missing)
//
// 1 renderNode runs logic which inspects this hard-coded rootNode
// 2 transtions to renderBranch because it has children
// 3 there are two children so the UI would display them
// 4 user chooses the first child
// 5 fetchNode finds no children property, so there is nothing more to load
// 6 fetchNode resolves success with the node
// 7 renderNode runs logic which inspects the node
// 8 transtions to renderLeaf because it has no children
export default function transformer(file, api) {
const j = api.jscodeshift;
const toPascalCase = str =>
`${str}`
.replace(new RegExp(/[-_]+/, 'g'), ' ')
.replace(new RegExp(/[^\w\s]/, 'g'), '')
.replace(
new RegExp(/\s+(.)(\w+)/, 'g'),
($1, $2, $3) => `${$2.toUpperCase() + $3.toLowerCase()}`
@JamieMason
JamieMason / pluck-unique-values-from-array-of-javascript-objects.md
Created September 14, 2018 08:14
Pluck Unique Values from Array of Javascript Objects

Pluck Unique Values from Array of Javascript Objects

Implementation

const pluck = key => array => Array.from(new Set(array.map(obj => obj[key])));

Usage

@JamieMason
JamieMason / group-objects-by-property.md
Created September 14, 2018 07:38
Group Array of JavaScript Objects by Key or Property Value

Group Array of JavaScript Objects by Key or Property Value

Implementation

const groupBy = key => array =>
  array.reduce((objectsByKeyValue, obj) => {
    const value = obj[key];
    objectsByKeyValue[value] = (objectsByKeyValue[value] || []).concat(obj);
    return objectsByKeyValue;
@JamieMason
JamieMason / transducers.md
Last active October 11, 2019 14:59
Learning about JavaScript Transducers