This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const printer = (function() { | |
| let printerInstance; | |
| function create() { | |
| function print() { | |
| console.log("Printing document..."); | |
| } | |
| function turnOn() { | |
| console.log("Turning on, checking for paper..."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> | |
| `; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() ); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| }; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const convertToBytes = (str) => { | |
| return new TextEncoder('utf-8').encode(str); | |
| }; | |
| const dataString = 'TT12345,1:49:52 PM,10/15/13'; | |
| // ODQsODQsNDksNTAsNTEsNTIsNTMsNDQsNDksNTgsNTIsNTcsNTgsNTMsNTAsMzIsODAsNzcsNDQsNDksNDgsNDcsNDksNTMsNDcsNDksNTE= | |
| btoa(convertToBytes(dataString)); |