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
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
// 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
// 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
// shared/components/HTML.js | |
import React from 'react'; | |
const HTML = (props) => ( | |
<html lang="en"> | |
<head> | |
<title>Isomorphic Router Demo</title> | |
<link | |
rel="stylesheet" |
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
// Parentheses Tally | |
const newOpenCnt = (c, openCnt) => { | |
if (c === '(') return openCnt + 1 | |
if (c === ')') return openCnt - 1 | |
return openCnt | |
} | |
function isBalanced(str, openCnt) { | |
// Continue or Stop? | |
if (typeof str !== 'string') return false |
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 testCases = [ | |
{test: '(', shouldBe: false}, | |
{test: '())', shouldBe: false}, | |
{test: null, shouldBe: false}, | |
{test: undefined, shouldBe: false}, | |
{test: 22, shouldBe: false}, | |
{test: ')(', shouldBe: false}, | |
{test: '', shouldBe: true}, | |
{test: '()', shouldBe: true}, | |
{test: '()()', shouldBe: 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
const test = (fun testCases) => { | |
testCases.map(t => { | |
const shouldBe = t.shouldBe; | |
const is = fun(t.test); | |
const res = (shouldBe === is) ? 'passed' : 'failed'; | |
const moreInfo = (res === 'failed') ? `testing ${t.test}. Should be ${shouldBe} but got ${is}` : '' | |
console.log(`${res} ${moreInfo}`); | |
}) | |
} |
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
import React from 'react'; | |
import PropTypes from 'prop-types'; | |
import { | |
convertToRaw, | |
EditorState, | |
RichUtils | |
} from 'draft-js'; | |
import Editor from 'draft-js-plugins-editor'; | |
import createMarkdownPlugin from 'draft-js-markdown-plugin'; | |
import createInlineToolbarPlugin, { Separator } from 'draft-js-inline-toolbar-plugin'; |
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
import React from 'react'; | |
import PropTypes from 'prop-types'; | |
import { CompositeDecorator, convertFromRaw, Editor, EditorState } from 'draft-js'; | |
// Following code based on: | |
// https://github.com/facebook/draft-js/blob/master/examples/draft-0-10-0/link/link.html | |
const Link = (props) => { | |
const {url} = props.contentState.getEntity(props.entityKey).getData(); | |
return ( | |
<a rel="nofollow noreferrer" href={url} target="_blank"> |