npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

yaml-diff-patch

v2.0.0

Published

Apply a JSON diff/patch to YAML while preserving whitespace, comments and overall structure

Downloads

154,432

Readme

npm version downloads build status coverage status Language grade: JavaScript Node.JS version

YAML is a great format, similar to JSON. It allows comments and flexibility in objects and arrays with regards to the elements. They can be inlined ([ ... ] and { ... }) or line-by-line.

YAML isn't however as easy to work with programmatically as JSON.

This package offers an API and standalone executables to allow for modifying a YAML as JSON, and then applying that JSON diff as a patch on the YAML, preserving whitespace, comments and overall structure. This also makes the final YAML diff minimal.

Versions

  • Version 2.0 requires Node 14

API

This package exports three functions: yamlPatch, yamlDiffPatch and yamlOverwrite.

yamlPatch

function yamlPatch( yaml: string, rfc6902: Array< Operation > ): string;

This function applies <rfc6902> "JSON Patch" operations on a <yaml> while trying to preserve whitespace, comments and structure for a minimal change.

Returns the patched YAML.

You can use the rfc6902 package do create the patch object, or use yamlDiffPatch below.

If the differences are too big, patching may fail (just like regular source code diff/patch operations). It's therefore recommended to - rather than re-using a JSON Patch for multiple different YAMLs - create the JSON Patch from each source YAML, transforming it as necessary and then apply that patch, whenever possible. That should never fail.

yamlDiffPatch

function yamlDiffPatch( yaml: string, oldJson: any, newJson: any ): string;

Uses two JSON's (<oldJson> and <newJson>) and makes a diff between them, then applies this as a patch to the <yaml>.

Returns the patched YAML.

This is the same as yamlPatch( yaml, makeJsonPatch( oldJson, newJson ) ); where makeJsonPatch would create an RFC6902 patch object.

yamlOverwrite

function yamlOverwrite( yaml: string, newJson: any ): string;

Uses the source <yaml> as the source object and diffs that against <newJson>, then applies this diff as a patch to the <yaml>. This will overwrite the fields that are different, while maintaining the structure of the source YAML.

Returns the patched YAML.

This is the same as yamlDiffPatch( yaml, yamlToJson( yaml ), newJson ); where yamlToJson would parse YAML into JSON.

Executables

yaml-patch

❯ yaml-patch

   Usage: yaml-patch source.yaml patch.json

   Patches <source.yaml> with the RFC6902 in <patch.json>

   Options:

   -h, --help           Print (this) help screen
   -o, --output <file>  Output filename to write to, or "-" for stdout (default: -)

yaml-diff-patch

❯ yaml-diff-patch

   Usage: yaml-diff-patch source.yaml old.json new.json

   Patches <source.yaml> with the diff between <old.json> and <new.json>

   Options:

   -h, --help           Print (this) help screen
   -o, --output <file>  Output filename to write to, or "-" for stdout (default: -)

yaml-overwrite

❯ yaml-overwrite

   Usage: yaml-overwrite source.yaml source.json

   Patches <source.yaml> with the diff between <source.yaml> and <source.json>

   Options:

   -h, --help           Print (this) help screen
   -o, --output <file>  Output filename to write to, or "-" for stdout (default: -)