Skip to content

Instantly share code, notes, and snippets.

@Reshetnyak
Reshetnyak / Why_Objec_freeze.ts
Created February 13, 2018 18:10
I think that Object.freeze is the easiest way to create readonly object
// ways to create readonly objects
// 1) Object.freeze
// but only for one level of nesting and primitives
const o = Object.freeze({ foo: 'bar' });
o.foo = 10 // error;
// 2) enum
// Pros: straightforward description
function getPropIfExist(propString, obj) {
return propString
.match(/(\w+)|(\[\d+\])/g)
.map(prop=>{
const index = (prop.match(/\[(\d+)\]/) || ['', ''])[1];
return index ? { index: parseInt(index, 10) } : { name: prop }
})
.reduce(
@Reshetnyak
Reshetnyak / canvas-fix.js
Last active July 12, 2016 08:31
Make relative dense of particles according to device width
// https://www.edge1s.com/uploads/set_resources_2/6662f1e8900008d8258d107ac1c7d0a9_bg_effect.js
var _gsScope="undefined"!=typeof module&&module.exports&&"undefined"!=typeof global?global:this||window;(_gsScope._gsQueue||(_gsScope._gsQueue=[])).push(function(){"use strict";_gsScope._gsDefine("easing.Back",["easing.Ease"],function(t){var e,i,s,r=_gsScope.GreenSockGlobals||_gsScope,n=r.com.greensock,a=2*Math.PI,o=Math.PI/2,h=n._class,l=function(e,i){var s=h("easing."+e,function(){},!0),r=s.prototype=new t;return r.constructor=s,r.getRatio=i,s},_=t.register||function(){},u=function(t,e,i,s){var r=h("easing."+t,{easeOut:new e,easeIn:new i,easeInOut:new s},!0);return _(r,t),r},c=function(t,e,i){this.t=t,this.v=e,i&&(this.next=i,i.prev=this,this.c=i.v-e,this.gap=i.t-t)},p=function(e,i){var s=h("easing."+e,function(t){this._p1=t||0===t?t:1.70158,this._p2=1.525*this._p1},!0),r=s.prototype=new t;return r.constructor=s,r.getRatio=i,r.config=function(t){return new s(t)},s},f=u("Back",p("BackOut",function(t){return(t-=1)
function* generatorMaker(){
var a = yield 10;
var b = yield 20;
return a + b;
}
var generator = generatorMaker();
console.log( generator.next() );
// 1) return right side of yield ( 10 )
var async = makeGenerator => (...args) => {
const generator = makeGenerator.apply(this, args);
const handle = result => {
console.log('result from consumer: ', result);
if (result.done){
return Promise.resolve( result.value )
}
@Reshetnyak
Reshetnyak / Promise.delay.js
Last active June 7, 2016 21:51
Q.delay behaviour implementation with native Promises
/**
* Q.delay behaviour implementation with native Promises
* @param ms - delay in milliseconds
* @returns {Promise} with the same result as original promise but will only be
* resolved or rejected after at least ms milliseconds
*/
Promise.prototype.delay = function(ms){
// Save original promise
const promiseToDelay = this;
@Reshetnyak
Reshetnyak / allSettled.js
Last active June 9, 2016 08:55
Q.allSettled behaviour implementation with native Promises
/**
* Q.allSettled behaviour implementation with native Promises
* Can be useful if you don't want to stop sequence after rejection
* @param {Array} promises - Array of promises
* @returns {Array} Results of each promise
*/
const allSettled = promises => {
// Wrap each promise from array with promise which resolves in both cases, either resolved of rejected
const wrap = promise => {
@Reshetnyak
Reshetnyak / pauseApp.js
Last active July 15, 2016 09:14
Pause app, trigger event, resume app.
var EVENT_NAME = 'beforeBootstrap';
pauseBootstrap();
// track event after application configuration was registered;
// it means all modules parts was registered, but compile phase was not started
window.addEventListener('DOMContentLoaded', function(){
setTimeout(function(){
// TODO: add comments
@Reshetnyak
Reshetnyak / getPropIfExist_Loop.js
Created February 8, 2016 13:02
getPropIfExist with loop implementation.
/**
* Get object property if it exist.
* @param {string} propStr - property of object with any level of nesting. 'person.address.home.floor'
* @returns {*} needed property
*/
function getPropIfExist(propStr){
var props = propStr.split('.');
var numOfProps = props.length;
@Reshetnyak
Reshetnyak / SaveAngularCore.js
Created February 3, 2016 15:33
With ui-router
/**
* Pause application loading, save references to Angular core, then run application.
* Reshetniak Denis 26.01.2015
*/
;(function saveAngularCore(){
//there is iframe which doesn't have angular, so we need to break execution in this case
if (window.top !== window){ return false }