Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added 'assign' convenience shortcut.

This shortcut can be used to cleanly assign the text content of a node to a property of an object.
  • Loading branch information...
commit 0b759b942d03d4534c976e3787b5907efdbd5d9f 1 parent 2c85bd3
@lucsky authored
Showing with 30 additions and 10 deletions.
  1. +4 −10 examples/contacts.js
  2. +6 −0 lib/exml.js
  3. +20 −0 test/test.js
View
14 examples/contacts.js
@@ -11,17 +11,11 @@ parser.on('address-book', function(attributes) {
parser.on('contact', function(attributes) {
var contact = {};
- contacts.push(contact);
+ parser.on('first-name', '$content', exml.assign(contact, 'firstName'));
+ parser.on('last-name', '$content', exml.assign(contact, 'lastName'));
+ parser.on('address', '$content', exml.assign(contact, 'address'));
- parser.on('first-name', '$content', function(content) {
- contact.firstName = content;
- });
- parser.on('last-name', '$content', function(content) {
- contact.lastName = content;
- });
- parser.on('address', '$content', function(content) {
- contact.address = content;
- });
+ contacts.push(contact);
});
});
View
6 lib/exml.js
@@ -107,3 +107,9 @@ Parser.prototype._getHandler = function() {
};
module.exports.Parser = Parser;
+
+module.exports.assign = function(obj, propertyName) {
+ return function(content) {
+ obj[propertyName] = content;
+ };
+};
View
20 test/test.js
@@ -196,3 +196,23 @@ module.exports['mixed content events'] = {
test.done();
}
};
+
+module.exports['automatic property assignment'] = function(test) {
+ var parser = new exml.Parser();
+ var nodes = [];
+
+ parser.on('root', 'node', function() {
+ var node = {};
+ nodes.push(node);
+ parser.on('$content', exml.assign(node, 'content'));
+ });
+
+ parser.end(TEXT_XML);
+
+ test.equal(nodes.length, '3');
+ test.equal(nodes[0].content, 'text content 1');
+ test.equal(nodes[1].content, 'text content 2');
+ test.equal(nodes[2].content, 'text content 3');
+
+ test.done();
+};
Please sign in to comment.
Something went wrong with that request. Please try again.