0.1.1 • Published 5 months ago

@typedly/property v0.1.1

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

typedly/property

npm version GitHub issues GitHub license

A TypeScript type definitions package to handle object property-related operations.

Table of contents

Installation

npm install @typedly/property --save-peer

Api

import {
  Add,
  DeepAdd, // Typedly.Property.Deep.Add
  DeepPick, // Typedly.Property.Deep.Pick
  DeepRemove, // Typedly.Property.Deep.Remove

  Get,
  PickByType,  // Typedly.Property.Pick.By.Type
  PickWithOptional, // Typedly.Property.Pick.With.Optional
  PickWithReadonly, // Typedly.Property.Pick.With.Readonly
  PickWithRenaming, // Typedly.Property.Pick.With.Renaming
  PickWithTransform, // Typedly.Property.Pick.With.Transform

  Remove,
  Set,
  Update
} from '@typedly/property';

// Namespace.
export * from './lib/property.namespace';

Typedly

type Example1 = Typedly.Property.Add<{ firstName: 'a', surName: 'b' }, 'city', 'London', false>;
// type Example1 = {
//   firstName: "a";
//   surName: "b";
// } & {
//   city: "London";
// }

type Example2 = Typedly.Property.Remove<{firstName: 'a', surName: 'b'}, 'firstName'>;
// type Example2 = { surName: "b"; }

type Example3 = Typedly.Property.Get<{firstName: 'a', surName: 'b'}, 'firstName'>;
// type Example3 = "a"

type Example4 = Typedly.Property.Set<{firstName: 'a', surName: 'b'}, 'age', 22>;
// type Example4 = {
//   firstName: "a";
//   surName: "b";
// } & {
//   age: 22;
// }

type Example5 = Typedly.Property.Update<{name: string | 'a'}, 'name', 'b'>;
// type Example5 = { name: "b"; }

type Example6 = Typedly.Property.PickByType<{name: 'a', age: 1}, string>;
// type Example6 = { name: "a"; }

type Example7 = Typedly.Property.Deep.Add<{user: {address: {city: 'a'}}}, 'user.address.zip', '123'>;
// type Example7 = {
//   user: {
//       address: {
//           city: "a";
//       } & {
//           zip: "123";
//       };
//   };
// }

type Example8 = Typedly.Property.Deep.Pick<{user: {address: {city: 'a', zip: '123'}}}, 'user.address.city'>;
// type Example8 = { user: { address: { city: "a"; }; }; }

Add

import { Add } from '@typedly/property';

const object = { firstName: 'Someone', lastName: 'Someone surname', age: 227 } as const;
type Example1 = Add<typeof object, 'city', 'London'>;
/*
  type Example1 = {
    readonly firstName: "Someone";
    readonly lastName: "Someone surname";
    readonly age: 227;
  } & {
    city: "London";
  }
*/

type Example2 = Typedly.Property.Add<typeof object, 'city', 'London', true>;
/*
  type Example2 = {
    readonly firstName: "Someone";
    readonly lastName: "Someone surname";
    readonly age: 227;
  } & {
    city?: "London" | undefined;
  }
*/

DeepAdd

import { DeepAdd } from '@typedly/property';

type Profile = { user: { name: string } };

type Example1 = DeepAdd<Profile, 'user.age', number>;
/*
  type Example1 = {
    user: {
      name: string;
    } & {
      age: number;
    };
  }

  {
    user: {
      name: string;
      age: number;
    }
  }
*/

type Example2 = Typedly.Property.Deep.Add<Profile, 'user.address.city', string>; // The use of Typedly namespace.
/*
  type Example2 = {
    user: {
      name: string;
    } & {
      address: {
        city: string;
      };
    };
  }

  {
    user: {
      name: string;
      address: {
        city: string;
      };
    }
  }
*/

type BrokenExample = { user: string };
type Example3 = DeepAdd<BrokenExample, 'user.age', number>;
/*
  type Example3 = {
    user: {
      age: number;
    };
  }

  {
    user: {
      age: number;
    };
  }
*/

type Example4 = DeepAdd<Profile, 'settings.theme', string>;
/*
  type Example4 = Example & {
    settings: {
      theme: string;
    };
  }

  {
    user: {
      name: string;
    };
    settings: {
      theme: string;
    };
  }
*/

DeepPick

import { DeepPick } from '@typedly/property';

const object = { user: { name: 'Someone', address: { city: 'London', zip: '12345' } } } as const;
type Example1 = DeepPick<typeof object, 'user.name'>;
/*
  type Example1 = {
    user: {
        name: "Someone";
    };
  }
*/

DeepRemove

import { DeepRemove } from '@typedly/property';

// Example 1: Removing a nested property
type Example1 = DeepRemove<{
  user: {
    address: {
      city: string;
      zip: string;
    };
    name: string;
  };
}, 'user.address.zip'>;

// Expected Result:
// {
//   user: {
//     address: {
//       city: string;
//     };
//     name: string;
//   };
// }

Get

import { Get } from '@typedly/property';

const object = { firstName: 'Someone', lastName: 'Someone surname', age: 227 } as const;
type Example1 = Get<typeof object, 'firstName'>;
/*
  type Example1 = "Someone"
*/

PickByType

import { PickByType } from '@typedly/property';

const object = { firstName: 'Someone', age: 227, city: 'London' } as const;
type PickedString = PickByType<typeof object, string>;
// Result: { firstName: "Someone", city: "London" }

PickWithOptional

import { PickWithOptional } from '@typedly/property';

const object = { firstName: 'Someone', lastName: 'Someone surname', age: 227 } as const;
type Picked1 = PickWithOptional<typeof object, 'firstName'>; // firstName?: "Someone"
type Picked2 = Typedly.Property.Pick.With.Optional<typeof object, 'firstName'>; // firstName?: "Someone"

PickWithReadonly

import { PickWithReadonly } from '@typedly/property';

const object = { firstName: 'Someone', lastName: 'Someone surname', age: 227 } as const;
type ReadonlyPick = PickWithReadonly<typeof object, 'firstName'>;
// Result: { readonly firstName: "Someone" }

PickWithRenaming

import { PickWithRenaming } from '@typedly/property';

const object = { firstName: 'Someone', lastName: 'Someone surname' };
type RenamedPick = PickWithRenaming<typeof object, 'firstName', { firstName: 'firstNameRenamed' }>;
// Result: { firstNameRenamed: "Someone", lastName: "Someone surname" }

PickWithTransform

import { PickWithTransform } from '@typedly/property';

const object = { firstName: 'Someone', lastName: 'Someone surname' };
type TransformedPick = PickWithTransform<typeof object, 'firstName', (val: string) => number>;
// Result: { firstName: number }

Remove

import { Remove } from '@typedly/property';

const object = { firstName: 'Someone', lastName: 'Someone surname', age: 227 } as const;
type Removed = Remove<typeof object, 'firstName'>;
/*
  type Removed = {
    readonly age: 227;
    readonly lastName: "Someone surname";
  }
*/

Set

import { Set } from '@typedly/property';

const object = { firstName: 'Someone', lastName: 'Someone surname', age: 227 } as const;
type Added = Set<typeof object, 'newProperty', 'The new property value'>;
/*
  {
    readonly firstName: "Someone";
    readonly lastName: "Someone surname";
    readonly age: 227;
  } & {
    newProperty: "The new property value";
  }
*/
type Saved = Set<typeof object, 'firstName', 'The new value'>;
/*
  {
    readonly firstName: "The new value";
    readonly lastName: "Someone surname";
    readonly age: 227;
  }
*/

Update

import { Update } from '@typedly/property';

const object = { firstName: 'Someone' as string, lastName: 'Someone surname', age: 227 } as const;
type Updated = Update<typeof object, 'firstName', 'The new value'>;
/*
  {
    readonly firstName: "The new value";
    readonly lastName: "Someone surname";
    readonly age: 227;
  }
*/

Contributing

Your contributions are valued! If you'd like to contribute, please feel free to submit a pull request. Help is always appreciated.

Support

If you find this package useful and would like to support its and general development, you can contribute through one of the following payment methods. Your support helps maintain the packages and continue adding new.

Support via:

Thanks for your support!

Code of Conduct

By participating in this project, you agree to follow Code of Conduct.

GIT

Commit

Versioning

Semantic Versioning 2.0.0

Given a version number MAJOR.MINOR.PATCH, increment the:

  • MAJOR version when you make incompatible API changes,
  • MINOR version when you add functionality in a backwards-compatible manner, and
  • PATCH version when you make backwards-compatible bug fixes.

Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.

FAQ How should I deal with revisions in the 0.y.z initial development phase?

The simplest thing to do is start your initial development release at 0.1.0 and then increment the minor version for each subsequent release.

How do I know when to release 1.0.0?

If your software is being used in production, it should probably already be 1.0.0. If you have a stable API on which users have come to depend, you should be 1.0.0. If you’re worrying a lot about backwards compatibility, you should probably already be 1.0.0.

License

MIT © typedly (license)

Packages

  • @typedly/array: A TypeScript type definitions package to handle array-related operations.
  • @typedly/callback: A TypeScript type definitions package for asynchronous and synchronous callback functions of various types.
  • @typedly/character: A TypeScript type definitions package for various character types.
  • @typedly/context: A TypeScript type definitions package for context data structures.
  • @typedly/descriptor: A TypeScript type definitions package for property descriptor.
  • @typedly/digit: A TypeScript type definitions package for digit types.
  • @typedly/letter: A TypeScript type definitions package for handling letter types.
  • @typedly/object: A TypeScript type definitions package to handle object-related operations.
  • @typedly/payload: A TypeScript type definitions package for payload data structures.
  • @typedly/regexp: A TypeScript type definitions package for RegExp.
  • @typedly/symbol: A TypeScript type definitions package for various symbols.