CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
stringify.js70 linesDownload Raw Back to lib
1"use strict";2 3const { encodeString } = require("./internals/querystring");4 5function getAsPrimitive(value) {6  const type = typeof value;7 8  if (type === "string") {9    // Length check is handled inside encodeString function10    return encodeString(value);11  } else if (type === "bigint") {12    return value.toString();13  } else if (type === "boolean") {14    return value ? "true" : "false";15  } else if (type === "number" && Number.isFinite(value)) {16    return value < 1e21 ? "" + value : encodeString("" + value);17  }18 19  return "";20}21 22/**23 * @param {Record<string, string | number | boolean24 * | ReadonlyArray<string | number | boolean> | null>} input25 * @returns {string}26 */27function stringify(input) {28  let result = "";29 30  if (input === null || typeof input !== "object") {31    return result;32  }33 34  const separator = "&";35  const keys = Object.keys(input);36  const keyLength = keys.length;37  let valueLength = 0;38 39  for (let i = 0; i < keyLength; i++) {40    const key = keys[i];41    const value = input[key];42    const encodedKey = encodeString(key) + "=";43 44    if (i) {45      result += separator;46    }47 48    if (Array.isArray(value)) {49      valueLength = value.length;50      for (let j = 0; j < valueLength; j++) {51        if (j) {52          result += separator;53        }54 55        // Optimization: Dividing into multiple lines improves the performance.56        // Since v8 does not need to care about the '+' character if it was one-liner.57        result += encodedKey;58        result += getAsPrimitive(value[j]);59      }60    } else {61      result += encodedKey;62      result += getAsPrimitive(value);63    }64  }65 66  return result;67}68 69module.exports = stringify;70