@bombaysoftwares/tskit
Version:
The @bombaysoftwares/tskit package provides a set of utility functions for working with dates and timestamps in JavaScript. These functions simplify common tasks such as date formatting, timestamp conversion, and JSON validation.
136 lines (103 loc) • 5.06 kB
Markdown
The @bombaysoftwares/tskit package provides a comprehensive set of utility functions for various operations in JavaScript. These functions simplify common tasks such as date formatting, timestamp conversion, JSON validation, manipulating strings, and working with HTML content. This package is designed to enhance the functionality of various operations in your JavaScript projects.
To use the Date Utils package, you can install it via npm:
```bash
npm i @bombaysoftwares/tskit
```
Import the desired functions from the package:
```bash
import { getCurrentDateTime, getCurrentTimestamp, getDateTimeFromTimestamp } from '@bombaysoftwares/tskit';
```
This function returns the current date and time in the format "YYYY-MM-DD HH:MM:SS".
```bash
const currentDateTime = getCurrentDateTime();
console.log(currentDateTime);
// Output: 2023-06-20 12:22:20
```
This function checks if the value provided is none of this - null, undefined, empty string, "undefined", empty array as string.
```bash
console.log(isSet("null"));
// Output: false
```
This function checks if the value provided is not null or undefined.
```bash
console.log(isSetNumber(1));
// Output: true
console.log(isSetNumber(null));
// Output: false
```
This function generates an array of numbers from 1 to the specified length.
```bash
console.log(getArray(5));
// Output: [1, 2, 3, 4, 5]
console.log(getArray(10));
// Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
```
This function returns a new array containing only unique values from the input array, preserving the order of first occurrences. Only supports arrays containing a single data type.
```bash
const values = ["a", "b", "a", "c", "b"];
console.log(getUniqueValueFromArray(values));
// Output: ["a", "b", "c"]
const nums = [1, 2, 2, 3];
console.log(getUniqueValueFromArray(nums));
// Output: [1, 2, 3]
```
This function capitalizes the first letter of a given text and converts the rest to lowercase. Returns an empty string if input is undefined, null, or not a string.
```bash
console.log(formatTextToCapitalized("hello world"));
// Output: "Hello world"
console.log(formatTextToCapitalized("JAVASCRIPT"));
// Output: "Javascript"
```
This function converts an underscore-separated string to a space-separated, capitalized text. Handles non-string, empty, or falsy inputs gracefully.
```bash
console.log(underscoreToCapitalizedText("hello_world_example"));
// Output: "Hello World Example"
console.log(underscoreToCapitalizedText("user_name_email"));
// Output: "User Name Email"
```
This function generates a random color in hexadecimal format.
```bash
console.log(getRandomColor());
// Output: #62C5B9
```
Extracts text from an HTML string by removing HTML tags.
```bash
console.log(getTextFromHtml("<h1>Title</h1><p>This is a paragraph.</p>"); returns TitleThis is a paragraph.);
// Output: The extracted text without HTML tags.
```
This function formats a timestamp into a string representation of date and time. You can choose whether to include seconds in the formatted string.
```bash
const timestamp = 1695193213;
const formattedDateTime = getDateTimeFromTimestamp(timestamp);
console.log(formattedDateTime);
// Output: 20-Sep-2023 12:30:13
```
The package also includes other useful functions:
- `evalBooleanValue(value: string | boolean | undefined): boolean` : This function checks if the value provided is "true" or true.
- `dateAndTimeFormat(dateObject: Date, showSeconds = true): string | null` : Formats a Date object into a string representation of date and time.
- `dateFormat(dateObject: Date): string | null` : Formats a Date object into a string representation of date in DD-Mmm-YYYY format.
- `dateFormatHHMM(dateObject: Date): string | null` : Formats a Date object into a string representation of date and time in DD-Mmm-YYYY, HH:mm format.
- `getTwodigitFormat(data: number): string | number | null` : Converts a number to a two-digit format by adding a leading zero if necessary.
- `isValidJsonData(data: string): object | boolean` : Checks if a string is valid JSON data by attempting to parse it.
- `getLocalDate(dateAndTime: string): string | null` : Retrieves the local date in a specific format from the provided date and time string.
- `strToDate = (dateString: string, format: string): Date | null` : Parses the input date string based on the specified format and returns a Date object.
- `camelCaseKeys= (obj: {[x: string]: string | number}):{[x: string]: string | number}` : Converts the keys of an object from snake_case to camelCase.
For detailed usage and examples, refer to the inline documentation and code samples within each function.
[](https://www.bombaysoftwares.com/)
This project is licensed under the MIT license. See the [LICENSE](LICENSE) file for more info.