From f925cb96ac5e226e9ab91b1f309068624339b3f2 Mon Sep 17 00:00:00 2001 From: John Mettraux Date: Sun, 3 Dec 2023 12:16:10 +0900 Subject: [PATCH] Prevent djan failing on negative indent for h keys --- CHANGELOG.md | 2 ++ lib/flor/djan.rb | 2 +- spec/djan_spec.rb | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b2e72499..19ef153a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ## flor 1.6.2 not yet released +* Prevent djan failing on negative indent for hash keys + ## flor 1.6.1 released 2023-10-20 diff --git a/lib/flor/djan.rb b/lib/flor/djan.rb index bccf1ab7..ad4d138c 100644 --- a/lib/flor/djan.rb +++ b/lib/flor/djan.rb @@ -140,7 +140,7 @@ def object_to_d(x, out, opts) # kml, vml = x.inject([ 0, 0 ]) { |(kl, vl), (k, v)| - [ [ kl, len(k, opts) ].max, [ vl, len(v, opts) ].max ] } + [ [ kl, len(k.to_s, opts) ].max, [ vl, len(v, opts) ].max ] } kml += 1 # if i && w && i + kml + 1 + vml < w diff --git a/spec/djan_spec.rb b/spec/djan_spec.rb index 4d257624..f7753cb1 100644 --- a/spec/djan_spec.rb +++ b/spec/djan_spec.rb @@ -152,6 +152,24 @@ "a+b" } }.strip) end + + it 'is ok with string keys' do + + d = + { "0": { user_id: 0, aum: 0, pitch_aum: 0, review_aum: 0, Pitch: 3, Others: 1, Social: 1, Review: 2 }, + "313327975":{ user_id: 313327975, aum: 0, pitch_aum: 0, review_aum: 0, items: [], Pitch: 1, Review: 1 }, + "837": { user_id: 837, aum: 0, pitch_aum: 0, review_aum: 0, items: [], Pitch: 1 }, + "899": { user_id: 899, aum: 0, pitch_aum: 0, review_aum: 0, items: [], Others: 1, Social: 1, Pitch: 1, Review: 1 } } + + expect( + Flor.to_djan(d, indent: 2, width: true, colours: false) + ).to eq(' ' + %{ + { "0": { user_id: 0, aum: 0, pitch_aum: 0, review_aum: 0, Pitch: 3, Others: 1, Social: 1, Review: 2 }, + "313327975": { user_id: 313327975, aum: 0, pitch_aum: 0, review_aum: 0, items: [], Pitch: 1, Review: 1 }, + "837": { user_id: 837, aum: 0, pitch_aum: 0, review_aum: 0, items: [], Pitch: 1 }, + "899": { user_id: 899, aum: 0, pitch_aum: 0, review_aum: 0, items: [], Others: 1, Social: 1, Pitch: 1, Review: 1 } } + }.strip) + end end end