File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -40,33 +40,38 @@ That is where Declarative Mapper came in:
4040
4141``` ts
4242import { createMapper } from ' declarative-mapper' ;
43- import { expect } from ' chai' ;
4443
45-
46- const sourceObject = {
47- foo: ' bar'
48- };
49-
50- const mappingInstructions = {
51- myObj: {
52- myField: ' foo' ,
53- myFieldLength: ' foo.length'
44+ // Source records from system A
45+ const sourceOrders = [
46+ {
47+ orderId: ' SO-1001' ,
48+ customerName: ' Acme Ltd' ,
49+ lineItems: [{ sku: ' A-1' , qty: 2 , unitPrice: 10 }]
50+ },
51+ {
52+ orderId: ' SO-1002' ,
53+ customerName: ' Globex' ,
54+ lineItems: [{ sku: ' B-9' , qty: 1 , unitPrice: 25 }]
5455 }
55- };
56-
57-
58- // Convert declarative instructions to a pre-compiled
59- // function that can be executed any number of times
60- const mapper = createMapper (mappingInstructions );
61-
62- const result = mapper (sourceObject );
56+ ];
6357
64- expect (result ).to .eql ({
65- myObj: {
66- myField: ' bar' ,
67- myFieldLength: 3
68- }
58+ // Compile once
59+ const mapper = createMapper ({
60+ id: ' orderId' ,
61+ customer: ' customerName' ,
62+ items: {
63+ forEach: ' lineItems' ,
64+ map: {
65+ code: ' sku' ,
66+ quantity: ' qty' ,
67+ amount: ' qty * unitPrice'
68+ }
69+ },
70+ totalAmount: ' lineItems.reduce((sum, i) => sum + (i.qty * i.unitPrice), 0)'
6971});
72+
73+ // Reuse in a loop: this is the fast path (compiled script reused per record)
74+ const results = sourceOrders .map (mapper );
7075```
7176
7277## Mapping Instructions
You can’t perform that action at this time.
0 commit comments