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 / 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 / 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 / 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 / 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 / ObjectsEqual.js
Created March 21, 2017 03:47
A Javascript function to test for object equality.
function isObjectEqual(a, b) {
let aProps = Object.getOwnPropertyNames(a),
bProps = Object.getOwnPropertyNames(b);
if (aProps.length !== bProps.length) {
return false;
}
for (let i = 0; i < aProps.length; i++) {
let propName = aProps[i];
@C-Rodg
C-Rodg / StacksAndQueues.js
Last active March 30, 2017 05:11
A simple example using linear data structures of Stacks and Queues in Javascript.
function Stack() {
this._size = 0;
this._storage = {};
}
Stack.prototype.push = function(data) {
this._size += 1;
this._storage[this._size] = data;
};
@C-Rodg
C-Rodg / SinglyLinkedList.js
Last active April 4, 2017 19:27
A Javascript implementation of a Singly-Linked List
function Node(data) {
this.data = data;
this.next = null;
}
function SinglyList() {
this._length = 0;
this.head = null;
}
@C-Rodg
C-Rodg / DoublyLinkedList.js
Created March 30, 2017 06:12
A Javascript implementation of a Doubly-Linked List.
function Node(value) {
this.data = value;
this.previous = null;
this.next = null;
}
function DoublyList() {
this._length = 0;
this.head = null;
this.tail = null;
@C-Rodg
C-Rodg / Trees.js
Last active April 1, 2017 20:22
An example of Trees implemented in Javascript. Using Depth-First Search (stack/DFS) and Breadth-First Search (queue/BFS).
function Queue() {
this._storage = {};
this._oldestIndex = 1;
this._newestIndex = 1;
}
Queue.prototype.size = function() {
return this._oldestIndex - this._newestIndex;
};
@C-Rodg
C-Rodg / ObserverPattern.js
Last active April 7, 2017 17:25
An example of the Observer pattern implemented with Javascript.
function Subject() {
let context = this;
this.observers = [];
return {
subscribeObserver(observer) {
context.observers.push(observer);
},
unsubscribeObserver(observer) {
const idx = context.observers.indexOf(observer);