Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@stackmeister/json-patch

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@stackmeister/json-patch

@stackmeister/json-patch ============================

latest
Source
npmnpm
Version
0.1.5
Version published
Maintainers
1
Created
Source

@stackmeister/json-patch

Typing and utilities for working with JSON-Patches

See RFC-6902 for more information on JSON-Patch documents.

Install

// Yarn
yarn add @stackmeister/json-patch

// NPM
npm i @stackmeister/json-patch

TypeScript typings are included (No @types/ package needed)

Usage

Creating patches manually with factories

import type { JsonPatch } from '@stackmeister/json-patch'
import { add, replace, remove, move, copy, test } from '@stackmeister/json-patch'

const myPatch: JsonPatch = [
  add('/c', 10),
  replace('/b', 5),
  remove('/a'),
  move('/b', '/d'),
  copy('/d', '/e'),
  test('/e', 5),
]

Apply patches to values

import { applyPatch } from '@stackmeister/json-patch'

const value = { a: 1, b: 2 }
const result = applyPatch(value, myPatch)

// Result:
// { c: 10, d: 5, e: 5 }

Create patches from mutations

import { createPatch } from '@stackmeister/json-patch'

const value = { id: 1, firstName: '' }
const patch = createPatch(value, draft => {
  draft.firstName = 'John'
  draft.lastName = 'Doe'
  draft.skills = ['js', 'ts']
  draft.skills.push('css')
})

// Patch:
// [
//   { op: 'replace', path: '/firstName', value: 'John' },
//   { op: 'add', path: '/lastName', value: 'Doe' },
//   { op: 'add', path: '/skills', value: ['js', 'ts'] },
//   { op: 'add', path: '/skills/-', value: 'css' },
// ]

FAQs

Package last updated on 09 Nov 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts