npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

evidence

v1.0.3

Published

Track the history of an object.

Downloads

424

Readme

evidence

Track the history of an object.

Build Status npm install

A stream that takes any objects written to it, and saves them into a history stack. Works in node, or with browserify.

Example

At its simplest, you simply write a value to the history, and it's saved as the latest state. Any previous values pushed further down the stack.

var evidence = require('evidence')

var instance = evidence()

instance.write({item: 'One'})
instance.get(0) // {item: 'One'}
instance.write({item: 'Two'})
instance.get(0) // {item: 'Two'}
instance.length // 2

But being a stream, you can pipe data directly to it:

var evidence = require('evidence')
  , stream = require('through')()

var instance = evidence()

stream.pipe(instance)
stream.write({item: 'One'})

instance.get(0) // {item: 'One'}

History is only saved if the last value written isn't the same as the item in the top of the stack. You can undo/redo by changing the head of the stack:

var evidence = require('evidence')

var instance = evidence()

instance.write({item: 'One'})
instance.write({item: 'Two'})
instance.write({item: 'Three'})

instance.offset++
instance.get(0) // {item: 'Two'}

instance.offset--
instance.get(0) // {item: 'Three'}

API

  • evidence([size]) -> Duplex Stream: Instantiates a new instance, and returns a duplex stream that saves objects written to it into its internal state, and emits that state so long as it's changed.
    • size (Number): The number of objects that should be saved before older objects are discarded. Defaults to 100.

Methods and Properties

  • instance.write([data]) - Write a new object to the stack. Any item written is deep-copied before insertion into the stack.
  • instance.get([index]) - Get the item saved at index, and get the last item written if index isn't provided. New items written are saved to the front of the stack, so index 0 is the latest, 1 is the item that was written prior to 0, and so on.

The following properties use getters and setters, so they won't be available in Internet Explorer 8:

  • instance.offset - Sets the new head element, or gets the current head. Any further get calls will act as though the element you set is 0; then 1 will be the item saved prior to the new 0, and so on.
  • instance.length - Get current length of the stack.

If you do need support for IE8, use these; they'll work in all browsers, and are what the properties above use to do their work:

  • instance.getOffset()
  • instance.setOffset(index)
  • instance.getLength()

Events

  • Emits a data event, like any stream. The value emitted is the last-written object. Any time the head element changes (like if the offset was changed), the new head element will be emitted.
  • Emits a truncated event when any item is truncated from the stack due to the stack outgrowing the specified size, or due to a new state being saved when an offset has been set. Emitted with the event will be an array of the removed items.
  • Emits an error event when the offset is set greater than the stack length, or less than 0.

Notes

  • For performance reasons, deep copy is implemented using JSON.parse(JSON.stringify(data)), which will fail for anything that cannot be encoded to or parsed from JSON.
  • When understanding this module, think about it in terms of undo/redo. Take the following example:
    1. You write three different states to the module. The length of the stack is now 3. The last state written is at the top of the stack.
    2. You increment the offset with offset++: this is like an "undo"; the head of the stack is now at the second to last item written.
    3. You decrement the offset with offset--: this is like a "redo"; the head of the stack is now at the item it was in step 1 above.
    4. You increment the head again with offset++, and then you write a new value to it. You lose the item that was originally at the head, since you've effectively done an "undo" step, and then written new history to the stack. In this process, the stream emits a truncate event that contains the item that was removed from history with your latest write.
  • Writing to a stack that has been offset will truncate any elements newer than offset when written to.
  • Trying to set an offset that is less than 0 or greater than the stack length will throw an error, and cause the stream to emit an 'error' event.

License

MIT. See LICENSE for details.