-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new functions to cast values to correct type
In many application repositories, we have an environmental variable validation engine. It is typical for this engine to have code that needs to transform incoming environmental variable values to values of a specific type. These tasks are repeatable in many projects: lidofinance/ethereum-validators-monitoring#224 lidofinance/node-operators-widget-backend-ts#41 lidofinance/lido-keys-api#216 It makes sense to move this repeated code from many projects to some common place. The new "transform" module introduced in this PR collects these common transformation functions.
- Loading branch information
1 parent
c0bf075
commit bad4c80
Showing
3 changed files
with
166 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
export const toNumber = | ||
({ defaultValue }: { defaultValue: number }) => | ||
({ value }: { value: unknown }) => { | ||
if (value === '' || value == null) { | ||
return defaultValue; | ||
} | ||
return Number(value); | ||
}; | ||
|
||
export const toBoolean = ({ defaultValue }: { defaultValue: boolean }) => { | ||
return function ({ value }: { value: unknown }) { | ||
if (value == null || value === '') { | ||
return defaultValue; | ||
} | ||
|
||
if (typeof value === 'boolean') { | ||
return value; | ||
} | ||
|
||
const str = String(value).toLowerCase().trim(); | ||
|
||
switch (str) { | ||
case 'true': | ||
case 'yes': | ||
case '1': | ||
return true; | ||
|
||
case 'false': | ||
case 'no': | ||
case '0': | ||
return false; | ||
|
||
default: | ||
return value; | ||
} | ||
}; | ||
}; | ||
|
||
export const toArrayOfUrls = (url: string | null | undefined): string[] => { | ||
if (url == null || url === '') { | ||
return []; | ||
} | ||
|
||
return url.split(',').map((str) => str.trim().replace(/\/$/, '')); | ||
}; |
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,120 @@ | ||
import { toNumber, toBoolean, toArrayOfUrls } from '../src'; | ||
|
||
describe('toNumber', () => { | ||
test('should return a default value if value is null or undefined', () => { | ||
expect(toNumber({ defaultValue: 3000 })({ value: undefined })).toEqual( | ||
3000, | ||
); | ||
expect(toNumber({ defaultValue: 3000 })({ value: null })).toEqual(3000); | ||
}); | ||
|
||
test('should return a default value if value is an empty string', () => { | ||
expect(toNumber({ defaultValue: 3000 })({ value: '' })).toEqual(3000); | ||
}); | ||
|
||
test('should convert a passed value to the number', () => { | ||
expect(toNumber({ defaultValue: 3000 })({ value: '5000' })).toEqual(5000); | ||
}); | ||
}); | ||
|
||
describe('toBoolean', () => { | ||
test('should return a default value if value is null or undefined', () => { | ||
expect(toBoolean({ defaultValue: true })({ value: undefined })).toEqual( | ||
true, | ||
); | ||
expect(toBoolean({ defaultValue: true })({ value: null })).toEqual(true); | ||
}); | ||
|
||
test('should return a default value if value is an empty string', () => { | ||
expect(toBoolean({ defaultValue: true })({ value: '' })).toEqual(true); | ||
}); | ||
|
||
test('should return the passed boolean value if it has a boolean type', () => { | ||
expect(toBoolean({ defaultValue: true })({ value: false })).toEqual(false); | ||
expect(toBoolean({ defaultValue: false })({ value: true })).toEqual(true); | ||
}); | ||
|
||
test('should return true if the passed value is the word "true" in any case', () => { | ||
expect(toBoolean({ defaultValue: false })({ value: 'true' })).toEqual(true); | ||
expect(toBoolean({ defaultValue: false })({ value: 'True' })).toEqual(true); | ||
expect(toBoolean({ defaultValue: false })({ value: 'TRUE' })).toEqual(true); | ||
}); | ||
|
||
test('should return true if the passed value is the word "yes" in any case', () => { | ||
expect(toBoolean({ defaultValue: false })({ value: 'yes' })).toEqual(true); | ||
expect(toBoolean({ defaultValue: false })({ value: 'Yes' })).toEqual(true); | ||
expect(toBoolean({ defaultValue: false })({ value: 'YES' })).toEqual(true); | ||
}); | ||
|
||
test('should return true if the passed value is 1', () => { | ||
expect(toBoolean({ defaultValue: false })({ value: '1' })).toEqual(true); | ||
expect(toBoolean({ defaultValue: false })({ value: 1 })).toEqual(true); | ||
}); | ||
|
||
test('should return false if the passed value is the word "false" in any case', () => { | ||
expect(toBoolean({ defaultValue: true })({ value: 'false' })).toEqual( | ||
false, | ||
); | ||
expect(toBoolean({ defaultValue: true })({ value: 'False' })).toEqual( | ||
false, | ||
); | ||
expect(toBoolean({ defaultValue: true })({ value: 'FALSE' })).toEqual( | ||
false, | ||
); | ||
}); | ||
|
||
test('should return false if the passed value is the word "no" in any case', () => { | ||
expect(toBoolean({ defaultValue: true })({ value: 'no' })).toEqual(false); | ||
expect(toBoolean({ defaultValue: true })({ value: 'No' })).toEqual(false); | ||
expect(toBoolean({ defaultValue: true })({ value: 'NO' })).toEqual(false); | ||
}); | ||
|
||
test('should return false if the passed value is 0', () => { | ||
expect(toBoolean({ defaultValue: true })({ value: '0' })).toEqual(false); | ||
expect(toBoolean({ defaultValue: true })({ value: 0 })).toEqual(false); | ||
}); | ||
|
||
test('should return the passed value if it is not equal to any of the above-listed strings', () => { | ||
expect(toBoolean({ defaultValue: true })({ value: 'abc' })).toEqual('abc'); | ||
expect(toBoolean({ defaultValue: true })({ value: '123' })).toEqual('123'); | ||
expect(toBoolean({ defaultValue: true })({ value: 123 })).toEqual(123); | ||
expect(toBoolean({ defaultValue: true })({ value: 2 })).toEqual(2); | ||
expect(toBoolean({ defaultValue: true })({ value: -1 })).toEqual(-1); | ||
|
||
expect(toBoolean({ defaultValue: false })({ value: 'abc' })).toEqual('abc'); | ||
expect(toBoolean({ defaultValue: false })({ value: '123' })).toEqual('123'); | ||
expect(toBoolean({ defaultValue: false })({ value: 123 })).toEqual(123); | ||
expect(toBoolean({ defaultValue: false })({ value: 2 })).toEqual(2); | ||
expect(toBoolean({ defaultValue: false })({ value: -1 })).toEqual(-1); | ||
}); | ||
}); | ||
|
||
describe('toArrayOfUrls', () => { | ||
test('should return an empty array if passed URL is null or undefined', () => { | ||
expect(toArrayOfUrls(undefined)).toEqual([]); | ||
expect(toArrayOfUrls(null)).toEqual([]); | ||
}); | ||
|
||
test('should return an empty array if passed URL is an empty string', () => { | ||
expect(toArrayOfUrls('')).toEqual([]); | ||
}); | ||
|
||
test('should split a string of comma-separated URLs into array of URLs', () => { | ||
expect(toArrayOfUrls('https://google.com,https://microsoft.com')).toEqual([ | ||
'https://google.com', | ||
'https://microsoft.com', | ||
]); | ||
}); | ||
|
||
test('should remove leading and trailing whitespaces from the URLs in the resulting list', () => { | ||
expect( | ||
toArrayOfUrls(' https://google.com , https://microsoft.com '), | ||
).toEqual(['https://google.com', 'https://microsoft.com']); | ||
}); | ||
|
||
test('should remove trailing slash from the URLs in the resulting list', () => { | ||
expect( | ||
toArrayOfUrls(' https://google.com/ , https://microsoft.com/ '), | ||
).toEqual(['https://google.com', 'https://microsoft.com']); | ||
}); | ||
}); |