Skip to content

Commit 72daa2d

Browse files
n1ru4lChris Brody
authored andcommitted
feat: omit objects with empty values (apache#24)
and add tests
1 parent 3440e48 commit 72daa2d

File tree

7 files changed

+2362
-4
lines changed

7 files changed

+2362
-4
lines changed

lib/pbxProject.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ pbxProject.prototype.parseSync = function() {
6464
return this;
6565
}
6666

67-
pbxProject.prototype.writeSync = function() {
68-
this.writer = new pbxWriter(this.hash);
67+
pbxProject.prototype.writeSync = function(options) {
68+
this.writer = new pbxWriter(this.hash, options);
6969
return this.writer.writeSync();
7070
}
7171

lib/pbxWriter.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,18 @@ function isArray(obj) {
4949
return Array.isArray(obj)
5050
}
5151

52-
function pbxWriter(contents) {
52+
function pbxWriter(contents, options) {
53+
if (!options) {
54+
options = {}
55+
}
56+
if (options.omitEmptyValues === undefined) {
57+
options.omitEmptyValues = false
58+
}
59+
5360
this.contents = contents;
5461
this.sync = false;
5562
this.indentLevel = 0;
63+
this.omitEmptyValues = options.omitEmptyValues
5664
}
5765

5866
util.inherits(pbxWriter, EventEmitter);
@@ -123,6 +131,8 @@ pbxWriter.prototype.writeProject = function () {
123131

124132
this.indentLevel--;
125133
this.write("};\n");
134+
} else if (this.omitEmptyValues && (obj === undefined || obj === null)) {
135+
continue;
126136
} else if (cmt) {
127137
this.write("%s = %s /* %s */;\n", key, obj, cmt)
128138
} else {
@@ -156,7 +166,9 @@ pbxWriter.prototype.writeObject = function (object) {
156166
this.indentLevel--;
157167
this.write("};\n");
158168
} else {
159-
if (cmt) {
169+
if (this.omitEmptyValues && (obj === undefined || obj === null)) {
170+
continue;
171+
} else if (cmt) {
160172
this.write("%s = %s /* %s */;\n", key, obj, cmt)
161173
} else {
162174
this.write("%s = %s;\n", key, obj)
@@ -255,6 +267,7 @@ pbxWriter.prototype.writeSection = function (section) {
255267

256268
pbxWriter.prototype.writeInlineObject = function (n, d, r) {
257269
var output = [];
270+
var self = this
258271

259272
var inlineObjectHelper = function (name, desc, ref) {
260273
var key, cmt, obj;
@@ -281,6 +294,8 @@ pbxWriter.prototype.writeInlineObject = function (n, d, r) {
281294
output.push("); ");
282295
} else if (isObject(obj)) {
283296
inlineObjectHelper(key, cmt, obj)
297+
} else if (self.omitEmptyValues && (obj === undefined || obj === null)) {
298+
continue;
284299
} else if (cmt) {
285300
output.push(f("%s = %s /* %s */; ", key, obj, cmt))
286301
} else {

test/parser/projects/expected/with_omit_empty_values_disabled_expected.pbxproj

Lines changed: 582 additions & 0 deletions
Large diffs are not rendered by default.

test/parser/projects/expected/with_omit_empty_values_enabled_expected.pbxproj

Lines changed: 581 additions & 0 deletions
Large diffs are not rendered by default.

test/parser/projects/with_omit_empty_values_disabled.pbxproj

Lines changed: 574 additions & 0 deletions
Large diffs are not rendered by default.

test/parser/projects/with_omit_empty_values_enabled.pbxproj

Lines changed: 574 additions & 0 deletions
Large diffs are not rendered by default.

test/pbxWriter.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,37 @@ exports.writeSync = {
9696
},
9797
'should write out the "file-references" test': function (test) {
9898
testProjectContents('test/parser/projects/file-references.pbxproj', test);
99+
},
100+
'should not null and undefined with the "omitEmptyValues" option set to false test': function (test) {
101+
var filename = 'test/parser/projects/with_omit_empty_values_disabled.pbxproj'
102+
var expectedFilename = 'test/parser/projects/expected/with_omit_empty_values_disabled_expected.pbxproj'
103+
var content = fs.readFileSync(expectedFilename, 'utf-8').replace(/ /g, '\t');
104+
var project = new pbx(filename);
105+
project.parse(function (err) {
106+
if (err) {
107+
return test.done(err);
108+
}
109+
const group = project.addPbxGroup([], 'CustomGroup', undefined)
110+
var written = project.writeSync();
111+
content = content.replace('CUSTOM_GROUP_UUID_REPLACED_BY_TEST', group.uuid)
112+
test.equal(content, written);
113+
test.done();
114+
});
115+
},
116+
'should drop null and undefined with the "omitEmptyValues" option set to true test': function (test) {
117+
var filename = 'test/parser/projects/with_omit_empty_values_enabled.pbxproj'
118+
var expectedFilename = 'test/parser/projects/expected/with_omit_empty_values_enabled_expected.pbxproj'
119+
var content = fs.readFileSync(expectedFilename, 'utf-8').replace(/ /g, '\t');
120+
var project = new pbx(filename);
121+
project.parse(function (err) {
122+
if (err) {
123+
return test.done(err);
124+
}
125+
var group = project.addPbxGroup([], 'CustomGroup', undefined);
126+
var written = project.writeSync({ omitEmptyValues: true });
127+
content = content.replace('CUSTOM_GROUP_UUID_REPLACED_BY_TEST', group.uuid)
128+
test.equal(content, written);
129+
test.done();
130+
});
99131
}
100132
}

0 commit comments

Comments
 (0)