Ensure
To convert properties to be used with Angular's @Input
Boolean
Transforms the input into a boolean.
Will throw an error if this fails.
Code
@Input({transform: (v: any) => ensureBool('exampleProperty', v)}) public exampleProperty: boolean;| Argument | Type | Description |
|---|---|---|
key | string | Name of the property, or key, that's being assigned to. Used for observability. |
value | any | Value to be transformed. |
defaultValue? | boolean | Optional: Default value that is returned if valuenullundefined |
Number
Transforms the input into a number.
Will throw an error if this fails.
Code
@Input({transform: (v: any) => ensureNumber('exampleProperty', v)}) public exampleProperty: number;ensureNumber(key, value, defaultValue)
| Argument | Type | Description |
|---|---|---|
key | string | Name of the property, or key, that's being assigned to. Used for observability. |
value | any | Value to be transformed. |
defaultValue? | boolean | Optional: Default value that is returned if valuenullundefined |
Mutually Exclusive
Ensures that only one of the two provided properties are being assigned a value.
Will throw an error if this fails.
Code
@Input({transform: function(v: any) => ensureMutuallyExclusive('exampleProperty', this, 'otherProperty', v)}) public exampleProperty: any;| Argument | Type | Description |
|---|---|---|
key | string | Name of the property, or key, that's being assigned to. Used for observability. |
component | Record<string, any> | The component this transform function is operating on. Can be retrieved using the `this` keyword in the transform block. |
mutuallyExclusiveKey | string | Name of the property, or key, that cannot also be truekeytrue |
value | any | Value to be transformed |
Enum
Transforms the input into the provided enum.
Will throw an error if this fails.
Code
@Input({transform: (v: any) => ensureEnum('exampleProperty', MyEnum, v)}) public exampleProperty: MyEnum;| Argument | Type | Description |
|---|---|---|
key | string | Name of the property, or key, that's being assigned to. Used for observability. |
targetEnum | <T> | Enum that the value must be a part of. |
value | any | Value to be transformed. |
defaultValue? | boolean | Optional: Default value that is returned if valuenullundefined |
Regexp
Transforms the input into a regex.
Will attempt to parse non-string inputs into a regex, which may have surprising results.
Will throw an error if an invalid regex pattern is passed in (eg. '(()').
Code
@Input({transform: (v: any) => ensureRegex('exampleProperty', v)}) public exampleProperty: Regex;| Argument | Type | Description |
|---|---|---|
key | string | Name of the property, or key, that's being assigned to. Used for observability. |
value | any | Value to be transformed. |
defaultValue? | any | Optional: Default value that is returned if valuenullundefined |