This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Async/await parallelism | |
async function foo() { | |
const [result1, result2] = await Promise.all([ | |
asyncFunc1(), | |
asyncFunc2(), | |
]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typeof null //object | |
NaN === NaN //false | |
typeof NaN //number |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// if condition | |
{someName && <div>{someName}</div>} | |
//if-else consdition | |
{ loggedIn && <LogoutButton /> || <LoginButton /> } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Encapsulate conditionals | |
function shouldShowSpinner(fsm, listNode) { | |
return fsm.state === 'fetching' && isEmpty(listNode); | |
} | |
if (shouldShowSpinner(fsmInstance, listNodeInstance)) { | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Dependency injection | |
// the Module object | |
var Module = function (someService) { | |
this.someService = someService; | |
}; | |
Module.prototype.do = function () { | |
this.someService.doSomething(); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Rest operator type | |
...args: number[] | |
OlderNewer