Skip to content

Instantly share code, notes, and snippets.

View ladas-larry's full-sized avatar

Ladislav M. ladas-larry

View GitHub Profile
@ladas-larry
ladas-larry / actionCreator.ts
Created July 9, 2019 07:29 — forked from jiayihu/actionCreator.ts
Minimal action creator factory for redux-observable and redux-saga
import { IAction } from '../types/redux.types';
import { actionTypes as errorTypes, showError } from './errors.actions';
interface IPayloadCreators {
request(...args: any[]): any;
success(...args: any[]): any;
failure?(errorMsg: string): any;
}
interface IActionCreator {
//Rest operator type
 
 ...args: number[]
//Dependency injection
// the Module object
var Module = function (someService) {
this.someService = someService;
};
Module.prototype.do = function () {
this.someService.doSomething();
};
//Encapsulate conditionals
function shouldShowSpinner(fsm, listNode) {
return fsm.state === 'fetching' && isEmpty(listNode);
}
if (shouldShowSpinner(fsmInstance, listNodeInstance)) {
// ...
}
Centering in the Unknown
/* Vertically centering an element of known dimensions inside an element of unknown dimensions */
/* image width and height is 24px. Plus 10px padding it comes to 34px. 17px is half of this width and height */
.centeredIcon {
padding: 5px;
position: absolute;
top: calc(50% - 17px);
// if condition
{someName && <div>{someName}</div>}
//if-else consdition
{ loggedIn && <LogoutButton /> || <LoginButton /> }
typeof null //object
NaN === NaN //false
typeof NaN //number
// Async/await parallelism
async function foo() {
const [result1, result2] = await Promise.all([
asyncFunc1(),
asyncFunc2(),
]);
}
function walkTree(node) {
if (node == null){
return;
}
// do something with the current node here
var childNodes = node.childNodes;
for (var i = 0; i < childNodes.length; i++) {
walkTree(node.childNodes[i]);
}
}
//array of objects to array of values
var arr = [{val: 'foo'}, {val: 'bar'}];
arr = arr.map((item) => {
return item.val;
});
console.log(arr) // ['foo', 'bar']
// reversing string
var str = 'abcde'