UNPKG

syphonx-core

Version:

SyphonX is a template-driven solution for extracting data from HTML in a highly efficient way. It combines the power of jQuery, Regular Expressions, and Javascript into a declarative template-driven format that extracts and reshapes HTML data into JSON.

41 lines 1.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isCoercibleTo = exports.coerceSelectValue = exports.coerce = void 0; var parse_js_1 = require("./parse.js"); function coerce(value, type) { if (type === "number") return (0, parse_js_1.parseNumber)(value); else if (type === "boolean") return (0, parse_js_1.parseBoolean)(value); else return value; } exports.coerce = coerce; function coerceSelectValue(value, type, repeated) { if (repeated) { return value instanceof Array ? value.map(function (v) { return coerceSelectValue(v, type, false); }) : [coerceSelectValue(value, type, false)]; } else if (type === "string") { return typeof value === "string" ? value : typeof value === "number" || typeof value === "boolean" ? value.toString() : null; } else if (type === "number") { return typeof value === "number" ? value : typeof value === "string" ? (0, parse_js_1.parseNumber)(value) : null; } else if (type === "boolean") { return typeof value === "boolean" ? value : typeof value === "string" ? value.trim().length > 0 : typeof value === "number" && !isNaN(value) ? value !== 0 : null; } else { return value; } } exports.coerceSelectValue = coerceSelectValue; function isCoercibleTo(value, type) { if (type === "number" && typeof value === "string" && (0, parse_js_1.parseNumber)(value) !== undefined) return true; else if (type === "boolean" && typeof value === "string" && (0, parse_js_1.parseBoolean)(value) !== undefined) return true; else return false; } exports.isCoercibleTo = isCoercibleTo; //# sourceMappingURL=coerce.js.map