Skip to content

Commit

Permalink
Add include precondition
Browse files Browse the repository at this point in the history
  • Loading branch information
cressie176 committed Mar 13, 2024
1 parent 9f69ef0 commit f517630
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to Semantic Versioning](https://semver.org/spec/v2.0.0.html)

## 4.2

- Add precondition to include to improve performance

## 4.1.1

- Add include to TypeScript definitions

## 4.1.0

- Add include processor
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,10 @@ Shallow copies the specified paths into a new log record. This is useful to avoi

It has the following options:

| name | type | required | default | notes |
| ----- | ----- | -------- | ------- | -------------------------------------------- |
| paths | array | no | [] | Specifies the paths of the fields to include |
| name | type | required | default | notes |
| ------------ | -------- | -------- | ---------- | ---------------------------------------------------------- |
| paths | array | no | [] | Specifies the paths of the fields to include |
| precondition | function | no | () => true | A function which must return true for the processor to run |

#### example

Expand Down
8 changes: 6 additions & 2 deletions lib/processors/include.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ const get = require("get-value");
const set = require("set-value");
const toPath = require("to-path");

const ALWAYS_PASS = () => true;

module.exports = (params = {}) => {
const { paths = [] } = params;
return ({ record }) => {
const { paths = [], precondition = ALWAYS_PASS } = params;
return ({ level, message, context, record }) => {
if (!precondition({ level, message, context, record })) return record;

const included = paths.reduce((acc, path) => {
if (!has(record, path, { split: getSplit })) return acc;
const value = get(record, path, { split: getSplit });
Expand Down
12 changes: 12 additions & 0 deletions test/processors/include.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,16 @@ describe("include", () => {
fn({ record });
eq(record, { a: { b: 1, c: 2 }, m: 1, x: { y: 1, z: 2 } });
});

it("should run the processor the precondition passes", () => {
const fn = include({ precondition: ({ record }) => record.a.b === 1, paths: ["a.b", "x"] });
const result = fn({ record: { a: { b: 1, c: 2 }, m: 1, x: { y: 1, z: 2 } } });
eq(result, { a: { b: 1 }, x: { y: 1, z: 2 } });
});

it("should bypass the processor the precondition fails", () => {
const fn = include({ precondition: () => false, paths: ["a.b", "x"] });
const result = fn({ record: { a: { b: 1, c: 2 }, m: 1, x: { y: 1, z: 2 } } });
eq(result, { a: { b: 1, c: 2 }, m: 1, x: { y: 1, z: 2 } });
});
});

0 comments on commit f517630

Please sign in to comment.