Permalink
Browse files

Tidy `lib/response.js`.

Tidy the CoffeeScript generatd JavaScript in lib response.
  • Loading branch information...
bigeasy committed Jun 28, 2012
1 parent 831c140 commit 165707b2c883d6556a503d608dfb7464d4bbf312
Showing with 65 additions and 77 deletions.
  1. +65 −77 lib/response.js
View
@@ -1,87 +1,75 @@
-var ResponseParser, isTimestamp, xml;
+var isTimestamp = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}).(\d*)Z/
+ , xml = require("node-xml")
+ ;
-xml = require("node-xml");
-
-isTimestamp = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}).(\d*)Z/;
-
-ResponseParser = (function() {
- function ResponseParser() {
- var accumulator, depth, names,
- _this = this;
- accumulator = [];
- depth = 0;
- names = [];
- this.sax = new xml.SaxParser(function(sax) {
- sax.onStartElementNS(function(elem) {
- if (/^item|Error$/.test(elem)) {
- if (_this.branches.length !== depth) _this.branches.push([]);
- names.push(elem);
- } else if (depth !== 0) {
- if (depth > 1 && _this.branches.length !== depth) {
- _this.branches.push({});
- }
- names.push(elem);
+function ResponseParser() {
+ var accumulator = [], depth = 0, names = [], _this = this;
+ this.sax = new xml.SaxParser(function(sax) {
+ sax.onStartElementNS(function(elem) {
+ if (/^item|Error$/.test(elem)) {
+ if (_this.branches.length !== depth) _this.branches.push([]);
+ names.push(elem);
+ } else if (depth !== 0) {
+ if (depth > 1 && _this.branches.length !== depth) {
+ _this.branches.push({});
}
- accumulator.length = 0;
- return depth++;
- });
- sax.onEndElementNS(function(elem) {
- var map, match, name, value;
- if (--depth === 0) {
- return _this.callback(null, _this.base);
- } else if (/^item|Error$/.test(names[depth - 1])) {
- map = _this.branches.pop();
- _this.branches[_this.branches.length - 1].push(map);
- return names.pop();
- } else if (_this.branches[depth]) {
- map = _this.branches.pop();
- return _this.branches[_this.branches.length - 1][names.pop()] = map;
+ names.push(elem);
+ }
+ accumulator.length = 0;
+ depth++;
+ });
+ sax.onEndElementNS(function(elem) {
+ var map, match, name, value;
+ if (--depth === 0) {
+ _this.callback(null, _this.base);
+ } else if (/^item|Error$/.test(names[depth - 1])) {
+ map = _this.branches.pop();
+ _this.branches[_this.branches.length - 1].push(map);
+ names.pop();
+ } else if (_this.branches[depth]) {
+ map = _this.branches.pop();
+ _this.branches[_this.branches.length - 1][names.pop()] = map;
+ } else {
+ name = names.pop();
+ if (accumulator.length === 0) {
+ value = null;
+ if (name === "Errors" || /Set$/.test(name)) value = [];
} else {
- name = names.pop();
- if (accumulator.length === 0) {
- value = null;
- if (name === "Errors" || /Set$/.test(name)) value = [];
- } else {
- value = accumulator.join("");
- if (/Time$/.test(name)) {
- match = isTimestamp.exec(value);
- if (match) {
- match = match.slice(1).map(function(part) {
- return parseInt(part, 10);
- });
- match[1]--;
- value = new Date(Date.UTC.apply(null, match));
- }
- }
- if (/size$/i.test(name)) value = parseInt(value, 10);
- if (name === "return" && /^true|false$/.test(value)) {
- value = value === "true";
+ value = accumulator.join("");
+ if (/Time$/.test(name)) {
+ match = isTimestamp.exec(value);
+ if (match) {
+ match = match.slice(1).map(function(part) { return +part });
+ match[1]--;
+ value = new Date(Date.UTC.apply(null, match));
}
}
- _this.branches[_this.branches.length - 1][name] = value;
- return accumulator.length = 0;
- }
- });
- sax.onCharacters(function(chars) {
- if (accumulator.length === 0 && /\S/.test(chars)) {
- return accumulator.push(chars);
+ if (/size$/i.test(name)) value = parseInt(value, 10);
+ if (name === "return" && /^true|false$/.test(value)) {
+ value = value === "true";
+ }
}
- });
- return sax.onError(function(msg) {
- console.log("blurgh", msg);
- return _this.callback(new Error(msg), null);
- });
+ _this.branches[_this.branches.length - 1][name] = value;
+ accumulator.length = 0;
+ }
});
- }
-
- ResponseParser.prototype.read = function(text, callback) {
- this.base = {};
- this.branches = [this.base];
- this.callback = callback || function() {};
- return this.sax.parseString(text);
- };
+ sax.onCharacters(function(chars) {
+ if (accumulator.length === 0 && /\S/.test(chars)) {
+ accumulator.push(chars);
+ }
+ });
+ sax.onError(function(msg) {
+ console.log("blurgh", msg);
+ _this.callback(new Error(msg), null);
+ });
+ });
+}
- return ResponseParser;
-})();
+ResponseParser.prototype.read = function(text, callback) {
+ this.base = {};
+ this.branches = [this.base];
+ this.callback = callback || function() {};
+ this.sax.parseString(text);
+};
module.exports.ResponseParser = ResponseParser;

0 comments on commit 165707b

Please sign in to comment.