Skip to content

Commit 7a2b497

Browse files
committed
Add array support
1 parent 3f8f482 commit 7a2b497

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

lib/react/rails/component_mount.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ def props_to_json(props, options = { null_to_undefined: false })
7676

7777
# This regex matches key:value with null values while ensuing no string with similar
7878
# pattern gets matched. It doesn't include null values in arrays.
79-
props.to_json.gsub(/([^\\]":)null([,}\]])/, '\1undefined\2')
79+
props.to_json
80+
.gsub(/([^\\]":)null([,}\]])/, '\1undefined\2') # match simple null values
81+
.gsub(/([^\\]":(\[[^\\"]+,|\[))null([,\]])/, '\1undefined\3') # Match nulls in array
8082
end
8183

8284
def rendered_tag(html_options, &block)

test/react/rails/component_mount_test.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,19 @@ def self.react_rails_prerenderer
178178
end
179179

180180
test "#props_to_json doesnt converts null-like values in arrays to undefined with null_to_undefined: true option" do
181-
props = { items1: "[null]", items2: "[1, null]", items3: "[null, 1]", items4: "[1, null, 2]" }
182-
expected_json = '{"items1":"[null]","items2":"[1,null]","items3":"[null,1]","items4":"[1,null,2]"}'
181+
props = {
182+
items1: "[null]",
183+
items2: "[1,null]",
184+
items3: "[null,1]",
185+
items4: "[1,null,2]",
186+
items5: '["a",null]',
187+
items6: '[null,"b"]',
188+
items7: '["a",null,"b"]',
189+
items8: '["a",nullx,"b"]'
190+
}
191+
expected_json = '{"items1":"[null]","items2":"[1,null]","items3":"[null,1]","items4":"[1,null,2]",' \
192+
'"items5":"[\"a\",null]","items6":"[null,\"b\"]","items7":"[\"a\",null,\"b\"]"' \
193+
',"items8":"[\"a\",nullx,\"b\"]"}'
183194
component_mount = React::Rails::ComponentMount.new
184195

185196
actual_json = component_mount.send(:props_to_json, props, { null_to_undefined: true })

0 commit comments

Comments
 (0)