query-obj

0.1.0 • Public • Published

query-obj

A non-opinionated way of querying javascript objects.

Installation

npm install query-obj

Example

var Query = require('query-obj')
  , fns = {}
 
fns.isString = function(){
  return (typeof this.value == 'string')
}
 
Query()
.setMethods(fns)
.where('str')
  .isString()
  .error('type', 'Str should be a string')
 
.end({str: 100}, function(errors, attrs){
  console.log(errors.str)
  //= [ValidatorError]
  console.log(attrs.str.value)
  //= 100
})
.end({str: 'a string'}, function(errors, attrs){
  console.log(errors.str)
  //= undefined
  console.log(attrs.str.value)
  //= 'a string'
})
 

API

Query()

Initialize a new Query

.inherit(query)

Inherit chains, methods and setters from another query or similar object.

.setMethods(fns)

Specifies the methods available to chain on each .where() call.

Methods that return false will trigger any subsequent .error() calls on that specific .where() chain.

Methods that return true, will skip any .error() calls and continue to next chained method.

.where(attributeName)

Sets up a chain of methods for a particular attributeName. This chain will run when .end() is called.

.error(title, message)

Setup an error to fire if a previous method in a .where() chain return false.

._set(key, value)

Sets up a setter attribute with value.

On .end() the setter will be assigned to this query object.

.end(obj, fn)

Run any given query chains against obj.

Once finished fn is invoked with any Attribute errors, and attributes.

License

MIT

/query-obj/

    Package Sidebar

    Install

    npm i query-obj

    Weekly Downloads

    3

    Version

    0.1.0

    License

    none

    Last publish

    Collaborators

    • nickjackson