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 / 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 / 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 / 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 / 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 / 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 / what-forces-layout.md
Created February 15, 2019 07:20 — forked from paulirish/what-forces-layout.md
What forces layout/reflow. The comprehensive list.

What forces layout / reflow

All of the below properties or methods, when requested/called in JavaScript, will trigger the browser to synchronously calculate the style and layout*. This is also called reflow or layout thrashing, and is common performance bottleneck.

Element

Box metrics
  • elem.offsetLeft, elem.offsetTop, elem.offsetWidth, elem.offsetHeight, elem.offsetParent
  • elem.clientLeft, elem.clientTop, elem.clientWidth, elem.clientHeight
  • elem.getClientRects(), elem.getBoundingClientRect()
@laphilosophia
laphilosophia / modal.html
Created February 21, 2019 12:39
simple-modal for once run
<style type="text/css">
.gaye-modal--modal {
width: 100%;
height: 100%;
position: fixed;
top: 0; left: 0;
z-index: 9999;
background-color: rgba(0, 0, 0, 0.5);
@laphilosophia
laphilosophia / closure-example.js
Created February 21, 2019 13:39
javascript closure example
function apiConnect(apiKey) {
function get(route) {
return fetch(`${route}?key=${apiKey}`)
}
function post(route, params) {
return fetch(route, {
method: 'POST',
body: JSON.stringify(params),
headers: {
@laphilosophia
laphilosophia / js-turkish-to-english.js
Created February 25, 2019 11:36 — forked from enginkartal/js-turkish-to-english.js
Javascript Turkish character to english characters change
String.prototype.turkishtoEnglish = function () {
return this.replace('Ğ','g')
.replace('Ü','u')
.replace('Ş','s')
.replace('I','i')
.replace('İ','i')
.replace('Ö','o')
.replace('Ç','c')
.replace('ğ','g')
.replace('ü','u')
@laphilosophia
laphilosophia / axios.js
Created November 6, 2019 19:57 — forked from wooooodward/axios.js
Axios plugin example with request interceptor that adds JWT token to the auth header and 401 response interceptor to refresh token
import Vue from 'vue'
import axios from 'axios'
import store from '../store'
import { TokenService } from '../services/storage.service'
// Full config: https://github.com/axios/axios#request-config
let config = {
baseURL:
process.env.baseURL ||