English | 中文
🖖 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.
Supported languages
Methods
- 🍑 filterValue
- 🍐 getValue
- 🍎 resetValue
Install Module
$ npm i ofilterjs
Or pnpm, Or cnpm, Or yarn ...
$ pnpm i ofilterjs
Import Module
import ofjs from 'ofilterjs'
1. Data Filter
filterValue([Object{}], [Config], ...[extraData])
1.1 Filter or Recombine for data
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)
1.2 Set value
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)
1.3 Set default value
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)
1.4 Custom filter callback
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)
1.5 Merge data of filter return to result
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)
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)
2. Read Data
getValue([nameStr], [defaultValue])
2.1 Read Value / Deep Read for value
const data = {
lib: {
pkg: {
name: 'ofilterjs',
version: 10001
},
support: ['js', 'ts', 'es']
}
}
const name = ofjs.getValue(data, 'lib.pkg.name', 'unknown')
console.log(name)
2.2 Priority reading value
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)
2.3 Array index read
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)
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
const data = {
lib: {
pkg: {
name: 'ofilterjs',
alias: '',
version: 10001
},
support: ['js', 'ts', 'es'],
_private: 'private attr'
},
lib2: {
pkg: {
name: 'ofilter'
}
}
}
ofjs.resetValue(data, false)
Depth Reset
const data = {
lib: {
pkg: {
name: 'ofilterjs',
alias: '',
version: 10001
},
support: ['js', 'ts', 'es'],
_private: 'private attr'
},
lib2: {
pkg: {
name: 'ofilter'
}
}
}
ofjs.resetValue(data, true)
Depth Reset, Set depth layer parameter
const data = {
name: 'lib_list',
lib: {
type: 'npm',
pkg: {
name: 'ofilterjs',
alias: '',
version: 10001
},
support: {
'js' : 'javascript',
'ts' : 'typescript'
},
_private: 'private attr'
},
lib2 : {
type: 'npm',
pkg: {
name: 'ofilter'
}
}
}
ofjs.resetValue(data, true, 2)
Depth Reset, Set depth layer and start position parameter
const data = {
name: 'lib_list',
lib: {
type: 'npm',
pkg: {
name: 'ofilterjs',
alias: '',
version: 10001,
support: {
'js' : 'javascript',
'ts' : 'typescript'
}
},
_private: 'private attr'
},
lib2 : {
type: 'npm',
pkg: {
name: 'ofilter'
}
}
}
ofjs.resetValue(data, true, 2, 1)
3.2 Configuration reset field
const data = {
lib: {
pkg: {
name: 'ofilterjs',
alias: '',
version: 10001
},
support: ['js', 'ts', 'es']
}
}
ofjs.resetValue(data, [
'lib.pkg.name',
'lib.pkg.version'
])
3.3 Configuration set data
const data = {
lib: {
pkg: {
name: 'ofilterjs',
alias: '',
version: 10001
},
support: ['js', 'ts', 'es']
}
}
ofjs.resetValue(data, {
'lib.pkg.name': 'newname',
'lib.pkg.version': 10002
})
Buy the author coffee: http://witkeycode.com/sponsor
LICENSE
MIT