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

@traders/dayjs

v1.0.0

Published

```ts /* eslint-disable ts/ban-ts-comment */ import mockdate from 'mockdate' import { afterEach } from 'node:test' import { beforeEach, expect, it } from 'vitest' import { dayjs } from '.'

Downloads

19

Readme

Usage

/* eslint-disable ts/ban-ts-comment */
import mockdate from 'mockdate'
import { afterEach } from 'node:test'
import { beforeEach, expect, it } from 'vitest'
import { dayjs } from '.'

const utcYYYYMMDD = `2024-01-01`
let utcNow = dayjs(`${utcYYYYMMDD}T00:00:00Z`)
const utcUnix10 = 1704067200
const utcUnix13 = 1704067200000

beforeEach(() => {
  utcNow = dayjs(`${utcYYYYMMDD}T00:00:00Z`)
})

afterEach(() => {
  mockdate.reset()
})

it('空參數一樣取得的是以 +8 為基準', () => {
  mockdate.set(utcUnix13)
  const now = dayjs()
  expect(now.formatDateTimeSeconds()).toBe(utcNow.formatDateTimeSeconds())
})

it('.getTimestamp() 來取得 10位數 或者 13位數的 timestamp', () => {
  expect(utcNow.toDate().getTime()).toBe(utcUnix13)

  expect(utcNow.time()).toBe(utcUnix13)
  expect(utcNow.unix()).toBe(utcUnix10)

  expect(utcNow.getTimestamp().seconds).toBe(utcUnix10)
  expect(utcNow.getTimestamp().milliseconds).toBe(utcUnix13)
})

it('.format**()', () => {
  expect(utcNow.formatDate()).toMatch(`${utcYYYYMMDD.replaceAll('-', '/')}`)
  expect(utcNow.formatDateTime()).toMatch(`${utcYYYYMMDD.replaceAll('-', '/')} 00:00`)
  expect(utcNow.formatDateTimeSeconds()).toMatch(`${utcYYYYMMDD.replaceAll('-', '/')} 00:00:00`)
})

it('.setTime(:any)', () => {
  expect(utcNow.setTime('i dont care').format()).toMatch(`Invalid Date`)

  // @ts-expect-error
  expect(utcNow.setTime(() => {}).format()).toMatch(`Invalid Date`)
})

it('.setTime(:number)', () => {
  expect(utcNow.setTime(-1).format()).toMatch(`${utcYYYYMMDD}T00:00:00`)
  expect(utcNow.setTime(0).format()).toMatch(`${utcYYYYMMDD}T00:00:00`)
  expect(utcNow.setTime(6.15).format()).toMatch(`${utcYYYYMMDD}T06:15:00`)
  expect(utcNow.setTime(6.3).format()).toMatch(`${utcYYYYMMDD}T06:30:00`)
  expect(utcNow.setTime(6.45).format()).toMatch(`${utcYYYYMMDD}T06:45:00`)
  expect(utcNow.setTime(6.5).format()).toMatch(`${utcYYYYMMDD}T06:50:00`)
  expect(utcNow.setTime(6.59).format()).toMatch(`${utcYYYYMMDD}T06:59:00`)
  expect(utcNow.setTime(6.77).format()).toMatch(`${utcYYYYMMDD}T06:59:59`)
  expect(utcNow.setTime(23.59).format()).toMatch(`${utcYYYYMMDD}T23:59:00`)
  expect(utcNow.setTime(23.5959).format()).toMatch(`${utcYYYYMMDD}T23:59:59`)
  expect(utcNow.setTime(24).format()).toMatch(`${utcYYYYMMDD}T23:59:59`)
})

it('.setTime(:string)', () => {
  expect(utcNow.setTime('i am invalid string').format()).toBe(`Invalid Date`)

  //
  // 冒號
  expect(utcNow.setTime('-1').format()).toMatch(`${utcYYYYMMDD}T00:00:00`)
  expect(utcNow.setTime('0').format()).toMatch(`${utcYYYYMMDD}T00:00:00`)
  expect(utcNow.setTime('6:15').format()).toMatch(`${utcYYYYMMDD}T06:15:00`)
  expect(utcNow.setTime('6:3').format()).toMatch(`${utcYYYYMMDD}T06:30:00`)
  expect(utcNow.setTime('6:45').format()).toMatch(`${utcYYYYMMDD}T06:45:00`)
  expect(utcNow.setTime('6:5').format()).toMatch(`${utcYYYYMMDD}T06:50:00`)
  expect(utcNow.setTime('6:59').format()).toMatch(`${utcYYYYMMDD}T06:59:00`)
  expect(utcNow.setTime('6:77').format()).toMatch(`${utcYYYYMMDD}T06:59:59`)
  expect(utcNow.setTime('23:59').format()).toMatch(`${utcYYYYMMDD}T23:59:00`)
  expect(utcNow.setTime('23:5959').format()).toMatch(`${utcYYYYMMDD}T23:59:59`)
  expect(utcNow.setTime('24').format()).toMatch(`${utcYYYYMMDD}T23:59:59`)

  //
  // 小數點也可以
  expect(utcNow.setTime('-1').format()).toMatch(`${utcYYYYMMDD}T00:00:00`)
  expect(utcNow.setTime('0').format()).toMatch(`${utcYYYYMMDD}T00:00:00`)
  expect(utcNow.setTime('6.15').format()).toMatch(`${utcYYYYMMDD}T06:15:00`)
  expect(utcNow.setTime('6.3').format()).toMatch(`${utcYYYYMMDD}T06:30:00`)
  expect(utcNow.setTime('6.45').format()).toMatch(`${utcYYYYMMDD}T06:45:00`)
  expect(utcNow.setTime('6.5').format()).toMatch(`${utcYYYYMMDD}T06:50:00`)
  expect(utcNow.setTime('6.59').format()).toMatch(`${utcYYYYMMDD}T06:59:00`)
  expect(utcNow.setTime('6.77').format()).toMatch(`${utcYYYYMMDD}T06:59:59`)
  expect(utcNow.setTime('23.59').format()).toMatch(`${utcYYYYMMDD}T23:59:00`)
  expect(utcNow.setTime('23.5959').format()).toMatch(`${utcYYYYMMDD}T23:59:59`)
  expect(utcNow.setTime('24').format()).toMatch(`${utcYYYYMMDD}T23:59:59`)
})

it('dayjs(:number) 接受參數為 10位數字(處理誤植) 或 13位數字 或 16位數字(處理誤植)', () => {
  const unix10 = 1704067200
  const toBeTimeString = `2024/01/01 00:00:00`

  expect(dayjs(unix10).formatDateTimeSeconds()).toBe(toBeTimeString)
  expect(dayjs(unix10 * 1000).formatDateTimeSeconds()).toBe(toBeTimeString)
  expect(dayjs(unix10 * 1000 * 1000).formatDateTimeSeconds()).toBe(toBeTimeString)
  expect(dayjs(`2024-01-01T00:00:00Z`).formatDateTimeSeconds()).toBe(toBeTimeString)
  expect(dayjs(`2024-01-01 00:00:00`).formatDateTimeSeconds()).toBe(toBeTimeString)
  expect(dayjs(`2024-01-01`).formatDateTimeSeconds()).toBe(toBeTimeString)
})

it('dayjs(:string) 接受參數為 10位數字(處理誤植) 或 13位數字 或 16位數字(處理誤植)', () => {
  const utcInputUnix10 = 1704067200
  const toBeTimeString = `2024/01/01 00:00:00`

  expect(dayjs(`${utcInputUnix10}`).formatDateTimeSeconds()).toBe(toBeTimeString)
  expect(dayjs(`${utcInputUnix10 * 1000}`).formatDateTimeSeconds()).toBe(toBeTimeString)
  expect(dayjs(`${utcInputUnix10 * 1000 * 1000}`).formatDateTimeSeconds()).toBe(toBeTimeString)
  expect(dayjs(`2024-01-01T00:00:00Z`).formatDateTimeSeconds()).toBe(toBeTimeString)
  expect(dayjs(`2024/01/01 00:00:00`).formatDateTimeSeconds()).toBe(toBeTimeString)
  expect(dayjs(`2024-01-01 00:00:00`).formatDateTimeSeconds()).toBe(toBeTimeString)
  expect(dayjs(`2024-01-01`).formatDateTimeSeconds()).toBe(toBeTimeString)
})

it('dayjs(:string) 接受參數為 含有下劃線、或者 e 符號的字串型態的數字', () => {
  const toBeTimeString = `2024/01/01 00:00:00`

  expect(dayjs(`1704067200`).formatDateTimeSeconds()).toBe(toBeTimeString)
  expect(dayjs(`1704067200_000`).formatDateTimeSeconds()).toBe(toBeTimeString)
  expect(dayjs(`1704067200_000_000`).formatDateTimeSeconds()).toBe(toBeTimeString)
  expect(dayjs(`1704067200e3`).formatDateTimeSeconds()).toBe(toBeTimeString)
  expect(dayjs(`1704067200e6`).formatDateTimeSeconds()).toBe(toBeTimeString)
})