static ensureBasic()

in packages/sqrl-common/src/SqrlObject.ts [115:145]


  static ensureBasic(val, expandData?) {
    // First do a quick scan to see if any of the values are actually SqrlObject
    // If not exit early with the original array
    if (SqrlObject.isBasic(val)) {
      return val;
    }

    if (val instanceof Set) {
      val = Array.from(val);
    }

    if (val instanceof SqrlObject) {
      if (expandData) {
        return SqrlObject.ensureBasic(val.getData(), expandData);
      } else {
        return val.getBasicValue();
      }
    } else if (val instanceof Buffer) {
      // @TODO: Perhaps we should have a StringBuffer, but for now be safe and
      //        return the hex encoded version
      // return val.toString('utf-8');
      return val.toString("hex");
    } else if (Array.isArray(val)) {
      return val.map((v) => SqrlObject.ensureBasic(v, expandData));
    } else if (typeof val === "object" && !hasConstructor(val)) {
      // @TODO: We should disallow these later
      return mapObject(val, (v) => SqrlObject.ensureBasic(v, expandData));
    } else {
      return val;
    }
  }