Skip to content

Instantly share code, notes, and snippets.

View C-Rodg's full-sized avatar
👽

Curtis C-Rodg

👽
View GitHub Profile

React Tweetbox

A beginners React component for a twitter tweetbox. Handles logic for adding photos, character counts, and overflow alert.

A Pen by C-Rodg on CodePen.

License.

@C-Rodg
C-Rodg / Material Action Button.markdown
Created July 13, 2016 19:26
Material Action Button

Material Action Button

A html/css material design action button to provide more options to a user

A Pen by C-Rodg on CodePen.

License.

@C-Rodg
C-Rodg / FindIdenticalNode.js
Created November 4, 2016 01:16
This algorithm displays how you can locate the same node in another identical DOM tree.
// Helper function to provide Array's indexOf method to DOM elements
function indexOf(arr, target) {
return Array.prototype.indexOf.call(arr, target);
}
// Get the path of the target node from Root A
function getPath(root, target) {
let current = target;
let path = [];
while(current !== root) {
@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;
}
@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 / 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 / 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 / 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 / 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;
});