Code Monkey home page Code Monkey logo

ts-utils's Introduction

ts-utils

A collection of useful utility functions and types for TypeScript developers.

Features

  • Type Guards: Functions to help narrow types safely.
  • Utility Types: Custom types that can be reused in projects.
  • Assertion Functions: Functions to assert types at runtime.
  • Data Validation: Simple schema-based validation for objects.
  • Common Helpers: Utility functions for everyday tasks.

Installation

You can install ts-utils using npm:

npm install @nodesleep/ts-utils

or using yarn:

yarn add @nodesleep/ts-utils

Usage

Type Guards

import { isString, isNumber } from '@nodesleep/ts-utils';

const value: unknown = 'hello';
if (isString(value)) {
  console.log(`The value is a string: ${value}`);
}

const anotherValue: unknown = 42;
if (isNumber(anotherValue)) {
  console.log(`The value is a number: ${anotherValue}`);
}

Utility Types

import { DeepPartial, Nullable } from '@nodesleep/ts-utils';

type ExampleType = {
  name: string;
  age: number;
};

const partialExample: DeepPartial<ExampleType> = { name: 'John' };
const nullableExample: Nullable<string> = null;

Assertion Functions

import { assertIsString, assertIsNumber } from '@nodesleep/ts-utils';

const someValue: unknown = 'hello';
assertIsString(someValue);
console.log(`The value is a string: ${someValue}`);

const anotherValue: unknown = 42;
assertIsNumber(anotherValue);
console.log(`The value is a number: ${anotherValue}`);

Data Validation

import { validateObject, validateArray } from '@nodesleep/ts-utils';

const schema = {
  name: (value: unknown): value is string => typeof value === 'string',
  age: (value: unknown): value is number => typeof value === 'number',
};

const person = {
  name: 'Alice',
  age: 30,
};

if (validateObject(person, schema)) {
  console.log('Person is valid');
} else {
  console.log('Person is invalid');
}

Common Helpers

import { deepClone, mergeObjects, flattenArray } from '@nodesleep/ts-utils';

const obj1 = { a: 1 };
const obj2 = { b: 2 };
const merged = mergeObjects(obj1, obj2);
console.log(merged); // { a: 1, b: 2 }

const nestedArray = [[1, 2], [3, 4]];
const flatArray = flattenArray(nestedArray);
console.log(flatArray); // [1, 2, 3, 4]

const original = { key: 'value' };
const clone = deepClone(original);
console.log(clone); // { key: 'value' }

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

License

This project is licensed under the MIT License.

ts-utils's People

Contributors

nodesleep avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.