Skip to content

Commit

Permalink
Avoid Number.isNaN
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanhamley committed Nov 21, 2019
1 parent c2289d7 commit 74c85f3
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 4 deletions.
4 changes: 1 addition & 3 deletions src/style-spec/util/get_type.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@

export default function getType(val) {
if (Number.isNaN(val)) {
return 'NaN';
} else if (val instanceof Number) {
if (val instanceof Number) {
return 'number';
} else if (val instanceof String) {
return 'string';
Expand Down
7 changes: 6 additions & 1 deletion src/style-spec/validate/validate_number.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ export default function validateNumber(options) {
const key = options.key;
const value = options.value;
const valueSpec = options.valueSpec;
const type = getType(value);
let type = getType(value);

// eslint-disable-next-line no-self-compare
if (type === 'number' && value !== value) {
type = 'NaN';
}

if (type !== 'number') {
return [new ValidationError(key, value, `number expected, ${type} found`)];
Expand Down
94 changes: 94 additions & 0 deletions test/unit/style-spec/fixture/numbers.input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"version": 8,
"sources": {
"point": {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [ 0, 0 ]
}
}
]
}
}
},
"layers": [
{
"id": "valid",
"type": "circle",
"source": "point",
"paint": {
"circle-radius": 5
}
},
{
"id": "zero",
"type": "circle",
"source": "point",
"paint": {
"circle-radius": 0
}
},
{
"id": "less-than-zero",
"type": "circle",
"source": "point",
"paint": {
"circle-radius": -1
}
},
{
"id": "null-not-number",
"type": "circle",
"source": "point",
"paint": {
"circle-radius": null
}
},
{
"id": "object-not-number",
"type": "circle",
"source": "point",
"paint": {
"circle-radius": {}
}
},
{
"id": "array-not-number",
"type": "circle",
"source": "point",
"paint": {
"circle-radius": []
}
},
{
"id": "boolean-not-number",
"type": "circle",
"source": "point",
"paint": {
"circle-radius": true
}
},
{
"id": "expression",
"type": "circle",
"source": "point",
"paint": {
"circle-radius": ["sqrt", 16]
}
},
{
"id": "expression-invalid",
"type": "circle",
"source": "point",
"paint": {
"circle-radius": ["/", 0, 0]
}
}
]
}
21 changes: 21 additions & 0 deletions test/unit/style-spec/fixture/numbers.output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[
{
"message": "layers[2].paint.circle-radius: -1 is less than the minimum value 0",
"line": 42
},
{
"message": "layers[3].paint.circle-radius: number expected, null found"
},
{
"message": "layers[4].paint.circle-radius: missing required property \"stops\"",
"line": 58
},
{
"message": "layers[5].paint.circle-radius: number expected, array found",
"line": 66
},
{
"message": "layers[6].paint.circle-radius: number expected, boolean found",
"line": 74
}
]

0 comments on commit 74c85f3

Please sign in to comment.