lightkit
TypeScript icon, indicating that this package has built-in type declarations

0.0.5 • Public • Published

LightKit

NPM Version NPM Downloads NPM License

This is a small and lightweight utility library that brings together commonly used functions during development. While there are already many major and excellent libraries available, they often include a lot of unused methods. Therefore, we are developing this library by focusing on only the essential features, keeping it lightweight.

Install

$ npm install lightkit

Array

multiFilter

Filters an array using multiple filter functions and returns the filtered results in separate arrays.

import { multiFilter } from "lightkit"

const array = [1, 2, 3, 4, 5, 6]
const filters = [
  (n) => n % 2 === 0, // Filter even numbers
  (n) => n % 2 !== 0 // Filter odd numbers
]
const result = multiFilter(array, filters)

console.log(result)
// [[2, 4, 6], [1, 3, 5]]

Date

Constructor

Initializes the class with a given date value or the current date if no value is provided.

The LDate class also supports the "YYYY-MM-DD HH:MM:SS" format as a local date.

import { LDate } from "lightkit"

const instance1 = new LDate()
// Initializes with the current date and time.
const instance2 = new LDate(1629918000000)
// Initializes with a timestamp.
const instance3 = new LDate("2023-08-27T10:15:00Z")
// Initializes with a valid date string.
const instance4 = new LDate("2023-08-27 10:15:00")
// Initializes with a custom date string.
const instance5 = new LDate(new Date())
// Initializes with a Date object.

getDate

Retrieves the current date.

import { LDate } from "lightkit"

const dateString = "2023-08-27T10:15:00Z"
const dateTime = new Date(dateString).getTime()
const lDateTime = new LDate(dateString).getDate().getTime()

console.log(new LDate(dateString).getDate() instanceof Date) // true
console.log(dateTime === lDateTime) // true

getDateParts

Extracts various date and time properties from the Date object.

import { LDate } from "lightkit"

const dateString = "2023-08-27 15:30:45"
const dateProperties = new LDate(dateString).getDateParts("en")

console.log(dateProperties)
// {
//   year: '2023',
//   month: '08',
//   day: '27',
//   hour: '15',
//   minute: '30',
//   second: '45',
//   yearMonthDay: '2023-08-27',
//   yearMonth: '2023-08',
//   monthDay: '08-27',
//   hourMinuteSecond: '15:30:45',
//   hourMinute: '15:30',
//   dayOfWeek: 0,
//   dayOfWeekLong: 'Sunday',
//   dayOfWeekShort: 'Sun',
//   longTime: 1693117845000
// }

differenceIn

Calculates the difference between the current date and the provided date in the specified unit.

import { LDate } from "lightkit"

const lDate = new LDate("2023-01-01T00:00:00Z")
const targetDate = new Date("2024-01-01T00:00:00Z")

const diffInYears = lDate.differenceIn(targetDate, "year")
console.log(diffInYears) // 1

const diffInMonths = lDate.differenceIn(targetDate, "month")
console.log(diffInMonths) // 12

const diffInDays = lDate.differenceIn(targetDate, "day")
console.log(diffInDays) // 365

const diffInHours = lDate.differenceIn(targetDate, "hour")
console.log(diffInHours) // 8760

const diffInMinutes = lDate.differenceIn(targetDate, "minute")
console.log(diffInMinutes) // 525600

const diffInSeconds = lDate.differenceIn(targetDate, "second")
console.log(diffInSeconds) // 31536000

Readme

Keywords

Package Sidebar

Install

npm i lightkit

Weekly Downloads

5

Version

0.0.5

License

The Unlicense

Unpacked Size

56 kB

Total Files

26

Last publish

Collaborators

  • falsylab