0.4.151 • Published 4 months ago

@thi.ng/transducers-patch v0.4.151

Weekly downloads
45
License
Apache-2.0
Repository
github
Last release
4 months 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.151

4 months ago

0.4.150

4 months ago

0.4.149

4 months ago

0.4.148

4 months ago

0.4.146

5 months ago

0.4.145

5 months ago

0.4.147

4 months ago

0.4.144

5 months ago

0.4.142

5 months ago

0.4.143

5 months ago

0.4.141

6 months ago

0.4.140

6 months ago

0.4.139

6 months ago

0.4.138

7 months ago

0.4.137

8 months ago

0.4.136

8 months ago

0.4.129

10 months ago

0.4.131

10 months ago

0.4.130

10 months ago

0.4.135

9 months ago

0.4.134

9 months ago

0.4.133

9 months ago

0.4.132

9 months ago

0.4.119

1 year ago

0.4.120

12 months ago

0.4.124

11 months ago

0.4.123

11 months ago

0.4.122

11 months ago

0.4.121

12 months ago

0.4.128

10 months ago

0.4.127

11 months ago

0.4.126

11 months ago

0.4.125

11 months ago

0.4.118

1 year ago

0.4.117

1 year ago

0.4.116

1 year ago

0.4.115

1 year ago

0.4.114

1 year ago

0.4.113

1 year ago

0.4.112

1 year ago

0.4.111

1 year ago

0.4.110

1 year ago

0.4.109

1 year ago

0.4.108

1 year ago

0.4.107

1 year ago

0.4.106

1 year ago

0.4.105

1 year ago

0.4.104

1 year ago

0.4.103

1 year ago

0.4.102

1 year ago

0.4.99

1 year ago

0.4.101

1 year ago

0.4.100

1 year ago

0.4.97

1 year ago

0.4.98

1 year ago

0.4.96

1 year ago

0.4.95

1 year ago

0.4.94

1 year ago

0.4.93

1 year ago

0.4.91

1 year ago

0.4.92

1 year ago

0.4.90

1 year ago

0.4.89

1 year ago

0.4.88

1 year ago

0.4.86

1 year ago

0.4.87

1 year ago

0.4.84

2 years ago

0.4.85

2 years ago

0.4.83

2 years ago

0.4.82

2 years ago

0.4.81

2 years ago

0.4.80

2 years ago

0.4.75

2 years ago

0.4.73

2 years ago

0.4.74

2 years ago

0.4.71

2 years ago

0.4.72

2 years ago

0.4.70

2 years ago

0.4.79

2 years ago

0.4.77

2 years ago

0.4.78

2 years ago

0.4.64

2 years ago

0.4.65

2 years ago

0.4.62

2 years ago

0.4.63

2 years ago

0.4.60

2 years ago

0.4.61

2 years ago

0.4.68

2 years ago

0.4.69

2 years ago

0.4.66

2 years ago

0.4.67

2 years ago

0.4.54

2 years ago

0.4.51

2 years ago

0.4.52

2 years ago

0.4.59

2 years ago

0.4.57

2 years ago

0.4.58

2 years ago

0.4.55

2 years ago

0.4.56

2 years ago

0.4.50

2 years ago

0.4.49

2 years ago

0.4.48

2 years ago

0.4.47

2 years ago

0.4.42

2 years ago

0.4.43

2 years ago

0.4.41

2 years ago

0.4.46

2 years ago

0.4.44

2 years ago

0.4.45

2 years ago

0.4.40

2 years ago

0.4.39

2 years ago

0.4.38

2 years ago

0.4.36

2 years ago

0.4.31

3 years ago

0.4.32

3 years ago

0.4.35

2 years ago

0.4.33

3 years ago

0.4.34

2 years ago

0.4.30

3 years ago

0.4.21

3 years ago

0.4.28

3 years ago

0.4.29

3 years ago

0.4.26

3 years ago

0.4.27

3 years ago

0.4.24

3 years ago

0.4.25

3 years ago

0.4.22

3 years ago

0.4.23

3 years ago

0.4.20

3 years ago

0.4.19

3 years ago

0.4.18

3 years ago

0.4.17

3 years ago

0.4.15

3 years ago

0.4.16

3 years ago

0.4.13

3 years ago

0.4.14

3 years ago

0.4.11

3 years ago

0.4.12

3 years ago

0.4.9

3 years ago

0.4.8

3 years ago

0.4.10

3 years ago

0.4.7

3 years ago

0.4.6

3 years ago

0.4.5

3 years ago

0.3.8

4 years ago

0.4.4

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.3.7

4 years ago

0.3.6

4 years ago

0.3.4

4 years ago

0.3.0

4 years ago

0.3.1

4 years ago

0.3.3

4 years ago

0.2.30

4 years ago

0.2.29

4 years ago

0.2.28

4 years ago

0.2.27

4 years ago

0.2.26

4 years ago

0.2.25

4 years ago

0.2.24

4 years ago

0.2.23

4 years ago

0.2.22

4 years ago

0.2.21

4 years ago

0.2.20

4 years ago

0.2.19

4 years ago

0.2.18

4 years ago

0.2.17

4 years ago

0.2.16

4 years ago

0.2.15

4 years ago

0.2.14

4 years ago

0.2.13

4 years ago

0.2.12

4 years ago

0.2.11

4 years ago

0.2.7

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

5 years ago

0.1.33

5 years ago

0.1.32

5 years ago

0.1.31

5 years ago

0.1.30

5 years ago

0.1.29

5 years ago

0.1.28

5 years ago

0.1.27

5 years ago

0.1.26

5 years ago

0.1.25

5 years ago

0.1.24

5 years ago

0.1.22

5 years ago

0.1.23

5 years ago

0.1.21

5 years ago

0.1.20

5 years ago

0.1.19

5 years ago

0.1.18

5 years ago

0.1.17

5 years ago

0.1.16

5 years ago

0.1.15

5 years ago

0.1.14

5 years ago

0.1.13

5 years ago

0.1.12

5 years ago

0.1.11

5 years ago

0.1.10

5 years ago

0.1.9

5 years ago

0.1.8

5 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago