Skip to content

typedly/data

Repository files navigation

@typedly/data

npm version GitHub issues GitHub license

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

Table of contents

Installation

Peer dependencies

npm install @typedly/constructor --save-peer

The package

npm install @typedly/data --save-peer

Api

import {
  // Interface.
  DataConstructor,
  DataShape,
  ValueConstructor,
  ValueShape,
  // Type.
  DataConstructorInput,
  DataConstructorTuple,
} from '@typedly/data';

Interface

DataConstructor

The constructor interface for data types.

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

DataShape

The shape of a Data type.

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

ValueConstructor

import { ValueConstructor } from '@typedly/data';

Source

ValueShape

The shape of a Value type.

import { ValueShape } from '@typedly/data';

Source

Type

DataConstructorInput

The input type for data constructors, with arguments support.

import { DataConstructorInput } from '@typedly/data';

Source

DataConstructorTuple

The input type for data constructors, with arguments support.

import { DataConstructorTuple } from '@typedly/data';

Source

Full example usage

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:

or via Trust Wallet

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)

Related packages

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •