Skip to content

Instantly share code, notes, and snippets.

View atomize's full-sized avatar
🎹
♩♩

Berti atomize

🎹
♩♩
View GitHub Profile
@atomize
atomize / promise_map.js
Created August 17, 2018 21:30 — forked from tokland/promise_map.js
Execute an array of promises sequentially and collect the result
function promiseMap(xs, f) {
const reducer = (ysAcc$, x) =>
ysAcc$.then(ysAcc => f(x).then(y => ysAcc.push(y) && ysAcc));
return xs.reduce(reducer, Promise.resolve([]));
}
/* Example */
const axios = require('axios');
@atomize
atomize / msBrowserAgentDetect.js
Created July 11, 2018 20:31
Check browser for Microsoft Edge/IE
var get_ie_version = function () {
var sAgent = window.navigator.userAgent;
var Idx = sAgent.indexOf("MSIE");
// If IE, return version number.
if (Idx > 0) {
return parseInt(sAgent.substring(Idx+ 5, sAgent.indexOf(".", Idx)));
}
// Condition Check IF IE 11 and or MS Edge
else if ( !!navigator.userAgent.match(/Trident\/7\./)
@atomize
atomize / localStorageShim.min.js
Created July 11, 2018 20:29
houd1ni/localStorage-by-IndexedDB-shim - Helps MS Edge be a real browser. [https://github.com/houd1ni/localStorage-by-IndexedDB-shim]
var options={poll_time:200},localStorageShim=function(t){var e,r="__localStorageShim",n="pairs",o="localStorageShim Error: ";const a=[];try{if(localStorage.setItem("a",4),4!=localStorage.getItem("a"))throw 1;e=function(){this.setItem=function(t,e){return localStorage.setItem(t,e)},this.getItem=function(t){return localStorage.getItem(t)}}}catch(c){e=function(){var e,c,i=indexedDB.open(r,7),u={},s=function(){e=db.transaction(n).objectStore(n),e.openCursor().onsuccess=function(t){var e,r=t.target.result;r&&(e=r.value,u[e.k]=[e.v,r.key],r["continue"]())}},l=function(t,r){if(t+="",r+="",!u[t]||u[t][0]!==r){u[t]=u[t]||[],u[t][0]=r,clearInterval(c),e=db.transaction(n,"readwrite").objectStore(n);var o=[{k:t,v:r}];u[t][1]&&o.push(u[t][1]),e=e.put.apply(window,o),e.onsuccess=d}},d=function(){s(),c=setInterval(s,t.poll_time)};i.onupgradeneeded=function(t){var e,r=t.target.result;try{e=t.target.transaction.objectStore(n)}catch(o){e=r.createObjectStore(n,{autoIncrement:!0})}try{e.createIndex("k","k",{unique:!0}),e.createI
@atomize
atomize / index.js
Created July 7, 2018 01:24 — forked from yandzee/index.js
Code retry with promises
// Author: Renat Tuktarov ([email protected])
const retry = function(fn, prev) {
return new Promise((current, reject) => {
const resolve = _ => (prev && prev()) || current();
fn(resolve, delay => {
setTimeout(_ => {
retry(fn, resolve);
}, delay);
@atomize
atomize / RateLimit.js
Created July 6, 2018 16:13
Pure Javascript rate limit function. [https://jsfiddle.net/47cbj/56/]
function RateLimit(fn, delay, context) {
var queue = [], timer = null;
function processQueue() {
var item = queue.shift();
if (item)
fn.apply(item.context, item.arguments);
if (queue.length === 0)
clearInterval(timer), timer = null;
}
@atomize
atomize / mapOrderES6.js
Last active May 21, 2018 22:20 — forked from ecarter/mapOrder.js
Order an array of objects based on another array order ES6
/*
Sort array of objects based on another array
Original function:
function mapOrder (array, order, key) {
array.sort( function (a, b) {
var A = a[key], B = b[key];
if (order.indexOf(A) > order.indexOf(B)) {
return 1;
} else {
@atomize
atomize / README.MD
Created May 9, 2018 20:04 — forked from KaraAJC/README.MD
ReadMe Template

Title

Description

Add a one-sentence description of this project. and a link to the live demo.

Features

  • completed feature: What this feature does
  • pending feature: What this feature does

ScreenShots

@atomize
atomize / devnull.txt
Created May 8, 2018 23:47
Advance I/O redirection in bash. /dev/null
For those unfamiliar with 'advanced' i/o redirection in bash:
1) 2>&- ("close output file descriptor 2", which is stderr) has the same result as 2> /dev/null;
2) >&2 is a shortcut for 1>&2, which you may recognize as "redirect stdout to stderr".
See the Advanced Bash Scripting Guide i/o redirection page for more info.
– mikewaters Dec 21 '11 at 19:48
-comment from : https://stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script
@atomize
atomize / rate_limit.js
Created May 4, 2018 00:11 — forked from mattheworiordan/rate_limit.js
Rate limiting function calls with JavaScript and Underscore.js
/* Extend the Underscore object with the following methods */
// Rate limit ensures a function is never called more than every [rate]ms
// Unlike underscore's _.throttle function, function calls are queued so that
// requests are never lost and simply deferred until some other time
//
// Parameters
// * func - function to rate limit
// * rate - minimum time to wait between function calls
// * async - if async is true, we won't wait (rate) for the function to complete before queueing the next request
@atomize
atomize / websockets-server.js
Created March 27, 2018 16:47 — forked from bradwright/websockets-server.js
Pure Node.js WebSockets server
/*
* node-ws - pure Javascript WebSockets server
* Copyright Bradley Wright <[email protected]>
*/
// Use strict compilation rules - we're not animals
'use strict';
var net = require('net'),
crypto = require('crypto');