Skip to content
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

add setDefaultBuildOption to support emoji when building plist #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 20 additions & 3 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var xmlbuilder = require('xmlbuilder');
* Module exports.
*/

exports.setDefaultBuildOption = setDefaultBuildOption;
exports.build = build;

/**
Expand Down Expand Up @@ -47,6 +48,15 @@ function type (obj) {
return m ? m[1] : m;
}

/**
* Set default opts to build plist
* @param opts {Object}
*/
var defaultOpts = {};
function setDefaultBuildOption (opts) {
defaultOpts = opts || defaultOpts;
}

/**
* Generate an XML plist string from the input object `obj`.
*
Expand All @@ -67,15 +77,22 @@ function build (obj, opts) {
sysid: 'http://www.apple.com/DTDs/PropertyList-1.0.dtd'
};

var doc = xmlbuilder.create('plist');
// extends defaultOpts
opts = opts || {};
for (var i in defaultOpts) {
if (!opts.hasOwnProperty(i)) {
opts[i] = defaultOpts[i];
}
}

var doc = xmlbuilder.create('plist', opts);

doc.dec(XMLHDR.version, XMLHDR.encoding, XMLHDR.standalone);
doc.dtd(XMLDTD.pubid, XMLDTD.sysid);
doc.att('version', '1.0');

walk_obj(obj, doc);

if (!opts) opts = {};
// default `pretty` to `true`
opts.pretty = opts.pretty !== false;
return doc.end(opts);
Expand Down Expand Up @@ -104,7 +121,7 @@ function walk_obj(next, next_child) {
} else if ('Object' == name) {
next_child = next_child.ele('dict');
for (prop in next) {
if (next.hasOwnProperty(prop)) {
if (next.hasOwnProperty(prop) && next[prop] !== null && next[prop] !== undefined) {
next_child.ele('key').txt(prop);
walk_obj(next[prop], next_child);
}
Expand Down