-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
651612f
commit 7b864a3
Showing
4 changed files
with
115 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |