Skip to content

Commit 6644d59

Browse files
hansthenConengmo
andauthored
Fix numeric keys for dict inputs in tojavascript (#2100)
* Fix numeric keys in arguments Closes #2098 * add a test case --------- Co-authored-by: Conengmo <[email protected]>
1 parent b2cb81a commit 6644d59

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

folium/template.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ def tojavascript(obj: Union[str, JsCode, dict, list, Element]) -> str:
1515
elif isinstance(obj, dict):
1616
out = ["{\n"]
1717
for key, value in obj.items():
18-
out.append(f' "{camelize(key)}": ')
18+
if isinstance(key, str):
19+
out.append(f' "{camelize(key)}": ')
20+
else:
21+
out.append(f" {key}: ")
1922
out.append(tojavascript(value))
2023
out.append(",\n")
2124
out.append("}")

tests/test_template.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ def test_tojavascript_with_dict():
1919
assert tojavascript(dict_obj) == '{\n "key": "value",\n}'
2020

2121

22+
def test_tojavascript_with_dict_with_mixed_key_types():
23+
dict_obj = {"key": "value", 1: "another value", 3.14: "pi"}
24+
expected = '{\n "key": "value",\n 1: "another value",\n 3.14: "pi",\n}'
25+
assert tojavascript(dict_obj) == expected
26+
27+
2228
def test_tojavascript_with_list():
2329
list_obj = ["value1", "value2"]
2430
assert tojavascript(list_obj) == '[\n"value1",\n"value2",\n]'

0 commit comments

Comments
 (0)