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
// 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"},
// 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);
@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(' ');