Sha256: 38f444cc4ba6c11c75b5d9537495e08c79cec1618cbcb90d65959de7b0e2ba42

Contents?: true

Size: 1.95 KB

Versions: 10

Compression:

Stored size: 1.95 KB

Contents

'use strict';
const {
  VOID, PRIMITIVE, ARRAY, OBJECT, DATE, REGEXP, MAP, SET, ERROR, BIGINT
} = require('./types.js');

const env = typeof self === 'object' ? self : globalThis;

const deserializer = ($, _) => {
  const as = (out, index) => {
    $.set(index, out);
    return out;
  };

  const unpair = index => {
    if ($.has(index))
      return $.get(index);

    const [type, value] = _[index];
    switch (type) {
      case PRIMITIVE:
      case VOID:
        return as(value, index);
      case ARRAY: {
        const arr = as([], index);
        for (const index of value)
          arr.push(unpair(index));
        return arr;
      }
      case OBJECT: {
        const object = as({}, index);
        for (const [key, index] of value)
          object[unpair(key)] = unpair(index);
        return object;
      }
      case DATE:
        return as(new Date(value), index);
      case REGEXP: {
        const {source, flags} = value;
        return as(new RegExp(source, flags), index);
      }
      case MAP: {
        const map = as(new Map, index);
        for (const [key, index] of value)
          map.set(unpair(key), unpair(index));
        return map;
      }
      case SET: {
        const set = as(new Set, index);
        for (const index of value)
          set.add(unpair(index));
        return set;
      }
      case ERROR: {
        const {name, message} = value;
        return as(new env[name](message), index);
      }
      case BIGINT:
        return as(BigInt(value), index);
      case 'BigInt':
        return as(Object(BigInt(value)), index);
    }
    return as(new env[type](value), index);
  };

  return unpair;
};

/**
 * @typedef {Array<string,any>} Record a type representation
 */

/**
 * Returns a deserialized value from a serialized array of Records.
 * @param {Record[]} serialized a previously serialized value.
 * @returns {any}
 */
const deserialize = serialized => deserializer(new Map, serialized)(0);
exports.deserialize = deserialize;

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
immosquare-cleaner-0.1.32 node_modules/@ungap/structured-clone/cjs/deserialize.js
immosquare-cleaner-0.1.31 node_modules/@ungap/structured-clone/cjs/deserialize.js
immosquare-cleaner-0.1.30 node_modules/@ungap/structured-clone/cjs/deserialize.js
immosquare-cleaner-0.1.29 node_modules/@ungap/structured-clone/cjs/deserialize.js
immosquare-cleaner-0.1.28 node_modules/@ungap/structured-clone/cjs/deserialize.js
immosquare-cleaner-0.1.27 node_modules/@ungap/structured-clone/cjs/deserialize.js
immosquare-cleaner-0.1.26 node_modules/@ungap/structured-clone/cjs/deserialize.js
immosquare-cleaner-0.1.25 node_modules/@ungap/structured-clone/cjs/deserialize.js
immosquare-cleaner-0.1.24 node_modules/@ungap/structured-clone/cjs/deserialize.js
immosquare-cleaner-0.1.23 node_modules/@ungap/structured-clone/cjs/deserialize.js