UNPKG

@azure/search-documents

Version:
179 lines (178 loc) 5.44 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var serialization_exports = {}; __export(serialization_exports, { deserialize: () => deserialize, serialize: () => serialize }); module.exports = __toCommonJS(serialization_exports); var import_geographyPoint = __toESM(require("./geographyPoint.js")); var import_walk = require("./walk.js"); const ISO8601DateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,3})?Z$/i; const GeoJSONPointTypeName = "Point"; const WorldGeodeticSystem1984 = "EPSG:4326"; const [serializeValue, deserializeValue] = [ [serializeSpecialNumbers, serializeDates, serializeGeoPoint], [deserializeSpecialNumbers, deserializeDates, deserializeGeoPoint] ].map( (fns) => (value) => fns.reduceRight((acc, fn) => fn(acc), value) ); function serialize(obj) { return (0, import_walk.walk)(obj, serializeValue); } function deserialize(obj) { return (0, import_walk.walk)(obj, deserializeValue); } function serializeSpecialNumbers(input) { if (typeof input === "number" && isNaN(input)) { return "NaN"; } else if (input === Infinity) { return "INF"; } else if (input === -Infinity) { return "-INF"; } else { return input; } } function serializeDates(input) { return input instanceof Date ? input.toISOString() : input; } function serializeGeoPoint(input) { return input instanceof import_geographyPoint.default ? input.toJSON() : input; } function deserializeSpecialNumbers(input) { switch (input) { case "NaN": return NaN; case "-INF": return -Infinity; case "INF": return Infinity; default: return input; } } function deserializeDates(input) { return typeof input === "string" && ISO8601DateRegex.test(input) ? new Date(input) : input; } function deserializeGeoPoint(input) { if (isGeoJSONPoint(input)) { const [longitude, latitude] = input.coordinates; return new import_geographyPoint.default({ longitude, latitude }); } return input; } function isGeoJSONPoint(obj) { const requiredKeys = ["type", "coordinates"]; return isValidObject(obj, { requiredKeys, propertyValidator: (key) => { switch (key) { case "type": return obj.type === GeoJSONPointTypeName; break; case "coordinates": return isCoordinateArray(obj.coordinates); break; case "crs": return isCrs(obj.crs); break; default: return false; } } }); } function isCoordinateArray(maybeCoordinates) { if (!Array.isArray(maybeCoordinates)) { return false; } if (maybeCoordinates.length !== 2) { return false; } if (typeof maybeCoordinates[0] !== "number" || typeof maybeCoordinates[1] !== "number") { return false; } return true; } function isCrs(maybeCrs) { return isValidObject(maybeCrs, { requiredKeys: ["type", "properties"], propertyValidator: (key) => { switch (key) { case "type": return maybeCrs.type === "name"; break; case "properties": return isCrsProperties(maybeCrs.properties); break; default: return false; } } }); } function isCrsProperties(maybeProperties) { return isValidObject(maybeProperties, { requiredKeys: ["name"], propertyValidator: (key) => { if (key === "name") { return maybeProperties.name === WorldGeodeticSystem1984; } else { return false; } } }); } function isValidObject(obj, options = {}) { if (typeof obj !== "object" || obj === null) { return false; } const keys = Object.keys(obj); if (options.requiredKeys) { for (const requiredKey of options.requiredKeys) { if (!keys.includes(requiredKey)) { return false; } } } if (options.propertyValidator) { for (const key of keys) { if (!options.propertyValidator(key)) { return false; } } } return true; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { deserialize, serialize }); //# sourceMappingURL=serialization.js.map