feat: jsv support for primitives
This commit is contained in:
parent
41c196f2ec
commit
b0b7114c3b
9 changed files with 132 additions and 0 deletions
9
lib/support/jsv.rb
Normal file
9
lib/support/jsv.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
# spec by dolist https://api.dolist.com/ConvertJSON-to-JSV.html
|
||||
#
|
||||
require_relative "jsv/core_ext/array"
|
||||
require_relative "jsv/core_ext/false_class"
|
||||
require_relative "jsv/core_ext/hash"
|
||||
require_relative "jsv/core_ext/number"
|
||||
require_relative "jsv/core_ext/string"
|
||||
require_relative "jsv/core_ext/symbol"
|
||||
require_relative "jsv/core_ext/true_class"
|
5
lib/support/jsv/core_ext/array.rb
Normal file
5
lib/support/jsv/core_ext/array.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
class Array
|
||||
def to_jsv
|
||||
"[" + reject(&:nil?).map(&:to_jsv).join(",") + "]"
|
||||
end
|
||||
end
|
5
lib/support/jsv/core_ext/false_class.rb
Normal file
5
lib/support/jsv/core_ext/false_class.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
class TrueClass
|
||||
def to_jsv
|
||||
"True"
|
||||
end
|
||||
end
|
11
lib/support/jsv/core_ext/hash.rb
Normal file
11
lib/support/jsv/core_ext/hash.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
class Hash
|
||||
def to_jsv
|
||||
js_array = filter_map do |key, value|
|
||||
next if value.nil? # skip nil values
|
||||
|
||||
"#{key.to_jsv}:#{value.to_jsv}"
|
||||
end
|
||||
|
||||
"{" + js_array.join(",") + "}"
|
||||
end
|
||||
end
|
5
lib/support/jsv/core_ext/number.rb
Normal file
5
lib/support/jsv/core_ext/number.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
class Numeric
|
||||
def to_jsv
|
||||
self
|
||||
end
|
||||
end
|
12
lib/support/jsv/core_ext/string.rb
Normal file
12
lib/support/jsv/core_ext/string.rb
Normal file
|
@ -0,0 +1,12 @@
|
|||
class String
|
||||
JSV_REGEX_SPECIAL_CHARS = /[\[\]\{\}"\,]/.freeze
|
||||
|
||||
def to_jsv
|
||||
double_quoted = self.gsub('"', '""')
|
||||
if match?(JSV_REGEX_SPECIAL_CHARS)
|
||||
"\"#{double_quoted}\""
|
||||
else
|
||||
double_quoted
|
||||
end
|
||||
end
|
||||
end
|
5
lib/support/jsv/core_ext/symbol.rb
Normal file
5
lib/support/jsv/core_ext/symbol.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
class Symbol
|
||||
def to_jsv
|
||||
to_s.to_jsv
|
||||
end
|
||||
end
|
5
lib/support/jsv/core_ext/true_class.rb
Normal file
5
lib/support/jsv/core_ext/true_class.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
class FalseClass
|
||||
def to_jsv
|
||||
"False"
|
||||
end
|
||||
end
|
75
spec/lib/support/jsv_spec.rb
Normal file
75
spec/lib/support/jsv_spec.rb
Normal file
|
@ -0,0 +1,75 @@
|
|||
require_relative "../../../lib/support/jsv"
|
||||
|
||||
describe ".to_jsv support" do
|
||||
it "converts a Hash to JSV" do
|
||||
expect({}.to_jsv).to eq("{}")
|
||||
expect({ "a" => "b" }.to_jsv).to eq("{a:b}")
|
||||
end
|
||||
|
||||
it "converts an Array to JSV" do
|
||||
expect([].to_jsv).to eq("[]")
|
||||
expect(["a", "b"].to_jsv).to eq("[a,b]")
|
||||
end
|
||||
|
||||
it "converts a String to JSV" do
|
||||
expect("".to_jsv).to eq("")
|
||||
expect("a".to_jsv).to eq("a")
|
||||
|
||||
# escape special characters
|
||||
expect("a[b".to_jsv).to eq('"a[b"')
|
||||
expect("a]b".to_jsv).to eq('"a]b"')
|
||||
expect("a,b".to_jsv).to eq('"a,b"')
|
||||
expect("a{b".to_jsv).to eq('"a{b"')
|
||||
expect("a}b".to_jsv).to eq('"a}b"')
|
||||
expect('a"b'.to_jsv).to eq('"a""b"')
|
||||
end
|
||||
|
||||
it "skip null values" do
|
||||
expect({ "a" => nil }.to_jsv).to eq("{}")
|
||||
expect([nil].to_jsv).to eq("[]")
|
||||
end
|
||||
|
||||
it "converts symbols like strings" do
|
||||
expect({ a: :b }.to_jsv).to eq("{a:b}")
|
||||
end
|
||||
|
||||
it "converts booleans" do
|
||||
expect(true.to_jsv).to eq("True")
|
||||
expect(false.to_jsv).to eq("False")
|
||||
end
|
||||
|
||||
it "converts numbers" do
|
||||
expect(1.to_jsv).to eq(1)
|
||||
expect(3.14.to_jsv).to eq(3.14)
|
||||
end
|
||||
|
||||
it "converts nested structures" do
|
||||
expect({ "a" => { "b" => "c" } }.to_jsv).to eq("{a:{b:c}}")
|
||||
expect({ "a" => ["b", "c"] }.to_jsv).to eq("{a:[b,c]}")
|
||||
end
|
||||
|
||||
it "converts relastic structures" do
|
||||
hash = {
|
||||
Type: :TransactionalService,
|
||||
"Contact": {
|
||||
"FieldList": [
|
||||
{
|
||||
"ID": 3,
|
||||
"Value": "glou[0]"
|
||||
}
|
||||
]
|
||||
},
|
||||
"Message": {
|
||||
"Subject": "You, and me",
|
||||
"ForceHttp": true,
|
||||
"IsTrackingValidated": false,
|
||||
"IgnoreMe": nil,
|
||||
"SourceCode": "<html><body><p>Un mail tout simple pour commencer</p></body></html>",
|
||||
"SourceWithQuote": 'Ceci est une double quote: "'
|
||||
}
|
||||
}
|
||||
|
||||
expected = '{Type:TransactionalService,Contact:{FieldList:[{ID:3,Value:"glou[0]"}]},Message:{Subject:"You, and me",ForceHttp:True,IsTrackingValidated:False,SourceCode:<html><body><p>Un mail tout simple pour commencer</p></body></html>,SourceWithQuote:"Ceci est une double quote: """}}'
|
||||
expect(hash.to_jsv).to eq(expected)
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue