-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added Latitude & Longitude scalars * Added Latitude & Longitude scalars fix
- Loading branch information
1 parent
897feb9
commit 0f78b62
Showing
10 changed files
with
534 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Inspired by Geolib: https://github.com/manuelbieh/geolib | ||
import { GraphQLError, GraphQLScalarType, Kind } from 'graphql'; | ||
import { isDecimal, isSexagesimal, sexagesimalToDecimal } from './utilities'; | ||
|
||
// Minimum latitude | ||
const MIN_LAT = -90.0; | ||
// Maximum latitude | ||
const MAX_LAT = 90.0; | ||
// See https://en.wikipedia.org/wiki/Decimal_degrees#Precision | ||
const MAX_PRECISION = 8; | ||
|
||
const validate = (value: any): number => { | ||
// Check if value is a string or a number | ||
if ( | ||
(typeof value !== 'string' && typeof value !== 'number') || | ||
value === null || | ||
typeof value === 'undefined' || | ||
Number.isNaN(value) | ||
) { | ||
throw new TypeError(`Value is neither a number nor a string: ${value}`); | ||
} | ||
|
||
if (isDecimal(value)) { | ||
const decimalValue = | ||
typeof value === 'string' ? Number.parseFloat(value) : value; | ||
|
||
if (decimalValue < MIN_LAT || decimalValue > MAX_LAT) { | ||
throw new RangeError( | ||
`Value must be between ${MIN_LAT} and ${MAX_LAT}: ${value}`, | ||
); | ||
} | ||
|
||
return Number.parseFloat(decimalValue.toFixed(MAX_PRECISION)); | ||
} | ||
|
||
if (isSexagesimal(value)) { | ||
return validate(sexagesimalToDecimal(value)); | ||
} | ||
|
||
throw new TypeError(`Value is not a valid latitude: ${value}`); | ||
}; | ||
|
||
export const GraphQLLatitude = /*#__PURE__*/ new GraphQLScalarType({ | ||
name: `Latitude`, | ||
|
||
description: `A field whose value is a valid decimal degrees latitude number (53.471): https://en.wikipedia.org/wiki/Latitude`, | ||
|
||
serialize(value) { | ||
return validate(value); | ||
}, | ||
|
||
parseValue(value) { | ||
return validate(value); | ||
}, | ||
|
||
parseLiteral(ast) { | ||
if (ast.kind !== Kind.FLOAT && ast.kind !== Kind.STRING) { | ||
throw new GraphQLError( | ||
`Can only validate floats or strings as latitude but got a: ${ast.kind}`, | ||
); | ||
} | ||
|
||
return validate(ast.value); | ||
}, | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Inspired by Geolib: https://github.com/manuelbieh/geolib | ||
import { GraphQLError, GraphQLScalarType, Kind } from 'graphql'; | ||
import { isDecimal, isSexagesimal, sexagesimalToDecimal } from './utilities'; | ||
|
||
// Minimum longitude | ||
const MIN_LON = -180.0; | ||
// Maximum longitude | ||
const MAX_LON = 180.0; | ||
// See https://en.wikipedia.org/wiki/Decimal_degrees#Precision | ||
const MAX_PRECISION = 8; | ||
|
||
const validate = (value: any): number => { | ||
// Check if value is a string or a number | ||
if ( | ||
(typeof value !== 'string' && typeof value !== 'number') || | ||
value === null || | ||
typeof value === 'undefined' || | ||
Number.isNaN(value) | ||
) { | ||
throw new TypeError(`Value is neither a number nor a string: ${value}`); | ||
} | ||
|
||
if (isDecimal(value)) { | ||
const decimalValue = | ||
typeof value === 'string' ? Number.parseFloat(value) : value; | ||
|
||
if (decimalValue < MIN_LON || decimalValue > MAX_LON) { | ||
throw new RangeError( | ||
`Value must be between ${MIN_LON} and ${MAX_LON}: ${value}`, | ||
); | ||
} | ||
|
||
return Number.parseFloat(decimalValue.toFixed(MAX_PRECISION)); | ||
} | ||
|
||
if (isSexagesimal(value)) { | ||
return validate(sexagesimalToDecimal(value)); | ||
} | ||
|
||
throw new TypeError(`Value is not a valid longitude: ${value}`); | ||
}; | ||
|
||
export const GraphQLLongitude = /*#__PURE__*/ new GraphQLScalarType({ | ||
name: `Longitude`, | ||
|
||
description: `A field whose value is a valid decimal degrees longitude number (53.471): https://en.wikipedia.org/wiki/Longitude`, | ||
|
||
serialize(value) { | ||
return validate(value); | ||
}, | ||
|
||
parseValue(value) { | ||
return validate(value); | ||
}, | ||
|
||
parseLiteral(ast) { | ||
if (ast.kind !== Kind.FLOAT && ast.kind !== Kind.STRING) { | ||
throw new GraphQLError( | ||
`Can only validate floats or strings as longitude but got a: ${ast.kind}`, | ||
); | ||
} | ||
|
||
return validate(ast.value); | ||
}, | ||
}); |
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
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
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
Oops, something went wrong.