1.3.1 • Published 8 months ago

ts-raw-utils v1.3.1

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

Typescript Utility Functions

Typescript License: MIT Build Status

A TypeScript project that provides utility functions for your TypeScript projects.

Installation

npm install ts-raw-utils

Usage

EnumValues

Retrieves the values of an enum as an array.

import { enumValues } from 'ts-raw-utils';

enum Color {
  Red = 'red',
  Green = 'green',
  Blue = 'blue',
}

const colors = enumValues(Color);
console.log(colors); // ['red', 'green', 'blue']

Writable

Creates a type that allows modifying properties of an object. The Writable type allows you to make properties of an interface or type mutable, even if they are originally defined as read-only.

import { writable } from 'ts-raw-utils';

interface Point {
  x: number;
  y: number;
}

const point: Writable<Point> = { x: 0, y: 0 };
point.x = 10;
console.log(point); // { x: 10, y: 0 }

DeepPartial

Creates a type that makes all properties of an object optional, including nested properties.

import { deepPartial } from 'ts-raw-utils';

interface Person {
  name: string;
  age: number;
  address: {
    city: string;
    country: string;
  };
}

const partialPerson: DeepPartial<Person> = {
  name: 'John',
  address: {
    city: 'New York',
  },
};
console.log(partialPerson); // { name: 'John', address: { city: 'New York' } }

Promisify

Converts a callback-based function into a Promise-based function.

import { promisify } from 'ts-raw-utils';

function exampleAsyncFunction(arg: number, callback: (result: string) => void) {
  setTimeout(() => {
    const result = `Processed: ${arg}`;
    callback(result);
  }, 1000);
}

const promisifiedFn = promisify(exampleAsyncFunction);
promisifiedFn(123).then((result) => {
  console.log(result); // 'Processed: 123'
});

The Promisify function takes a callback-based function and returns a new function that returns a Promise. The original callback-based function is invoked with the same arguments, but instead of providing a callback, the Promise is resolved with the result.

UnionToIntersection

Creates a new type that represents the intersection of multiple types in a union. The UnionToIntersection utility function takes a union of types and creates a new type that represents the intersection of those types. The resulting type contains all the properties and methods from each type in the union.

import { unionToIntersection } from 'ts-raw-utils';

type A = { a: string };
type B = { b: number };
type C = { c: boolean };

type ABC = UnionToIntersection<A | B | C>;
// Equivalent to: { a: string } & { b: number } & { c: boolean }

const obj: ABC = { a: 'hello', b: 123, c: true };
console.log(obj); // { a: 'hello', b: 123, c: true }

ChunkArray

Splits an array into chunks of a specified size.

import { ChunkArray } from 'ts-raw-utils';

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const chunks = ChunkArray(numbers, 3);
console.log(chunks); // [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]

DeepMerge

Deeply merges two objects, combining their properties into a new object. If a property exists in both objects and they are both objects themselves, the function recursively merges them. If the property exists in both objects and at least one of them is not an object, the value from the second object overwrites the value from the first object.

import { DeepMerge } from 'ts-raw-utils';

const target = { a: { b: 1 }, c: 2 };
const source = { a: { c: 3 }, d: 4 };
const merged = DeepMerge(target, source);
console.log(merged); // { a: { b: 1, c: 3 }, c: 2, d: 4 }

Deserialize

Converts a JSON string to a JavaScript object.

import { Deserialize } from 'ts-raw-utils';

const json = '{"name":"John","age":30}';
const obj = Deserialize<Person>(json);
console.log(obj); // { name: 'John', age: 30 }

FilterFalsy

Filters out falsy values from an array, returning a new array with only the truthy values.

import { FilterFalsy } from 'ts-raw-utils';

const values = [0, '', null, undefined, false, 'Hello', 123];
const filtered = FilterFalsy(values);
console.log(filtered); // ['', 'Hello', 123]

FlattenArray

Flattens a nested array into a single-level array.

import { FlattenArray } from 'ts-raw-utils';

const nestedArray = [[1, 2], [3, [4, 5]], [6]];
const flattened = FlattenArray(nestedArray);
console.log(flattened); // [1, 2, 3, 4, 5, 6]

FormatNumber

Formats a number with optional formatting options using the toLocaleString method.

import { FormatNumber } from 'ts-raw-utils';

const number = 1234567.89;
const formatted = FormatNumber(number, { style: 'currency', currency: 'USD' });
console.log(formatted); // $1,234,567.89

Pluralize

Returns the singular or plural form of a word based on a count.

import { Pluralize } from 'ts-raw-utils';

const item = 'apple';
const count = 3;
const pluralized = Pluralize(item, count);
console.log(pluralized); // 'apples'

Serialize

Converts a JavaScript object to a JSON string.

import { Serialize } from 'ts-raw-utils';

const obj = { name: 'John', age: 30 };
const json = Serialize(obj);
console.log(json); // '{"name":"John","age":30}'

ShuffleArray

Randomly shuffles the elements of an array using the Fisher-Yates algorithm.

import { ShuffleArray } from 'ts-raw-utils';

const numbers = [1, 2, 3, 4, 5];
const shuffled = ShuffleArray(numbers);
console.log(shuffled); // [3, 1, 4, 5, 2] (random order)

TruncateString

Truncates a string to a specified length, appending an ellipsis if needed.

import { TruncateString } from 'ts-raw-utils';

const str = 'Lorem ipsum dolor sit amet';
const truncated = TruncateString(str, 10);
console.log(truncated); // 'Lorem ipsu...'

UniqueArray

Removes duplicate values from an array, returning a new array with only unique values.

import { UniqueArray } from 'ts-raw-utils';

const numbers = [1, 2, 2, 3, 3, 3, 4, 5, 5];
const unique = UniqueArray(numbers);
console.log(unique); // [1, 2, 3, 4, 5]

replaceAll

Replaces all occurrences of a substring in a string with a new substring.

import { replaceAll } from 'ts-raw-utils';
const str = 'Hello, World!';
const replaced = replaceAll(str, 'l', 'x');
console.log(replaced); // 'Hexxo, Worxd!'

capitalize

Capitalizes the first letter of a string.

import {capitalize} from 'ts-raw-utils';
import {getEscapedCapitalizedStringLiteral, getEscapedStringLiteral} from "./index";

const str = 'hello, world!';
const capitalized = capitalize(str);
console.log(capitalized); // 'Hello, world!'
const escaped = getEscapedStringLiteral('"hello, world!"');
console.log(escaped); // 'Hello, world!'
const escapedCapitalized = getEscapedCapitalizedStringLiteral("'hello, world!'");
console.log(escapedCapitalized); // 'Hello, world!'

removeWhitespace

Removes all whitespace from a string.

import { removeWhitespace } from 'ts-raw-utils';
const str = '  Hello, World!  ';
const trimmed = removeWhitespace(str);
console.log(trimmed); // 'Hello,World!'

transformer

transforms intersection types in your TypeScript code into type guard signatures. This function can help you generate type guard signatures for complex types, making your TypeScript code more expressive and maintainable.

import { transformer } from 'ts-raw-utils';
const inputCode = `
type IntersectionType = TypeA & TypeB;
type TypeA = { a: string };
type TypeB = { b: number };
`;
const transformedCode = transformer(inputCode);
console.log(transformedCode); 
// type IntersectionType = {};
// type TypeA = { a: string };
// type TypeB = { b: number };

Logger

Logger is a TypeScript utility class for flexible logging with different log levels. It provides an easy way to control and customize logging in your applications.

Installation

To use Logger in your TypeScript project, you can import the class directly:

import Logger from './Logger'; // Adjust the import path as needed

Usage

Enabling and Disabling Logging

By default, logging is enabled. You can enable or disable it as needed.

Logger.enableLogging(); // Enable logging (default)
Logger.disableLogging(); // Disable logging

Log Levels

Logger supports various log levels, each with its own prefix:

  • Logger.log(message: string, ...args: any[]): void (Regular log)
  • Logger.info(message: string, ...args: any[]): void (Informational)
  • Logger.warn(message: string, ...args: any[]): void (Warning)
  • Logger.error(message: string, ...args: any[]): void (Error)
  • Logger.debug(message: string, ...args: any[]): void (Debugging)
  • Logger.trace(message: string, ...args: any[]): void (Trace)
  • Logger.fatal(message: string, ...args: any[]): void (Fatal error)

Example usage:

Logger.log("This is a regular log message.");
Logger.info("This is an info message.");
Logger.warn("This is a warning message.");
Logger.error("This is an error message.");
Logger.debug("This is a debug message.");
Logger.trace("This is a trace message.");
Logger.fatal("This is a fatal message.");

Customization

You can customize the behavior of each log level by modifying the Logger class to suit your needs. For example, you can redirect logs to different outputs or format log messages differently.

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on the GitHub repository.

License

This project is licensed under the MIT License.

Feel free to customize and expand this README file with more information about your utility functions, usage examples, guidelines for contributions, and any other relevant details.

Let me know if you have any further questions or need additional assistance!

1.3.1

8 months ago

1.3.0

8 months ago

1.2.1

8 months ago

1.2.0

8 months ago

1.1.0

9 months ago

1.0.2

9 months ago

1.0.1

9 months ago