Skip to content

A library for cactus stack works on Node.js

License

Notifications You must be signed in to change notification settings

kei-g/cactus-stack.js

cactus-stack.js license npm

dependency maintenance quality GitHub CI (Build) GitHub CI (Coverage)

cactus-stack - A library for cactus stack works on Node.js

Installation

npm i cactus-stack --save

Usage

import { CactusStack, CactusStackError } from 'cactus-stack'

type Foo = {
  id: number
  name: string
}

const root = new CactusStack<Foo>()

const firstNode = root.push({
  id: 1,
  name: 'foo',
})

const secondNode = root.push({
  id: 2,
  name: 'bar',
})

const thirdNode = secondNode.push({
  id: 3,
  name: 'baz',
})

const baz = thirdNode.pop()
if (baz instanceof CactusStackError) {
  console.error(baz.message)
  process.exit(1)
}
console.assert(baz.id === 3)
console.assert(baz.name === 'baz')

const foo = firstNode.pop()
if (foo instanceof CactusStackError) {
  console.error(foo.message)
  process.exit(1)
}
console.assert(foo.id === 1)
console.assert(foo.name === 'foo')

const err = thirdNode.pop()
console.assert(err instanceof CactusStackError)