GitHub - azer/new-list: Array-like objects with PubSub interfaces that you can subscribe to changes.

1 min read Original article ↗

Array-like objects with PubSub interfaces that you can subscribe to changes. See also: new-object

List = require('new-list')
todo = List('Buy milk', 'Take shower')

todo.pop()
todo.push('Cook Dinner')
todo.splice(0, 1, 'Buy Milk And Bread')

todo(0)
// => 'Buy Milk and Break'
todo(1)
// => 'Take shower'
todo(1, 'Take a long shower')
todo(1)
// => 'Take a long shower'
todo.len()
// => 2
todo()
// => ['Buy Milk and Bread', 'Take a long shower']

todo.subscribe(function(update){ // or todo.subscribe.once

  update.add
  // => { 0: 'Buy Milk And Bread', 1: 'Cook Dinner' }

  update.remove
  // => [0, 1]

  update.sort
  // => undefined

})
people = List({ name: 'Joe', age: 27 }, { name: 'Smith', age: 19 })

people.subscribe(function(update){

  if (update.person) {

    update.index
    // => 1

    update.person
    // => { name: 'Smith', age: 20 }

  }

})

people[1].age = 20
people.publish({ person: people[1], index: 1 })
fruits = List('Apple', 'Orange', 'Banana')

i = fruits.len()

while( i --> 0 ) {

  fruits(i)
  // => Apple (or Orange, Banana)

  fruits.array[i]
  // => Apple  (or Orange, Banana)

}