Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wenlng/ofilter-js
π OfilterJs is a data object filter processor for Javascript, which provides simpler, more convenient and more efficient data operations for development.
https://github.com/wenlng/ofilter-js
es6 filter javascript json json-filter object object-filter ofilter typescript
Last synced: about 2 months ago
JSON representation
π OfilterJs is a data object filter processor for Javascript, which provides simpler, more convenient and more efficient data operations for development.
- Host: GitHub
- URL: https://github.com/wenlng/ofilter-js
- Owner: wenlng
- License: mit
- Created: 2022-11-11T11:09:27.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2022-12-21T05:31:30.000Z (about 2 years ago)
- Last Synced: 2024-08-11T00:47:43.274Z (5 months ago)
- Topics: es6, filter, javascript, json, json-filter, object, object-filter, ofilter, typescript
- Language: JavaScript
- Homepage:
- Size: 167 KB
- Stars: 12
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
> English | [δΈζ](README_zh.md)
π OfilterJs is a data object filter processor for Javascript, which provides simpler, more convenient and more efficient data operations for development.
βοΈ If it helps you, please give a star.
- [https://github.com/wenlng/ofilter-js](https://github.com/wenlng/ofilter-js)
### Supported languages
- Javascript
- TypeScript### Methods
- π `filterValue`
- π `getValue`
- π `resetValue`### Install Module
``` shell
$ npm i ofilterjs
```
Or `pnpm`, Or `cnpm`, Or `yarn` ...
``` shells
$ pnpm i ofilterjs
```### Import Module
``` ts
const ofjs = require('ofilterjs')
// import ofjs from 'ofilterjs'
```
### Browser
```htmlconsole.log(ofilterjs)
```
### 1. Data Filter
> filterValue([Object{}], [Config], ...[extraData])#### 1.1 Filter or Recombine for data
``` ts
const data = {
lib: {
pkg: {
name: 'ofilterjs',
alias: '',
version_number: 10001
}
}
}const newData = ofjs.filterValue(data, {
name: 'lib.pkg.name',
versionNumber: 'lib.pkg.version_number',
})
console.log(newData)/** result
newData = {
name: 'ofilterjs',
versionNumber: 10001
}
*/
```#### 1.2 Set value
``` ts
const data = {
lib: {
pkg: {
name: 'ofilterjs',
alias: '',
version_number: 10001
}
}
}const newData = ofjs.filterValue(data, {
name: 'lib.pkg.name',
type: {
value: 'type value'
}
})
console.log(newData)/** result
newData = {
name: 'ofilterjs',
type: 'type value'
}
*/
```#### 1.3 Set default value
``` ts
const data = {
lib: {
pkg: {
name: 'ofilterjs',
alias: '',
version_number: 10001
}
}
}const newData = ofjs.filterValue(data, {
name: 'lib.pkg.name',
alias: {
key: 'lib.pkg.alias',
default: 'Default alias'
},
type: {
key: 'lib.pkg.type',
default: 'Npm pkg'
}
})
console.log(newData)/** result
newData = {
name: 'ofilterjs',
alias: 'Default alias',
type: 'Npm pkg'
}
*/
```#### 1.4 Custom filter callback
``` ts
const data = {
lib: {
pkg: {
name: 'ofilterjs',
alias: '',
version_number: 10001
}
}
}const newData = ofjs.filterValue(data, {
name: 'lib.pkg.name',
alias: {
key: 'lib.pkg.alias',
filter: (value, source) => {
if (value !== '') return value
return 'This is ' + (source?.lib?.pkg?.name || 'unknown')
}
},
type: {
key: 'lib.pkg.type',
filter: (value, source) => {
return 'Filter npm'
}
}
})
console.log(newData)/** result
newData = {
name: 'ofilterjs',
alias: 'This is ofilterjs',
type: 'Filter npm'
}
*/
```#### 1.5 Merge data of filter return to result
``` ts
const data = {
lib: {
pkg: {
name: 'ofilterjs',
alias: '',
version_number: 10001
}
}
}const newData = ofjs.filterValue(data, {
name: 'lib.pkg.name',
_: {
merge: true,
filter: (_, source) => {
if (source?.lib?.pkg?.name === 'ofilterjs') {
return {
support: ['js', 'ts', 'es']
}
}
return {}
}
},
_1: {
merge: true,
filter: (_, source) => {
return { more: 'more data ...' }
}
},
}
})
console.log(newData)/** result
newData = {
name: 'ofilterjs',
support: ['js', 'ts', 'es'],
more: 'more data ...'
}
*/
```#### 1.6 Merge extra data to result
``` ts
const data = {
lib: {
pkg: {
name: 'ofilterjs',
alias: '',
version_number: 10001
}
}
}const newData = ofjs.filterValue(data, {
name: 'lib.pkg.name'
}
}, {
name1: 'ofilter'
}, {
name2: 'object filter'
})
console.log(newData)/** result
newData = {
name: 'ofilterjs',
name1: 'ofilter',
name2: 'object filter'
}
*/
```### 2. Read Data
> getValue([nameStr], [defaultValue])#### 2.1 Read Value / Deep Read for value
``` ts
const data = {
lib: {
pkg: {
name: 'ofilterjs',
version: 10001
},
support: ['js', 'ts', 'es']
}
}const name = ofjs.getValue(data, 'lib.pkg.name', 'unknown')
console.log(name) // ofilterjs
```#### 2.2 Priority reading value
``` ts
const data = {
lib: {
pkg: {
name: 'ofilterjs',
alias: '',
version: 10001
},
support: ['js', 'ts', 'es']
}
}const alias = ofjs.getValue(data, 'lib.pkg.alias|lib.pkg.name', 'unknown')
console.log(name) // ofilterjs
```
#### 2.3 Array index read
``` ts
const data = {
lib: {
pkg: {
name: 'ofilterjs',
alias: '',
version: 10001
},
support: ['js', 'ts', 'es']
}
}const su = ofjs.getValue(data, 'lib.support.0', 'unknown')
console.log(su) // js
```### 3. Reset Data
> resetValue([Object{}], [Config,?Optional])Tip: Prefixes with '_' attribute names will not be reset automatically.
#### 3.1 Auto set at data type
Shallow Reset
``` ts
const data = {
lib: {
pkg: {
name: 'ofilterjs',
alias: '',
version: 10001
},
support: ['js', 'ts', 'es'],
_private: 'private attr'
},
lib2: {
pkg: {
name: 'ofilter'
}
}
}ofjs.resetValue(data, false)
/** result
const data = {
lib: {},
lib2: {}
}
*/
```Depth Reset
``` ts
const data = {
lib: {
pkg: {
name: 'ofilterjs',
alias: '',
version: 10001
},
support: ['js', 'ts', 'es'],
_private: 'private attr'
},
lib2: {
pkg: {
name: 'ofilter'
}
}
}ofjs.resetValue(data, true)
/** result
const data = {
lib: {
pkg: {
name: '',
alias: '',
version: 0
},
support: [],
_private: 'private attr'
},
lib2: {
pkg: {
name: ''
}
}
}
*/
```Depth ResetοΌ Set depth layer parameter
``` ts
const data = {
// level 0
name: 'lib_list',
lib: {
// level 1
type: 'npm',
pkg: {
// level 2
name: 'ofilterjs',
alias: '',
version: 10001
},
support: {
'js' : 'javascript',
'ts' : 'typescript'
},
_private: 'private attr'
},
lib2 : {
type: 'npm',
pkg: {
name: 'ofilter'
}
}
}// 0 ~ (0+2)
ofjs.resetValue(data, true, 2)/** result
const data = {
// level 0
name: '', // Was reset
lib: {
// level 1
type: '', // Was reset
pkg: {
// level 2
name: 'ofilterjs',
alias: '',
version: 10001
},
support: {
'js' : 'javascript',
'ts' : 'typescript'
},
_private: 'private attr'
},
lib2 : {
type: '', // Was reset
pkg: {
name: 'ofilter'
}
}
}
*/
```Depth ResetοΌ Set depth layer and start position parameter
``` ts
const data = {
// level 0
name: 'lib_list',
lib: {
// level 1
type: 'npm',
pkg: {
// level 2
name: 'ofilterjs',
alias: '',
version: 10001,
support: {
// level 3
'js' : 'javascript',
'ts' : 'typescript'
}
},
_private: 'private attr'
},
lib2 : {
type: 'npm',
pkg: {
name: 'ofilter'
}
}
}// 1 ~ (1+2)
ofjs.resetValue(data, true, 2, 1)/** result
const data = {
// level 0
name: 'lib_list',
lib: {
// level 1
type: '', // Was reset
pkg: {
// level 2
name: '', // Was reset
alias: '', // Was reset
version: 0, // Was reset
support: {
// level 3
'js' : 'javascript',
'ts' : 'typescript'
}
},
_private: 'private attr'
},
lib2 : {
type: '', // Was reset
pkg: {
name: '' // Was reset
}
}
}
```#### 3.2 Configuration reset field
``` ts
const data = {
lib: {
pkg: {
name: 'ofilterjs',
alias: '',
version: 10001
},
support: ['js', 'ts', 'es']
}
}ofjs.resetValue(data, [
'lib.pkg.name',
'lib.pkg.version'
])/** result
const data = {
lib: {
pkg: {
name: '',
alias: '',
version: 0
},
support: ['js', 'ts', 'es']
}
}
*/
```#### 3.3 Configuration set data
``` ts
const data = {
lib: {
pkg: {
name: 'ofilterjs',
alias: '',
version: 10001
},
support: ['js', 'ts', 'es']
}
}// us ofilterjs
ofjs.resetValue(data, {
'lib.pkg.name': 'newname',
'lib.pkg.version': 10002
})/** result
const data = {
lib: {
pkg: {
name: 'newname',
alias: '',
version: 10002
},
support: ['js', 'ts', 'es']
}
}
*/
```> Buy the author coffee: [http://witkeycode.com/sponsor](http://witkeycode.com/sponsor)
## LICENSE
MIT