Gathering detailed insights and metrics for object-rewrite
Gathering detailed insights and metrics for object-rewrite
Gathering detailed insights and metrics for object-rewrite
Gathering detailed insights and metrics for object-rewrite
@huntersofbook/schob
Rewrite the new object according to the schema. Delete the excess.
babel-plugin-react-native-style-rewrite
rewrite react-native style plain Object to StyleSheet object
schob
Rewrite the new object according to the schema. Delete the excess.
sql-view
Rewrite a select statement embedding a filter, sort, group or pagination using an otions object
npm install object-rewrite
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
1 Stars
2,435 Commits
4 Forks
1 Watchers
4 Branches
5 Contributors
Updated on Jan 29, 2025
Latest Version
12.0.1
Package Id
object-rewrite@12.0.1
Unpacked Size
28.46 kB
Size
9.04 kB
File Count
18
NPM Version
10.9.2
Node Version
22.13.1
Published on
Jan 29, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
4
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.
npm i --save object-rewrite
1import { 2 injectPlugin, 3 filterPlugin, 4 sortPlugin, 5 rewriter 6} from 'object-rewrite'; 7 8const queryDataStore = (fields) => { /* ... */ }; 9 10const inject = injectPlugin({ 11 target: 'idNeg', 12 requires: ['id'], 13 fn: ({ value }) => -value.id 14}); 15const filter = filterPlugin({ 16 target: '*', 17 requires: ['idNeg'], 18 fn: ({ value }) => [-2, -1].includes(value.idNeg) 19}); 20const sort = sortPlugin({ 21 target: '*', 22 requires: ['idNeg'], 23 fn: ({ value }) => value.idNeg 24}); 25const rew = rewriter({ '': [inject, filter, sort] }, ['id']); 26 27const desiredFields = ['id']; 28const rewInstance = rew.init(desiredFields, {/* init context */}); 29 30const data = queryDataStore(rewInstance.fieldsToRequest); 31// data => [{ id: 0 }, { id: 1 }, { id: 2 }] 32 33rewInstance.rewrite(data); 34// data => [{ id: 2 }, { id: 1 }]
Please see the tests for more in-depth examples on how to use this library.
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 typefn
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 disabledonRewrite({ data, context, cache })
Function (optional): if present called once per rewrite, used to update cache, if returns other than true
, the plugin is disabledschema
: 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 entityvalue
is the value of the processed entityparents
are the parents of the processed entitycontext
is global as passed into the executioncache = {}
is locally defined per pluginschema.fnInput
(optional) is used to validate value before passed into fn
schema.fnOutput
is used by the inject plugin onlyUsed to inject data
target
: field that is created or overwritten, can be '*'
requires
: See abovefn
: return value is used for target. Relative to prefixschema.fnOutput
: Object schema structure of what is being injected (strict result of fn
)Used to filter arrays
target
: array that should be filteredrequired
: See abovefn
: target is removed iff function returns false
. Similar to
Array.filter(). Relative to targetUsed to sort arrays
target
: array that should be sortedrequired
: See abovefn
: called for each object in array. Final array is sorted using the result. Relative to targetlimit
: 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
1import { injectPlugin, rewriter } from 'object-rewrite'; 2 3const plugin = injectPlugin(/* ... */); 4 5rewriter({ 6 '': [plugin], 7 nodes: [plugin] 8}, [/* 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.
Under the hood this library uses object-scan. Please refer to the docs for what key pattern are supported.
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.
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.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
4 existing vulnerabilities detected
Details
Reason
Found 9/18 approved changesets -- score normalized to 5
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More