UNPKG

@typedly/data

Version:

A TypeScript type definitions package for @typescript-package/data.

436 lines (331 loc) 14 kB
<a href="https://www.typescriptlang.org/"> <img src="https://avatars.githubusercontent.com/u/189665258?s=400&u=712e292bae048947d1f7d2020d7d38875c40e63a&v=4" width="20%" title="@typedly/data - A TypeScript type definitions package for @typescript-package/data." /> </a> ## @typedly/data <!-- npm badge --> [![npm version][typedly-npm-badge-svg]][typedly-npm-badge] [![GitHub issues][typedly-badge-issues]][typedly-issues] [![GitHub license][typedly-badge-license]][typedly-license] A **TypeScript** type definitions package for [**@typescript-package/data**](https://github.com/typescript-package/data). ## Table of contents - [Installation](#installation) - [Api](#api) - [Interface](#interface) - [`DataConstructor`](#dataconstructor) - [`DataShape`](#datashape) - [`ValueConstructor`](#valueconstructor) - [`ValueShape`](#valueshape) - [Type](#type) - [`DataConstructorInput`](#dataconstructorinput) - [`DataConstructorTuple`](#dataconstructortuple) - [Full example usage](#full-example-usage) - [Contributing](#contributing) - [Support](#support) - [Code of Conduct](#code-of-conduct) - [Git](#git) - [Commit](#commit) - [Versioning](#versioning) - [License](#license) - [Related packages](#related-packages) ## Installation Peer dependencies ```bash npm install @typedly/constructor --save-peer ``` The package ```bash npm install @typedly/data --save-peer ``` ## Api ```typescript import { // Interface. DataConstructor, DataShape, ValueConstructor, ValueShape, // Type. DataConstructorInput, DataConstructorTuple, } from '@typedly/data'; ``` ### Interface #### `DataConstructor` The constructor interface for data types. ```typescript import { DataConstructor, DataShape } from '@typedly/data'; // Import DataShape and DataConstructor. // Create a data class that implements `DataShape` of `Type`. export class ProfileData< Value extends { age: number, name: string } > implements DataShape<Value> { get value(): Value { return { age: this.#age, name: this.#name } as Value; } #age; #name; constructor(value: Value, ...args: any[]) { console.log(`Instantiated DataConstructor`, value, ...args); this.#age = value.age; this.#name = value.name; } set(value: Value): this { this.validate(value); return this; } clear(): this { return this; } destroy(): this { return this; } lock(): this { return this; }; validate(value: Value): boolean { return true; } } // Create `ProfileClass` with customizable data. export class ProfileClass< Value extends { age: number, name: string }, DataType extends DataShape<Value>, Args extends any[] > { public get age(): Value['age'] { return this.#data.value.age; } public get name(): Value['name'] { return this.#data.value.name; } public get data() { return this.#data; } #data: DataType; constructor(value: Value, dataCtor: DataConstructor<Value, DataType>); constructor(value: Value, dataCtor: [DataConstructor<Value, DataType>, ...Args]); constructor(value: Value, dataCtor: any) { // ...implementation console.log(`DataConstructor`, value, dataCtor[1]); this.#data = Array.isArray(dataCtor) ? new dataCtor[0](value, ...dataCtor.slice(1)) : new dataCtor(value); } } // Initialize. // const frankProfile: ProfileClass<object, ProfileData<object>, any[]> const frankProfile = new ProfileClass({ age: 37, name: 'Frank' }, ProfileData); frankProfile.age; // 37 frankProfile.name; // Frank // Set the data. frankProfile.data.set({ age: 37, name: 'Frank' }); frankProfile.data.clear(); frankProfile.data.lock(); frankProfile.data.value; ``` [Source](https://github.com/typedly/data/blob/main/src/lib/interface/data-constructor.interface.ts) #### `DataShape` The shape of a `Data` type. ```typescript import { DataShape } from '@typedly/data'; class TestData implements DataShape<number> { get value(): number { return this.initialValue; } constructor(private initialValue: number) {} public clear(): this { return this; } public destroy(): this { return this; } public lock(): this { return this; } public set(value: number): this { return this; } } ``` [Source](https://github.com/typedly/data/blob/main/src/lib/interface/data-shape.interface.ts) #### `ValueConstructor` ```typescript import { ValueConstructor } from '@typedly/data'; ``` [Source](https://github.com/typedly/data/blob/main/src/lib/interface/value-constructor.interface.ts) #### `ValueShape` The shape of a `Value` type. ```typescript import { ValueShape } from '@typedly/data'; ``` [Source](https://github.com/typedly/data/blob/main/src/lib/interface/value-shape.interface.ts) ### Type #### `DataConstructorInput` The input type for data constructors, with arguments support. ```typescript import { DataConstructorInput } from '@typedly/data'; ``` [Source](https://github.com/typedly/data/blob/main/src/lib/type/data-constructor-input.type.ts) #### `DataConstructorTuple` The input type for data constructors, with arguments support. ```typescript import { DataConstructorTuple } from '@typedly/data'; ``` [Source](https://github.com/typedly/data/blob/main/src/lib/type/data-constructor-tuple.type.ts) ### Full example usage ```typescript import { DataConstructor, DataShape, ValueConstructor, ValueShape } from '@typedly/data'; // Import ValueShape and ValueConstructor. // Create a profile data value. export class ProfileDataValue< Type extends { age: number, name: string }, Args extends any[] = any[] > implements ValueShape<Type> { get value(): Type { return { age: this.#age, name: this.#name } as Type; } #age: Type['age']; #name: Type['name']; constructor(value: Type, ...args: Args) { console.log(`Instantiated ValueConstructor`, value, ...args); this.#age = value.age; this.#name = value.name; } set(value: Type): this { return this; } } export class ProfileDataOfValue< Value extends { age: number, name: string }, Args extends any[] = any[], ValueInstance extends ValueShape<Value> = ProfileDataValue<Value, Args>, > implements DataShape<Value> { get value(): Value { return { } as Value; } get valueInstance(): ValueInstance { return this.#value; } #value; constructor( value: Value, valueCtor: ValueConstructor<Value, ValueInstance, Args> = ProfileDataValue<Value, Args> as any, ...args: Args ) { console.log(`Instantiated DataConstructor`, value, ...args); this.#value = new valueCtor(value, ...args); } set(value: Value): this { this.validate(value); return this; } clear(): this { return this; } destroy(): this { return this; } lock(): this { return this; }; validate(value: Value): boolean { return true; } } // const profileDataOfValue: ProfileDataOfValue<{ // age: number; // name: string; // }, ProfileDataValue<{ // age: number; // name: string; // }, []>, []> const profileDataOfValue = new ProfileDataOfValue({ age: 37, name: 'Mark' }, ProfileDataValue); const dataSymbol = Symbol('data'); // Create `ProfileClass` with customizable data. export class ProfileClass< Value extends { age: number, name: string }, DataType extends DataShape<Value>, Args extends any[] > { public get age(): Value['age'] { return this.#data.value.age; } public get name(): Value['name'] { return this.#data.value.name; } public get [dataSymbol]() { return this.#data; } #data: DataType; constructor(value: Value, dataCtor: DataConstructor<Value, DataType, Args>); constructor(value: Value, dataCtor: [DataConstructor<Value, DataType, Args>, ...Args]); constructor(value: Value, dataCtor: any) { // ...implementation console.log(`DataConstructor`, value, dataCtor[1]); this.#data = Array.isArray(dataCtor) ? new dataCtor[0](value, ...dataCtor.slice(1)) : new dataCtor(value); } } const markProfile = new ProfileClass({ age: 37, name: 'Mark' }, ProfileDataOfValue); markProfile.age // 37 markProfile.name // Mark markProfile[dataSymbol].value // { age, name } markProfile[dataSymbol].valueInstance.set({ age: 27, name: 'Frank' }); ``` ## 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: - [Stripe](https://donate.stripe.com/dR614hfDZcJE3wAcMM) - [Revolut](https://checkout.revolut.com/pay/048b10a3-0e10-42c8-a917-e3e9cb4c8e29) - [GitHub](https://github.com/sponsors/angular-package/sponsorships?sponsor=sciborrudnicki&tier_id=83618) - [DonorBox](https://donorbox.org/become-a-sponsor-to-the-angular-package?default_interval=o) - [Patreon](https://www.patreon.com/checkout/angularpackage?rid=0&fan_landing=true&view_as=public) or via Trust Wallet - [XLM](https://link.trustwallet.com/send?coin=148&address=GAFFFB7H3LG42O6JA63FJDRK4PP4JCNEOPHLGLLFH625X2KFYQ4UYVM4) - [USDT (BEP20)](https://link.trustwallet.com/send?coin=20000714&address=0xA0c22A2bc7E37C1d5992dFDFFeD5E6f9298E1b94&token_id=0x55d398326f99059fF775485246999027B3197955) - [ETH](https://link.trustwallet.com/send?coin=60&address=0xA0c22A2bc7E37C1d5992dFDFFeD5E6f9298E1b94) - [BTC](https://link.trustwallet.com/send?coin=0&address=bc1qnf709336tfl57ta5mfkf4t9fndhx7agxvv9svn) - [BNB](https://link.trustwallet.com/send?coin=20000714&address=0xA0c22A2bc7E37C1d5992dFDFFeD5E6f9298E1b94) Thanks for your support! ## Code of Conduct By participating in this project, you agree to follow **[Code of Conduct](https://www.contributor-covenant.org/version/2/1/code_of_conduct/)**. ## GIT ### Commit - [AngularJS Git Commit Message Conventions][git-commit-angular] - [Karma Git Commit Msg][git-commit-karma] - [Conventional Commits][git-commit-conventional] ### Versioning [Semantic Versioning 2.0.0][git-semver] **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][typedly-license]) ## Related packages - **[@typescript-package/chain-descriptor](https://github.com/typescript-package/chain-descriptor)**: A **TypeScript** library for chain property descriptor. - **[@typescript-package/controlled-descriptor](https://github.com/typescript-package/controlled-descriptor)**: A **TypeScript** library for controlled property descriptor. - **[@typescript-package/controller](https://github.com/typescript-package/controller)**: A **TypeScript** package with for various kind of controllers. - **[@typescript-package/descriptor-chain](https://github.com/typescript-package/descriptor-chain)**: A **TypeScript** library for property descriptor chain. - **[@typescript-package/descriptor](https://github.com/typescript-package/descriptor)**: A **TypeScript** library for property descriptor. - **[@typescript-package/descriptors](https://github.com/typescript-package/descriptors)**: A **TypeScript** library for property descriptors. - **[@typescript-package/property](https://github.com/typescript-package/property)**: A **TypeScript** package with features to handle object properties. - **[@typescript-package/wrap-descriptor](https://github.com/typescript-package/wrap-descriptor)**: A **TypeScript** package for wrapping object descriptors. - **[@typescript-package/wrap-property](https://github.com/typescript-package/wrap-property)**: A **TypeScript** package for wrapping object properties. - **[@typescript-package/wrapped-descriptor](https://github.com/typescript-package/wrapped-descriptor)**: A lightweight **TypeScript** library for wrapped property descriptor. - **[@xtypescript/property](https://github.com/xtypescript/property)** - A comprehensive, reactive **TypeScript** library for precise and extensible object property control. <!-- This package: typedly --> <!-- GitHub: badges --> [typedly-badge-issues]: https://img.shields.io/github/issues/typedly/data [typedly-badge-forks]: https://img.shields.io/github/forks/typedly/data [typedly-badge-stars]: https://img.shields.io/github/stars/typedly/data [typedly-badge-license]: https://img.shields.io/github/license/typedly/data <!-- GitHub: badges links --> [typedly-issues]: https://github.com/typedly/data/issues [typedly-forks]: https://github.com/typedly/data/network [typedly-license]: https://github.com/typedly/data/blob/master/LICENSE [typedly-stars]: https://github.com/typedly/data/stargazers <!-- This package --> <!-- Package: typedly --> <!-- npm --> [typedly-npm-badge-svg]: https://badge.fury.io/gh/typedly%2Fdata.svg [typedly-npm-badge]: https://badge.fury.io/js/@typedly%2Fdata <!-- GIT --> [git-semver]: http://semver.org/ <!-- GIT: commit --> [git-commit-angular]: https://gist.github.com/stephenparish/9941e89d80e2bc58a153 [git-commit-karma]: http://karma-runner.github.io/0.10/dev/git-commit-msg.html [git-commit-conventional]: https://www.conventionalcommits.org/en/v1.0.0/