UNPKG

diffusion

Version:

Diffusion JavaScript client

179 lines (178 loc) 6.21 kB
"use strict"; /** * @module diffusion.datatypes */ var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.JSONImpl = void 0; var decoder_1 = require("./../../cbor/decoder"); var encoder_1 = require("./../../cbor/encoder"); var binary_delta_type_impl_1 = require("./../../data/binary/binary-delta-type-impl"); var bytes_impl_1 = require("./../../data/bytes-impl"); var json_delta_impl_1 = require("./../../data/json/json-delta-impl"); var int64_impl_1 = require("./../../data/primitive/int64-impl"); var function_1 = require("./../../util/function"); var encoder = new encoder_1.Encoder(); /** * Create a new JSON object from a raw object or a {@link RecordV2Impl}. If a * {@link RecordV2Impl} is passed as argument it is simply returned. * * @param value the object to create a JSON from * @return the JSON created */ function fromValue(value) { // tslint:disable-next-line:no-use-before-declare if (value instanceof JSONImpl) { return value; } else { // tslint:disable-next-line:no-use-before-declare return new JSONImpl(encoder.encode(value).flush()); } } /** * Replacer function for JSON.stringify that replaces {@link Int64Impl} objects * with their string representation * * @param key the object key (this argument is ignored) * @param value the value to replace * @return if `value` is an {@link Int64Impl} then the string * representation is returned. Otherwise `value` is returned * unchanged. */ function int64Replacer(key, value) { if (value instanceof int64_impl_1.Int64Impl) { return value.toString(); } return value; } /** * Implementation of {@link JSON} object. */ var JSONImpl = /** @class */ (function (_super) { __extends(JSONImpl, _super); /** * Create a new JSONImpl instance * * @param buffer the internal buffer containing the JSON` * @param offset the offset of the data in the buffer * @param length the number of bytes of the data in the buffer */ function JSONImpl(buffer, offset, length) { if (offset === void 0) { offset = 0; } if (length === void 0) { length = buffer.length; } return _super.call(this, buffer, offset, length) || this; } /** * Return the name of the data type * * @return `JSONImpl` */ JSONImpl.toString = function () { return 'JSONImpl'; }; /** * Create a new JSON object from a raw object or a {@link RecordV2Impl}. If a * {@link RecordV2Impl} is passed as argument it is simply returned. * * @param value the object to create a JSON from * @return the JSON created */ JSONImpl.from = function (value) { return fromValue(value); }; /** * @inheritdoc */ JSONImpl.prototype.get = function () { var decoder = new decoder_1.Decoder(this.$buffer, this.$offset, this.$length); return decoder.nextValue(); }; /** * @inheritdoc */ JSONImpl.prototype.diff = function (original, type) { var originalJson = fromValue(original); var binaryDiff = this.binaryDiff(originalJson); if (type === 'json') { return new json_delta_impl_1.JSONDeltaImpl(JSONImpl, originalJson, this, binaryDiff); } else { return binaryDiff; } }; /** * Compare this JSON value with an earlier version to create a structural * {@link JSONDelta json delta}. * * Convenient equivalent to: * `this.diff(original, 'binary');` * * Standard JSON objects may also be provided as the value to diff instead * of a {@link JSON} instance. * * **Example:** * ``` * const delta = binaryDiff.jsonDiff({ foo : 'bar' }); * ``` * * @param original the value to diff against this * @return a delta representing the difference between this and * the provided value */ JSONImpl.prototype.binaryDiff = function (original) { // tslint:disable-next-line:no-use-before-declare return BINARY_DELTA_TYPE.diff(original, this); }; /** * @inheritdoc */ JSONImpl.prototype.jsonDiff = function (original) { return this.diff(original, 'json'); }; /** * @inheritdoc */ JSONImpl.prototype.apply = function (delta) { // tslint:disable-next-line:no-use-before-declare return BINARY_DELTA_TYPE.apply(this, delta); }; /** * Convert the objet to a string * * @return a string representation of the JSONImpl * @throws an {@link InvalidDataError} if the value cannot be read */ JSONImpl.prototype.toString = function () { return JSON.stringify(this.get()); }; /** * Convert the objet to a JSON string * * @return a JSON string representation of the JSON object * @throws an {@link InvalidDataError} if the value cannot be read */ JSONImpl.prototype.toJsonString = function () { return JSON.stringify(this.get(), int64Replacer); }; return JSONImpl; }(bytes_impl_1.BytesImpl)); exports.JSONImpl = JSONImpl; /** * A unique binary delta type instance to calculate deltas of {@link JSONImpl} */ var BINARY_DELTA_TYPE = new binary_delta_type_impl_1.BinaryDeltaTypeImpl(JSONImpl, fromValue, function_1.identity);