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 / 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 / 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 / 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 / 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 / MicroLibrary.js
Last active October 9, 2017 17:46
A small micro-library that shows how libraries like jQuery are made.
const get = (selector, context) => {
// Select the items to manipulate
const GetNodes = function() {
this.nodes = context ? context.querySelectorAll(selector) : document.querySelectorAll(selector);
};
// Add new class to nodes
GetNodes.prototype.addClass = function(className) {
for (let i = 0, j = this.nodes.length; i < j; i++) {
this.nodes[i].classList.add(className);
@C-Rodg
C-Rodg / AsyncSyncPatterns.js
Created October 10, 2017 22:54
Example of doing a typically async action as synchronous with reduce() or async/await.
const itemIds = [1,2,3,4,5,6];
// Using reduce
itemIds.reduce((promise, id) => {
return promise.then(_ => api.deleteItem(id));
}, Promise.resolve());
// Using Async/Await
itemIds.forEach(async (item) => {
await api.deleteItem(item);
@C-Rodg
C-Rodg / doOnLoad.js
Created October 13, 2017 21:24
A quick snippet using Javascript's requestAnimationFrame to determine when the document is ready or a library is loaded.
const ready = () => {
// Example of checking if library is available
if ('jQuery' in window) {
return;
}
// Example of checking if document is loaded
if (document.body) {
// Do code here...
@C-Rodg
C-Rodg / compareObjectOrArray.js
Last active October 17, 2017 15:58
A function to fully compare objects and arrays.
const isEqual = (value, other) => {
// Tests - same object type, same length, same items
const type = Object.prototype.toString.call(value);
if (type !== Object.prototype.toString.call(other)) {
return false;
}
if (['[object Array]', '[object Object]'].indexOf(type) < 0) {
return false;
@C-Rodg
C-Rodg / ColorHexToRGB.js
Created October 31, 2017 22:24
A quick script using a bitwise operator to convert a color hex string into RGB.
const convertHexToRGB = (hex) => {
hex = hex[0] === '#' ? hex.substr(1) : hex;
const rgb = parseInt(hex, 16);
return {
Red: (rgb >> 16) & 0xFF,
Green: (rgb >> 8) & 0xFF,
Blue: rgb & 0xFF
};
};
@C-Rodg
C-Rodg / StringToBytesToBase64.js
Created November 1, 2017 23:03
Convert a string to a byte array and then encode with Base64. Useful for putting complex data structures in XML.
const convertToBytes = (str) => {
return new TextEncoder('utf-8').encode(str);
};
const dataString = 'TT12345,1:49:52 PM,10/15/13';
// ODQsODQsNDksNTAsNTEsNTIsNTMsNDQsNDksNTgsNTIsNTcsNTgsNTMsNTAsMzIsODAsNzcsNDQsNDksNDgsNDcsNDksNTMsNDcsNDksNTE=
btoa(convertToBytes(dataString));