object-functional

0.9.1 • Public • Published

js-object-functional

Object-Functional programming paradigm for JavaScript, including change isolation, observables, and frozen views.

Class-based Example

import {ObjectFunctional} from 'object-functional'

class CounterWithList extends ObjectFunctional ::
  asAction = this.init
  init(counter=0) ::
    this.counter = counter
    this.lst = Object.freeze @ @[] counter
    return this

  asAction = this.increment
  increment(howHigh=1) ::
    this.counter += howHigh
    this.lst = Object.freeze @ this.lst.concat @ this.counter
    return this

  asAction = this.decrement
  decrement(howLow=1) ::
    this.counter -= howLow
    this.lst = Object.freeze @ this.lst.concat @ this.counter
    return this

  last() ::
    return this.lst[this.lst.length - 1]


// ...

const obj = new CounterWithList().init()
obj.subscribe @ view =>
  console.log('Update:', {view})

setInterval @
  () => obj.increment()
  1000

Prototype-based Example

import {asFunctionalProto} from 'object-functional'

const createCounterWithList = asFunctionalProto @:
  counter: 0
  lst: Object.freeze @ []

  asAction: @{}
    increment(howHigh=1) ::
      this.counter += howHigh
      this.lst = Object.freeze @ this.lst.concat @ this.counter
      return this

    decrement(howLow=1) ::
      this.counter -= howLow
      this.lst = Object.freeze @ this.lst.concat @ this.counter
      return this

  last() ::
    return this.lst[this.lst.length - 1]



// ...

const obj = createCounterWithList()
obj.subscribe @ view =>
  console.log('Update:', {view})

setInterval @
  () => obj.increment()
  1000

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.9.1
    4
    • latest

Version History

Package Sidebar

Install

npm i object-functional

Weekly Downloads

4

Version

0.9.1

License

BSD-2-Clause

Unpacked Size

662 kB

Total Files

19

Last publish

Collaborators

  • shanewholloway