Permalink
Browse files

fixes for a newline bug with comments

  • Loading branch information...
1 parent 8a4a5cd commit fa0f5e0fc6d35f65f7407748d6ee9718c3d38512 @substack committed Feb 21, 2012
Showing with 43 additions and 14 deletions.
  1. +15 −0 example/diff.js
  2. +28 −14 index.js
View
@@ -0,0 +1,15 @@
+var difflet = require('./');
+var a = {
+ x : 4,
+ z : 8,
+ xs : [ 5, 2, 1, { 0 : 'c' } ],
+};
+
+var b = {
+ x : 4,
+ y : 5,
+ xs : [ 5, 2, 2, { c : 5 } ],
+};
+
+var s = difflet({ comment : true, indent : 2 }).compare(a, b);
+console.log(s);
View
@@ -217,24 +217,16 @@ function difflet (opts, prev, next) {
});
this.post(function (child) {
+ if (!child.isLast && !(indent && commaFirst)) {
+ write(',');
+ }
+
if (child.isLast && deleted.length) {
if (insertedKey) unset('inserted');
insertedKey = false;
-
- if (indent && commaFirst) {
- write('\n' + indentx + ', ')
- }
- else if (indent) write(',\n' + indentx)
- else write(',');
}
- else {
- if (!child.isLast) {
- if (indent && commaFirst) {
- write('\n' + indentx + ', ');
- }
- else write(',');
- }
- if (insertedKey) unset('inserted');
+ else if (insertedKey) {
+ unset('inserted');
insertedKey = false;
}
@@ -250,6 +242,28 @@ function difflet (opts, prev, next) {
unset('comment');
}
+ if (child.isLast && deleted.length) {
+ if (insertedKey) unset('inserted');
+ insertedKey = false;
+
+ if (indent && commaFirst) {
+ write('\n' + indentx + ', ')
+ }
+ else if (opts.comment && indent) {
+ write('\n' + indentx);
+ }
+ else if (indent) {
+ write(',\n' + indentx);
+ }
+ else write(',');
+ }
+ else {
+ if (!child.isLast) {
+ if (indent && commaFirst) {
+ write('\n' + indentx + ', ');
+ }
+ }
+ }
});
this.after(function () {

0 comments on commit fa0f5e0

Please sign in to comment.