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

apl-timeline

v1.0.0

Published

Allow your data to change over a time period

Downloads

1

Readme

Timeline data

Wrap data in a timeline, where the value of the data changes over a range of time.

Features

  • Supports open-ended start/end dates
  • Customisable granularity (defaults to 'day')
  • Does not mutate entries
  • Computed timeline is cached so single entry lookups are fast
  • Typescript definitions provided
  • Full code coverage

How to use

You can supply any value to an entry, overlapping entries are automatically resolved. An example usage:

//optional configuration (defaults shown)
const timeline = new Timeline({
  dateFormat: 'YYYY-MM-DD',
  granularity: 'day'
}); 

timeline.addEntry({
  startDate: '2017-05-04', 
  endDate: '2019-06-09', 
  value: 'Test'
});

timeline.addEntry({
  startDate: '2018-04-23', 
  endDate: '2018-09-09', 
  value: 'New value'
});

timeline.getEntryOn('2018-03-04').value; //'Test'
timeline.getEntryOn('2018-05-12').value; //'New value'
timeline.getEntryOn('2019-03-04').value; //'Test'

timeline.getDateEntries(); 
/*
Returns:
[
  { startDate: '2017-05-04', endDate: '2018-04-22', value: 'Test' }, 
  { startDate: '2018-04-23', endDate: '2018-09-09', value: 'New value' }, 
  { startDate: '2018-09-10', endDate: '2019-06-09', value: 'Test' }
]
*/

Hints

  • Start and end dates are inclusive
  • Designed to treat the entries as immutable - add new entries to replace the old ones

Using in node

Install via npm with npm install apl-timeline, then import via ES6 Modules:

import { Timeline } from 'apl-timeline';

Using in the browser

This library has a dependency on moment.js. You can include it yourself and use the small minified file inside lib or at unpkg (4KB):

<script src="https://unpkg.com/[email protected]/min/moment-with-locales.min.js"></script>
<script src="http://unpkg.com/apl-timeline/lib/apl-timeline.min.js"></script>

If you are not already using moment, you can use the single umd bundle that includes all dependencies in the same package (260KB):

<script src="http://unpkg.com/apl-timeline/lib/apl-timeline.umd.js"></script>

API

Interfaces

TimelineEntry

Used to represent a value that changes over a start and end date/time

| Name | Type | Optional | Description | |---|---|---|---| | startDate | string | yes | The start date to use, should match format of dateFormat (inclusive) (optional) | | endDate | string | yes | The end date to use, should match format of dateFormat (inclusive) (optional) | | value | any | yes | The data that should be used between the two dates (optional) |

TimelineConfig

Used to customise the functionality of the timeline, all have defaults and can be optionally overwritten:

| Name | Type | Default | Description | |---|---|---|---| | dateFormat | string | 'YYYY-MM-DD' | Used for parsing and format new dates created for entries in the timeline | | granularity | string | 'day' | How specific the dates are for the value in the entry |

Classes

Timeline

The main class used to create and manage creating entries over a timeline.

| Method | Description | Paramaters | Return Type | |---|---|---|---| | constructor | Creates a new timeline instance | config: TimelineConfig (optional) | new Timeline | | addEntry | Add a new entry object to the timeline | entry: TimelineEntry | none | | getEntryOn | Gets a single entry for a specific date | date: string | entry: TimelineEntry | | getDateEntries | Gets a flat list of all of the entries ordered by date (earliest first) | none | arr: Array<TimelineEntry> |

How to

Add a default value for all entries on a timeline

Simply create a timeline and add an entry with the default value with no startDate or endDate.

Identify timeline entries with an id

The interface is designed to be as simple as possible with only start and end dates required, you can add an object to the 'value' field and assign an id to the object instead.

Licence

All code is licenced under MIT.