Skip to content

Instantly share code, notes, and snippets.

@zhuangya
Created July 31, 2013 09:39
Show Gist options
  • Select an option

  • Save zhuangya/6120755 to your computer and use it in GitHub Desktop.

Select an option

Save zhuangya/6120755 to your computer and use it in GitHub Desktop.
var fs = require('fs');
var http = require('http');
if(!Array.prototype.last) {
Array.prototype.last = function() {
return this[this.length - 1];
};
}
http.createServer(function(req, res) {
fs.readFile('./mock.json', 'utf-8', function(err, mockData) {
if(err) throw err;
var mock = JSON.parse(mockData);
var production = [];
var oldCategory = '';
var oldSection = '';
mock.forEach(function(what) {
what.expression.replace(/([^=]*)=(.*)/, function(match, exp, value) {
what.exp = exp;
what.value = value;
});
var currentCategory = what.category;
var currentSection = what.section;
if(currentCategory === oldCategory) {
if(currentSection === oldSection) {
this.last().section.last().items.push({
name: what.item,
value: what.value
});
} else {
this.last().section.push({
name: what.section,
items: [
{name: what.item, value: what.value}
]
});
oldSection = currentSection;
}
} else {
this.push({
category: what.category,
section: [{
name: what.section,
items: [
{ name: what.item, value: what.value }
]
}]
});
oldCategory = currentCategory;
oldSection = currentSection;
}
}, production);
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(production));
});
}).listen(9009, '127.0.0.1');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment