Skip to content

Instantly share code, notes, and snippets.

View johncmunson's full-sized avatar

John Munson johncmunson

View GitHub Profile
@johncmunson
johncmunson / deromanize.js
Created January 29, 2017 03:19
Accepts a roman numeral and returns the corresponding integer.
var deromanize = function(str) {
var str = str.toUpperCase(),
validator = /^M*(?:D?C{0,3}|C[MD])(?:L?X{0,3}|X[CL])(?:V?I{0,3}|I[XV])$/,
token = /[MDLV]|C[MD]?|X[CL]?|I[XV]?/g,
key = {
M: 1000,
CM: 900,
D: 500,
CD: 400,
C: 100,
@johncmunson
johncmunson / romanize.js
Created January 29, 2017 03:32
Accepts a number and returns the corresponding roman numeral.
var romanize = function(num) {
var rom = {
M: 1000,
CM: 900,
D: 500,
CD: 400,
C: 100,
XC: 90,
L: 50,
XL: 40,
@johncmunson
johncmunson / piglatin.js
Created January 29, 2017 06:54
Accepts a word and returns the same word in pig latin
const pigLatin = str => {
let pigStr = "";
const key = ["a", "e", "i", "o", "u"];
if (key.some(val => val === str.charAt(0))) {
pigStr = str + "way";
return pigStr;
} else {
pigStr = str.slice(1) + str.charAt(0) + "ay";
return pigStr;
}
'JSX comment':
'prefix': 'reJSXComment'
'body': '{/* ${1:Comments...} */}'
'ReactDOM.render':
'prefix': 'reDOMRender'
'body': 'ReactDOM.render(${1:<App />}, document.getElementById(\'${2:root}\'));'
'class component w/ extras':
'prefix': 'reCompFull'
'body': """
class ${1:Component} extends React.Component {
'configure store':
'prefix': 'rdStore'
'body': """
export default function configureStore(preloadedState) {
return createStore(
rootReducer,
preloadedState,
applyMiddleware(
$1
)
@johncmunson
johncmunson / snippet-snippet.cson
Created February 16, 2017 09:09
A snippet for quickly creating other snippets.
'snippet block':
'prefix': 'snippet'
'body': """
\'${1:Snippet Name}\':
\'prefix\': \'${2:Snippet Shortcut}\'
\'body\': \'${3:Snippet}\'
"""
@johncmunson
johncmunson / import-export-snippets.cson
Created February 16, 2017 09:11
Import/Export snippets
'default import':
'prefix': 'impDefault'
'body': 'import ${1:defaultMember} from \'${2:module-name}\''
'named import':
'prefix': 'impNamed'
'body': 'import { ${1:namedMember} } from \'${2:module-name}\''
'export default':
'prefix': 'expDefault'
'body': 'export default ${1:name}'
'export':
// Sometimes we do cool stuff like this...
let tacoType = hasQueso ? 'supreme' : 'normal'
// Or this in our markup...
`ng-class="isActive && 'md-primary'"`
// Or this to set default values...
this.color = config.color || 'blue'
// Sometimes though we'd like to have more
@johncmunson
johncmunson / app.js
Created February 8, 2018 07:28 — forked from hagino3000/app.js
JSON-RPC for node (express)
/**
* Module dependencies.
*/
var express = require('express');
var rpcMethods = require('./methods.js');
var app = module.exports = express.createServer();
// Configuration
@johncmunson
johncmunson / promise-concurrency.js
Created September 27, 2019 16:09
Throttling concurrent API calls
const pMap = require('p-map')
const Chance = require('chance')
const chance = new Chance()
const userIds = [ 52, 84, 71, 66, 12, 39, 18, 99, 7, 48 ]
// Simulate a network call
const getUser = async (id) => {
await new Promise(resolve => setTimeout(resolve, 1000))