@kodex-data/prototypes
Version:
A library of TypeScript prototypes that extend built-in types in JavaScript, providing additional functionality to make development easier and more efficient. This library includes prototypes for working with big numbers, Ethereum Virtual Machine (EVM) fu
59 lines (45 loc) • 2.41 kB
Markdown
This code defines additional methods on the built-in Date, String, and Number interfaces. These methods can be used globally throughout the application.
`yarn add @kodex-data/date-prototypes` or `npm install @kodex-data/date-prototypes`
* getDaysArray(year?: number, month?: number): string\[\] - This method returns an array of strings representing the days of the specified month and year. The strings are in the format "DD-dayName", where "DD" is the day of the month and "dayName" is the name of the day (e.g. "01-Sun").
* formatMMDDYYYY(): string - This method formats a date object as a string in the format "YYYY-MM-DD".
* formatHHMMSS(addMs?: boolean): string - This method returns the time part of a Date object in the format "HH:MM:SS" or "HH:MM:SS:MS" if addMS is true.
* formatMMDDYYYY\_HHMMSS(): string - This method formats a date object as a string in the format "YYYY-MM-DD HH:MM:SS".
* unix(): number - This method returns the Unix timestamp (number of seconds since January 1, 1970) rounded up to the nearest second.
## String Interface:
* sleep(): Promise<unknown\> - This method returns a promise that resolves after a given number of milliseconds.
* ts2date(): string - This method converts a Unix timestamp to a date string in the format "MM/DD/YYYY HH:MM:SS".
* sec2read(): string - This method converts a number of seconds to a human-readable string in the format "X units".
## Number Interface:
* sleep(): Promise<unknown\> - This method returns a promise that resolves after a given number of milliseconds.
* ts2date(): string - This method converts a Unix timestamp to a date string in the format "MM/DD/YYYY HH:MM:SS".
* sec2read(): string - This method converts a number of seconds to a human-readable string in the format "X units".
## Example
```typescript
import '@kodex-data/date-prototypes'
const { mul, div } = '0.3'.toFraction()
console.log({ mul, div })
```
```typescript
declare global {
interface Date {
getDaysArray(year?: number, month?: number): string[]
formatMMDDYYYY(): string
formatHHMMSS(addMs?: boolean): string
formatMMDDYYYY_HHMMSS(): string
unix(): number
}
interface String {
sleep(): Promise<unknown>
ts2date(): string
sec2read(): string
}
interface Number {
sleep(): Promise<unknown>
ts2date(): string
sec2read(): string
}
}
```