Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/npm/parse-conflict-json
Parse a JSON string that has git merge conflicts, resolving if possible
https://github.com/npm/parse-conflict-json
npm-cli
Last synced: about 1 month ago
JSON representation
Parse a JSON string that has git merge conflicts, resolving if possible
- Host: GitHub
- URL: https://github.com/npm/parse-conflict-json
- Owner: npm
- License: other
- Created: 2019-08-07T19:58:21.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-09-25T17:20:00.000Z (about 2 months ago)
- Last Synced: 2024-10-01T00:47:08.569Z (about 1 month ago)
- Topics: npm-cli
- Language: JavaScript
- Homepage:
- Size: 237 KB
- Stars: 32
- Watchers: 10
- Forks: 4
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
- Security: SECURITY.md
Awesome Lists containing this project
README
# parse-conflict-json
Parse a JSON string that has git merge conflicts, resolving if possible.
If the JSON is valid, it just does `JSON.parse` as normal.
If either side of the conflict is invalid JSON, then an error is thrown for
that.## USAGE
```js
// after a git merge that left some conflicts there
const data = fs.readFileSync('package-lock.json', 'utf8')// reviverFunction is passed to JSON.parse as the reviver function
// preference defaults to 'ours', set to 'theirs' to prefer the other
// side's changes.
const parsed = parseConflictJson(data, reviverFunction, preference)// returns true if the data looks like a conflicted diff file
parsed.isDiff(data)
```## Algorithm
If `prefer` is set to `theirs`, then the vaules of `theirs` and `ours` are
switched in the resolver function. (Ie, we'll apply their changes on top
of our object, rather than the other way around.)- Parse the conflicted file into 3 pieces: `ours`, `theirs`, and `parent`
- Get the [diff](https://github.com/angus-c/just#just-diff) from `parent`
to `ours`.- [Apply](https://github.com/angus-c/just#just-diff-apply) each change of
that diff to `theirs`.If any change in the diff set cannot be applied (ie, because they
changed an object into a non-object and we changed a field on that
object), then replace the object at the specified path with the object
at the path in `ours`.