fluid-class

1.0.7 • Public • Published

fluid-class

NPM version Linux macOS Windows compatible

Build status Coverage status Dependency status Renovate enabled

Open in Gitpod

This Node.js module allows to define fluid classes in a simple, declarative way, without the boilerplate code. Fluid interfaces are similar to the moment.js API, for example.

Install

# NPM 
$ npm install fluid-class
 
# Yarn 
$ yarn add fluid-class

Usage

query()
  .where(x => x > 5)
  .sort('name')
  .fields(['name', 'description'])
  .exec()
const { fluidClass, property, object } = require('fluid-class')
 
const A = fluidClass()
  // simple property
  .property('prop', property())
 
  // property with default value
  .property('propWithDefault', property().defaultValue('john'))
 
  // property with transformer
  .property('propTransformed', property().transformer(x => x * 2))
 
  // object property
  .property('object', property().type(object()).defaultValue({}))
 
  // object property with key
  // (derives the key from the inner object instead of setting it as a parameter)
  .property('objectWithKey', property().type(object().key('name')).defaultValue({}))
 
  // methods
  // do not use arrow notation since we need the context via this
  .method('double', function () {
    return this.propTransformed() * 2
  })
  .create()
 
var a = new A()
a.prop('john').prop('doe') // set property foo to 'doe'
a.prop() // returns 'doe'
a.propWithDefault() // returns 'john', which is the default value
a.propTransformed(2)
a.propTransformed() // returns 4
a.object('foo', 'bar') // set object property to 'bar'
a.object('foo') // returns 'bar'
 
var Inner = fluidClass().property('name', property()).property('value', property()).create()
 
function inner() {
  return new Inner()
}
 
a.objectWithKey(inner().name('foo').value('bar'))
a.objectWithKey('foo') // returns the object above
 
a.double() // returns 8

License

Unless stated otherwise all works are:

Copyright © Sebastian Landwehr info@dword-design.de

and licensed under:

MIT License

Package Sidebar

Install

npm i fluid-class

Weekly Downloads

7

Version

1.0.7

License

MIT

Unpacked Size

9.77 kB

Total Files

5

Last publish

Collaborators

  • dword-design