Skip to content

Instantly share code, notes, and snippets.

@softwarespot
softwarespot / LinkedList.js
Last active April 21, 2017 19:11
LinkedList implementation
function LinkedList() {
if (!(this instanceof LinkedList)) {
throw new TypeError('"LinkedList" cannot be called as a function. Use the "new" keyword to construct a new "LinkedList" object');
}
this._head = undefined;
}
LinkedList.prototype = {
clear: function () {
@softwarespot
softwarespot / Map_Set.js
Last active April 23, 2017 13:24
Map & Set implementations
// The following is a proof of concept for creating Map and Set data structures found in ES2015
// URL: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
// URL: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
function Map(iterable) {
if (!(this instanceof Map)) {
throw new TypeError('"Map" cannot be called as a function. Use the "new" keyword to construct a new "Map" object');
}
this._keys = [];
@softwarespot
softwarespot / on.js
Last active July 2, 2017 13:55
Event delegation
var on = _createEvent.bind(null, 'on');
var remove = on(document, 'click', 'body', function (event, target) {
// "this" is set to the target element node
console.log('On::', this, event, target);
});
var once = _createEvent.bind(null, 'once');
once(document, 'click', 'body', function (event, target) {
// "this" is set to the target element node
console.log('Once::', this, event, target);
function DOM(selector) {
if (!(this instanceof DOM)) {
return new DOM(selector);
}
// Helper for pushing an array-like object onto the current instance
Array.prototype.push.apply(this, document.querySelectorAll(selector));
}
_inheritFrom(DOM, Array);
function ArrayObject(arr) {
arr.forEach((value, index) => {
this[index] = value;
});
this.guid = 'RANDOM_ARRAY_LIKE_GUID_' + Math.floor(Math.random() * 9999);
// Create an immutable length property
Object.defineProperty(this, 'length', {
value: arr.length
@softwarespot
softwarespot / good_bad_promises.js
Last active October 2, 2016 09:50
The good/bad promise examples
// Idea 1: http://www.datchley.name/promise-patterns-anti-patterns/
// good
good();
function good() {
// getJSON returns a promise, so just continue the chain
return getJSON()
.then(function (artists) {
// Map the array of artists to an array of names
@softwarespot
softwarespot / yield_generator.js
Created September 18, 2016 19:37
yield/generator/bluebird
// Idea 1: https://www.promisejs.org/generators/
// Idea 2: http://bluebirdjs.com/docs/api/promise.coroutine.html
const asyncDemo = Promise.coroutine(function* () {
try {
const res = yield getJSON()
console.log(res.value)
} catch (err) {
console.log(err)
}
@softwarespot
softwarespot / async_await.js
Last active September 18, 2016 19:33
async/await Examples
// Idea 1: https://medium.com/@bluepnume/learn-about-promises-before-you-start-using-async-await-eb148164a9c8#.s6914ps6a
// Use: https://babeljs.io/repl/ to transpile as ES5
async function asyncDemo() {
try {
const res = await getJSON() // Promise.all([getJSON(), getJSON()]) for concurrent requests
console.log(res.value)
} catch (err) {
console.log(err)
}
@softwarespot
softwarespot / jsonp.php
Created May 1, 2016 19:09 — forked from mathiasbynens/jsonp.php
Basic JSON/JSON-P service in PHP
<?php
// Prevent content sniffing attacks such as http://mths.be/bst.
header('X-Content-Type-Options: nosniff');
// Note: The user-provided callback name must be filtered to prevent attack
// vectors. This script simply removes any symbols other than `[a-zA-Z0-9$_]`
// from the input. Sadly, this blocks the use of some valid JavaScript
// identifiers, and also accepts a few invalid ones. See
// http://mathiasbynens.be/notes/javascript-identifiers for details.
@softwarespot
softwarespot / domReady.js
Last active April 11, 2016 19:28
Modern day DOM ready
var dom = (function dom(window, document) {
// Based on the idea of jQuery 3
var _resolveReady;
var _isReady = false;
var _listReady = new window.Promise(function promiseReady(resolve) {
_resolveReady = resolve;
});
// Check if the DOM has completed loading or is not in a loading state
if (document.readyState === 'complete' || document.readyState !== 'loading') {