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 / mini-react.js
Created September 29, 2017 16:39
A simple view framework based off of React.
// Render a template string or function to a node
const render = (template, node) => {
if (!node) return;
node.innerHTML = (typeof template === 'function' ? template() : template);
// Dispatch event on render
const event = new CustomEvent('elementRenders', { bubbles: true });
node.dispatchEvent(event);
return node;
};
@C-Rodg
C-Rodg / RequestVideoAudio.js
Created July 19, 2017 19:16
Using WebRTC to request camera and microphone access in the browser.
navigator.getUserMedia( { video: true, audio: true }, (stream) => {
localStream = stream;
const cameraBox = document.getElementById('camera-box');
cameraBox.setAttribute('src', URL.createObjectURL( stream ) );
init();
}, (error) => {
alert('error accessing usermedia ' + error.toString() );
});
@C-Rodg
C-Rodg / CustomElements.js
Last active April 26, 2017 22:18
An example of custom HTML elements and using the shadow DOM.
customElements.define('my-element', class extends HTMLElement {
constructor() {
super();
const shadow = this.attachShadow({mode: 'open'});
shadow.innerHTML = `
<style> #custom-shadow { color: red; } ::slotted(h1) { font-size: 38px; } :host { width: 600px; }</style>
<slot id="headerSlot" name="header"></slot>
<div id="custom-shadow">My custom shadow element</div>
<slot id="footerSlot" name="footer"></slot>
`;
@C-Rodg
C-Rodg / Singleton.js
Created April 7, 2017 17:38
Singleton pattern implemented with Javascript.
const printer = (function() {
let printerInstance;
function create() {
function print() {
console.log("Printing document...");
}
function turnOn() {
console.log("Turning on, checking for paper...");
@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);
@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 / 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 / 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 / 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 / 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];