Skip to content

Commit

Permalink
Extracted serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
evangelion1204 committed Jun 29, 2015
1 parent 651612f commit 7b864a3
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 105 deletions.
82 changes: 16 additions & 66 deletions lib/multi-ini-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,7 @@ _ = require('lodash');

Parser = require('./parser');

Serializer = (function() {
function Serializer(options) {
if (options == null) {
options = {};
}
this.options = options;
}

return Serializer;

})();
Serializer = require('./serializer');

MultiIni = (function() {
MultiIni.prototype["default"] = {
Expand All @@ -35,53 +25,29 @@ MultiIni = (function() {
}
this.options = _.extend(_.clone(this["default"]), options);
this.parser = new Parser(this.options);
this.serializer = new Serializer(this.options);
}

MultiIni.prototype.read = function(filename) {
var lines;
if (filename == null) {
filename = {};
}
lines = this.fetchLines(filename);
return this.parser.parse(lines);
};

MultiIni.prototype.fetchLines = function(filename) {
var content;
content = fs.readFileSync(filename, this.options);
return content.split('\n');
};

MultiIni.prototype.needToBeQuoted = function(value) {
if (value.match(/^"[\s\S]*?"$/g)) {
return false;
}
if (value.match(/^[\s\S]*?\\"$/g)) {
return true;
}
if (value.match(/^[\s\S]*?"$/g)) {
return false;
}
if (value.match(/^"[\s\S]*?$/g)) {
return false;
}
return true;
};

MultiIni.prototype.serializeContent = function(content, path) {
var key, serialized, subContent, value, _i, _len;
serialized = '';
for (key in content) {
subContent = content[key];
if (_.isArray(subContent)) {
for (_i = 0, _len = subContent.length; _i < _len; _i++) {
value = subContent[_i];
if (this.needToBeQuoted(value)) {
value = "\"" + value + "\"";
}
serialized += path + (path.length > 0 ? '.' : '') + key + "[]=" + value + "\n";
}
} else if (_.isObject(subContent)) {
serialized += this.serializeContent(subContent, path + (path.length > 0 ? '.' : '') + key);
} else {
if (this.needToBeQuoted(subContent)) {
subContent = "\"" + subContent + "\"";
}
serialized += path + (path.length > 0 ? '.' : '') + key + "=" + subContent + "\n";
}
MultiIni.prototype.write = function(filename, content) {
if (content == null) {
content = {};
}
return serialized;
return fs.writeFileSync(filename, this.serialize(content), this.options);
};

MultiIni.prototype.serialize = function(data) {
Expand All @@ -90,27 +56,11 @@ MultiIni = (function() {
for (section in data) {
sectionContent = data[section];
out += "[" + section + "]\n";
out += this.serializeContent(sectionContent, '');
out += this.serializer.serializeContent(sectionContent, '');
}
return out;
};

MultiIni.prototype.read = function(filename) {
var lines;
if (filename == null) {
filename = {};
}
lines = this.fetchLines(filename);
return this.parser.parse(lines);
};

MultiIni.prototype.write = function(filename, content) {
if (content == null) {
content = {};
}
return fs.writeFileSync(filename, this.serialize(content), this.options);
};

return MultiIni;

})();
Expand Down
58 changes: 58 additions & 0 deletions lib/serializer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
var Serializer, _;

_ = require('lodash');

Serializer = (function() {
function Serializer(options) {
if (options == null) {
options = {};
}
this.options = options;
}

Serializer.prototype.needToBeQuoted = function(value) {
if (value.match(/^"[\s\S]*?"$/g)) {
return false;
}
if (value.match(/^[\s\S]*?\\"$/g)) {
return true;
}
if (value.match(/^[\s\S]*?"$/g)) {
return false;
}
if (value.match(/^"[\s\S]*?$/g)) {
return false;
}
return true;
};

Serializer.prototype.serializeContent = function(content, path) {
var key, serialized, subContent, value, _i, _len;
serialized = '';
for (key in content) {
subContent = content[key];
if (_.isArray(subContent)) {
for (_i = 0, _len = subContent.length; _i < _len; _i++) {
value = subContent[_i];
if (this.needToBeQuoted(value)) {
value = "\"" + value + "\"";
}
serialized += path + (path.length > 0 ? '.' : '') + key + "[]=" + value + "\n";
}
} else if (_.isObject(subContent)) {
serialized += this.serializeContent(subContent, path + (path.length > 0 ? '.' : '') + key);
} else {
if (this.needToBeQuoted(subContent)) {
subContent = "\"" + subContent + "\"";
}
serialized += path + (path.length > 0 ? '.' : '') + key + "=" + subContent + "\n";
}
}
return serialized;
};

return Serializer;

})();

module.exports = Serializer;
49 changes: 10 additions & 39 deletions src/multi-ini-class.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ fs = require 'fs'
_ = require 'lodash'

Parser = require './parser'


class Serializer
constructor: (options = {}) ->
@options = options

Serializer = require './serializer'

class MultiIni
default:
Expand All @@ -23,52 +18,28 @@ class MultiIni
@options = _.extend(_.clone(@default), options)

@parser = new Parser(@options)
@serializer = new Serializer(@options)

read: (filename = {}) ->
lines = @fetchLines(filename)

return @parser.parse(lines)

fetchLines: (filename) ->
content = fs.readFileSync(filename, @options)
return content.split '\n'

needToBeQuoted: (value) ->
return false if value.match /^"[\s\S]*?"$/g
return true if value.match /^[\s\S]*?\\"$/g
return false if value.match /^[\s\S]*?"$/g
return false if value.match /^"[\s\S]*?$/g

return true

serializeContent: (content, path) ->
serialized = ''
for key, subContent of content
if _.isArray(subContent)
for value in subContent
value = "\"" + value + "\"" if @needToBeQuoted(value)

serialized += path + (if path.length > 0 then '.' else '') + key + "[]=" + value + "\n"
else if _.isObject(subContent)
serialized += @serializeContent(subContent, path + (if path.length > 0 then '.' else '') + key)
else
subContent = "\"" + subContent + "\"" if @needToBeQuoted(subContent)
serialized += path + (if path.length>0 then '.' else '') + key + "=" + subContent + "\n"

return serialized
write: (filename, content = {}) ->
fs.writeFileSync(filename, @serialize(content), @options)

serialize: (data) ->
out = ""
for section, sectionContent of data
out += "[" + section + "]\n"
out += @serializeContent(sectionContent, '')
out += @serializer.serializeContent(sectionContent, '')

return out

read: (filename = {}) ->
lines = @fetchLines(filename)

return @parser.parse(lines)

write: (filename, content = {}) ->
fs.writeFileSync(filename, @serialize(content), @options)


module.exports =
Class: MultiIni

Expand Down
31 changes: 31 additions & 0 deletions src/serializer.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
_ = require 'lodash'

class Serializer
constructor: (options = {}) ->
@options = options

needToBeQuoted: (value) ->
return false if value.match /^"[\s\S]*?"$/g
return true if value.match /^[\s\S]*?\\"$/g
return false if value.match /^[\s\S]*?"$/g
return false if value.match /^"[\s\S]*?$/g

return true

serializeContent: (content, path) ->
serialized = ''
for key, subContent of content
if _.isArray(subContent)
for value in subContent
value = "\"" + value + "\"" if @needToBeQuoted(value)

serialized += path + (if path.length > 0 then '.' else '') + key + "[]=" + value + "\n"
else if _.isObject(subContent)
serialized += @serializeContent(subContent, path + (if path.length > 0 then '.' else '') + key)
else
subContent = "\"" + subContent + "\"" if @needToBeQuoted(subContent)
serialized += path + (if path.length>0 then '.' else '') + key + "=" + subContent + "\n"

return serialized

module.exports = Serializer

0 comments on commit 7b864a3

Please sign in to comment.