0.4.117 • Published 7 days ago

@thi.ng/transducers-patch v0.4.117

Weekly downloads
45
License
Apache-2.0
Repository
github
Last release
7 days ago

transducers-patch

npm version npm downloads Twitter Follow

This project is part of the @thi.ng/umbrella monorepo.

About

Reducers for patch-based, immutable-by-default array & object editing. This is a support package for @thi.ng/transducers.

The patchArray and patchObj reducers can be used in any reducer/transducer scenario and are useful for any form of declarative state update. By default all edits are performed non-destructively, but pushArray also supports in place editing (mutation).

Status

ALPHA - bleeding edge / work-in-progress

Search or submit any issues for this package

Installation

yarn add @thi.ng/transducers-patch

ES module import:

<script type="module" src="https://cdn.skypack.dev/@thi.ng/transducers-patch"></script>

Skypack documentation

For Node.js REPL:

# with flag only for < v16
node --experimental-repl-await

> const transducersPatch = await import("@thi.ng/transducers-patch");

Package sizes (gzipped, pre-treeshake): ESM: 519 bytes

Dependencies

API

Generated API docs

TODO

Basic usage

import { patchArray, patchObj } from "@thi.ng/transducers-patch";
import { reduce, reductions } from "@thi.ng/transducers";

// flat array editing
patchArray(
    // pass false to perform in-place edits (else immutable updates)
    false,
    // orig array to edit
    [1, 2, 3],
    // edits
    [
        // set idx #0 to 42
        ["set", 0, 42],
        // update idx #1 (here: times 10)
        ["update", 1, (x, n) => x * n, 10],
        // insert values @ idx #2
        ["insert", 2, [10, 11]],
        // delete (remove) idx #3
        ["delete", 3]
    ]
);
// [ 42, 20, 10, 3 ]

// flat (immutable by default) array editing
reduce(
    // reductions() used here to obtain step-wise edit results
    reductions(patchArray<number>()),
    // original array (wrapped here only for `reductions`)
    [[1, 2, 3]],
    [
        ["insert", 0, [10, 11]],
        ["update", 1, (x, n) => x * n, 10],
        ["delete", 3],
        ["set", 2, 200]
    ]
);
// [
//     [1, 2, 3],
//     [10, 11, 1, 2, 3],
//     [10, 110, 1, 2, 3],
//     [10, 110, 1, 3],
//     [10, 110, 200, 3]
// ]

// nested (always immutable) object editing
// (uses @thi.ng/paths for edits)
reduce(
    reductions(patchObj()),
    [{ x: 23 }],
    [
        ["set", ["a", "b"], 1],
        ["update", ["a", "b"], (x, n) => x + n, 10],
        ["delete", ["x"]]
    ]
),
// [
//     { x: 23 },
//     { x: 23, a: { b: 1 } },
//     { x: 23, a: { b: 11 } },
//     { a: { b: 11 } }
// ]

Stream-based processing

.This example uses constructs from the @thi.ng/rstream package.

import { stream, trace } from "@thi.ng/rstream";

const initialState: any = { x: 23 };

// create transformed stream mapping patch commands to states
// since `patchArray` is only a reducer, we need to wrap it w/ the `scan` transducer
// see: https://docs.thi.ng/umbrella/transducers/modules.html#scan
export const state = stream<PatchObjOp>().transform(
    scan(patchObj(), initialState)
);

// add debug subscription
state.subscribe(trace("state: "));

state.next(["set", "a.b", 1]);
// state: { x: 23, a: { b: 1 } }

state.next(["update", ["a", "b"], (x, n)=> x + n, 10]);
// state: { x: 23, a: { b: 11 } }

state.next(["delete", "x"]);
// state: { a: { b: 11 } }

Authors

Karsten Schmidt

If this project contributes to an academic publication, please cite it as:

@misc{thing-transducers-patch,
  title = "@thi.ng/transducers-patch",
  author = "Karsten Schmidt",
  note = "https://thi.ng/transducers-patch",
  year = 2020
}

License

© 2020 - 2021 Karsten Schmidt // Apache Software License 2.0

0.4.117

7 days ago

0.4.116

10 days ago

0.4.115

12 days ago

0.4.114

21 days ago

0.4.113

24 days ago

0.4.112

1 month ago

0.4.111

1 month ago

0.4.110

1 month ago

0.4.109

1 month ago

0.4.108

2 months ago

0.4.107

2 months ago

0.4.106

2 months ago

0.4.105

2 months ago

0.4.104

2 months ago

0.4.103

2 months ago

0.4.102

2 months ago

0.4.99

2 months ago

0.4.101

2 months ago

0.4.100

2 months ago

0.4.97

2 months ago

0.4.98

2 months ago

0.4.96

3 months ago

0.4.95

3 months ago

0.4.94

3 months ago

0.4.93

3 months ago

0.4.91

3 months ago

0.4.92

3 months ago

0.4.90

3 months ago

0.4.89

3 months ago

0.4.88

3 months ago

0.4.86

4 months ago

0.4.87

4 months ago

0.4.84

5 months ago

0.4.85

5 months ago

0.4.83

5 months ago

0.4.82

5 months ago

0.4.81

5 months ago

0.4.80

5 months ago

0.4.75

6 months ago

0.4.73

6 months ago

0.4.74

6 months ago

0.4.71

6 months ago

0.4.72

6 months ago

0.4.70

6 months ago

0.4.79

5 months ago

0.4.77

6 months ago

0.4.78

6 months ago

0.4.64

8 months ago

0.4.65

8 months ago

0.4.62

8 months ago

0.4.63

8 months ago

0.4.60

8 months ago

0.4.61

8 months ago

0.4.68

7 months ago

0.4.69

7 months ago

0.4.66

7 months ago

0.4.67

7 months ago

0.4.54

9 months ago

0.4.51

10 months ago

0.4.52

9 months ago

0.4.59

8 months ago

0.4.57

9 months ago

0.4.58

8 months ago

0.4.55

9 months ago

0.4.56

9 months ago

0.4.50

11 months ago

0.4.49

12 months ago

0.4.48

1 year ago

0.4.47

1 year ago

0.4.42

1 year ago

0.4.43

1 year ago

0.4.41

1 year ago

0.4.46

1 year ago

0.4.44

1 year ago

0.4.45

1 year ago

0.4.40

1 year ago

0.4.39

1 year ago

0.4.38

1 year ago

0.4.36

1 year ago

0.4.31

1 year ago

0.4.32

1 year ago

0.4.35

1 year ago

0.4.33

1 year ago

0.4.34

1 year ago

0.4.30

1 year ago

0.4.21

2 years ago

0.4.28

1 year ago

0.4.29

1 year ago

0.4.26

2 years ago

0.4.27

2 years ago

0.4.24

2 years ago

0.4.25

2 years ago

0.4.22

2 years ago

0.4.23

2 years ago

0.4.20

2 years ago

0.4.19

2 years ago

0.4.18

2 years ago

0.4.17

2 years ago

0.4.15

2 years ago

0.4.16

2 years ago

0.4.13

2 years ago

0.4.14

2 years ago

0.4.11

2 years ago

0.4.12

2 years ago

0.4.9

2 years ago

0.4.8

2 years ago

0.4.10

2 years ago

0.4.7

2 years ago

0.4.6

2 years ago

0.4.5

2 years ago

0.3.8

2 years ago

0.4.4

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago

0.4.3

2 years ago

0.4.2

2 years ago

0.3.7

2 years ago

0.3.6

3 years ago

0.3.4

3 years ago

0.3.0

3 years ago

0.3.1

3 years ago

0.3.3

3 years ago

0.2.30

3 years ago

0.2.29

3 years ago

0.2.28

3 years ago

0.2.27

3 years ago

0.2.26

3 years ago

0.2.25

3 years ago

0.2.24

3 years ago

0.2.23

3 years ago

0.2.22

3 years ago

0.2.21

3 years ago

0.2.20

3 years ago

0.2.19

3 years ago

0.2.18

3 years ago

0.2.17

3 years ago

0.2.16

3 years ago

0.2.15

3 years ago

0.2.14

3 years ago

0.2.13

3 years ago

0.2.12

3 years ago

0.2.11

3 years ago

0.2.7

3 years ago

0.2.6

3 years ago

0.2.5

3 years ago

0.2.4

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.33

3 years ago

0.1.32

3 years ago

0.1.31

3 years ago

0.1.30

4 years ago

0.1.29

4 years ago

0.1.28

4 years ago

0.1.27

4 years ago

0.1.26

4 years ago

0.1.25

4 years ago

0.1.24

4 years ago

0.1.22

4 years ago

0.1.23

4 years ago

0.1.21

4 years ago

0.1.20

4 years ago

0.1.19

4 years ago

0.1.18

4 years ago

0.1.17

4 years ago

0.1.16

4 years ago

0.1.15

4 years ago

0.1.14

4 years ago

0.1.13

4 years ago

0.1.12

4 years ago

0.1.11

4 years ago

0.1.10

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago