UNPKG

@flatfile/records

Version:

Record management utilities for Flatfile

120 lines (119 loc) 4.46 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.asDate = asDate; exports.asNumber = asNumber; exports.asString = asString; exports.asNullableString = asNullableString; exports.asBool = asBool; const chrono = __importStar(require("chrono-node")); function asDate(input) { const str_value = asString(input); if (!str_value) { return null; } return chrono.parseDate(str_value); } function asNumber(input) { // Handle direct number input if (typeof input === "number" && !isNaN(input)) { return input; } // Handle null, undefined, empty strings, and non-string/non-number types if (!input || (typeof input !== "string" && typeof input !== "number")) { return 0; } // Convert to string and trim const trimmedInput = input.toString().trim(); if (!trimmedInput) return 0; // Try direct conversion first (handles scientific notation automatically) const directNum = Number(trimmedInput); if (!isNaN(directNum)) return directNum; // Remove thousand separators and try again const withoutSeparators = trimmedInput.replace(/,/g, ""); const numWithoutSeparators = Number(withoutSeparators); if (!isNaN(numWithoutSeparators)) return numWithoutSeparators; // Handle percentages if (trimmedInput.endsWith("%")) { const percentValue = Number(trimmedInput.slice(0, -1)); if (!isNaN(percentValue)) { // Round to 12 decimal places to maintain high precision while avoiding floating point artifacts return Math.round((percentValue / 100) * 1e12) / 1e12; } } // Handle currency values const currencyRegex = /^[^\d\s-]*\s*-?\d+(?:,\d{3})*(?:\.\d{1,2})?$/; if (currencyRegex.test(trimmedInput)) { const numericValue = trimmedInput.replace(/[^\d.-]/g, ""); const currencyNum = Number(numericValue); if (!isNaN(currencyNum)) return currencyNum; } // Return 0 if no conversion was successful return 0; } function asString(input) { // Check if the input is null or undefined if (input === null || input === undefined) { return ""; } // Check if the input is an object or an array (excluding null, which is technically an object in JS) if (typeof input === "object") { // Attempt to convert objects and arrays to a JSON string try { return JSON.stringify(input); } catch (error) { // In case of circular references or other JSON.stringify errors, return a fallback string return "[object]"; } } // For numbers, booleans, and other types, convert directly to string return String(input); } function asNullableString(input) { // Check if the input is null or undefined if (input === null || input === undefined || input === "undefined" || input === "null" || input === "") { return undefined; } return asString(input); } function asBool(input) { // todo: make this way smarter soon return !!input; }