truekeys
TypeScript icon, indicating that this package has built-in type declarations

5.0.0 • Public • Published

truekeys

v5

  • TypeScript-ified
  • Renamed
  • Documentation
  • Clearer function definition.
  • Removed a lot of dev dependencies, including eslint and chai.

Find if a bunch of keys in an object or objects are true - node module

Utilizes Constraints Based Programming

CBP

Use case

This is common in javascript code:

if (req.body.email && req.body.username && req.body.password && req.body.passwordConf) {
    // do something
}

Becomes easier and cleaner as shown in Usage.

Usage

const {hasTruthyKeys: has} = require('truekeys')

const req = {
    body: {
        email: 'email',
        username: 'username',
        password: 'password',
        passwordConf: 'passwordConf'
    }
}

const passwordConf = [
    'email',
    'username',
    'password',
    'passwordConf'
]

has(req.body, hasPasswordConf) // true

If there are many such objects and interesting keys, use hasTruthyKeysMultiple function.

const {hasTruthyKeysMultiple} = require('truekeys')

const req = {
    body: {
        email: 'email',
        username: 'username',
        password: 'password',
        passwordConf: 'passwordConf'
    },
    headers: {
        safety: 'on'
    },
    attempt: 'success'
}

const array = [{
    obj: req,
    keys: [
        'attempt'
    ]
},
    {
        obj: req.body,
        keys: [
            'email',
            'username',
            'password',
            'passwordConf'
        ]
    },
    {
        obj: req.headers,
        keys: [
            'safety'
        ]
    }]

hasTruthyKeysMultiple(array) // true

Going deeper into a value

const hasNestedTruthyKeys = require('truekeys').hasNestedTruthyKeys

var obj = {items: [{issue: [1, 2, 3]}, {issue: [2, 3, 4]}]}

hasNestedTruthyKeys(obj, ['items', '0', 'issues', 'length'])
// false

hasNestedTruthyKeys(obj, ['items', '0', 'issue', 'length'])
// true

License

MIT © 2018 Git Faf

Readme

Keywords

none

Package Sidebar

Install

npm i truekeys

Weekly Downloads

1

Version

5.0.0

License

MIT

Unpacked Size

23.6 kB

Total Files

19

Last publish

Collaborators

  • iamanubhavsaini
  • git.faf
  • anusaini