Skip to content

Instantly share code, notes, and snippets.

View zerobias's full-sized avatar
💭
Set your status

Dmitry zerobias

💭
Set your status
View GitHub Profile
@zerobias
zerobias / API.js
Last active September 28, 2016 16:25
Metaprogramming in ES6
//https://www.keithcirkel.co.uk/metaprogramming-in-es6-symbols/
//Symbol
//Giving developers ability to add hooks to their objects, through your API
// Retreive the magic inspect Symbol from the API's Symbol constants
var inspect = console.Symbols.INSPECT
var myVeryOwnObject = {}
console.log(myVeryOwnObject) // logs out `{}`
@zerobias
zerobias / PrivateFieldsModule.js
Last active October 20, 2016 15:37
Private fields in js
//module version
var Module = (function (window) {
function privateFunction() {
console.log('pass');
};
var privateClass = function () {};
var publicClass = function () {};
@zerobias
zerobias / isEmpty.js
Created October 20, 2016 15:39
How to filter really empty data
var isEmpty = (a, b, c) => {
return ![a, b, c].join("");
}
var isEmpty = (...rest) => {
return !rest.join("");
}
isEmpty(void 0, [], null) // => true
isEmpty(void 0, [], null, 0) // => false
@zerobias
zerobias / asyncActionWithStartDispatch.js
Created October 22, 2016 18:56
Redux async action creator with initial dispatching
function makeAjaxCall(someValue) {
return (dispatch, getState) => {
dispatch({type : "REQUEST_STARTED"});
myAjaxLib.post("/someEndpoint", {data : someValue})
.then(response => dispatch({type : "REQUEST_SUCCEEDED", payload : response})
.catch(error => dispatch({type : "REQUEST_FAILED", error : error});
};
}
@zerobias
zerobias / importsanctuary.js
Created October 26, 2016 09:32
Import sanctuary monad functional library without runtime typecheck
const {create, env} = require('sanctuary')
const checkTypes = false
//or enable it only in developement
//const checkTypes = process.env.NODE_ENV !== 'production'
const S = create({checkTypes: checkTypes, env: env})
module.exports = S
@zerobias
zerobias / fpipe.js
Last active November 9, 2016 23:51
As R.pipe, but for functions itself: fp( f, g, h ) instead of a=>f( g( h ) )(a)
const R = require('ramda')
const flatRestArgs = p=>(...e)=>(p)(R.flatten(e))
const reducer = R.reduceRight(R.flip(R.call))
const listAdapter = R.converge(reducer,[R.last,R.init])
const fpipe = flatRestArgs( listAdapter )
module.exports = fpipe
//===EXAMPLE===
@zerobias
zerobias / humint.js
Last active December 9, 2016 13:06
Logger
const winston = require('winston')
const R = require('ramda')
const chalk = require('chalk') //console colorizer
const P = R.pipe //Short form for pipe: the most frequently used function
const yellowTag = chalk.bold.yellow
const redTag = chalk.bold.bgRed.black
//Simply write 'error' and its will be marked red,
//and any other wiil be yellow if its hasnt have color yet
@zerobias
zerobias / MixinES6.js
Last active December 12, 2016 10:12
Native dynamic es6 class mixin
const Storage = Sup => class extends Sup {
save(database) { }
}
const Validation = Sup => class extends Sup {
validate(schema) { }
}
class Person { }
class Employee extends Storage(Validation(Person)) { }
@zerobias
zerobias / pause.js
Created January 4, 2017 15:28
Async pause
const resolver = delay => resolve => setTimeout(() => resolve(delay), delay)
const pause = (delay=5000) => new Promise(resolver(delay))
module.exports = pause
@zerobias
zerobias / equation.JSweirdness.js
Last active February 3, 2017 09:33
JS weirdness
0 < null; // false
0 > null; // false
0 == null; // false
0 <= null; // true
0 >= null // true
-1 == false; // => false
-1 == true; // => false