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
// iso-middleware/renderRoutes.js | |
import React from 'react'; | |
import { renderToString } from 'react-dom/server'; | |
import { StaticRouter } from 'react-router-dom'; | |
import { matchRoutes } from 'react-router-config'; | |
import routes from '../shared/routes'; | |
import HTML from '../shared/components/HTML'; | |
import App from '../shared/App'; |
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
// BETTER - more testable | |
const incrementedVal = (val) => val + 1 | |
function substituteVal(val, low, high) { | |
if(val >= high) return high | |
else if(val <= low) return low | |
else return val | |
} | |
function shouldIncrement(val, disabled) { | |
return val >= 0 && val < 10 && !disabled |
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
let vals = Array.from({length: 13}, (v,i) => i) | |
//> (13) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] | |
vals = vals.map(v => v-1) | |
//> (13) [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] | |
let res = vals.map(v => ( | |
{val: v, notDisabled: valIncrementer(v, false), disabled: valIncrementer(v, true) } | |
)) |
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
// BAD | |
function valIncrementer(val, disabled) { | |
var nextVal = val + 1 | |
function firstStageSetter1() { | |
if(val >= 10) { | |
nextVal = 10 //clip it to high limit value | |
secondStageSetter() | |
} | |
} |
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
var myFun2 = function() { | |
var num = -1 | |
function incrementUntil(max) { | |
if(num >= max) return num | |
num++ | |
incrementUntil(max) | |
} | |
incrementUntil(3) | |
return num | |
} |
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
var incrementUntil = function(max) { | |
var inc = function(num) { | |
if(num >= max) return num | |
return inc(num+1) | |
} | |
return (num) => inc(num) | |
} | |
incrementUntil(4)(1) //> 4 | |
incrementUntil(8)(3) //> 8 |
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
var multThenAdd = num => { | |
return mul => add => { | |
return num * mul + add | |
} | |
} | |
var timesTwoPlusFour = (num) => multThenAdd(num)(2)(4) | |
timesTwoPlusFour(1) //> 6 | |
timesTwoPlusFour(10) //> 24 |
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
const getInfoFromURL = path => { | |
const URL = require(“url”).URL; | |
const myUrl = new URL(path) | |
const pathname = myUrl.pathname | |
const getUsernameFromURL = pathname => { | |
const regex = new RegExp(‘/@’); | |
const username = pathname.split(regex).slice(1)[0] | |
if(!username) { | |
return “Error in parsing: URL needs to be in format://hostname:port/@username” |
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
num = 0 | |
var myFun2 = function() { | |
var num = -1 | |
incrementUntil(3) | |
return num | |
} | |
myFun2() //> -1 ……Why? | |
num //> 3 |
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
num = 0 | |
var myFun = function() { | |
incrementUntil(3) | |
return num | |
} | |
myFun() //> 3 | |
num //> 3 |