Skip to content

Instantly share code, notes, and snippets.

View caracal7's full-sized avatar

Dmitrii Vasilev caracal7

  • This planet
View GitHub Profile
@neonerd
neonerd / wait.js
Created March 24, 2017 17:29
Async / await wait
function wait (s) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(true), s)
})
}
async function main () {
console.log('start now')
await wait(2000)
console.log('end after 2 seconds')
@gvergnaud
gvergnaud / lazy-list.js
Last active September 13, 2024 23:03
Lazy List, implemented with es6 generators
/* ----------------------------------------- *
Lazy List Implementation
* ----------------------------------------- */
// Haskell-like infinite List, implemented with es6 generators
// Lazyness lets you do crazy stuff like
List.range(0, Infinity)
.drop(1000)
.map(n => -n)
@scottshane
scottshane / proxy-reflect.js
Last active January 28, 2018 12:11
Proxy / Reflect Experiment
var targetObject = {
sayHi(){ return `returning show: ${p.show}` },
show: 'family guy',
characters: {
dad: 'peter',
mom: 'lois',
daughter: 'meg',
son: 'chris',
baby: 'stewie',
dog: 'brian'
@purag
purag / easing.js
Last active March 18, 2019 17:29
A collection of simple easing functions for JavaScript
let easing = {
// classic linear
linear: t => t,
// slow -> fast
easeInQuad: t => t*t,
easeInCubic: t => t*t*t,
easeInQuint: t => t*t*t*t,
// fast -> slow
@thetutlage
thetutlage / scroll.js
Created December 13, 2016 17:21
Scroll to the bottom of an element with smooth animation - Pure Javascript
function scrollTo (element, duration) {
if (!element) {
return
}
var target = element.scrollHeight
target = Math.round(target)
duration = Math.round(duration)
if (duration < 0) {
return false
}
(function () {
var module = {};
// maps
var LOBBY = [
[ 1, 1, 1, 1, 1, 1, 1, 1 ],
[ 1, 0, 0, 0, 1, 0, 0, 1 ],
[ 1, 0, 0, 0, 1, 0, 0, 1 ],
[ 1, 0, 0, 0, 1, 0, 0, 1 ],
[ 1, 0, 0, 0, 1, 1, 0, 1 ],
@hyamamoto
hyamamoto / string.hashcode.js
Created September 30, 2016 07:19
JavaScript Implementation of String.hashCode() .
/**
* Returns a hash code for a string.
* (Compatible to Java's String.hashCode())
*
* The hash code for a string object is computed as
* s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
* using number arithmetic, where s[i] is the i th character
* of the given string, n is the length of the string,
* and ^ indicates exponentiation.
* (The hash value of the empty string is zero.)
@velipso
velipso / astar.js
Last active February 3, 2018 13:44
Single function that implements A-Star pathfinding algorithm in ~75 lines of code
// PUBLIC DOMAIN
//
// Single function that implements A-Star pathfinding algorithm in ~75 lines of code
//
// (why use a huge library..?)
//
// Parameters:
// solid array of booleans (of size width * height) that determine if a tile is solid
// width width of the map
// height height of the map
@ddmills
ddmills / geotic.js
Created July 20, 2016 03:33
Component-Entity-System
let _id;
const id = () => {
let now = Date.now();
if (now <= _id) now++;
_id = now;
return now;
}
const hash = (n) => n.sort((a, b) => a > b).join('$');
const remove = (a, v) => a.splice(a.indexOf(v), 1);
@yasirtaher
yasirtaher / draggable-for-touch-device.js
Created June 25, 2016 07:51
JS Drag and Drop for touch devices
//source: http://stackoverflow.com/questions/5186441/javascript-drag-and-drop-for-touch-devices
function touchHandler(event) {
var touch = event.changedTouches[0];
var simulatedEvent = document.createEvent("MouseEvent");
simulatedEvent.initMouseEvent({
touchstart: "mousedown",
touchmove: "mousemove",
touchend: "mouseup"
}[event.type], true, true, window, 1,