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

@servicetitan/temporal-lite

v3.1.0

Published

A lightweight, forward-compatible version of [Temporal](https://tc39.es/proposal-temporal/docs/). Its purpose is to reap the benefits of Temporal before it's released while providing a relatively seamless migration to Temporal when it's released.

Downloads

2,879

Readme

Temporal-lite

A lightweight, forward-compatible version of Temporal. Its purpose is to reap the benefits of Temporal before it's released while providing a relatively seamless migration to Temporal when it's released.

Benefits include:

  • timezone support
  • built-in immutability
  • differentiating between time-zoned date/time vs plain date/time
  • handles DST calculations
  • Intl compatible (in that it preserves epoch time)

This package also includes

  • custom Axios handlers to serialize/deserialize Temporal's ZonedDateTime and Duration for ServiceTitan APIs that use ServiceTitan's TypescriptCodeGen attribute with Temporal = true.

Sandbox

Feel free to play around with Temporal-lite here (does not include ServiceTitan-specific TemporalFactory, TemporalFormatter, or Axios handlers). Please report any bugs.

Usage

ZonedDateTime

import { ZonedDateTime } from '@servicetitan/temporal-lite';
ZonedDateTime.from({ timeZone: 'Asia/Taipei', year: 2022, month: 1, day: 1 });
// -> 2022-01-01T00:00+08:00

PlainDateTime

import { PlainDateTime } from '@servicetitan/temporal-lite';
new PlainDateTime(2022, 1, 1);
// -> 2022-01-01T00:00

PlainDate

import { PlainDate } from '@servicetitan/temporal-lite';
new PlainDate(2022, 1, 1);
// -> 2022-01-01

PlainTime

import { PlainTime } from '@servicetitan/temporal-lite';
new PlainTime(23, 30, 30, 30);
// -> 23:30:30.030

Now

import { Now } from '@servicetitan/temporal-lite';
Now.zonedDateTimeISO(); // -> 2022-08-17T00:00-08:00
Now.plainDateTimeISO(); // -> 2022-08-17T00:00
Now.plainDateISO(); // -> 2022-08-17
Now.plainTimeISO(); // -> 23:30:30.030

Duration

import { Duration } from '@servicetitan/temporal-lite';
const duration = Duration.from({ hours: 48 });
duration.total('day'); // -> 2
duration.total('hour'); // -> 48
duration.total('minute'); // -> 2880

Temporal Factory

const [{ getZonedDateTime, getZonedDateTimeNow, getPlainDateNow, getPlainTimeNow }] =
    useDependencies(TemporalFactory);

// results change depending on TemporalFactory's auto-calculation of time zone
const plainDateNow = getPlainDateNow();
const plainTimeNow = getPlainTimeNow();
const zonedDateTimeNow = getZonedDateTimeNow();
getZonedDateTime(plainDateNow, plainTimeNow);
getZonedDateTime(zonedDateTimeNow);
getZonedDateTime(new Date());

Common Scenario - weekly view

import { Duration } from '@servicetitan/temporal-lite';
const [{ getPlainDateNow }, { format }] = useDependencies(TemporalFactory, TemporalFormatter);
const today = getPlainDateNow();
const displayWeek = (Array(7).fill("") as PlainDate[])
    .map((_value, index) => {
        const current = todayPlainDate.add(Duration.from({ days: index }));
        return format(current, {
          month: "short",
          day: "numeric"
        });
    })
    .join(", ");
// America/Los_Angeles -> Aug 17, Aug 18, Aug 19, Aug 20, Aug 21, Aug 22, Aug 23
// Asia/Taipei -> Aug 18, Aug 19, Aug 20, Aug 21, Aug 22, Aug 23, Aug 24

const nextWeekStart = today.add(Duration.from({ weeks: 1 }); // America/Los_Angeles -> Aug 24
const previousWeekStart = today.add(Duration.from({ weeks: -1 }); // America/Los_Angeles -> Aug 10

Common Scenario - formstate

@injectable()
export class FormStore {
    formState = new FormState({
        startDate: new FieldState<PlainDate>(this.temporalFactory.getPlainDateNow()),
        endDate: new FieldState<PlainDate>(this.temporalFactory.getPlainDateNow()),
        startTime: new FieldState<PlainTime>(PlainTime.from({ hour: 9 })),
        endTime: new FieldState<PlainTime>(PlainTime.from({ hour: 17 })),
    });

    constructor(@inject(TemporalFactory) private temporalFactory: TemporalFactory) {
        makeObservable(this);
    }

    @computed
    get formData() {
        const { getZonedDateTime } = this.temporalFactory;
        const formData = formStateToJS(this.formState);
        return {
            start: getZonedDateTime(formData.startDate, formData.startTime),
            end: getZonedDateTime(formData.endDate, formData.endTime),
        };
    }
}

Common Scenario - comparisons

import { ZonedDateTime } from '@servicetitan/temporal-lite';
const appointments = [
    {
        id: 1,
        date: ZonedDateTime.from({
            timeZone: 'America/Los_Angeles',
            year: 2022,
            month: 8,
            day: 17,
            hour: 8,
        }),
    },
    {
        id: 2,
        date: ZonedDateTime.from({
            timeZone: 'Asia/Taipei',
            year: 2022,
            month: 8,
            day: 17,
            hour: 13,
        }),
    },
    {
        id: 3,
        date: ZonedDateTime.from({
            timeZone: 'Asia/Taipei',
            year: 2022,
            month: 8,
            day: 17,
            hour: 9,
        }),
    },
];

appointments.map(a => a.date).sort(ZonedDateTime.compare); // id order -> 3, 2, 1

Common Scenario - conversions

// Developers will almost always want to utilize TemporalFactory for creating or converting
// to ZoneDateTime instances so that the time zone is abstracted away.

// legacy Date => ZonedDateTime
const [{ getZonedDateTime }] = useDependencies(TemporalFactory);
const legacyDate = new Date();
getZonedDateTime(legacyDate);
ZonedDateTime.from(legacyDate.toISOString()); // non-TemporalFactory way

// ZonedDateTime => legacy Date
const zonedDateTime = Now.zonedDateTimeISO();
new Date(zonedDateTime.toString());

// Plain Date + Plain Time => PlainDateTime
const plainDate = Now.plainDateISO();
const plainTime = Now.plainTimeISO();
PlainDateTime.from({ …plainDate, …plainTime })

Contributing

Pull requests are welcome. Please make sure to update tests as appropriate.