All Projects → sanity-io → Mendoza Js

sanity-io / Mendoza Js

Licence: mit
Mendoza decoder in TypeScript

Programming Languages

typescript
32286 projects

mendoza-js

npm versionBuild Statusnpm bundle size

Mendoza decoder in TypeScript.

Installation

$ npm install mendoza
// or
$ yarn add mendoza

Usage

Basic example:

import {applyPatch} from "mendoza"

let left = {};
let patch = [];
let right = applyPatch(left, patch);

Incremental patcher:

import {incremental} from "mendoza"

const {Value, rebaseValue, wrap, unwrap, getType, applyPatch} = incremental

// Create an empty initial version:
const ROOT = wrap(null, null);

// Input of patches:
let patches = [];

// `origin` can be whatever you want to identify where a change comes from:
let origin = 0;

// Reference to the latest version:
let value = ROOT;

// Rebasing is for maintaing history across deleted versions:
let rebaseTarget;

for (let patch of patches) {
  // Apply the patch:
  let newValue = applyPatch(value, patch, origin);

  // Rebase if needed:
  if (rebaseTarget) {
    newValue = rebaseValue(rebaseTarget, newValue);
  }

  // If the document was deleted, store the previous version so we can rebase:
  if (getType(newValue) === "null") {
    rebaseTarget = value;
  } else {
    rebaseTarget = null;
  }

  value = newValue;
  origin++;
}

// Return the final full object:
console.log(unwrap(value));

License

MIT © Sanity.io

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].