1.0.7 • Published 4 years ago
fluid-class v1.0.7
fluid-class
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: