Skip to content

Commit

Permalink
feat(cdk): new util tuiCoerceBooleanProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
nsbarsukov committed Apr 19, 2022
1 parent 59f58ec commit 21c0ed5
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 6 deletions.
9 changes: 9 additions & 0 deletions projects/cdk/coercion/coerce-boolean-property.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Coerces a data-bound value (typically a string) to a boolean.
* @example {@link https://material.angular.io/cdk/coercion/api#functions}
*/
export function tuiCoerceBooleanProperty(
value: string | number | boolean | null | undefined | object,
): boolean {
return value != null && `${value}` !== 'false';
}
1 change: 1 addition & 0 deletions projects/cdk/coercion/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './coerce-boolean-property';
7 changes: 7 additions & 0 deletions projects/cdk/coercion/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"ngPackage": {
"lib": {
"entryFile": "index.ts"
}
}
}
47 changes: 47 additions & 0 deletions projects/cdk/coercion/test/coerce-boolean-property.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {tuiCoerceBooleanProperty} from '../coerce-boolean-property';

describe('tuiCoerceBooleanProperty', () => {
it('should coerce undefined to false', () => {
expect(tuiCoerceBooleanProperty(undefined)).toBe(false);
});

it('should coerce null to false', () => {
expect(tuiCoerceBooleanProperty(null)).toBe(false);
});

it('should coerce the empty string to true', () => {
expect(tuiCoerceBooleanProperty('')).toBe(true);
});

it('should coerce zero to true', () => {
expect(tuiCoerceBooleanProperty(0)).toBe(true);
});

it('should coerce the string "false" to false', () => {
expect(tuiCoerceBooleanProperty('false')).toBe(false);
});

it('should coerce the boolean false to false', () => {
expect(tuiCoerceBooleanProperty(false)).toBe(false);
});

it('should coerce the boolean true to true', () => {
expect(tuiCoerceBooleanProperty(true)).toBe(true);
});

it('should coerce the string "true" to true', () => {
expect(tuiCoerceBooleanProperty('true')).toBe(true);
});

it('should coerce an arbitrary string to true', () => {
expect(tuiCoerceBooleanProperty('pink')).toBe(true);
});

it('should coerce an object to true', () => {
expect(tuiCoerceBooleanProperty({})).toBe(true);
});

it('should coerce an array to true', () => {
expect(tuiCoerceBooleanProperty([])).toBe(true);
});
});
1 change: 1 addition & 0 deletions projects/cdk/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from '@taiga-ui/cdk/abstract';
export * from '@taiga-ui/cdk/classes';
export * from '@taiga-ui/cdk/coercion';
export * from '@taiga-ui/cdk/components';
export * from '@taiga-ui/cdk/constants';
export * from '@taiga-ui/cdk/date-time';
Expand Down
13 changes: 7 additions & 6 deletions projects/kit/components/slider/slider-readonly.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import {
Self,
} from '@angular/core';
import {NgControl} from '@angular/forms';
import {tuiDefaultProp, TuiDestroyService, watch} from '@taiga-ui/cdk';
import {
tuiCoerceBooleanProperty,
tuiDefaultProp,
TuiDestroyService,
watch,
} from '@taiga-ui/cdk';
import {Observable} from 'rxjs';
import {takeUntil} from 'rxjs/operators';

Expand All @@ -29,10 +34,6 @@ export class TuiSliderReadonlyDirective {
@tuiDefaultProp()
readonly: '' | boolean = true;

get computedReadonly(): boolean {
return this.readonly === '' || this.readonly;
}

constructor(
@Optional()
@Self()
Expand All @@ -51,7 +52,7 @@ export class TuiSliderReadonlyDirective {

@HostListener('input')
onInput() {
if (this.computedReadonly) {
if (tuiCoerceBooleanProperty(this.readonly)) {
this.slider.value = this.lastValue;
}
}
Expand Down

0 comments on commit 21c0ed5

Please sign in to comment.