Files
klz-cables.com/.pnpm-store/v10/files/50/e2e76a05ec4d43eb04c4117a39aed35d37a3a609f779930394f70b310f3cbb1ab883c905b73fdb7bdb55e9f84d54f1b0dc294dbedd4ea522a43128d2d61781
Marc Mintel 5397309103
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
fix(products): fix breadcrumbs and product filtering (backport from main)
2026-02-24 16:04:21 +01:00

57 lines
2.2 KiB
Plaintext

// Inspired by Geolib: https://github.com/manuelbieh/geolib
import { GraphQLScalarType, Kind } from 'graphql';
import { createGraphQLError } from '../error.js';
import { isDecimal, isSexagesimal, sexagesimalToDecimal } from './utilities.js';
// 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, ast) => {
// 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 createGraphQLError(`Value is neither a number nor a string: ${value}`, ast ? { nodes: ast } : undefined);
}
if (isDecimal(value)) {
const decimalValue = typeof value === 'string' ? Number.parseFloat(value) : value;
if (decimalValue < MIN_LON || decimalValue > MAX_LON) {
throw createGraphQLError(`Value must be between ${MIN_LON} and ${MAX_LON}: ${value}`, ast ? { nodes: ast } : undefined);
}
return Number.parseFloat(decimalValue.toFixed(MAX_PRECISION));
}
if (isSexagesimal(value)) {
return validate(sexagesimalToDecimal(value));
}
throw createGraphQLError(`Value is not a valid longitude: ${value}`, ast ? { nodes: ast } : undefined);
};
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 createGraphQLError(`Can only validate floats or strings as longitude but got a: ${ast.kind}`, {
nodes: [ast],
});
}
return validate(ast.value, ast);
},
extensions: {
codegenScalarType: 'string | number',
jsonSchema: {
type: 'number',
minimum: MIN_LON,
maximum: MAX_LON,
},
},
});