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

@barelyhuman/range

v0.1.5

Published

<p align="center"> <img src="images/range.png" height="64"> <p align="center">availability ranges as a tiny little library</p>

Downloads

16

Readme

CI

NOTE: Still adding more and more cases and fixes, feel free to raise issues as you find them

When working with date based bookings and orders, it's mostly preferred to have the ranges and checks on the DB by using a timeseries database or by simplifying query by setting up good database schemas. Still, sometimes you need the date based checks on the application layer as well and this library was built to help with that.

Also, I wanted to build one.

Highlights

  • Tiny (upto 543B minified)
  • Zero Deps

Who is this for?

This is for others to build libraries on top of, you can use this as is but it's written in a way that there are close to no abstractions on the prototypes and can be scaled or masked as needed.

Usage

# install
npm i @barelyhuman/range
// esm
import { createRange } from '@barelyhuman/range'
// cjs
const { createRange } = require('@barelyhuman/range')

Example

You can find more such examples in the tests files.

The below goes through most of the API

import { createRange } from '@barelyhuman/range'

// decide the start and end of the range
const start = new Date()
const end = new Date()

// start of day
start.setHours(0, 0, 0, 0)

// end of the next day
end.setDate(end.getDate() + 1)
end.setHours(23, 59, 59, 0)

// create a range object out of it
const range = createRange(start, end)

// decide what part of the range to be blocked
const blockStart = new Date(start)
const blockEnd = new Date(start)

blockStart.setHours(10, 0, 0, 0)
blockEnd.setHours(18, 0, 0, 0)

range.beforeChange(({ available }) => {
  // triggered before a change is done to the ranges
  // `available` is the ranges before the change is done
})

range.afterChange(({ available, effectedRanges, changed }) => {
  // only triggered  **if** a range is changed
  // `changed` is always true here
})

// attempt a block on the range
// should break the original range of today-00:00:00 - tomorrow-23:59:59
// into 2 parts
// [today-00:00:00,today-10:00:00] and [today-18:00:00,tomorrow-23:59:59]
const blocked = range.block(blockStart, blockEnd)

// `blocked.changed` will be true if a range was changed / effected
assert.ok(blocked.changed)
// `blocked.effectedRanges` is an array of the ranges that were effected
assert.equal(blocked.effectedRanges.length, 1)

// you can be verbose and check if the split up ranges are valid.
assert.equal(range.available[0].start.valueOf(), start.valueOf())
assert.equal(range.available[0].end.valueOf(), blockStart.valueOf())

assert.equal(range.available[1].start.valueOf(), blockEnd.valueOf())
assert.equal(range.available[1].end.valueOf(), end.valueOf())

Inclusions

since v0.1.4

The library allows modifying the comaprison and blocking mechanism by passing in inclusion patterns. [] include both start and end into the comparison and also in the blocking ex: If you have a date range from 12:30AM - 2:30AM it can be represented as so

RANGE: 00:30:00 - 02:30:00

By default, the inclusivity is to include both start and end so if you create a block for let's say 1:00AM - 1:30AM the ranges would split to

RANGE: 00:30:00-00:59:00
RANGE: 01:31:00-02:30:00

Which, means I can no longer overlap on 1:00 or 1:30 , this is the default behaviour when working with booking type systems.

If I wish to allow there to be overlaps on the blocking times then I can just send () exclusion flags to it and the ranges would be like so.

RANGE: 00:30:00-01:00:00
RANGE: 01:30:00-02:30:00

a.k.a, I can overlap on the 1:00 and 1:30 minutes while using another blocker

Possible flags

  • [] - include both
  • (] - exclude start, include end
  • [) - include start, exclude end
  • () - exclude both

Example

const range = createRange(rangeStart, rangeEnd)
range.block(blockStart, blockEnd, '[]')

License

MIT