Skip to content

Commit a4765c1

Browse files
committed
Improve isNumber detection
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 495987d commit a4765c1

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { expect } from 'chai'
2+
3+
import isNumber from './isNumber.js'
4+
5+
describe('isNumber is true', () => {
6+
7+
const numbers = [
8+
'1',
9+
'-1',
10+
'10',
11+
'1.0',
12+
'.0',
13+
'1,0',
14+
'1 000,00',
15+
'127.0.0.0',
16+
'10E-05',
17+
'10E+05',
18+
]
19+
20+
numbers.forEach(value => {
21+
it(`${value}`, () => {
22+
expect(isNumber(value)).to.be.true
23+
})
24+
})
25+
})
26+
describe('isNumber is false', () => {
27+
28+
const nonNumbers = [
29+
'hello',
30+
'hello world',
31+
'-',
32+
'+',
33+
'1.',
34+
'1,',
35+
'Ramayan 3392 A.D.',
36+
'E+10',
37+
]
38+
39+
nonNumbers.forEach(value => {
40+
it(`${value}`, () => {
41+
expect(isNumber(value)).to.be.false
42+
})
43+
})
44+
45+
46+
})

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)