Skip to content

Commit

Permalink
refactor(transducers-patch): fix #256 replace enum
Browse files Browse the repository at this point in the history
BREAKING CHANGE: replace Patch enum w/ type alias,
update PatchArrayOp/PatchObjOp
  • Loading branch information
postspectacular committed Dec 22, 2020
1 parent ef7ba74 commit 5086a33
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 34 deletions.
21 changes: 8 additions & 13 deletions packages/transducers-patch/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import type { FnO, Path } from "@thi.ng/api";

export enum Patch {
SET,
INSERT,
UPDATE,
DELETE,
}
export type Patch = "set" | "insert" | "update" | "delete";

export type PatchArrayOp<T> =
| [Patch.SET, number, T]
| [Patch.INSERT, number, T[]]
| [Patch.UPDATE, number, FnO<T, T>, ...any[]]
| [Patch.DELETE, number];
| ["set", number, T]
| ["insert", number, T[]]
| ["update", number, FnO<T, T>, ...any[]]
| ["delete", number];

export type PatchObjOp =
| [Patch.SET, Path, any]
| [Patch.UPDATE, Path, FnO<any, any>, ...any[]]
| [Patch.DELETE, Path];
| ["set", Path, any]
| ["update", Path, FnO<any, any>, ...any[]]
| ["delete", Path];
14 changes: 7 additions & 7 deletions packages/transducers-patch/src/patch-array.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isNumber } from "@thi.ng/checks";
import { isString } from "@thi.ng/checks";
import { illegalArgs, illegalArity } from "@thi.ng/errors";
import { reduce, reducer, Reducer } from "@thi.ng/transducers";
import { Patch, PatchArrayOp } from "./api";
import type { PatchArrayOp } from "./api";

/**
* Reducer for {@link Patch} based array edits. Only numeric indices are
Expand Down Expand Up @@ -64,16 +64,16 @@ export function patchArray<T>(...args: any[]) {

const edit = (acc: T[], x: PatchArrayOp<T> | PatchArrayOp<T>[]) => {
switch (x[0]) {
case Patch.SET:
case "set":
acc[x[1]] = x[2];
break;
case Patch.UPDATE:
case "update":
acc[x[1]] = x[2](acc[x[1]], ...x.slice(3));
break;
case Patch.INSERT:
case "insert":
acc.splice(x[1], 0, ...x[2]);
break;
case Patch.DELETE:
case "delete":
acc.splice(x[1], 1);
break;
default:
Expand All @@ -88,7 +88,7 @@ export function patchArray<T>(...args: any[]) {
() => [],
(acc, x) => {
immutable && (acc = acc.slice());
if (isNumber(x[0])) {
if (isString(x[0])) {
acc = edit(acc, x);
} else {
for (let e of <PatchArrayOp<T>[]>x) {
Expand Down
12 changes: 6 additions & 6 deletions packages/transducers-patch/src/patch-obj.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { isNumber } from "@thi.ng/checks";
import { isString } from "@thi.ng/checks";
import { illegalArgs } from "@thi.ng/errors";
import { deleteInUnsafe, setInUnsafe, updateInUnsafe } from "@thi.ng/paths";
import { reduce, reducer, Reducer } from "@thi.ng/transducers";
import { Patch, PatchObjOp } from "./api";
import type { PatchObjOp } from "./api";

/**
* Reducer for {@link Patch} based immutable object updates.
Expand All @@ -18,11 +18,11 @@ export function patchObj(init: any, patches: Iterable<PatchObjOp>): any;
export function patchObj(init?: any, patches?: Iterable<PatchObjOp>) {
const edit = (acc: any, x: PatchObjOp) => {
switch (x[0]) {
case Patch.SET:
case "set":
return setInUnsafe(acc, x[1], x[2]);
case Patch.UPDATE:
case "update":
return updateInUnsafe(acc, x[1], x[2], ...x.slice(3));
case Patch.DELETE:
case "delete":
return deleteInUnsafe(acc, x[1]);
default:
illegalArgs(`patch op: ${x}`);
Expand All @@ -33,7 +33,7 @@ export function patchObj(init?: any, patches?: Iterable<PatchObjOp>) {
: reducer<any, PatchObjOp>(
() => <any>{},
(acc, x) => {
if (isNumber(x[0])) {
if (isString(x[0])) {
acc = edit(acc, x);
} else {
for (let e of <PatchObjOp[]>x) {
Expand Down
16 changes: 8 additions & 8 deletions packages/transducers-patch/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { reduce, reductions } from "@thi.ng/transducers";
import * as assert from "assert";
import { Patch, patchArray, patchObj } from "../src";
import { patchArray, patchObj } from "../src";

describe("transducers-patch", () => {
it("patchArray", () => {
Expand All @@ -9,10 +9,10 @@ describe("transducers-patch", () => {
reductions(patchArray<number>()),
[[1, 2, 3]],
[
[Patch.INSERT, 0, [10, 11]],
[Patch.UPDATE, 1, (x, n) => x * n, 10],
[Patch.DELETE, 3],
[Patch.SET, 2, 200],
["insert", 0, [10, 11]],
["update", 1, (x, n) => x * n, 10],
["delete", 3],
["set", 2, 200],
]
),
[
Expand All @@ -31,9 +31,9 @@ describe("transducers-patch", () => {
reductions(patchObj()),
[{ x: 23 }],
[
[Patch.SET, ["a", "b"], 1],
[Patch.UPDATE, "a.b", (x, n) => x + n, 10],
[Patch.DELETE, "x"],
["set", ["a", "b"], 1],
["update", "a.b", (x, n) => x + n, 10],
["delete", "x"],
]
),
[
Expand Down

0 comments on commit 5086a33

Please sign in to comment.