Skip to content

Instantly share code, notes, and snippets.

ladas-larry

  • Berlin, Germany
Block or report user

Report or block ladas-larry

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View badParts.js
typeof null //object
NaN === NaN //false
typeof NaN //number
View asynchronicity.js
// Async/await parallelism
async function foo() {
const [result1, result2] = await Promise.all([
asyncFunc1(),
asyncFunc2(),
]);
}
View jsx.js
// if condition
{someName && <div>{someName}</div>}
//if-else consdition
{ loggedIn && <LogoutButton /> || <LoginButton /> }
View dom.js
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]);
}
}
View cleanCode.js
//Encapsulate conditionals
function shouldShowSpinner(fsm, listNode) {
return fsm.state === 'fetching' && isEmpty(listNode);
}
if (shouldShowSpinner(fsmInstance, listNodeInstance)) {
// ...
}
@ladas-larry
ladas-larry / parseQuery.js
Last active Aug 13, 2017
Parse query string
View parseQuery.js
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;
}
View dataTransformations.js
//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'
View tricks.css
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);
View patterns.js
//Dependency injection
// the Module object
var Module = function (someService) {
this.someService = someService;
};
Module.prototype.do = function () {
this.someService.doSomething();
};
View typescript.js
//Rest operator type
 
 ...args: number[]
You can’t perform that action at this time.