object-rewrite

11.0.1 • Public • Published

object-rewrite

Build Status Test Coverage Dependabot Status Dependencies NPM Downloads Semantic-Release Gardener

Rewrite Object(s) in place using plugins.

This library is used for doing complex in-memory modifications of data. It allows to define use cases in a dynamic way that allows for powerful abstraction.

Install

npm i --save object-rewrite

Getting Started

import {
  injectPlugin,
  filterPlugin,
  sortPlugin,
  rewriter
} from 'object-rewrite';

const queryDataStore = (fields) => { /* ... */ };

const inject = injectPlugin({
  target: 'idNeg',
  requires: ['id'],
  fn: ({ value }) => -value.id
});
const filter = filterPlugin({
  target: '*',
  requires: ['idNeg'],
  fn: ({ value }) => [-2, -1].includes(value.idNeg)
});
const sort = sortPlugin({
  target: '*',
  requires: ['idNeg'],
  fn: ({ value }) => value.idNeg
});
const rew = rewriter({ '': [inject, filter, sort] }, ['id']);

const desiredFields = ['id'];
const rewInstance = rew.init(desiredFields, {/* init context */});

const data = queryDataStore(rewInstance.fieldsToRequest);
// data => [{ id: 0 }, { id: 1 }, { id: 2 }]

rewInstance.rewrite(data);
// data => [{ id: 2 }, { id: 1 }]

Please see the tests for more in-depth examples on how to use this library.

Plugins

There are three types of plugins INJECT, FILTER and SORT.

All plugins define:

  • target String: target field relative to the plugin path.
  • required Array: required fields relative to the plugin path. Can specify relative to root by prefixing with /. Will influence fieldsToRequest. Can be specified as function that takes initContext and expected to return array.
  • beforeFn Function: executed before any fn execution happens for plugin type
  • fn Function: result of this function is used by the plugin. Signature is fn({ key, value, parents, context, cache }).
  • onInit({ context, cache }) Function (optional): if present called once per init, used to initialize cache, if returns other than true, the plugin is disabled
  • onRewrite({ data, context, cache }) Function (optional): if present called once per rewrite, used to update cache, if returns other than true, the plugin is disabled
  • schema: Object schema structure of form { initContext: {}, rewriteContext: {}, fnInput: {}, fnOutput: {} } of what is expected to be present in corresponding context (subset)

where:

  • key: is the key for the processed entity
  • value is the value of the processed entity
  • parents are the parents of the processed entity
  • context is global as passed into the execution
  • cache = {} is locally defined per plugin
  • schema.fnInput (optional) is used to validate value before passed into fn
  • schema.fnOutput is used by the inject plugin only

Inject Plugin

Used to inject data

  • target: field that is created or overwritten, can be '*'
  • requires: See above
  • fn: return value is used for target. Relative to prefix
  • schema.fnOutput: Object schema structure of what is being injected (strict result of fn)

Filter Plugin

Used to filter arrays

  • target: array that should be filtered
  • required: See above
  • fn: target is removed iff function returns false. Similar to Array.filter(). Relative to target

Sort Plugin

Used to sort arrays

  • target: array that should be sorted
  • required: See above
  • fn: called for each object in array. Final array is sorted using the result. Relative to target
  • limit: optional limit function that takes the context object as a kwarg and is expected to return a non-negative integer or undefined. If multiple sort plugins are defined, the smallest limit is used. If the limit function returns undefined it is ignored. If set, after sorting only the first limit entries are returned.

Only one sort plugin can be specified per target.

Allows for complex sort comparisons and uses cmp-fn.js under the hood (see source code).

rewriter(pluginMap: Object, dataStoreFields: Array)

Used to combine multiple plugins. Plugins can be re-used in different rewriters. Rewriters are then used to modify input data.

Constructor takes in an object that maps absolute paths to plugins and the available dataStoreFields. Could for example re-use a plugin as

import { injectPlugin, rewriter } from 'object-rewrite';

const plugin = injectPlugin(/* ... */);

rewriter({
  '': [plugin],
  nodes: [plugin]
}, [/* data store fields */]);

allowedFields: Array

Fields that are allowed to be requested.

init(fields: Array)

Initialize the rewriter for a specific set of fields.

fieldsToRequest

Exposes fields which should be requested from data store. Dynamically computed fields are excluded since they would not be present in the data store.

rewrite(data: Object/Array, context: Object = {})

Pass in object that should be rewritten. The context allows for additional data to be made available for all plugins.

Notes

Dynamic Keys

Under the hood this library uses object-scan. Please refer to the docs for what key pattern are supported.

Execution Order

Plugins are executed in the order INJECT, FILTER and then SORT.

Plugins within the same type are evaluated bottom-up. While this is less performant, it allows plugins to rely on previous executed plugins of the same type.

Plugins of the same type that operate on the same target are executed in order.

Async

To apply an async rewrite, please use

rewInstance.rewriteAsync(data);

Only plugin that can use an async fn in an Inject plugin. An async result is evaluated after all inject plugins have run, and hence can only be used in filter and sort plugins, but not in other inject plugins.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
11.0.1257latest

Version History

VersionDownloads (Last 7 Days)Published
11.0.1257
11.0.00
10.1.00
10.0.90
10.0.80
10.0.70
10.0.60
10.0.50
10.0.40
10.0.30
10.0.20
10.0.10
10.0.00
9.0.10
9.0.00
8.0.00
7.2.30
7.2.20
7.2.10
7.2.00
7.1.00
7.0.20
7.0.10
7.0.00
6.0.60
6.0.50
6.0.41
6.0.30
6.0.20
6.0.10
6.0.00
5.0.00
4.4.10
4.4.00
4.3.40
4.3.30
4.3.20
4.3.10
4.3.00
4.2.80
4.2.70
4.2.60
4.2.50
4.2.40
4.2.30
4.2.20
4.2.10
4.2.00
4.1.130
4.1.120
4.1.110
4.1.100
4.1.90
4.1.80
4.1.70
4.1.60
4.1.50
4.1.40
4.1.30
4.1.20
4.1.10
4.1.00
4.0.110
4.0.100
4.0.90
4.0.80
4.0.70
4.0.60
4.0.50
4.0.40
4.0.30
4.0.20
4.0.10
4.0.00
3.2.230
3.2.220
3.2.210
3.2.200
3.2.190
3.2.180
3.2.170
3.2.160
3.2.150
3.2.140
3.2.130
3.2.121
3.2.110
3.2.100
3.2.90
3.2.80
3.2.70
3.2.60
3.2.50
3.2.40
3.2.30
3.2.20
3.2.10
3.2.00
3.1.00
3.0.280
3.0.270
3.0.260
3.0.250
3.0.240
3.0.230
3.0.220
3.0.210
3.0.200
3.0.190
3.0.180
3.0.170
3.0.160
3.0.150
3.0.140
3.0.130
3.0.120
3.0.110
3.0.100
3.0.90
3.0.80
3.0.70
3.0.60
3.0.50
3.0.40
3.0.30
3.0.20
3.0.10
3.0.00
2.1.360
2.1.350
2.1.340
2.1.330
2.1.320
2.1.310
2.1.300
2.1.290
2.1.280
2.1.270
2.1.260
2.1.250
2.1.240
2.1.230
2.1.220
2.1.210
2.1.200
2.1.190
2.1.180
2.1.170
2.1.160
2.1.150
2.1.140
2.1.130
2.1.120
2.1.110
2.1.100
2.1.91
2.1.80
2.1.70
2.1.60
2.1.50
2.1.40
2.1.30
2.1.20
2.1.10
2.1.00
2.0.00
1.1.40
1.1.30
1.1.20
1.1.10
1.1.00
1.0.70
1.0.60
1.0.50
1.0.40
1.0.30
1.0.20
1.0.11
1.0.00

Package Sidebar

Install

npm i object-rewrite

Weekly Downloads

261

Version

11.0.1

License

MIT

Unpacked Size

29.1 kB

Total Files

18

Last publish

Collaborators

  • simlu