-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BHBC-2140: Add functions for safely lowercasing/trimming unknown valu…
…es (#939)
- Loading branch information
Showing
9 changed files
with
235 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { expect } from 'chai'; | ||
import { safeToLowerCase, safeTrim } from './string-utils'; | ||
|
||
describe('safeToLowerCase', () => { | ||
describe('returns value lowercase', () => { | ||
it('when value is a lowercase string', () => { | ||
expect(safeToLowerCase('string')).to.equal('string'); | ||
}); | ||
|
||
it('when value is an uppercase string', () => { | ||
expect(safeToLowerCase('STRING')).to.equal('string'); | ||
}); | ||
|
||
it('when value is a mixed case string', () => { | ||
expect(safeToLowerCase('sTRiNG')).to.equal('string'); | ||
}); | ||
}); | ||
|
||
describe('returns value unaltered', () => { | ||
it('when value is a negative number', () => { | ||
expect(safeToLowerCase(-123)).to.equal(-123); | ||
}); | ||
|
||
it('when value is a zero', () => { | ||
expect(safeToLowerCase(0)).to.equal(0); | ||
}); | ||
|
||
it('when value is a positive number', () => { | ||
expect(safeToLowerCase(123)).to.equal(123); | ||
}); | ||
|
||
it('when value is `false`', () => { | ||
expect(safeToLowerCase(false)).to.equal(false); | ||
}); | ||
|
||
it('when value is `true`', () => { | ||
expect(safeToLowerCase(true)).to.equal(true); | ||
}); | ||
|
||
it('when value is an empty object', () => { | ||
expect(safeToLowerCase({})).to.eql({}); | ||
}); | ||
|
||
it('when value is an empty array', () => { | ||
expect(safeToLowerCase([])).to.eql([]); | ||
}); | ||
|
||
it('when value is a non-empty array of numbers', () => { | ||
expect(safeToLowerCase([1, 2, 3])).to.eql([1, 2, 3]); | ||
}); | ||
|
||
it('when value is a non-empty array of strings', () => { | ||
expect(safeToLowerCase(['1', 'string', 'false'])).to.eql(['1', 'string', 'false']); | ||
}); | ||
|
||
it('when value is a function', () => { | ||
const fn = (a: number, b: number) => a * b; | ||
expect(safeToLowerCase(fn)).to.equal(fn); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('safeTrim', () => { | ||
describe('returns value trimmed', () => { | ||
it('when value is a lowercase string', () => { | ||
expect(safeTrim(' string ')).to.equal('string'); | ||
}); | ||
|
||
it('when value is an uppercase string', () => { | ||
expect(safeTrim(' STRING ')).to.equal('STRING'); | ||
}); | ||
|
||
it('when value is a mixed case string', () => { | ||
expect(safeTrim(' sTRiNG ')).to.equal('sTRiNG'); | ||
}); | ||
}); | ||
|
||
describe('returns value unaltered', () => { | ||
it('when value is a negative number', () => { | ||
expect(safeTrim(-123)).to.equal(-123); | ||
}); | ||
|
||
it('when value is a zero', () => { | ||
expect(safeTrim(0)).to.equal(0); | ||
}); | ||
|
||
it('when value is a positive number', () => { | ||
expect(safeTrim(123)).to.equal(123); | ||
}); | ||
|
||
it('when value is `false`', () => { | ||
expect(safeTrim(false)).to.equal(false); | ||
}); | ||
|
||
it('when value is `true`', () => { | ||
expect(safeTrim(true)).to.equal(true); | ||
}); | ||
|
||
it('when value is an empty object', () => { | ||
expect(safeTrim({})).to.eql({}); | ||
}); | ||
|
||
it('when value is an empty array', () => { | ||
expect(safeTrim([])).to.eql([]); | ||
}); | ||
|
||
it('when value is a non-empty array of numbers', () => { | ||
expect(safeTrim([1, 2, 3])).to.eql([1, 2, 3]); | ||
}); | ||
|
||
it('when value is a non-empty array of strings', () => { | ||
expect(safeTrim([' 1 ', ' string ', ' false '])).to.eql([' 1 ', ' string ', ' false ']); | ||
}); | ||
|
||
it('when value is a function', () => { | ||
const fn = (a: number, b: number) => a * b; | ||
expect(safeTrim(fn)).to.equal(fn); | ||
}); | ||
}); | ||
}); |
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,37 @@ | ||
import { isString } from 'lodash'; | ||
|
||
/** | ||
* Safely apply `.toLowerCase()` to a value of unknown type. | ||
* | ||
* If the value is not a string, then the original unaltered value will be returned. | ||
* | ||
* @export | ||
* @template T | ||
* @param {T} value | ||
* @return {*} {T} | ||
*/ | ||
export function safeToLowerCase<T>(value: T): T { | ||
if (isString(value)) { | ||
return (value.toLowerCase() as unknown) as T; | ||
} | ||
|
||
return value; | ||
} | ||
|
||
/** | ||
* Safely apply `.trim()` to a value of unknown type. | ||
* | ||
* If the value is not a string, then the original unaltered value will be returned. | ||
* | ||
* @export | ||
* @template T | ||
* @param {T} value | ||
* @return {*} {T} | ||
*/ | ||
export function safeTrim<T>(value: T): T { | ||
if (isString(value)) { | ||
return (value.trim() as unknown) as T; | ||
} | ||
|
||
return value; | ||
} |
Oops, something went wrong.