Skip to content

Instantly share code, notes, and snippets.

View pilgreen's full-sized avatar

Jay Pilgreen pilgreen

View GitHub Profile
@pilgreen
pilgreen / csvToJSON.js
Created February 25, 2020 21:31
Simple lightweight CSV conversion to JSON.
function csvToJSON(csv) {
const lines = csv.split('\n')
const result = []
const headers = lines[0].split(',')
for (let i = 1; i < lines.length; i++) {
if (!lines[i])
continue
const obj = {}
const currentline = lines[i].split(',')
@pilgreen
pilgreen / nextUntil.js
Created January 22, 2020 17:43
Gets elements between specific tags
function nextUntil(elem, selector) {
let siblings = [];
elem = elem.nextElementSibling;
while(elem) {
if(elem.matches(selector)) break;
siblings.push(elem);
elem = elem.nextElementSibling;
}
@pilgreen
pilgreen / pbcopy.js
Created August 14, 2019 15:21
Copy output to the clipboard in Node
function pbcopy(data) {
var proc = require('child_process').spawn('pbcopy');
proc.stdin.write(data); proc.stdin.end();
}
pbcopy(somObject);
@pilgreen
pilgreen / git lg
Last active April 1, 2019 14:27
`git lg` global config
git config --global alias.lg "log --color --graph --pretty=format:'%Cblue%h%Creset -%C(red)%d%Creset %s %Cgreen(%cr)' --abbrev-commit"
function getParam(key, q) {
let pair = new RegExp(key + "=([^&#]*)");
let match = q.match(pair);
return match ? match[1] : '';
}
@pilgreen
pilgreen / gofer.js
Created June 29, 2017 22:00
Very basic seamless iframes
window.addEventListener("message", e => {
let d = e.data;
if(d.sentinel == 'gofer' && d.type == 'resize') {
let f = document.querySelector(`iframe[src="${d.location}"]`);
if(f && f.style) {
f.style.height = `${d.height}px`;
}
}
});
@pilgreen
pilgreen / dom-module.html
Last active December 29, 2016 22:42
A small copy of Polymer's dom-module.
<script>
class DomModule extends HTMLElement {
constructor() {
super();
}
connectedCallback() {
this.register();
}
@pilgreen
pilgreen / shuffle.js
Created March 4, 2016 15:24
The Fisher-Yates shuffle.
function shuffle(array) {
var m = array.length, t, i;
while (m) {
i = Math.floor(Math.random() * m--);
t = array[m];
array[m] = array[i];
array[i] = t;
}
return array;
}
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
@pilgreen
pilgreen / extend.js
Last active August 29, 2015 14:11
Vanilla Object extension
Object.extend = function() {
var args = Array.prototype.slice.call(arguments);
var destination = args.shift();
var i = 0;
while(i < args.length) {
var source = args[i];
for(prop in source) {
if(Object.prototype.hasOwnProperty.call(source, prop)) {