Merge pull request #21 from alexwlchan/consistent-indentation
- ID
59d203e- date
2024-08-26 13:55:51+00:00- author
Alex Chan <alex@alexwlchan.net>- parents
9c05127,4b1d129- message
Merge pull request #21 from alexwlchan/consistent-indentation When you append values, indent them consistently with `write_js()`- changed files
2 files, 37 additions, 2 deletions
Changed files
src/javascript/__init__.py (5749) → src/javascript/__init__.py (5886)
diff --git a/src/javascript/__init__.py b/src/javascript/__init__.py
index f40cb41..8c976dc 100644
--- a/src/javascript/__init__.py
+++ b/src/javascript/__init__.py
@@ -14,6 +14,7 @@ Think of this like the JSON module, but for JavaScript files.
import json
import pathlib
import re
+import textwrap
import typing
import uuid
@@ -109,7 +110,11 @@ def append_to_js_array(p: pathlib.Path | str, *, value: typing.Any) -> None:
p = pathlib.Path(p)
file_size = p.stat().st_size
- json_to_append = b",\n" + json.dumps(value).encode("utf8") + b"\n];\n"
+ json_to_append = (
+ b",\n"
+ + textwrap.indent(json.dumps(value, indent=2), prefix=" ").encode("utf8")
+ + b"\n];\n"
+ )
with open(p, "rb+") as out_file:
out_file.seek(file_size - 4)
@@ -159,7 +164,7 @@ def append_to_js_object(p: pathlib.Path | str, *, key: str, value: typing.Any) -
file_size = p.stat().st_size
enc_key = json.dumps(key)
- enc_value = json.dumps(value)
+ enc_value = textwrap.indent(json.dumps(value, indent=2), prefix=" ").lstrip()
json_to_append = f",\n {enc_key}: {enc_value}\n}};\n".encode("utf8")
tests/test_javascript.py (10705) → tests/test_javascript.py (11871)
diff --git a/tests/test_javascript.py b/tests/test_javascript.py
index afad739..34717a0 100644
--- a/tests/test_javascript.py
+++ b/tests/test_javascript.py
@@ -251,6 +251,21 @@ class TestAppendToObject:
"sideLengths": [1, 2, 3, 4, 5],
}
+ def test_indentation_is_consistent(self, tmp_path: pathlib.Path) -> None:
+ """
+ If you append to an object, the file looks as if you'd read and rewritten
+ the whole thing with ``write_js()``.
+ """
+ js_path1 = tmp_path / "data1.js"
+ js_path2 = tmp_path / "data2.js"
+
+ write_js(js_path1, varname="shape", value={"colour": "red"})
+ append_to_js_object(js_path1, key="sides", value=[1, 2, 3])
+
+ write_js(js_path2, varname="shape", value={"colour": "red", "sides": [1, 2, 3]})
+
+ assert js_path1.read_text() == js_path2.read_text()
+
def test_error_if_file_doesnt_look_like_object(self, js_path: pathlib.Path) -> None:
"""
Appending to a file which doesn't contain a JSON object throws
@@ -323,3 +338,18 @@ class TestRoundTrip:
"coconut",
"damson",
]
+
+ def test_indentation_is_consistent(self, tmp_path: pathlib.Path) -> None:
+ """
+ If you append to an array, the file looks as if you'd read and rewritten
+ the whole thing with ``write_js()``.
+ """
+ js_path1 = tmp_path / "data1.js"
+ js_path2 = tmp_path / "data2.js"
+
+ write_js(js_path1, varname="numbers", value=[1, 2, 3])
+ append_to_js_array(js_path1, value=[4, 5, 6])
+
+ write_js(js_path2, varname="numbers", value=[1, 2, 3, [4, 5, 6]])
+
+ assert js_path1.read_text() == js_path2.read_text()