Skip to content

Preserve trailing newlines #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions lib/git-json-merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ var detectIndent = require('detect-indent');

var encoding = 'utf-8';

function detectTrailingNewline (str) {
if (str.endsWith('\r\n')) {
return '\r\n';
} else if (str.endsWith('\n')) {
return '\n';
} else {
return '';
}
}

function mergeJsonFiles (oursFileName, baseFileName, theirsFileName) {
var oursJson = stripBom(fs.readFileSync(oursFileName, encoding));
var baseJson = stripBom(fs.readFileSync(baseFileName, encoding));
Expand All @@ -13,15 +23,13 @@ function mergeJsonFiles (oursFileName, baseFileName, theirsFileName) {
}

function mergeJson (oursJson, baseJson, theirsJson) {
var oursIndent = detectIndent(oursJson).indent;
var baseIndent = detectIndent(baseJson).indent;
var theirsIndent = detectIndent(theirsJson).indent;
var newOursIndent = selectIndent(oursIndent, baseIndent, theirsIndent);
var indent = mapAndSelect((str) => detectIndent(str).indent, oursJson, baseJson, theirsJson);
var trailingNewline = mapAndSelect(detectTrailingNewline, oursJson, baseJson, theirsJson);
var ours = JSON.parse(oursJson);
var base = JSON.parse(baseJson);
var theirs = JSON.parse(theirsJson);
var newOurs = merge(ours, base, theirs);
var newOursJson = JSON.stringify(newOurs, null, newOursIndent);
var newOursJson = JSON.stringify(newOurs, null, indent) + trailingNewline;

return newOursJson;
}
Expand All @@ -36,18 +44,24 @@ function merge (ours, base, theirs) {
return base;
}

function selectIndent (oursIndent, baseIndent, theirsIndent) {
return oursIndent !== baseIndent ? oursIndent : theirsIndent !== baseIndent ? theirsIndent : baseIndent;
function selectVersion (ours, base, theirs) {
return ours !== base ? ours : theirs !== base ? theirs : base;
}

function mapAndSelect (fn, ours, base, theirs) {
return selectVersion(fn(ours), fn(base), fn(theirs));
}

function stripBom (str) {
return str[0] === '\uFEFF' ? str.slice(1) : str;
}

module.exports = {
detectTrailingNewline: detectTrailingNewline,
mergeJsonFiles: mergeJsonFiles,
mergeJson: mergeJson,
merge: merge,
selectIndent: selectIndent,
selectVersion: selectVersion,
mapAndSelect: mapAndSelect,
stripBom: stripBom
}
40 changes: 31 additions & 9 deletions test/git-json-merge.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,35 @@ describe('gitJsonMerge', function () {
describeMergeJsonTest(bar, fooBar, fooBar, bar);
});

describe('selectIndent', function () {
describeSelectIndentTest(4, 2, 2, 4);
describeSelectIndentTest(4, 4, 2, 2);
describeSelectIndentTest(4, 4, 4, 4);
describeSelectIndentTest(2, 4, 2, 2);
describeSelectIndentTest(2, 2, 4, 4);
describeSelectIndentTest(2, 4, 4, 2);
describe('selectVersion', function () {
describeSelectVersionTest(4, 2, 2, 4);
describeSelectVersionTest(4, 4, 2, 2);
describeSelectVersionTest(4, 4, 4, 4);
describeSelectVersionTest(2, 4, 2, 2);
describeSelectVersionTest(2, 2, 4, 4);
describeSelectVersionTest(2, 4, 4, 2);
});

describe('mapAndSelect', function () {
it('calls the function with the arguments and selects the result', function () {
var repeatX = (count) => repeatCharacter('x', count);

expect(gitJsonMerge.mapAndSelect(repeatX, 4, 2, 2)).to.equal('xxxx');
});
});

describe('detectTrailingNewline', function () {
it('returns \\r\\n for Windows-style newlines', function () {
expect(gitJsonMerge.detectTrailingNewline('string\r\n')).to.equal('\r\n');
});

it('returns \\n for Unix-style newlines', function () {
expect(gitJsonMerge.detectTrailingNewline('string\n')).to.equal('\n');
});

it('returns an empty string without trailing newline', function () {
expect(gitJsonMerge.detectTrailingNewline('string')).to.equal('');
});
});

describe('stripBom', function () {
Expand Down Expand Up @@ -61,15 +83,15 @@ function describeMergeJsonTest (ours, base, theirs, expected) {
});
}

function describeSelectIndentTest (ours, base, theirs, expected) {
function describeSelectVersionTest (ours, base, theirs, expected) {
var character = ' ';
ours = repeatCharacter(character, ours);
base = repeatCharacter(character, base);
theirs = repeatCharacter(character, theirs);
expected = repeatCharacter(character, expected);

describe('given arguments of ' + ours.length + ' as ours, ' + base.length + ' as base and ' + theirs.length + ' as theirs', function () {
var actual = gitJsonMerge.selectIndent(ours, base, theirs);
var actual = gitJsonMerge.selectVersion(ours, base, theirs);
it('should return ' + expected.length, function () {
expect(actual).to.equal(expected);
})
Expand Down