UNPKG

ontotext-platform-custom-scalars

Version:
150 lines (123 loc) 5.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseAsBigNumber = parseAsBigNumber; exports.parseUnsignedLiteral = parseUnsignedLiteral; exports.parseUnsignedValue = parseUnsignedValue; exports.isInteger = isInteger; exports.isPositive = isPositive; exports.isNegative = isNegative; exports.isNumber = isNumber; exports.throwConversionError = throwConversionError; exports.throwNegativeValueError = throwNegativeValueError; exports.throwOutOfRangeError = throwOutOfRangeError; exports.normalizeFloatingPointNumbers = normalizeFloatingPointNumbers; var _graphql = require("graphql"); function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } /** * Parses the input value as big number. The passed value will be checked, whether it is a number or not and then * directly converted to string, skipping the parsing as it will attempt to round the number, if it exceed the limits * of the JS primitives. * * @param value which should be parsed * @param typeName the expected type of the value * @returns {string} representation of the input value * @throws GraphQLError when the input value is not a number */ function parseAsBigNumber(value, typeName) { if (isNumber(value) && isInteger(value)) { return Number(value).toLocaleString('fullwide', { useGrouping: false }); } throw new _graphql.GraphQLError("Expected '".concat(typeName, "' value, but got ").concat(_printValueAndType(value))); } /** * Checks whether the given value is integer or not. * * @param value to be checked * @returns {boolean} */ function isInteger(value) { return Number.isInteger(Number(value)); } /** * Checks whether the passed number is positive. Zeros are considered not positive. * * @param number to be checked * @returns {boolean} */ function isPositive(number) { return Math.sign(number) === 1; } /** * Checks whether the passed number is negative. Zeros are considered not negative. * * @param number to be checked * @returns {boolean} */ function isNegative(number) { return Math.sign(number) === -1; } /** * Checks whether the input argument is a number or not. The method detects empty string, 'null' values and booleans as * not a number values, although in most cases they could be represented as numbers. * * @param value to be checked * @returns {boolean} */ function isNumber(value) { return value !== '' && typeof value !== 'boolean' && value !== null && !isNaN(value) && !Number.isNaN(value) && value !== Number.NaN; } function _printValueAndType(value) { return "Type: '".concat(_typeof(value), "' with value: ").concat(JSON.stringify(value)); } /** * Parses unsigned literals as positive integer values. The logic could process strings and integer AST types. * * @param node which contains the type of the value and the actual value which should be parsed * @param maxValue the maximum value which is allowed to be contained by the scalar, which value should be parsed * @param typeName the name of scalar/type, which value is parsed * @returns {string} */ function parseUnsignedLiteral(node, maxValue, typeName) { var kind = node.kind; if (_graphql.Kind.INT !== kind && _graphql.Kind.STRING !== kind && _graphql.Kind.FLOAT !== kind) { throwConversionError(kind, typeName); } return parseUnsignedValue(node.value, maxValue, typeName); } /** * Parses unsigned value as positive integer. * * @param value to be parsed * @param maxValue the maximum value which is allowed to be contained by the scalar * @param typeName the name of scalar/type, which value is parsed * @returns {string} */ function parseUnsignedValue(value, maxValue, typeName) { if (!isNumber(value)) { throw new _graphql.GraphQLError("Expected '".concat(typeName, "' value, but got ").concat(_printValueAndType(value))); } var parsed = parseInt(value, 10); if (parsed < 0 || parsed > maxValue) { throwOutOfRangeError(value, typeName); } return parsed.toLocaleString('fullwide', { useGrouping: false }); } function normalizeFloatingPointNumbers(value) { var asStr = value + ''; return asStr.includes('.') ? asStr : asStr + '.0'; } function throwConversionError(actualType, expectedType) { throw new _graphql.GraphQLError("AST type of '".concat(actualType, "' could not be turned into '").concat(expectedType, "'")); } function throwNegativeValueError(value, typeName) { throw new _graphql.GraphQLError("The input value '".concat(value, "' for type '").concat(typeName, "' should not be negative.")); } function throwOutOfRangeError(value, typeName) { throw new _graphql.GraphQLError("The input value '".concat(value, "' is out of the '").concat(typeName, "' range.")); }