Skip to content

Instantly share code, notes, and snippets.

View SeanJM's full-sized avatar

Sean MacIsaac SeanJM

  • Sean J MacIsaac
  • Montreal, QC
View GitHub Profile
@SeanJM
SeanJM / fnChain.js
Last active March 15, 2016 18:45
A function which allows the augmentation of an Object with functional methods.
// Here is a JSFiddle with a concrete example: https://jsfiddle.net/SeanJM/rg3ftcgk/1/
// this is one my most leveraged functions that I use to keep my code nice and modular
// -
// Changes made from https://gist.github.com/lewisje/041f4d25d12c8a135a8b
// Based on a post in reddit https://www.reddit.com/r/javascript/comments/46xaln/what_goto_libraries_frameworks_tools_or_otherwise/d08wr56
// from https://www.reddit.com/user/lewisje
function fnChain(target, source, args) {
'use strict';
var name;
/*
createNode(tagNameOrNode, attributes, callback(createNode))
eg >
createNode('div', {
class : 'my-class-name'
}, function (newNode) {
});
returns a list of chained methods
- .append(Node)
*/
function createSubscriber() {
'use strict';
var subscriber = {};
var f = {};
if (typeof f !== 'object') {
throw 'The argument for \'createSubscriber\' must be type object';
}
@SeanJM
SeanJM / generateId.js
Last active March 14, 2017 12:58
A function which generates a random id
(function () {
var lib = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
var n = lib.length - 1;
function generateId(index) {
var id = [];
while (index-- > 0) {
id.push(lib[Math.round(Math.random() * n)]);
}
return id.join('');
function partial(fn) {
var left = new Array(arguments.length - 1);
var leftIndex = 0;
for (; leftIndex < left.length; leftIndex++) {
left[leftIndex] = arguments[leftIndex + 1];
}
return function () {
var right = new Array(arguments.length);
@SeanJM
SeanJM / hasKey.js
Created April 21, 2016 14:04
A function to see if an object has a key
function hasKey (object, key) {
for (var k in object) {
if (k === key && object.hasOwnProperty(key)) {
return true;
}
}
return false;
}
@SeanJM
SeanJM / getUsStates.js
Created June 27, 2016 20:18
A function which returns an array of US states.
function getUsStates(number) {
var states = [
'Alabama',
'Alaska',
'American Samoa',
'Arizona',
'Arkansas',
'California',
'Colorado',
'Connecticut',
@SeanJM
SeanJM / promisify.js
Last active October 31, 2017 20:50
A function which will turn node style callbacks into Promises.
module.exports = function promisify(fn) {
const args = [];
for (var i = 1, n = arguments.length; i < n; i++) {
args.push(arguments[i]);
}
return new Promise(function (resolve, reject) {
args.push(function (err) {
const args = [];
@SeanJM
SeanJM / .babelrc
Last active January 14, 2020 03:03
TypeScript, VueJS and JSX webpack configuration
{
"presets": [],
"plugins": ["transform-vue-jsx", "transform-es2015-modules-commonjs"]
}
@SeanJM
SeanJM / generateId.ts
Last active September 20, 2017 14:39
A TypeScript function to generate a random ID
interface ILib {
alpha: string[],
number: string[],
symbols: string[]
}
type Key = keyof ILib;
const lib : ILib = {
alpha: [