Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/qard/breadth-filter
Breadth-first deep object filter
https://github.com/qard/breadth-filter
breadth filter nodejs object traversal
Last synced: 11 days ago
JSON representation
Breadth-first deep object filter
- Host: GitHub
- URL: https://github.com/qard/breadth-filter
- Owner: Qard
- Created: 2018-12-04T14:27:32.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-10-24T06:54:51.000Z (about 1 year ago)
- Last Synced: 2024-04-13T22:20:29.494Z (7 months ago)
- Topics: breadth, filter, nodejs, object, traversal
- Language: JavaScript
- Size: 43 KB
- Stars: 5
- Watchers: 3
- Forks: 2
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# breadth-filter
[![Greenkeeper badge](https://badges.greenkeeper.io/Qard/breadth-filter.svg)](https://greenkeeper.io/)
Apply a deep object filter via breadth traversal. It allows replacing values, including retargeting objects and arrays and even detecting circular references to allow the user to decide how to handle the circle. Note that the filter will _not_ descend _into_ circular objects but will only _reach_ them.
## Install
```sh
npm install breadth-filter
```## Example
```js
const breadthFilter = require('breadth-filter')const data = {
user: {
name: 'someone',
password: 'THIS SHOULD BE SECRET!!'
}
}function onValue (value, key, path) {
if (key === 'password') {
console.log('redacted field at', path.join('.'))
return '[redacted]'
}
return value
}// build a new filtered object...
const filtered = breadthFilter(data, {
onValue
})// or, mutate in-place and break cycles...
breadthFilter(data, {
onValue,
onObject (value, key, path, isNew) {
return isNew ? value : '[Circular]'
},
onArray (value, key, path, isNew) {
return isNew ? value : '[Circular]'
}
})
```## Options
### onValue(value, key, path)
This function handles primitive value types such as numbers or strings. It can be used to filter things like sensitive data such as passwords or credit card numbers.
Arguments:
* `value` - any
The encountered value to filter.
* `key` - string | number
The property key of the encountered value, at the current depth level. Will be `null` when encountering the root object.
* `path` - array
Any array of all property keys leading from the root to this encountered value.### onObject(value, key, path, isNew)
This handles encountered objects. It can be used to filter entire objects out of the result, by returning `undefined`, it can enable in-place mutation by return the value directly, and the `isNew` property can be used to identify and break out of circular references, replacing them with something else such as `'[Circular]'`.
Arguments:
* `value` - any
The encountered value to filter.
* `key` - string | number
The property key of the encountered value, at the current depth level. Will be `null` when encountering the root object.
* `path` - array
Any array of all property keys leading from the root to this encountered value.
* `isNew` - boolean
Indicates if this is the first time encountering this value, a false value indicates a circular reference.### onArray(value, key, path, isNew)
This handles encountered arrays. It can be used to filter entire arrays out of the result, by returning `undefined`, it can enable in-place mutation by return the value directly, and the `isNew` property can be used to identify and break out of circular references, replacing them with something else such as `'[Circular]'`.
Arguments:
* `value` - any
The encountered value to filter.
* `key` - string | number
The property key of the encountered value, at the current depth level. Will be `null` when encountering the root object.
* `path` - array
Any array of all property keys leading from the root to this encountered value.
* `isNew` - boolean
Indicates if this is the first time encountering this value, a false value indicates a circular reference.---
### Copyright (c) 2019 Stephen Belanger
#### Licensed under MIT LicensePermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.