Skip to content

Commit 290fd28

Browse files
authored
Improve isNumber detection (#405)
The original regex didn't match the start or end of line. Because of that any text with a number in the middle would be considered a number. Expanded it a little bit to also consider spaces and periods as thousands- and decimal-separators. Fixes: #404
1 parent cfed725 commit 290fd28

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
9+
### Fixed
10+
- Text with numbers is no longer right aligned ([#405](https://github.com/cucumber/react-components/pull/405))
11+
912
### Added
1013
- Include duration for each test step ([#396](https://github.com/cucumber/react-components/pull/396))
1114
- Include pass rate in execution summary ([#397](https://github.com/cucumber/react-components/pull/397))
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { expect } from 'chai'
2+
3+
import isNumber from './isNumber.js'
4+
5+
describe('isNumber is true', () => {
6+
const numbers = ['1', '-1', '10', '1.0', '.0', '1,0', '1 000,00', '127.0.0.0', '10E-05', '10E+05']
7+
8+
numbers.forEach((value) => {
9+
it(`${value}`, () => {
10+
expect(isNumber(value)).to.be.true
11+
})
12+
})
13+
})
14+
describe('isNumber is false', () => {
15+
const nonNumbers = ['hello', 'hello world', '-', '+', '1.', '1,', 'Ramayan 3392 A.D.', 'E+10']
16+
17+
nonNumbers.forEach((value) => {
18+
it(`${value}`, () => {
19+
expect(isNumber(value)).to.be.false
20+
})
21+
})
22+
})

src/components/gherkin/isNumber.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Lifted from cucumber-expressions/javascript/src/ParameterTypeRegistry#FLOAT_REGEXP
2-
const numberPattern = /(?=.*\d.*)[-+]?\d*(?:\.(?=\d.*))?\d*(?:\d+[E][+-]?\d+)?/
2+
// Modified to allow spaces, commas and periods as decimal- and/or as thousand-separators
3+
const numberPattern = /^(?=.*\d.*)[-+]?\d*(?:[., ](?=\d.*)\d*)*(?:\d+E[+-]?\d+)?$/
34

45
export default function isNumber(s: string): boolean {
56
return !!s.match(numberPattern)

0 commit comments

Comments
 (0)