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 / parseQuery.js
Last active August 13, 2017 09:50
Parse query string
function parseQuery(locationSearch) {
locationSearch = locationSearch.substring(1); // remove "?" at the beginning
var values = locationSearch.split('&');
var queryObj = values.reduce(function (result, item) {
var parts = item.split('=');
result[decodeURIComponent(parts[0])] = parts[1];
return result;
}, {});
return queryObj;
}
//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'
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]);
}
}
// Async/await parallelism
async function foo() {
const [result1, result2] = await Promise.all([
asyncFunc1(),
asyncFunc2(),
]);
}
typeof null //object
NaN === NaN //false
typeof NaN //number
// if condition
{someName && <div>{someName}</div>}
//if-else consdition
{ loggedIn && <LogoutButton /> || <LoginButton /> }
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);
//Encapsulate conditionals
function shouldShowSpinner(fsm, listNode) {
return fsm.state === 'fetching' && isEmpty(listNode);
}
if (shouldShowSpinner(fsmInstance, listNodeInstance)) {
// ...
}
//Dependency injection
// the Module object
var Module = function (someService) {
this.someService = someService;
};
Module.prototype.do = function () {
this.someService.doSomething();
};
//Rest operator type
 
 ...args: number[]