npm.io
0.0.2 • Published 2d ago

@time-provider/plugin-native

Licence
MIT
Version
0.0.2
Deps
0
Size
3 kB
Vulns
0
Weekly
0

NPM CI codecov

Time-Provider

A tiny library to rapidly have time !

Description

It's a very simple typescript library to setup a source of time. A time provider works with a compatible adapter (even for native Date or Temporal), so you must both import the core library and the plugin of your choice (See usage).

Core library

Name NPM package
@time-provider/core NPM

Plugins

Currently supported plugins are :

Plugin Name Returned Type NPM package
Temporal @time-provider/plugin-temporal Instant NPM
Native @time-provider/plugin-native Date NPM
Luxon @time-provider/plugin-luxon DateTime NPM
Day.js @time-provider/plugin-dayjs Dayjs NPM
Moment.js @time-provider/plugin-moment Moment NPM

Usage

  • Each plugin (adapter) exports a TimeAdapter and a FixedTimeAdapter class
  • Select your desired plugin (native/dayjs/moment/luxon/temporal)
  • call createTimeProvider.for(/*your adapter here*/)
For your production code
import { createTimeProvider } from "@time-provider/core";
//Import the plugin of your choice (here the temporal plugin)
import { TimeAdapter } from "@time-provider/plugin-temporal";
import { Temporal } from "@js-temporal/polyfill";
const timeProvider = createTimeProvider.for(new TimeAdapter());
Or for your tests (fixed time)
import { createTimeProvider } from "@time-provider/core";
//Import the plugin of your choice (here the temporal plugin)
import { FixedTimeAdapter } from "@time-provider/plugin-temporal";
import { Temporal } from "@js-temporal/polyfill";
const fixedTimeProvider = createTimeProvider.for(
  new FixedTimeAdapter(Temporal.Instant.from("2026-01-01T00:00Z")),
);

API

interface ITimeProvider<TDate> {
  localNow(): TDate;
  utcNow(): TDate;
  parse(input: string | number | TDate): TDate;
}

Keywords