1.0.7 • Published 4 years ago

fluid-class v1.0.7

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

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

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.2

5 years ago

0.1.1

6 years ago