A TypeScript type definitions package for @typescript-package/data.
- Installation
- Api
- Full example usage
- Contributing
- Support
- Code of Conduct
- Git
- License
- Related packages
Peer dependencies
npm install @typedly/constructor --save-peerThe package
npm install @typedly/data --save-peerimport {
// Interface.
DataConstructor,
DataShape,
ValueConstructor,
ValueShape,
// Type.
DataConstructorInput,
DataConstructorTuple,
} from '@typedly/data';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;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; }
}import { ValueConstructor } from '@typedly/data';The shape of a Value type.
import { ValueShape } from '@typedly/data';The input type for data constructors, with arguments support.
import { DataConstructorInput } from '@typedly/data';The input type for data constructors, with arguments support.
import { DataConstructorTuple } from '@typedly/data';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' });Your contributions are valued! If you'd like to contribute, please feel free to submit a pull request. Help is always appreciated.
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!
By participating in this project, you agree to follow Code of Conduct.
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.
MIT © typedly (license)
- @typescript-package/chain-descriptor: A TypeScript library for chain property descriptor.
- @typescript-package/controlled-descriptor: A TypeScript library for controlled property descriptor.
- @typescript-package/controller: A TypeScript package with for various kind of controllers.
- @typescript-package/descriptor-chain: A TypeScript library for property descriptor chain.
- @typescript-package/descriptor: A TypeScript library for property descriptor.
- @typescript-package/descriptors: A TypeScript library for property descriptors.
- @typescript-package/property: A TypeScript package with features to handle object properties.
- @typescript-package/wrap-descriptor: A TypeScript package for wrapping object descriptors.
- @typescript-package/wrap-property: A TypeScript package for wrapping object properties.
- @typescript-package/wrapped-descriptor: A lightweight TypeScript library for wrapped property descriptor.
- @xtypescript/property - A comprehensive, reactive TypeScript library for precise and extensible object property control.