# mendoza

> Mendoza, differ for structured documents

Latest version **3.0.8** (published 2024-12-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install mendoza
pnpm add mendoza
yarn add mendoza
bun add mendoza
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; has provenance; high maintenance score; high quality score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 3.0.8 |
| Published | 2024-12-04 |
| First published | 2020-09-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=14.18 |
| Dependencies | 0 |
| Unpacked size | 136.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 46 |
| Author | Sanity.io |
| Maintainers | kmelve, bjoerge, rexxars, skogsmaskin, tonina, mattcraig, joneidejohnsen, rubioz, robinpyon, mariuslundgard, sanity-io, evenw, radhe_sanity, rbotten, danielsgroves, judofyr, ryanblock, obliadp, dcilke, madken, fredcarlsen, hermanw, tambet, sgulseth, atombender, simeonsanity, stipsan, snorreeb, rankers, rdunk, michael-sanity, vincentquigley, ritasdias, kenjonespizza, rneatherway-sanity, josh_sanity_io, cngonzalez-sanity, jjburbridge, tdfka_rick, svirs, ryanbonial-sanity, indrek.karner, ash, sergeisarviro, refiito, drewsanity, kaspar.lippmaa.sanity, dam, simen.svale, tbeseda, daniel.malmer, jordanl17, colepeters, armandocerna, joan_miralles_paez, christianhg, pedro-sanity, jwoods-sanity, ricokahler, tiit.kass.saity, binoy14, pauloborgesf, ausha, nuurtamo |
| Keywords | mendoza, diff |

## Links

- npm: https://www.npmjs.com/package/mendoza
- Repository: https://github.com/sanity-io/mendoza-js
- Homepage: https://www.sanity.io/
- Issues: https://github.com/sanity-io/mendoza-js/issues
- npm.io page: https://npm.io/package/mendoza

## Recent versions

- 3.0.8 (latest) — 2024-12-04
- 3.0.3-canary.0 (canary) — 2023-08-04
- 1.150.9-review-changes.1024 (review-changes) — 2020-10-07
- 3.0.7 — 2024-04-11
- 3.0.6 — 2024-03-18
- 3.0.5 — 2024-02-26
- 3.0.4 — 2024-01-25
- 3.0.3 — 2023-08-04
- 3.0.2 — 2023-08-04
- 3.0.1 — 2023-08-04
- 3.0.1-canary.7 — 2023-08-04
- 3.0.1-canary.6 — 2023-08-04
- 3.0.1-canary.5 — 2023-08-04
- 3.0.1-canary.4 — 2023-08-04
- 3.0.1-canary.3 — 2023-08-04
- … 19 more at https://npm.io/package/mendoza/versions

## README

# mendoza-js

[![npm stat](https://img.shields.io/npm/dm/mendoza.svg?style=flat-square)](https://npm-stat.com/charts.html?package=mendoza)
[![npm version](https://img.shields.io/npm/v/mendoza.svg?style=flat-square)](https://www.npmjs.com/package/mendoza)
[![gzip size][gzip-badge]][bundlephobia]
[![size][size-badge]][bundlephobia]

[Mendoza](https://github.com/sanity-io/mendoza) decoder in TypeScript.

## Installation

```bash
npm install mendoza
```

or

```bash
pnpm install mendoza
```

or

```bash
yarn add mendoza
```

## Usage

Basic example:

```typescript
import {applyPatch} from "mendoza"

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

Incremental patcher:

```typescript
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](https://www.sanity.io/)

## Release new version

Run the ["CI & Release" workflow](https://github.com/sanity-io/mendoza-js/actions).
Make sure to select the main branch and check "Release new version".

Semantic release will only release on configured branches, so it is safe to run release on any branch.

[gzip-badge]: https://img.shields.io/bundlephobia/minzip/mendoza?label=gzip%20size&style=flat-square
[size-badge]: https://img.shields.io/bundlephobia/min/mendoza?label=size&style=flat-square
[bundlephobia]: https://bundlephobia.com/package/mendoza

---
_Source: https://npm.io/package/mendoza · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
