Skip to content

Commit 085bcb7

Browse files
committed
Docs: Add example of an extension function
1 parent f0332e6 commit 085bcb7

2 files changed

Lines changed: 10 additions & 6 deletions

File tree

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,14 +414,16 @@ const mapping = {
414414
}
415415
},
416416

417-
// property mapping from an array
418-
total: 'LINE_ITEMS.reduce((sum, { QTY, PRICE }) => sum + (QTY * PRICE), 0)'
417+
// property mapping from an array,
418+
// with a custom reducer function defined in `extensions`
419+
total: '$sum(LINE_ITEMS, i => i.QTY * i.PRICE)'
419420
};
420421

421422
// Pre-compiled function that can be executed any number of times
422423
const mapper = createMapper(mapping, {
423424
extensions: {
424-
itemCatalog
425+
itemCatalog,
426+
$sum: (arr, cb) => arr.reduce((t, el) => t + cb(el), 0)
425427
}
426428
});
427429

tests/unit/example.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,16 @@ describe('example', () => {
7474
}
7575
},
7676

77-
// property mapping from an array
78-
total: 'LINE_ITEMS.reduce((sum, { QTY, PRICE }) => sum + (QTY * PRICE), 0)'
77+
// property mapping from an array,
78+
// with a custom reducer function defined in `extensions`
79+
total: '$sum(LINE_ITEMS, i => i.QTY * i.PRICE)'
7980
};
8081

8182
// Pre-compiled function that can be executed any number of times
8283
const mapper = createMapper(mapping, {
8384
extensions: {
84-
itemCatalog
85+
itemCatalog,
86+
$sum: (arr, cb) => arr.reduce((t, el) => t + cb(el), 0)
8587
}
8688
});
8789

0 commit comments

Comments
 (0)