Skip to content

Instantly share code, notes, and snippets.

View laphilosophia's full-sized avatar
🖖
live long and prosper

Erdem Arslan laphilosophia

🖖
live long and prosper
View GitHub Profile
@laphilosophia
laphilosophia / storage-listener.js
Last active November 12, 2018 11:24
storage api listener
// Listener
window.addEventListener('storage', handlerEvent)
// Whenever we set a localStorage, window object will listen and invoke this handlerEvent function.
// We do all validation and stuff to act accordingly.
// Handler
const handlerEvent = event => {
if (event.originalEvent.key != 'message') return
@laphilosophia
laphilosophia / README.md
Created November 6, 2018 08:30 — forked from datchley/README.md
Micro templating library for javascript

Micro-Template: README.md

Valid template expressions:

  • expressions can reference any property on the passed in context, including nested properties and indexed access to array properties.
     {{ name }}
 {{ obj.name }}
@laphilosophia
laphilosophia / polling.js
Created November 5, 2018 12:12
Javascript polling functions
// deferred
function poll(fn, timeout, interval) {
let endTime = Number(new Date()) + (timeout || 2000)
interval = interval || 100
let checkCondition = (resolve, reject) => {
// If the condition is met, we're done!
let result = fn()
if (result) {
@laphilosophia
laphilosophia / object-copy.js
Created November 5, 2018 12:11
Object copy methods
// SHALLOW COPY
const obj1 = {
a: 1,
b: 2,
c: 'three',
d: new Date()
}
const obj2 = {
@laphilosophia
laphilosophia / debounce-resize-event.js
Last active November 5, 2018 12:11
on the window resizes, recalculate the canvas width as well, using a debounce to avoid calling too many times our canvas resizing
const canvas = document.querySelector('canvas')
const debounce = (func) => {
let timer
return (event) => {
if (timer) { clearTimeout(timer) }
timer = setTimeout(func, 100, event)
}
}
@laphilosophia
laphilosophia / currying.js
Created November 5, 2018 12:10
Javascript Currying and Partial Application Examples
// Javascript Currying and Partial Application Examples
const multiplier = x => {
return (y, z) => {
return z * y * z
}
}
const mult = multiplier(10) // x
mult(20, 30) // y, z
@laphilosophia
laphilosophia / cache-api.js
Created November 5, 2018 12:09
Cache API
// https://alligator.io/js/cache-api/
if ('caches' in window) {
const cacheName = 'sample-caches'
const url = '/resource'
caches.open(cacheName).then(cache => {
return console.log(cache)
})
@laphilosophia
laphilosophia / message-api.js
Created November 5, 2018 12:09
Message API
// Instantiate the Channel
const channel = new MessageChannel()
// Sending Messages Through the Channel
const data = {
color: 'blue',
title: 'Lorem ipsum dolor',
number: '000998909874564781',
content: 'Lorem ipsum dolor sit amet. Consectetur adicisping alet.',
}
@laphilosophia
laphilosophia / cookies.js
Created October 4, 2018 17:09 — forked from arozwalak/cookies.js
Javascript: Cookies set/get/delete
function getCookie(c_name) {
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1) {
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1) {
c_value = null;
}
else {
@laphilosophia
laphilosophia / IndexedDB101.js
Created July 24, 2018 14:20 — forked from JamesMessinger/IndexedDB101.js
Very Simple IndexedDB Example
// This works on all devices/browsers, and uses IndexedDBShim as a final fallback
var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB || window.shimIndexedDB;
// Open (or create) the database
var open = indexedDB.open("MyDatabase", 1);
// Create the schema
open.onupgradeneeded = function() {
var db = open.result;
var store = db.createObjectStore("MyObjectStore", {keyPath: "id"});