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 / getNodePath.js
Last active September 29, 2015 18:01
Get the CSS selector path for a Node (eg: div.className span#id div[data-attribute="value"]). Includes two separate functions, one which converts a Node to a selector, the other which generates a path)
function getNodePath (node) {
var path = [];
while (node) {
path.unshift(toSelector(node));
if (node === document.body || node.id.length > 0) {
return path.join(' ');
}
node = node.parentNode;
}
return path.join(' ');
// A function which which allows you to apply a list of functions (supplied as arguments) to a value.
//
// Usage: pipe(functions...)(value)
//
// Example: var getFirstWordLetter = pipe(getFirstWord, getFirstLetter);
// getFirstWordLetter('Sean MacIsaac')
// -> 'S'
function pipe () {
var functionList = [].slice.call(arguments);
// A function to return an array of indexes for a matching string.
// Usage : indexesOf('this, this this', 'this');
// Usage : indexesOf('this, this this', /t[a-z]{3}/);
// -> [{"index":0,"length":4,"match":"this"},
// {"index":6,"length":4,"match":"this"},
// {"index":11,"length":4,"match":"this"}]
//
// The 'and' method:
// Usage : indexesOf('this, this this', /t[a-z]{3}/).and(/i/);
// -> [{"index":0,"length":4,"match":"this"},
@SeanJM
SeanJM / stringSplice.js
Created December 3, 2015 13:01
A function to splice strings.
// Usage : stringSplice('my string', 2, 0, ' awesome');
// -> 'my awesome string'
//
// How to extend the native string prototype (if you want):
// String.prototype.splice = function () {
// var args = [].slice.call(arguments);
// return stringSplice.apply(null, [this.valueOf()].concat(args));
// };
function stringSplice(baseString, start, length, newString) {
@SeanJM
SeanJM / lumber.js
Last active December 30, 2015 17:44
A framework to build a number manipulation library with built in functions for currency, padding and grouping
// A framework to build a number manipulation library
// Usage: lumber(5).padLeft(2).value;
// -> 05
// Usage: lumber(5).padRight(2).value;
// -> 50
// Usage: lumber(5.1).currency().value;
// Default arguments for '.currency()' is a template of value: '($0.00)'
// -> $5.10
// Usage: lumber(-5.1).currency().value;
// -> ($5.10)
@SeanJM
SeanJM / control.js
Last active December 9, 2015 15:32
an event binder
function control (node) {
var hookMatch = /^-js-[a-zA-Z-\_]+|\s-js-[a-zA-Z-\_]+/g;
function jsCase(string) {
return string.match(/[a-zA-Z0-9_]+/g).map(function (a, i) {
if (i === 0) {
return a.toLowerCase();
}
return a[0].toUpperCase() + a.substr(1, a.length).toLowerCase();
}).join('');
}
@SeanJM
SeanJM / arrayRemove.js
Last active December 23, 2015 14:22
A function which removes members from an Array
function arrayRemove(array, removeList) {
var clone = array.slice();
removeList.forEach(function (a) {
while (clone.indexOf(a) > -1) {
clone.splice(clone.indexOf(a), 1);
}
});
return clone;
}
@SeanJM
SeanJM / arrayUniq.js
Created December 23, 2015 14:24
A function which returns the uniq members of an array
function arrayUniq(array) {
var u = [];
array.forEach(function (a) {
if (u.indexOf(a) === -1) {
u.push(a);
}
});
return u;
}
@SeanJM
SeanJM / kebabCase.js
Created December 23, 2015 14:27
A function which 'kebab' cases a string
function kebabCase(string) {
return string.split(/ |_|-/).join('-').split('').map(function (a) {
if (a.toUpperCase() === a && a !== '-') {
return '-' + a.toLowerCase();
}
return a;
}).join('').toLowerCase();
}
function jsCase(string) {
return string.match(/[a-zA-Z0-9_]+/g).map(function (a, i) {
if (i === 0) {
return a.toLowerCase();
}
return a[0].toUpperCase() + a.substr(1, a.length).toLowerCase();
}).join('');
}