-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
65 additions
and
5 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,5 @@ | ||
--- | ||
"@smithy/util-config-provider": minor | ||
--- | ||
|
||
Add numberSelector utility |
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from "./booleanSelector"; | ||
export * from "./numberSelector"; | ||
export * from "./types"; |
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,32 @@ | ||
import { numberSelector } from "./numberSelector"; | ||
import { SelectorType } from "./types"; | ||
|
||
describe(numberSelector.name, () => { | ||
const key = "key"; | ||
const obj: { [key]: any } = {} as any; | ||
|
||
describe.each(Object.entries(SelectorType))(`Selector %s`, (selectorKey, selectorValue) => { | ||
beforeEach(() => { | ||
delete obj[key]; | ||
}); | ||
|
||
it(`should return undefined if ${key} is not defined`, () => { | ||
expect(numberSelector(obj, key, SelectorType[selectorKey])).toBeUndefined(); | ||
}); | ||
|
||
it.each([ | ||
[0, "0"], | ||
[1, "1"], | ||
])(`should return number %s if ${key}="%s"`, (output, input) => { | ||
obj[key] = input; | ||
expect(numberSelector(obj, key, SelectorType[selectorKey])).toBe(output); | ||
}); | ||
|
||
it.each(["yes", "no", undefined, null, void 0, ""])(`should throw if ${key}=%s`, (input) => { | ||
obj[key] = input; | ||
expect(() => numberSelector(obj, key, SelectorType[selectorKey])).toThrow( | ||
new TypeError(`Cannot load ${selectorValue} '${key}'. Expected number, got '${obj[key]}'.`) | ||
); | ||
}); | ||
}); | ||
}); |
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,19 @@ | ||
import { SelectorType } from "./types"; | ||
|
||
/** | ||
* Returns number value for string value, if the string is defined in obj[key]. | ||
* Returns undefined, if obj[key] is not defined. | ||
* Throws error for all other cases. | ||
* | ||
* @internal | ||
*/ | ||
export const numberSelector = (obj: Record<string, string | undefined>, key: string, type: SelectorType) => { | ||
if (!(key in obj)) return undefined; | ||
|
||
const numberValue = parseInt(obj[key] as string, 10); | ||
if (Number.isNaN(numberValue)) { | ||
throw new TypeError(`Cannot load ${type} '${key}'. Expected number, got '${obj[key]}'.`); | ||
} | ||
|
||
return numberValue; | ||
}; |
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,4 @@ | ||
export enum SelectorType { | ||
ENV = "env", | ||
CONFIG = "shared config entry", | ||
} |