{"name":"@pnpm/yaml.document-sync","version":"1100.0.1","keywords":["pnpm","pnpm11","patch","yaml"],"license":"MIT","_id":"@pnpm/yaml.document-sync@1100.0.1","maintainers":[{"name":"pnpmuser","email":"publish-bot@pnpm.io"},{"name":"zkochan","email":"z@kochan.io"}],"homepage":"https://github.com/pnpm/pnpm/tree/main/pnpm11/yaml/document-sync#readme","bugs":{"url":"https://github.com/pnpm/pnpm/issues"},"dist":{"shasum":"9a5556a00fccf6bee555ac1cf55ecb437224446a","tarball":"http://registry-npm.ywwl.com/@pnpm/yaml.document-sync/download/@pnpm/yaml.document-sync-1100.0.1.tgz","fileCount":8,"integrity":"sha512-1zs3MBnqsfYrcGa1ZhC2TNLdBb1p5RUAcuKF4BGLIolqe0GoJhhex0vEfaE3UezUoqyNbf0TDId/gfOBqA402g==","signatures":[{"sig":"MEUCIHOn+rXBioStoOSayI6MTE5oFJvDMPJHxovwpSaHjOzjAiEAu8pX/WIY2bBh4LgfJqdKLEDWGdqXlBcLBqF94ovuphE=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/@pnpm%2fyaml.document-sync@1100.0.1","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":14240,"size":5414,"noattachment":false,"key":"/@pnpm/yaml.document-sync/-/@pnpm/yaml.document-sync-1100.0.1.tgz"},"jest":{"preset":"@pnpm/jest-config"},"main":"lib/index.js","type":"module","types":"lib/index.d.ts","engines":{"node":">=22.13"},"exports":{".":"./lib/index.js"},"funding":"https://opencollective.com/pnpm","scripts":{"lint":"eslint \"src/**/*.ts\" \"test/**/*.ts\"","test":"pn compile && pn .test",".test":"cross-env NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules --disable-warning=ExperimentalWarning --disable-warning=DEP0169\" jest","compile":"tsgo --build && pn lint --fix"},"_npmUser":{"name":"pnpmuser","email":"publish-bot@pnpm.io"},"repository":{"url":"https://github.com/pnpm/pnpm/tree/main/pnpm11/yaml/document-sync","type":"git"},"description":"Update a YAML document to match the contents of an in-memory object.","directories":{},"dependencies":{"yaml":"^2.9.0"},"_hasShrinkwrap":false,"devDependencies":{"@jest/globals":"30.4.1","@pnpm/yaml.document-sync":"1100.0.1"},"_npmOperationalInternal":{"tmp":"tmp/yaml.document-sync_1100.0.1_1784825084282_0.27742032670719974","host":"s3://npm-registry-packages-npm-production"},"readme":"# @pnpm/yaml.document-sync\n\n> Update a YAML document to match the contents of an in-memory object.\n\n<!--@shields('npm')-->\n[![npm version](https://img.shields.io/npm/v/@pnpm/yaml.document-sync.svg)](https://npmx.dev/package/@pnpm/yaml.document-sync)\n<!--/@-->\n\n## Installation\n\n```sh\npnpm add @pnpm/yaml.document-sync\n```\n\n## Usage\n\nGiven a \"_source_\" document such as:\n\n```yaml\nfoo:\n  bar:\n    # 25 is better than 24\n    baz: 25\n\nqux:\n  # He was number 1\n  - 1\n```\n\nAnd a \"_target_\" object in-memory:\n\n```ts\nimport fs from 'node:fs'\nimport { patchDocument } from '@pnpm/yaml.document-sync'\nimport yaml from 'yaml'\n\nconst source = await fs.promises.readFile('source.yaml', 'utf8')\nconst document = yaml.parseDocument(source)\n\nconst target = {\n  foo: { bar: { baz: 25 } },\n  qux: [1, 2, 3]\n}\n\npatchDocument(document, target)\n```\n\nThe `patchDocument` function will mutate `document` to match the `target`'s contents, retaining comments along the way. In the example above, the final rendered document will be:\n\n```yaml\nfoo:\n  bar:\n    # 25 is better than 24\n    baz: 25\n\nqux:\n  # He was number 1\n  - 1\n  - 2\n  - 3\n```\n\nFor package manifests, retain null values and empty maps and keep existing keys in their original order:\n\n```ts\npatchDocument(document, target, {\n  preserveKeyOrder: true,\n  preserveScalarAliases: true,\n  pruneEmptyValues: false,\n})\n```\n\nNew keys are appended to their mapping. By default, keys follow the target object's order, and null values and empty maps are pruned from existing nodes.\n\nConsumers that use a different conversion for scalar mapping keys can pass `stringifyKey`. For example, `stringifyKey: String` matches `js-yaml`, which exposes a null mapping key as the property name `\"null\"`. The default follows `yaml` and uses an empty string.\n\n## Purpose\n\nThis package is useful when your codebase:\n\n1. Uses the [yaml](https://npmx.dev/package/yaml) library.\n2. Calls `.toJSON()` on the parse result and performs changes to it.\n3. Needs to \"sync\" those changes back to the source document.\n\nInstead of this package, consider performing mutations directly on the `yaml.Document` returned from `yaml.parseDocument()` instead. Directly modifying will be faster and more accurate. This package exists as a workaround for codebases that make changes to a JSON object instead and need to reconcile changes back into the source `yaml.Document`.\n\n## Caveats\n\nThere are several cases where comment preservation is inherently ambiguous. If the caveats outlined below are problematic, consider modifying the source `yaml.Document` before running the patch function in this package.\n\n### Key Renames\n\nFor example, renames of a key are ambiguous. Given:\n\n```yaml\n- foo:\n    # Test\n    bar: 1\n```\n\nAnd a target object to match:\n\n```json\n{\n  \"baz\": {\n    \"bar\": 1\n  }\n}\n```\n\nThe comment on `bar` won't be retained.\n\n### List Reconciliation\n\nFor simple lists (e.g. lists with only primitives), items will be uniquely matched using their contents. However, updates to complex lists are inherently ambiguous.\n\nFor example, given a source list with objects as elements:\n\n```yaml\n- foo: 1\n# Comment\n- bar: 2\n```\n\nAnd a target:\n\n```json\n[\n  { \"foo\": 1 },\n  { \"baz\": 3 },\n  { \"bar\": 2 }\n]\n```\n\nThe result will erase the comment:\n\n```yaml\n- foo: 1\n- baz: 3\n- bar: 2\n```\n\nIt's not trivial to detect that the object with `bar` as a field moved down. Detecting this case would require a diffing algorithm, which would be best effort anyway.\n\nVirtual DOM libraries such as React have the same problem. In React, list elements need to specify a `key` prop to uniquely identify each item. This library may take a similar approach in the future if needed. This is not a problem for primitive lists since their values can be compared using simple equality checks.\n\n### Aliases\n\nGiven:\n\n```yaml\nfoo: &config\n  - 1\n  - 2\n\nbar: *config\n```\n\nAnd a target object:\n\n```json\n{\n  \"foo\": [1, 2],\n  \"bar\": [1, 2, 3]\n}\n```\n\nFor correctness, the YAML alias needs to be removed.\n\n```yaml\nfoo: &config\n  - 1\n  - 2\n\nbar:\n  - 1\n  - 2\n  - 3\n```\n\nSet `preserveScalarAliases: true` to retain scalar aliases whose final values\nagree. If an anchor's defining entry is removed, the first surviving entry\ndefines it. An entry whose value differs from the defining entry is written\nas a scalar. This option does not coordinate dependency updates; it preserves\nYAML representation while applying the supplied target values.\n\n```ts\npatchDocument(document, target, { preserveScalarAliases: true })\n```\n\n## License\n\nMIT\n","publish_time":1784825084469,"_cnpm_publish_time":1784825084469,"dist-tags":{"latest":"1100.0.2"}}