// Each of these functions should output a truthy/falsy value if the input is
// a number
const solutionsToTest = [
v => parseFloat(v),
v => Number(v),
v => !isNaN(v),
v => typeof v != "boolean" && !isNaN(v),
v => isFinite(String(v)),
v => !isNaN(parseFloat(v)) && isFinite(v)
];
const testCases = [
//[ Test Name, Test Value, Expected Output, Is Ambiguous ]
// Whitespace
['""', "", false, false],
['"\\t"', "\t", false, false],
['" "', " ", false, false],
// Infinity
['"Infinity"', "Infinity", false, true],
['"+Infinity"', "Infinity", false, true],
["-Infinity", -Infinity, false, true],
["Infinity", Infinity, false, true],
// Numbers mixed with symbols
['"123abc"', "123abc", false, true],
['"abc123"', "abc123", false, false],
['".0."', ".0.", false, false],
['"1."', "1.", true, true],
['"."', ".", false, true],
['"01"', "01", true, true],
['"-0"', "-0", true, true],
["+1", +1, true, true],
["-1", -1, true, true],
// Other js types
["'null'", "null", false, false],
["'true'", "true", false, false],
["'false'", "false", false, false],
["null", null, false, false],
["true", true, false, false],
["false", false, false, false],
["NaN", NaN, false, false],
["[]", [], false, false],
["{}", {}, false, false],
["/./", /./, false, false],
["() => {}",() => {}, false, false]
];
const styles = {
code: {
fontFamily: "monospace",
fontSize: 16
},
success: {
backgroundColor: "#00ff5478"
},
failure: {
backgroundColor: "#ff00008c"
}
};
class TestCaseTable extends React.Component {
static renderTableHeader(solutionsToTest) {
return (
<tr>
<th>
<p>Test Case</p>
</th>
{solutionsToTest.map(f => (
<th key={f.toString()}>
<p style={styles.code}>{f.toString()}</p>
</th>
))}
</tr>
);
}
static renderTableRow(testCase, solutionsToTest) {
const [testName, input, expectedOutput, isAmbiguous] = testCase;
return (
<tr key={testName}>
<td style={styles.code}>{testName}</td>
{solutionsToTest.map(f => {
const output = Boolean(f(input));
const style = isAmbiguous
? {}
: output == expectedOutput ? styles.success : styles.failure;
return (
<td style={style} key={f.toString()}>
<p>{output + ""}</p>
</td>
);
})}
</tr>
);
}
render() {
// Sort test cases, put the ambiguous ones after (but maintain stable sort
// order)
let sortedCases = [
...testCases.filter(([a, b, c, ambiguous]) => !ambiguous),
...testCases.filter(([a, b, c, ambiguous]) => ambiguous)
];
return (
<table>
<thead>{TestCaseTable.renderTableHeader(solutionsToTest)}</thead>
<tbody>
{sortedCases.map(tc =>
TestCaseTable.renderTableRow(tc, solutionsToTest)
)}
</tbody>
</table>
);
}
}
class TestCaseSummaryTable extends React.Component {
renderTableHeader(solutionsToTest) {
return (
<tr>
<th>Summary</th>
{solutionsToTest.map(f => (
<th key={f.toString()}>
<p style={styles.code}>{f.toString()}</p>
</th>
))}
</tr>
);
}
renderSuccessRateRow(solutionsToTest, testCases) {
// Ignore potentially ambiguous test cases
testCases = testCases.filter(
([name, input, expected, ambiguous]) => !ambiguous
);
const numSuccess = testSolution =>
testCases.reduce((succeeded, [name, input, expected]) => {
return succeeded + (Boolean(testSolution(input)) == expected ? 1 : 0);
}, 0);
return (
<tr>
<td>
<p>Test Success</p>
</td>
{solutionsToTest.map(f => (
<td>
<p>
{numSuccess(f)}/{testCases.length}
</p>
</td>
))}
</tr>
);
}
render() {
return (
<table>
<thead>{this.renderTableHeader(solutionsToTest)}</thead>
<tbody>{this.renderSuccessRateRow(solutionsToTest, testCases)}</tbody>
</table>
);
}
}
const root =() => {
return (
<div>
<TestCaseSummaryTable />
<TestCaseTable />
</div>
);
};
ReactDOM.render(root(), document.querySelector("#application"));
td {
text-align: center;
vertical-align: middle;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="application"></div>
posibles duplicados de [Validar números en JavaScript - IsNumeric()] (http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric) – acdcjunior