Skip to content

Instantly share code, notes, and snippets.

View C-Rodg's full-sized avatar
👽

Curtis C-Rodg

👽
View GitHub Profile
@C-Rodg
C-Rodg / RoundRotation.js
Last active February 28, 2017 05:28
A simple function to determine if one string is a round rotation of another.
function roundStrings(s1, s2) {
for (let i in s1) {
if (s1.hasOwnProperty(i)) {
if (s1[i] !== s2[s2.length - i - 1]) {
return -1;
}
}
}
return 1;
}
@C-Rodg
C-Rodg / ScrapeGoogle.js
Last active March 21, 2017 03:43
A quick script that after navigating to a google search page will download the scrapped content to a csv file.
function CsvWriter(del, enc) {
this.del = del || ',';
this.enc = enc || '"';
this.escapeCol = (col) => {
if(isNaN(col)) {
if(!col) {
col = '';
} else {
col = String(col);
@C-Rodg
C-Rodg / Mixin.js
Created January 16, 2017 22:58
A mixin for acquiring object properties without modifying the prototype chain.
function mixin(receiver, supplier) {
if(Object.getOwnPropertyDescriptor) {
Object.keys(supplier).forEach(function(prop) {
let descriptor = Object.getOwnPropertyDescriptor(supplier, prop);
Object.defineProperty(receiver, prop, descriptor);
});
} else { // For older browsers, does not support accessor properties
for(let prop in supplier) {
if (supplier.hasOwnProperty(prop) {
receiver[prop] = supplier[prop];
@C-Rodg
C-Rodg / ArrayToCSV.js
Created January 11, 2017 19:28
A more full featured csv writer class for generating csv strings, downloading a csv, etc. Cleans out many invalid csv characters.
function csvWriter(del, enc) {
this.del = del || ',';
this.enc = enc || '"';
this.escapeCol = (col) => {
if(isNaN(col)) {
if(!col) {
col = '';
} else {
col = String(col);
@C-Rodg
C-Rodg / QuickArrayToCSV.js
Last active January 11, 2017 19:26
A simple Javascript function that takes a two-dimensional array and converts it to a downloaded CSV file.
const fieldHeaders = ["First Name", "Last Name", "City", "State", "Country", "Phone Number"];
let data = [["Tom", "Stevens", "Seattle", "WA", "USA", "123-123-1234"], ["Mike", "Williams", "Detroit", "MI", "USA", "234-234-2345"], ["Janie", "Jackson", "Vancouver", "BC", "Canada", "987-977-9876"]];
data.unshift(fieldHeaders);
function createCSV(data) {
let csvContent = "data:text/csv;charset=utf-8,";
data.forEach((dataArray, index) => {
let dataStr = dataArray.join(',');
csvContent += index < data.length ? dataStr + '\n' : dataStr;
});
@C-Rodg
C-Rodg / MutatateToMatchIndexes.js
Created November 4, 2016 01:48
Mutate an input array so that each element ends up in their index from a corresponding array.
let arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
let idx = [4, 3, 5, 0, 6, 1, 2];
arr = idx.map((idxVal, index) => {
return arr[idx.indexOf(index)];
});
@C-Rodg
C-Rodg / BinarySearch.js
Created November 4, 2016 01:42
A binary search algorithm for sorted arrays. More efficient than the Array's 'indexOf' method
function binarySearch(items, value) {
let startIndex = 0,
stopIndex = items.length - 1,
middle = Math.floor((startIndex + stopIndex) / 2);
while(value !== items[middle] && startIndex < stopIndex) {
if(value < items[middle]) {
stopIndex = middle - 1;
} else if (value > items[middle]) {
startIndex = middle + 1;
@C-Rodg
C-Rodg / Debounce.js
Created November 4, 2016 01:35
A function that wraps another function and prevents it from repeatedly firing.
function debounce(func, immediate, wait) {
let timeout;
return function() {
let context = this,
args = arguments;
let callNow = immediate && !timeout;
clearTimeout(timeout);
@C-Rodg
C-Rodg / FlattenArray.js
Created November 4, 2016 01:31
A method for recursively flattening an array using a closure.
function flatten(input) {
var out = [];
var loop = function(arr) {
return arr.map.((val) => {
return Array.isArray(val) ? loop(val) : out.push(val);
});
};
loop(input);
return out;
}
@C-Rodg
C-Rodg / findNode.js
Created November 4, 2016 01:27
A recursive DOM traversal method for finding a specific node.
function traverse(node, current) {
if(node.isEqualNode(current)){
return current;
} else {
let el = null;
for(let i = 0; !el && i < current.children.length; i++){
el = traverse(node, current.children[i]);
}
return el;
}