Skip to content

Instantly share code, notes, and snippets.

Avatar
🖖
live long and prosper

Erdem Arslan laphilosophia

🖖
live long and prosper
View GitHub Profile
@laphilosophia
laphilosophia / promise_map.js
Created May 4, 2021 — forked from tokland/promise_map.js
Execute promises sequentially (one at at a time) and return array with the results
View promise_map.js
function promiseMap(inputValues, mapper) {
const reducer = (acc$, inputValue) =>
acc$.then(acc => mapper(inputValue).then(result => acc.push(result) && acc));
return inputValues.reduce(reducer, Promise.resolve([]));
}
/* Example */
const axios = require('axios');
@laphilosophia
laphilosophia / fpsMeter.js
Created Dec 21, 2020 — forked from capfsb/fpsMeter.js
JavaScript FPS meter - Calculating frames per second
View fpsMeter.js
fpsMeter() {
let prevTime = Date.now(),
frames = 0;
requestAnimationFrame(function loop() {
const time = Date.now();
frames++;
if (time > prevTime + 1000) {
let fps = Math.round( ( frames * 1000 ) / ( time - prevTime ) );
prevTime = time;
@laphilosophia
laphilosophia / spinner.ts
Created Dec 9, 2020
Typescript spinner sample
View spinner.ts
export default class Spinner {
#elements: NodeListOf<HTMLElement> = document.querySelectorAll('[data-zm-spinner]')
#config: {
title: string,
max: number,
min: number,
} = {
title: 'ADET',
max: 10,
min: 1,
View generator.js
/**
* @param {number} range
*/
function* generator (range) {
let i = 0
while (i < range) {
i += 1
yield i
}
View long-polling.js
// Long Polling
let ID = 1
const foo = document.createElement('p')
async function timeout (callback, delay) {
return await setTimeout(callback, delay)
}
function polling () {
return timeout(() => {
View scroll-distance.js
const scrollDistance = (callback, refresh) => {
if (!callback || typeof callback !== 'function') return
let isScrolling, start, end, distance
window.addEventListener('scroll', event => {
if (!start) {
start = window.pageYOffset
}
View scroll-converter.js
export default () => {
let docElem = document.documentElement
let active = false
let hasDeactivated = false
let eventsBound = false
let mouseWheelHandler
let scrollHandler
const scrollCallback = (offset, event, callback) => {
@laphilosophia
laphilosophia / onChange.js
Created Dec 11, 2019
Proxy / Reflect based object watcher
View onChange.js
const obj = {
name: 'Erdem',
surname: 'Arslan'
}
const onChange = (watch, callback) => {
const handler = {
get (target, prop, receiver) {
callback(target, prop, receiver)
@laphilosophia
laphilosophia / js-tricks.js
Created Dec 11, 2019
javascript tricks - array and objects
View js-tricks.js
[...Array(10).keys()]
// [1, 2, 3, 4, 5, 6, 7, 8, 9]
[...Array({ one:'1' }, { two: '2' }, { three: '3' }).keys()]
// [1, 2, 3]
let arr1 = [1, 2, 3]
let arr2 = [2, 3, 4]
View array-methods.js
let array = []
// Joins two or more arrays, and returns a copy of the joined arrays
array.concat(otherArray)
// Copies array elements within the array, to and from specified positions
array.copyWithin(target, start, end)
// Returns a key/value pair Array Iteration Object
array.entries()