Skip to content

Commit

Permalink
Add reverse
Browse files Browse the repository at this point in the history
  • Loading branch information
T4rk1n committed Mar 1, 2023
1 parent 9f55b3d commit c5d4dd6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
3 changes: 3 additions & 0 deletions dash/_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ def insert(self, index, item):
def clear(self):
self._operations.append(_operation("Clear", self._location))

def reverse(self):
self._operations.append(_operation("Reverse", self._location))

def extend(self, item):
if not isinstance(item, (list, tuple)):
raise TypeError(f"{item} should be a list or tuple")
Expand Down
7 changes: 6 additions & 1 deletion dash/dash-renderer/src/actions/patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
insert,
is,
path,
prepend
prepend,
reverse
} from 'ramda';

type PatchOperation = {
Expand Down Expand Up @@ -132,6 +133,10 @@ const patchHandlers: {[k: string]: PatchHandler} = {
Clear: (previous, patchOperation) => {
const prev: any = path(patchOperation.location, previous);
return assocPath(patchOperation.location, empty(prev), previous);
},
Reverse: (previous, patchOperation) => {
const prev: any = path(patchOperation.location, previous);
return assocPath(patchOperation.location, reverse(prev), previous);
}
};

Expand Down
20 changes: 20 additions & 0 deletions tests/integration/test_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def test_pch001_patch_operations(dash_duo):
html.Button("Delete", id="delete-btn"),
html.Button("Delete index", id="delete-index"),
html.Button("Clear", id="clear-btn"),
html.Button("Reverse", id="reverse-btn"),
dcc.Store(
data={
"value": "unset",
Expand Down Expand Up @@ -181,6 +182,17 @@ def on_clear(_):

return p

@app.callback(
Output("store", "data", allow_duplicate=True),
Input("reverse-btn", "n_clicks"),
prevent_initial_call=True,
)
def on_reverse(_):
p = Patch()
p.array.reverse()

return p

dash_duo.start_server(app)

assert dash_duo.get_logs() == []
Expand Down Expand Up @@ -263,6 +275,14 @@ def get_output():
"Extend",
]

dash_duo.find_element("#reverse-btn").click()
assert get_output().get("array") == [
"Extend",
"Append",
"initial",
"Prepend",
]

dash_duo.find_element("#clear-btn").click()
assert get_output()["array"] == []

Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,15 @@ def test_pat017_patch_clear():
p.clear()
data = patch_to_dict(p)
assert data["operations"][0] == {"operation": "Clear", "location": [], "params": {}}


def test_pat018_patch_reverse():
p = Patch()

p.reverse()
data = patch_to_dict(p)
assert data["operations"][0] == {
"operation": "Reverse",
"location": [],
"params": {},
}

0 comments on commit c5d4dd6

Please sign in to comment.