-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cdk): new util
tuiCoerceBooleanProperty
- Loading branch information
1 parent
59f58ec
commit 21c0ed5
Showing
6 changed files
with
72 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './coerce-boolean-property'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
47
projects/cdk/coercion/test/coerce-boolean-property.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters