@angular-package/property
TypeScript icon, indicating that this package has built-in type declarations

0.0.1-alpha.0 • Public • Published

Packages

Useful and simple to use packages based on the angular.io.

Package Description Status Readme
change-detection Improve application performance. In Progress Readme
prism Prism highlighter module. In Progress Readme
property Features to handle object properties. In Progress Readme
ui User interface. In Progress Github
type Common types, type guards and type checkers. npm version Github | npm

angular-package/property

Features to handle properties.

npm version GitHub issues GitHub forks GitHub stars GitHub license

/**
 * Object.
 */
import {
  get
} from '@angular-package/property';
/**
 * Function.
 */
import {
  getDescriptor,
  getDescriptors,
  getExistProperty,
  getProperties,
  getProperty,
  setProperty,
} from '@angular-package/property';
/**
 * Descriptor.
 */
// Class.
import {
  AccessorDescriptors,
  DataDescriptors,
  Descriptor
} from '@angular-package/property';
// Interface.
import {
  AccessorDescriptor,
  CommonDescriptor,
  DataDescriptor
} from '@angular-package/property';
// Type.
import {
  ThisAccessorDescriptor
} from '@angular-package/property';

How angular-package understands

Check

Is to check the provided argument to be the same as expected.

Type guard (constrain)

Is to constrain the parameter type to not let input unexpected value in the code editor.

Guard

Is a combination of both above to guard type in the code editor and in the provided argument by checking it.


Installation

Install @angular-package/property package with command:

npm i --save @angular-package/property

Callback

Wrapper for the ResultCallback type function to throw an Error with the specified message on the specified false or true state.

const errorCallback: ErrorCallback  = (
  message: string,
  on: boolean = false
): ResultCallback => {
  return (result: boolean, value: any): boolean => {
    if (result === on) {
      throw new Error(
        `${message}, got value ${
          is.object(value) ? JSON.stringify(value) : value
        }`
      );
    }
    return result;
  };
};

Object

get

Description:

Get object with all prefixed with get functions.

const get: Get = {
  descriptor: getDescriptor,
  descriptors: getDescriptors,
  existProperty: getExistProperty,
  object: getObject,
  properties: getProperties,
  property: getProperty
};

Function

getDescriptor()

Description:

Wrapper function for the Object static method getOwnPropertyDescriptor(). Use getDescriptor() or get.descriptor() to return descriptor of the specified property from the specified object.

Gets the own property descriptor of the specified object. An own property descriptor is one that is defined directly on the object and is not inherited from the object's prototype.

Features:

Additional features instead of the default from the wrapped getOwnPropertyDescriptor() method.

  • Constraints the object parameter with a generic Obj variable of an object type.
  • Constraints the key parameter with a Key variable which is of a key of the Obj variable.

Import:

import { get, getDescriptor } from '@angular-package/property';

Syntax:

const getDescriptor: GetDescriptor = <Obj extends object, Key extends keyof Obj>(
  object: Obj,
  key: Key
): PropertyDescriptor | undefined;

Generic type variables:

Name Description
Obj extends object Constrained with the object type, by default of the value from the captured type of the provided object
Key extends keyof Obj Constrained with the property name from the Obj variable to ensure to not grab accidentally a property that does not exist in the Obj, by default of the value from the provided key

Parameters:

Name: type Description
object: Obj An object of a generic Obj type, by default of the type captured from the provided object, to get the property descriptor from it. The value is not being checked against the proper object type
key: Key A keyof type property name from the object, by default of type captured from the provided key as the name of the property that the object contains. The value is not being checked against its existence in the object

Throws:

Function throws nothing.

Returns:

Returns Type Description
PropertyDescriptor | undefined object -

The return value is a property descriptor from the object.

Usage:

// Example usage.
import { get, getDescriptor } from '@angular-package/property';

interface PersonShape {
  firstName: string;
}

class Person implements PersonShape {
  firstName = 'first name';
  age = 5;
}

class People {
  firstName!: string;
  age!: number;
}

const person: Person = new Person();
const people: People = new People();

getDescriptor(person, 'firstName'); // Returns {value: "first name", writable: true, enumerable: true, configurable: true}
getDescriptor(people, 'age'); // Returns undefined

const noProperty: any = 'no property';
const noObject: any = 'my string object';

getDescriptor(person, noProperty); // Returns undefined,
                                   // It won't give you any `Error`, it's like an object has property with undefined value.
getDescriptor(noObject, 'age'); // Returns undefined
                                // The same here.

getDescriptors()

Description:

Wrapper function for the Object static method getOwnPropertyDescriptors(). Use getDescriptors() or get.descriptors() to return all property descriptors from the specified object.

Returns an object containing all own property descriptors of an object.

Features:

Additional features instead of the default from the wrapped getOwnPropertyDescriptors() method.

  • Constraints the object parameter with a generic Obj variable of an object type.

Import:

import { get, getDescriptors } from '@angular-package/property';

Syntax:

const getDescriptors: GetDescriptors = <Obj extends object, Keys extends keyof Obj>(
  object: Obj,
  keys?: Keys[] // Not working in this version.
): ObjectPropertyDescriptors<Obj> | undefined;

Generic type variables:

Name Description
Obj extends object Constrained with the object type, by default of the value from the captured type of the provided object

Parameters:

Name: type Description
object: Obj An object of a generic Obj type, by default of the type captured from the provided object, to get all property descriptors from it. The value is being checked against the proper object type
keys?: Keys[] not working

Returns:

Returns Type Description
ObjectPropertyDescriptors<Obj> | undefined object -

The return value is an object with all property descriptors from the object.

Usage:

// Example usage.
import { get, getDescriptors } from '@angular-package/property';

interface PersonShape {
  firstName: string;
}

class Person implements PersonShape {
  firstName = 'first name';
  age = 5;
}

class People {
  firstName!: string;
  age!: number;
}

const person: Person = new Person();
const people: People = new People();

getDescriptors(person); // Returns {firstName: {…}, age: {…}}
getDescriptors(people); // Returns {}

getExistProperty()

Description:

Use getExistProperty() or get.existProperty() to return the value of the existing specified property from the specified object.

Features:

  • Guards getting the object property value by:
    • Constraints the object parameter with a generic Obj variable of an object type.
    • Constraints the key parameter with a Key variable which is of a key of the Obj variable.
    • Checks whether the provided object is of an object type and key of a Key type, and if not, throws an Error.
    • Checks whether the provided object has own property by using Object.prototype.hasOwnProperty() method.
  • Possibility to use custom callback function of a ResultCallback type.

Import:

import { get, getExistProperty } from '@angular-package/property';

Syntax:

const getExistProperty: GetExistProperty = <
  Obj extends object,
  Key extends keyof Obj
>(
  object: Obj,
  key: Key,
  callback: ResultCallback = callbacks.getExistProperty
): Obj[Key] =>
  guard.is.objectKey(object, key, callback)
    ? getProperty(object, key)
    : getProperty(object, key);

Generic type variables:

Name Description
Obj extends object Guarded with the object type, by default of the value from the captured type of the provided object linked with the return type Obj[Key]
Key extends keyof Obj Guarded with the property name from the Obj variable to ensure to not grab accidentally a property that does not exist in the Obj, by default of the value from the key argument that's linked to the return type Obj[Key]

Parameters:

Name: type Description
object: Obj An object of a generic Obj type, by default of the type captured from the provided object, to get the existing property value from it. The value is being checked against the proper object type
key: Key A keyof type property name from the existing object, by default of type captured from the provided key as the name of the property that the object contains. The value is being checked against its existence in the object

Throws:

By default throws an Error if the specified object does not exist or the object exists, but its key doesn't.

Returns:

Returns Type Description
Obj[Key] Captured The return type is of type captured from the property value from the object

The return value is a property value from the object.

Usage:

// Example usage.
import { get, getExistProperty } from '@angular-package/property';

interface PersonShape {
  firstName: string;
}

class Person implements PersonShape {
  firstName = 'first name';
  age = 5;
}

class People {
  firstName!: string;
  age!: number;
}

const person: Person = new Person();
const people: People = new People();

getExistProperty(person, 'firstName'); // Returns 'first name'
getExistProperty(people, 'age'); // Uncaught Error: Object with the specified key does not exist, got value {}
// Custom callback.
getExistProperty(people, 'age', (result: boolean, value: any) => {
  console.log(result); // `result` of the check is equal to the `false`
  console.log(value); // `value` is equal to `{}` - object is empty
  return result;
}); // Returns `undefined`, does not throws an Error cause of custom callback.

getProperties()

Description:

Use getProperties() or get.properties() to get specified properties from the specified object.

Features:

  • Constraints the object parameter with a generic Obj variable of an object type.
  • Constraints the key parameter with a Key variable which is of a key of the Obj variable.
  • Checks whether the provided object is of an object type and key of a Key type, and if not, throws an Error.
  • Checks whether the provided object has own property by using Object.prototype.hasOwnProperty() method.
  • Uses custom callback function of a ResultCallback type.
  • Returns an object with the specified properties from the specified object.

Import:

import { get, getProperties } from '@angular-package/property';

Syntax:

const getProperties: GetProperties = <
  Obj extends object,
  Keys extends keyof Obj
>(
  object: Obj,
  keys: Keys[]
): Pick<Obj, Keys> =>
  Object.assign(
    {},
    ...keys.map((key) =>
      !is.undefined(object[key]) ? { [key]: object[key] } : undefined
    )
  );

Generic type variables:

Name Description
Obj extends object Constrained with the object type, Obj variable by default of the value from the captured type of the provided object that is linked with the return type Pick<Obj, Keys>
Keys extends keyof Obj Constrained with the property name from the Obj variable to ensure to not grab accidentally a properties that does not exist in the Obj, by default of the value from the provided key that's linked to the return type Pick<Obj, Keys>

Parameters:

Name: type Description
object: Obj An object of a generic Obj type, by default of the type captured from the provided object, to get the values of the specified keys from it. The value is not being checked against the proper object type
keys: Keys[] An array of a keyof type property names from the object, by default of type captured from the provided keys in the array as the names of the properties that the object contains. The value is not being checked against the proper key type

Returns:

Returns Type Description
Pick<Obj, Keys> object The return type is an object of a generic Obj type, by default of type captured from the provided object with picked properties from the keys

The return value is an object with the specified properties.

Usage:

// Example usage.
import { getProperties } from '@angular-package/property';

interface PersonShape {
  firstName: string;
  age: number;
  lastName: string;
}

class Person implements PersonShape {
  firstName = 'first name';
  age = 5;
  lastName = 'last name';
}

class People {
  firstName!: string;
  age!: number;
}

const person: Person = new Person();
const people: People = new People();

getProperties(person, ['age',  'firstName', 'lastName']); // returns {age: 5, firstName: "first name", lastName: "last name"}
getProperties(people, ['age']); // returns {}

getProperty()

Description:

Use getProperty() or get.property() to return the value of the specified property from the object.

Import:

import { get, getProperty } from '@angular-package/property';

Syntax:

const getProperty: GetProperty = <
  Obj extends object,
  Key extends keyof Obj
>(
  object: Obj,
  key: Key
): Obj[Key] => object[key];

Generic type variables:

Name Description
Obj extends object Constrained with the object type, Obj variable by default of the value from the captured type of the provided object linked with the return type Obj[Key]
Key extends keyof Obj Constrained with the property name from the Obj variable to ensure to not grab accidentally a property that does not exist in the Obj, by default of the value from the provided key that's linked to the return type Obj[Key]

Parameters:

Name: type Description
object: Obj An object of a generic Obj type, by default of the type captured from the provided object, to get property value from it. The value is not being checked against the proper object type
key: Key A keyof type property name from the object, by default of type captured from the provided key as the name of the property that the object contains. The value is not being checked against proper key type

Returns:

Returns Type Description
Obj[Key] Captured The return type is of type captured from the property (Key) value from the object (Obj)

The return value is a property value from the object.

Usage:

// Example usage.
import { getProperty } from '@angular-package/property';

interface PersonShape {
  firstName: string;
  age: number;
  lastName: string;
}

class Person implements PersonShape {
  firstName = 'first name';
  age = 5;
  lastName = 'last name';
}

class People {
  firstName!: string;
  age!: number;
}

const person: Person = new Person();
const people: People = new People();

getProperty(person, 'age'); // Returns 5
getProperty(people, 'age'); // Returns undefined

setProperty()

Description:

Sets the value of indicated property by its name in the object.

Import:

import { setProperty } from '@angular-package/property';

Syntax:

const setProperty: SetProperty = <
  Obj extends object,
  Key extends keyof Obj
>(
  object: Obj,
  key: Key,
  value: Obj[Key]
): Obj[Key] => (object[key] = value);

Generic type variables:

Name Description
Obj extends object Constrained with the object type, Obj variable by default of the value from the captured type of the argument object linked with the return type Obj[Key]
Key extends keyof Obj Constrained with the property name from the Obj variable to ensure to not grab accidentally a property that does not exist in the Obj, by default of the value from the key argument that's linked to the return type Obj[Key]

Parameters:

Name: type Description
object: Obj An object of a generic Obj type, by default of the type captured from the provided object, to set the value with the indicated key as its property name. The value is not checked against the proper object type
key: Key A keyof type property name from the object, by default of type captured from the provided key as the name of the property that the object contains
value: Obj[Key] The value of the type captured from the provided key in the provided object. The value is not checked against the proper type

Returns:

Returns Type Description
Obj[Key] Captured The return type is of type captured from the property (Key) value from the object (Obj)

The return value is the value from the property of the object.

Usage:

// Example usage.
import { setProperty } from '@angular-package/property';

interface PersonShape {
  firstName: string;
  age: number;
  lastName: string;
}

class Person implements PersonShape {
  firstName = 'first name';
  age = 5;
  lastName = 'last name';
}

class People {
  firstName!: string;
  age!: number;
}

const person: Person = new Person();
const people: People = new People();

setProperty(person, 'age', 7); // Returns 7
setProperty(people, 'age', 27); // Returns 27

Descriptor sub package

Descriptor features to import.

// Class.
import {
  AccessorDescriptors,
  DataDescriptors,
  Descriptor,
} from '@angular-package/property';
// Interface.
import {
  AccessorDescriptor,
  DataDescriptor
} from '@angular-package/property';
// Type.
import {
  ThisAccessorDescriptor
} from '@angular-package/property';

Descriptor

Description:

Handles object property descriptor.

Features:

  • Strictly defines accessor and data descriptor with the defineAccessor() and defineData() static methods.
  • Strictly sets, and stores accessor and data descriptor with the Descriptor instance respectively set.accessor() and set.data() methods of the instance.
  • Get privately stored accessor descriptor defined by the set.accessor() method by using get.accessor property of the instance.
  • Get privately stored data descriptor defined by the set.data() method by using get.data property of the instance.

Strictly means, it guards provided descriptor by checking it against its unique keys and by picking only properties that belong to the appropriate descriptor.

Import:

import { Descriptor } from '@angular-package/property';

Syntax:

Descriptor<Value, Obj = any> { ... }

Descriptor static methods

Descriptor.defineAccessor()

Description:

Returns defined accessor descriptor of a ThisAccessorDescriptor<Value, Obj> type, on get or set property detected.

Syntax:

static defineAccessor<Value, Obj>(
  descriptor: ThisAccessorDescriptor<Value, Obj>,
  callback?: ResultCallback
): ThisAccessorDescriptor<Value, Obj> { ... }

Generic type variables:

Name Description
Value Guards the value type of the get() and set() methods of the descriptor object, and in the return type ThisAccessorDescriptor<Value, Obj>
Obj Gives the possibility to use the this keyword that refers to the Obj variable inside the get() and set() methods of the descriptor object, and in the return type ThisAccessorDescriptor<Value, Obj>

Parameters:

Name: type Description
descriptor: ThisAccessorDescriptor<Value, Obj> An object of a ThisAccessorDescriptor<Value, Obj> type to define with the default values of the CommonDescriptor
callback?: ResultCallback An optional ResultCallback function to handle the result of the check whether or not the descriptor is an object with get or set property, by default it uses accessorCallback() function

Throws:

Throws an Error if the descriptor is not an object of a ThisAccessorDescriptor<Value, Obj> type, which means it doesn't contain get or set property.

Returns:

The return value is an object of a ThisAccessorDescriptor<Value, Obj> type.

Usage:

// Example usage.
import { Descriptor } from '@angular-package/property';

interface PersonShape {
  firstName: string;
}

class Person implements PersonShape {
  firstName = '';
}

class People {
  firstName!: string;
}

const person: Person = new Person();
const people: People = new People();

const firstNameDescriptor = Descriptor.defineAccessor<string, Person>({
  configurable: false,
  enumerable: false,
  get(): string {
    return people.firstName;
  },
  set(value: string): void {
    people.firstName = value;
  },
});

// Define the property `firstName` in the `person` object to link with the same property in the `people` object.
// Changes to the property `firstName` in the `person` object affect the property `firstName` in the `people` object.
Object.defineProperty(person, 'firstName', firstNameDescriptor);

Descriptor.defineData()

Description:

Returns defined data descriptor of a DataDescriptor<Value> interface, on writable or value property detected.

Syntax:

static defineData<Value>(
  descriptor: DataDescriptor<Value>,
  callback: ResultCallback = dataCallback
): DataDescriptor<Value> { ... }

Generic type variables:

Name Description
Value Guards the value property from the descriptor object, and the return type of a DataDescriptor<Value> interface

Parameters:

Name: type Description
descriptor: DataDescriptor<Value> An object of a DataDescriptor<Value> interface to define with the default values of the CommonDescriptor
callback?: ResultCallback An optional ResultCallback function to handle the result of the check whether or not the descriptor is an object with writable or value property, by default it uses dataCallback() function

Throws:

Throws an Error if the descriptor is not an object of a DataDescriptor<Value> type, which means it doesn't contain writable or value property.

Returns:

The return value is an object of a DataDescriptor<Value> interface.

Usage:

// Example usage.
import { Descriptor } from '@angular-package/property';

interface PersonShape {
  firstName: string;
}

class Person implements PersonShape {
  firstName = '';
}

class People {
  firstName!: string;
}

const person: Person = new Person();
const people: People = new People();

const firstNameDescriptor = Descriptor.defineData<string>({
  configurable: false,
  enumerable: false,
  writable: false,
  value: people.firstName
});

// Defines the property `firstName` of a type string in the `person` object with the same value as the property in the `people` object.
Object.defineProperty(person, 'firstName', firstNameDescriptor);

Descriptor.fromObject()

Description:

Returns property descriptors from the specified detected object.

Syntax:

static fromObject<Obj extends object>(
  object: Obj
): ObjectPropertyDescriptors<Obj> | undefined { ... }

Generic type variables:

Name Description
Value Constraints the value property from the descriptor object, and the return type of a DataDescriptor<Value> interface

Parameters:

Name: type Description
descriptor: DataDescriptor<Value> An object of a DataDescriptor<Value> interface to define with the default values of the CommonDescriptor
callback?: ResultCallback An optional ResultCallback function to handle the result of the check whether or not the descriptor is an object with writable or value property, by default it uses dataCallback() function

Throws:

Throws an Error if the descriptor is not an object of a DataDescriptor<Value> type, which means it doesn't contain writable or value property.

Returns:

The return value is an object of a DataDescriptor<Value> interface.

Usage:

// Example usage.
import { Descriptor } from '@angular-package/property';

interface PersonShape {
  firstName: string;
}

class Person implements PersonShape {
  firstName = '';
}

class People {
  firstName!: string;
}

const person: Person = new Person();
const people: People = new People();

const firstNameDescriptor = Descriptor.fromObject();

// Defines the property `firstName` of a type string in the `person` object with the same value as the property in the `people` object.
Object.defineProperty(person, 'firstName', firstNameDescriptor);

Descriptor.fromProperty()

Description:

Returns property descriptors from the specified detected object.

Syntax:

static fromObject<Obj extends object>(
  object: Obj
): ObjectPropertyDescriptors<Obj> | undefined { ... }

Generic type variables:

Name Description
Value Guards the value property from the descriptor object, and the return type of a DataDescriptor<Value> interface

Parameters:

Name: type Description
descriptor: DataDescriptor<Value> An object of a DataDescriptor<Value> interface to define with the default values of the CommonDescriptor
callback?: ResultCallback An optional ResultCallback function to handle the result of the check whether or not the descriptor is an object with writable or value property, by default it uses dataCallback() function

Throws:

Throws an Error if the descriptor is not an object of a DataDescriptor<Value> type, which means it doesn't contain writable or value property.

Returns:

The return value is an object of a DataDescriptor<Value> interface.

Usage:

// Example usage.
import { Descriptor } from '@angular-package/property';

interface PersonShape {
  firstName: string;
}

class Person implements PersonShape {
  firstName = '';
}

class People {
  firstName!: string;
}

const person: Person = new Person();
const people: People = new People();

const firstNameDescriptor = Descriptor.fromObject();

// Defines the property `firstName` of a type string in the `person` object with the same value as the property in the `people` object.
Object.defineProperty(person, 'firstName', firstNameDescriptor);

AccessorDescriptors

Description:

Strictly defines, sets, and stores privately property accessor descriptor of a ThisAccessorDescriptor<Value, Obj> type.

Strictly means, methods picks configurable, enumerable, get, set properties from the provided descriptor object.

Features:

  • Guarded process of defining the object descriptor, but properties are not being checked against proper values.
  • Strictly defines property accessor descriptor.
  • Strictly sets, and stores at the same time property accessor descriptor.
  • Accessor descriptor is of a ThisAccessorDescriptor<Value, Obj> type:
    • The return value of the get() function is of a generic Value type.
    • The parameter of the set() function is of a generic Value type.
    • Keyword this refers to an Obj variable in both get() and set() functions.
  • Method set() of the instance and static define() picks configurable, enumerable, get, set properties from the provided data.
  • Get privately stored accessor descriptor defined by the set() method of the instance.

Import:

import { AccessorDescriptors } from '@angular-package/property';

Syntax:

AccessorDescriptors<Value, Obj = any> { ... }

AccessorDescriptors callback

accessorCallback()

The default callback function for the AccessorDescriptors.guard() static method that's used to guard provided value.

const accessorCallback: ResultCallback = callbackErrorMessage(
  `Accessor descriptor must be an \`ThisAccessorDescriptor<Value, Obj>\` type`
);

AccessorDescriptors static methods

AccessorDescriptors.define()

Description:

Returns defined accessor descriptor of a ThisAccessorDescriptor<Value, Obj> type, on get or set property detected.

Syntax:

static define<Value, Obj>(
  descriptor: ThisAccessorDescriptor<Value, Obj>,
  callback?: ResultCallback
): ThisAccessorDescriptor<Value, Obj> { ... }

Generic type variables:

Name Description
Value Guards the value type of the get() and set() functions of the descriptor object, and the return type ThisAccessorDescriptor<Value, Obj>
Obj Gives the possibility to use the this keyword that refers to the Obj variable inside the get() and set() functions of the descriptor object, and in the return type ThisAccessorDescriptor<Value, Obj>

Parameters:

Name: type Description
descriptor: ThisAccessorDescriptor<Value, Obj> An object of a ThisAccessorDescriptor<Value, Obj> type to define with the default values of the CommonDescriptor
callback?: ResultCallback An optional ResultCallback function to handle the result of the check whether or not the descriptor is an object with get or set property, by default it uses accessorCallback() function

Throws:

Throws an Error if the descriptor is not an object of a ThisAccessorDescriptor<Value, Obj> type, which means it doesn't contain get or set property.

Returns:

The return value is an object of a ThisAccessorDescriptor<Value, Obj> type.

Usage:

// Example usage.
import { AccessorDescriptors } from '@angular-package/property';

interface PersonShape {
  firstName: string;
}

class Person implements PersonShape {
  firstName = '';
}

class People {
  firstName!: string;
}

const person: Person = new Person();
const people: People = new People();

const firstNameDescriptor = AccessorDescriptors.define<string, Person>({
  get(): string {
    return people.firstName;
  },
  set(value: string): void {
    people.firstName = value;
  },
});

// Define the property `firstName` in the `person` object to link with the same property in the `people` object.
// Changes to the property `firstName` in the `person` object affect the property `firstName` in the `people` object.
Object.defineProperty(person, 'firstName', firstNameDescriptor);

AccessorDescriptors Constructor

AccessorDescriptors()

Description:

Creates an instance, and optionally sets an accessor descriptor of a ThisAccessorDescriptor<Value, Obj> type.

Syntax:

AccessorDescriptors<Value, Obj>(descriptor?: ThisAccessorDescriptor<Value, Obj>)

Generic type variables:

Name Description
Value Guards the value type of the get() and set() functions of the descriptor object
Obj Gives the possibility to use the this keyword that refers to the Obj variable inside the get() and set() functions of the descriptor object

Parameters:

Name: type Description
descriptor?: ThisAccessorDescriptor<Value, Obj> An optional object of a ThisAccessorDescriptor<Value, Obj> type to initially set accessor descriptor

Usage:

// Example usage.
import { AccessorDescriptors } from '@angular-package/property';

interface PersonShape {
  firstName: string;
}

class Person implements PersonShape {
  firstName = '';
}

class People {
  firstName!: string;
}

const person: Person = new Person();
const people: People = new People();

const firstNameDescriptor = new AccessorDescriptors<string, Person>({
  get(): string {
    return people.firstName;
  },
  set(value: string): void {
    people.firstName = value;
  },
});

AccessorDescriptors instance methods

AccessorDescriptors.prototype.set()

Description:

Strictly sets with the last saved descriptor values, and stores privately accessor descriptor of a ThisAccessorDescriptor<Value, Obj> type.

Syntax:

set(
  descriptor: ThisAccessorDescriptor<Value, Obj>,
  callback?: ResultCallback
): this { ... }

Parameters:

Name: type Description
descriptor: ThisAccessorDescriptor<Value, Obj> An object of a ThisAccessorDescriptor<Value, Obj> interface, to set with the last saved descriptor values
callback?: ResultCallback An optional ResultCallback function to handle the result of the check whether or not the descriptor is an object containing the get or set property, by default it uses accessorCallback() from the static guard() method

Throws:

Throws an Error if the descriptor is not an object of a ThisAccessorDescriptor<Value, Obj> type, which means doesn't contain get or set property.

Returns:

The return value is the AccessorDescriptors instance for the chaining.

Usage:

// Example usage.
import { AccessorDescriptors } from '@angular-package/property';

interface PersonShape {
  firstName: string;
}

class Person implements PersonShape {
  firstName = '';
}

class People {
  firstName!: string;
}

const person: Person = new Person();
const people: People = new People();

const firstNameDescriptor = new AccessorDescriptors<string, Person>().set({
  configurable: false,
  enumerable: false,
  get(): string {
    return people.firstName;
  },
  set(value: string): void {
    people.firstName = value;
  },
});

AccessorDescriptors instance properties

AccessorDescriptors.prototype.get

Description:

Get privately stored accessor descriptor of a ThisAccessorDescriptor<Value, Obj> type defined by the set() method.

Syntax:

get get(): ThisAccessorDescriptor<Value, Obj> { ... }

Usage:

// Example usage.
import { AccessorDescriptors } from '@angular-package/property';

interface PersonShape {
  firstName: string;
}

class Person implements PersonShape {
  firstName = '';
}

class People {
  firstName!: string;
}

const person: Person = new Person();
const people: People = new People();

const firstNameDescriptor = new AccessorDescriptors<string, Person>().set({
  configurable: false,
  enumerable: false,
  get(): string {
    return people.firstName;
  },
  set(value: string): void {
    people.firstName = value;
  },
});

// Define the property `firstName` in the `person` object to link with the same property in the `people` object.
// Changes to the property `firstName` in the `person` object affect the property `firstName` in the `people` object.
Object.defineProperty(person, 'firstName', firstNameDescriptor.get);

DataDescriptors

Description:

Strictly defines, sets, and stores privately property data descriptor of a DataDescriptor<Value> interface.

Strictly means, data descriptor of a DataDescriptor<Value> is type guarded and methods picks configurable, enumerable, writable, value properties from the provided descriptor object.

Features:

  • Data descriptor is of a DataDescriptor<Value> interface:
    • The value property is of a generic Value type.
  • Guarded process of defining the object descriptor, but properties are not being checked against proper values.
  • Strictly defines property data descriptor.
  • Strictly sets, and stores at the same time property data descriptor.
  • Method set() of the instance and static [define()][datadescriptors-define] picks configurable, enumerable, writable, value properties from the provided data.
  • Get privately stored data descriptor defined by the set() method of the instance.

Import:

import { DataDescriptors } from '@angular-package/property';

Syntax:

DataDescriptors<Value> { ... }

DataDescriptors static methods

DataDescriptors.define()

Description:

Returns strictly defined data descriptor of a DataDescriptor<Value> interface, on writable or value property detected.

Syntax:

static define<Value>(
  descriptor: DataDescriptor<Value>,
  callback?: ResultCallback
): DataDescriptor<Value> { ... }

Generic type variables:

Name Description
Value Guards the value property from the descriptor object, and the return type of a DataDescriptor<Value> interface

Parameters:

Name: type Description
descriptor: DataDescriptor<Value> An object of a DataDescriptor<Value> interface, to set with the default values of the CommonDescriptor
callback?: ResultCallback An optional ResultCallback function to handle the result of the check whether or not the descriptor is an object with writable or value property, by default it uses dataCallback() function from the static guard() method

Throws:

Throws an Error if the descriptor is not an object of a DataDescriptor<Value> interface, which means it doesn't contain writable or value property.

Returns:

The return value is an object of a DataDescriptor<Value> interface.

Usage:

// Example usage.
import { DataDescriptors } from '@angular-package/property';

interface PersonShape {
  firstName: string;
}

class Person implements PersonShape {
  firstName = '';
}

class People {
  firstName!: string;
}

const person: Person = new Person();
const people: People = new People();

const firstNameDescriptor = DataDescriptor.define<string, Person>({
  get(): string {
    return people.firstName;
  },
  set(value: string): void {
    people.firstName = value;
  },
});

// Define the property `firstName` in the `person` object to link with the same property in the `people` object.
// Changes to the property `firstName` in the `person` object affect the property `firstName` in the `people` object.
Object.defineProperty(person, 'firstName', firstNameDescriptor);

DataDescriptors Constructor

DataDescriptors()

Description:

Creates an instance, and optionally sets a data descriptor of a DataDescriptor<Value> interface.

Syntax:

DataDescriptors<Value>(descriptor?: DataDescriptor<Value>)

Generic type variables:

Name Description
Value Guards the value property from the descriptor object

Parameters:

Name: type Description
descriptor?: DataDescriptor<Value> An optional object of a DataDescriptor<Value> interface to initially set data descriptor

Usage:

// Example usage.
import { DataDescriptors } from '@angular-package/property';

interface PersonShape {
  firstName: string;
}

class Person implements PersonShape {
  firstName = '';
}

class People {
  firstName!: string;
}

const person: Person = new Person();
const people: People = new People();

const firstNameDescriptor = new DataDescriptors<string>({ // Initialize
  writable: false,
  value: 'not writable'
});

DataDescriptors instance methods

DataDescriptors.prototype.set()

Description:

Strictly sets with the last saved descriptor values, and stores privately data descriptor of a DataDescriptor<Value> interface.

Syntax:

set(
  descriptor: DataDescriptor<Value>,
  callback?: ResultCallback
): this { ... }

Generic type variables:

Name Description
Value Guards the value property from the descriptor object

Parameters:

Name: type Description
descriptor: DataDescriptor<Value> An object of a DataDescriptor<Value> interface, to set with the last saved descriptor
callback?: ResultCallback An optional ResultCallback function to handle the result of the check whether or not the descriptor is an object containing the writable or value property, by default it uses dataCallback() function from the static guard() method

Throws:

Throws an Error if the descriptor is not an object of a DataDescriptor<Value> type, which means doesn't contain writable or value property.

Returns:

The return value is the DataDescriptors instance for the chaining.

Usage:

// Example usage.
import { DataDescriptors } from '@angular-package/property';

interface PersonShape {
  firstName: string;
}

class Person implements PersonShape {
  firstName = '';
}

class People {
  firstName!: string;
}

const person: Person = new Person();
const people: People = new People();

const firstNameDescriptor = new DataDescriptors<string>()
.set({
  configurable: false,
  enumerable: false,
  writable: false,
  value: people.firstName
});

// Defines the property `firstName` in the `person` object with the same value as the property in the `people` object.
Object.defineProperty(person, 'firstName', firstNameDescriptor.get);

DataDescriptors instance properties

DataDescriptors.prototype.get

Description:

Get privately stored data descriptor of a DataDescriptor<Value> interface defined by the instance set() method.

Syntax:

get get(): DataDescriptor<Value> { ... }

Usage:

// Example usage.
import { DataDescriptors } from '@angular-package/property';

interface PersonShape {
  firstName: string;
}

class Person implements PersonShape {
  firstName = '';
}

class People {
  firstName!: string;
}

const person: Person = new Person();
const people: People = new People();

const firstNameDescriptor = new DataDescriptors<string>()
.set({
  configurable: false,
  enumerable: false,
  writable: false,
  value: people.firstName
})
// After set, get the value.
.get;

// Defines the property `firstName` in the `person` object with the same value as the property in the `people` object.
Object.defineProperty(person, 'firstName', firstNameDescriptor);

Descriptor interface

AccessorDescriptor

Description:

Descriptor with its unique optional get() and set() functions, of the Value type. For the accessor descriptor with also the object type, please use the type ThisAccessorDescriptor<Value, Obj>. More about property descriptors here.

interface AccessorDescriptor<Value> extends CommonDescriptor {
  get?: () => Value;
  set?: (value: Value) => void;
}

CommonDescriptor

Description:

Common keys configurable and enumerable of a boolean type for accessor and data descriptor, picked from the default PropertyDescriptor. More about property descriptors here.

Syntax:

interface CommonDescriptor
  extends Pick<PropertyDescriptor, 'configurable' | 'enumerable'> {}

DataDescriptor

Description:

Descriptor with its unique optional keys, writable of a boolean type and value of a generic Value type. More about property descriptors here.

Syntax:

interface DataDescriptor<Value> extends CommonDescriptor {
  writable?: boolean;
  value?: Value;
}

Descriptor type

ThisAccessorDescriptor

Description:

AccessorDescriptor interface as a type cause of ease of use this of an Obj type in the get() and set() functions. More about property descriptors here.

Syntax:

type ThisAccessorDescriptor<Value, Obj> = AccessorDescriptor<Value> &
  ThisType<Obj>;

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 © angular-package (license)


Package Sidebar

Install

npm i @angular-package/property

Weekly Downloads

1

Version

0.0.1-alpha.0

License

MIT

Unpacked Size

484 kB

Total Files

105

Last publish

Collaborators

  • sciborrudnicki
  • angularpackage