New:Microsoft Teams Notifications Are Now Available in Socket.Learn more
Get Started

ofilterjs

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ofilterjs

OfilterJs is a data object {} filter processor for Javascript, which provides simpler, more convenient and more efficient data operations for development.

Source
npmnpm
Version
1.0.1
Version published
Weekly downloads
86
207.14%
Maintainers
1
Weekly downloads
 
Created
Source

OfilterJs

Data filter processor of Javascript object{}


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

  • Javascript
  • TypeScript
  • ECMAScript 6+

Methods

  • 🍑 filterValue
  • 🍐 getValue
  • 🍎 resetValue

Install Module

$ npm i ofilterjs

Or pnpm、cnpm、yarn ...

$ pnpm i 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)

/** result
   newData = {
        name: 'ofilterjs',
        versionNumber: 10001
   } 
*/

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)

/** result
   newData = {
        name: 'ofilterjs',
        type: 'type value'
   } 
*/

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',
        dv: 'Default alias'
    },
    type: {
        key: 'lib.pkg.type',
        dv: 'Npm pkg'
    }
})
console.log(newData)

/** result
   newData = {
        name: 'ofilterjs',
        alias: 'Default alias',
        type: 'Npm pkg'
   } 
*/

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)

/** result
   newData = {
        name: 'ofilterjs',
        alias: 'This is ofilterjs',
        type: 'Filter npm'
   } 
*/

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 (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

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

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

const data = {
    lib: {
        pkg: {
            name: 'ofilterjs',
            alias: '',
            version: 10001
        },
        support: ['js', 'ts', 'es']
    }
}

const alias = ofjs.getValue('data.lib.pkg.alias|data.lib.pkg.name', 'unknown')
console.log(name)   // ofilterjs

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)   // js

3. Reset Data

resetValue([Object{}], [Config,?Optional])

3.1 Auto set at data type

const data = {
    lib: {
        pkg: {
            name: 'ofilterjs',
            alias: '',
            version: 10001
        },
        support: ['js', 'ts', 'es']
    }
}

ofjs.resetValue(data)

/**  result
const data = {
    lib: {
        pkg: {
            name: '',
            alias: '',
            version: 0
        },
        support: []
    }
}
*/

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'
])

/**  result
const data = {
    lib: {
        pkg: {
            name: '',
            alias: '',
            version: 0
        },
        support: ['js', 'ts', 'es']
    }
}
*/

3.3 Configuration set data

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


LICENSE

MIT

Keywords

ofilter

FAQs

Package last updated on 12 Nov 2022

Related posts