-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support and enforce number and boolean transform functions in a…
…ngular
- Loading branch information
1 parent
5a09508
commit b7f4e21
Showing
23 changed files
with
341 additions
and
87 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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import {auBooleanAttribute, auNumberAttribute} from './coercion'; | ||
import {describe, expect, it} from 'vitest'; | ||
|
||
describe('coercion functions', () => { | ||
describe('auBooleanAttribute', () => { | ||
it('should coerce undefined to undefined', () => { | ||
expect(auBooleanAttribute(undefined)).toBe(undefined); | ||
}); | ||
|
||
it('should coerce null to false', () => { | ||
expect(auBooleanAttribute(null)).toBe(false); | ||
}); | ||
|
||
it('should coerce the empty string to true', () => { | ||
expect(auBooleanAttribute('')).toBe(true); | ||
}); | ||
|
||
it('should coerce zero to true', () => { | ||
expect(auBooleanAttribute(0)).toBe(true); | ||
}); | ||
|
||
it('should coerce the string "false" to false', () => { | ||
expect(auBooleanAttribute('false')).toBe(false); | ||
}); | ||
|
||
it('should coerce the boolean false to false', () => { | ||
expect(auBooleanAttribute(false)).toBe(false); | ||
}); | ||
|
||
it('should coerce the boolean true to true', () => { | ||
expect(auBooleanAttribute(true)).toBe(true); | ||
}); | ||
|
||
it('should coerce the string "true" to true', () => { | ||
expect(auBooleanAttribute('true')).toBe(true); | ||
}); | ||
|
||
it('should coerce an arbitrary string to true', () => { | ||
expect(auBooleanAttribute('pink')).toBe(true); | ||
}); | ||
|
||
it('should coerce an object to true', () => { | ||
expect(auBooleanAttribute({})).toBe(true); | ||
}); | ||
|
||
it('should coerce an array to true', () => { | ||
expect(auBooleanAttribute([])).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('auNumberAttribute', () => { | ||
it('should coerce undefined to undefined', () => { | ||
expect(auNumberAttribute(undefined)).toBe(undefined); | ||
}); | ||
|
||
it('should coerce null to NaN', () => { | ||
expect(auNumberAttribute(null)).toBeNaN(); | ||
}); | ||
|
||
it('should coerce true to NaN', () => { | ||
expect(auNumberAttribute(true)).toBeNaN(); | ||
}); | ||
|
||
it('should coerce false to NaN', () => { | ||
expect(auNumberAttribute(false)).toBeNaN(); | ||
}); | ||
|
||
it('should coerce the empty string to NaN', () => { | ||
expect(auNumberAttribute('')).toBeNaN(); | ||
}); | ||
|
||
it('should coerce the string "1" to 1', () => { | ||
expect(auNumberAttribute('1')).toBe(1); | ||
}); | ||
|
||
it('should coerce the string "123.456" to 123.456', () => { | ||
expect(auNumberAttribute('123.456')).toBe(123.456); | ||
}); | ||
|
||
it('should coerce the string "-123.456" to -123.456', () => { | ||
expect(auNumberAttribute('-123.456')).toBe(-123.456); | ||
}); | ||
|
||
it('should coerce an arbitrary string to NaN', () => { | ||
expect(auNumberAttribute('pink')).toBeNaN(); | ||
}); | ||
|
||
it('should coerce an arbitrary string prefixed with a number to NaN', () => { | ||
expect(auNumberAttribute('123pink')).toBeNaN(); | ||
}); | ||
|
||
it('should coerce the number 1 to 1', () => { | ||
expect(auNumberAttribute(1)).toBe(1); | ||
}); | ||
|
||
it('should coerce the number 123.456 to 123.456', () => { | ||
expect(auNumberAttribute(123.456)).toBe(123.456); | ||
}); | ||
|
||
it('should coerce the number -123.456 to -123.456', () => { | ||
expect(auNumberAttribute(-123.456)).toBe(-123.456); | ||
}); | ||
|
||
it('should coerce an object to NaN', () => { | ||
expect(auNumberAttribute({})).toBeNaN(); | ||
}); | ||
|
||
it('should coerce an array to NaN', () => { | ||
expect(auNumberAttribute([])).toBeNaN(); | ||
}); | ||
}); | ||
}); |
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,35 @@ | ||
import {booleanAttribute, numberAttribute} from '@angular/core'; | ||
|
||
/** | ||
* Transforms a value (typically a string) to a boolean. | ||
* Intended to be used as a transform function of an input. | ||
* | ||
* @usageNotes | ||
* ```typescript | ||
* @Input({ transform: auBooleanAttribute }) status: boolean | undefined; | ||
* ``` | ||
* @param value Value to be transformed. | ||
*/ | ||
export function auBooleanAttribute(value: unknown): boolean | undefined { | ||
if (value === undefined) { | ||
return undefined; | ||
} | ||
return booleanAttribute(value); | ||
} | ||
|
||
/** | ||
* Transforms a value (typically a string) to a number. | ||
* Intended to be used as a transform function of an input. | ||
* @param value Value to be transformed. | ||
* | ||
* @usageNotes | ||
* ```typescript | ||
* @Input({ transform: auNumberAttribute }) id: number | undefined; | ||
* ``` | ||
*/ | ||
export function auNumberAttribute(value: unknown): number | undefined { | ||
if (value === undefined) { | ||
return undefined; | ||
} | ||
return numberAttribute(value); | ||
} |
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
Oops, something went wrong.