-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utils):
capitalize
, isCapitalized
Signed-off-by: Lexus Drumgold <[email protected]>
- Loading branch information
1 parent
188633c
commit 4bda650
Showing
9 changed files
with
141 additions
and
4 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,16 @@ | ||
/** | ||
* @file Type Tests - capitalize | ||
* @module tutils/utils/tests/unit-d/capitalize | ||
*/ | ||
|
||
import type testSubject from '../capitalize' | ||
|
||
describe('unit-d:utils/capitalize', () => { | ||
it('should return Capitalize<T>', () => { | ||
// Arrange | ||
type T = string | ||
|
||
// Expect | ||
expectTypeOf<typeof testSubject<T>>().returns.toEqualTypeOf<Capitalize<T>>() | ||
}) | ||
}) |
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,22 @@ | ||
/** | ||
* @file Unit Tests - capitalize | ||
* @module tutils/utils/tests/unit/capitalize | ||
*/ | ||
|
||
import testSubject from '../capitalize' | ||
|
||
describe('unit:utils/capitalize', () => { | ||
it('should return capitalized string', () => { | ||
// Arrange | ||
const cases: [...Parameters<typeof testSubject>, Capitalize<string>][] = [ | ||
['', ''], | ||
['foo', 'Foo'], | ||
['Foobar', 'Foobar'] | ||
] | ||
|
||
// Act + Expect | ||
cases.forEach(([string, expected]) => { | ||
expect(testSubject(string)).to.deep.equal(expected) | ||
}) | ||
}) | ||
}) |
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,14 @@ | ||
/** | ||
* @file Type Tests - isCapitalized | ||
* @module tutils/utils/tests/unit-d/isCapitalized | ||
*/ | ||
|
||
import type testSubject from '../is-capitalized' | ||
|
||
describe('unit-d:utils/isCapitalized', () => { | ||
it('should guard Capitalize<T>', () => { | ||
expectTypeOf<typeof testSubject>().guards.toEqualTypeOf< | ||
Capitalize<string> | ||
>() | ||
}) | ||
}) |
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,27 @@ | ||
/** | ||
* @file Unit Tests - isCapitalized | ||
* @module tutils/utils/tests/unit/isCapitalized | ||
*/ | ||
|
||
import testSubject from '../is-capitalized' | ||
|
||
describe('unit:utils/isCapitalized', () => { | ||
it('should return false if value is not capitalized string', () => { | ||
// Arrange | ||
const cases: Parameters<typeof testSubject>[] = [ | ||
[faker.date.anytime()], | ||
[faker.person.firstName().toLowerCase()] | ||
] | ||
|
||
// Act + Expect | ||
cases.forEach(([value]) => expect(testSubject(value)).to.be.false) | ||
}) | ||
|
||
it('should return true if value is capitalized string', () => { | ||
// Arrange | ||
const cases: Parameters<typeof testSubject>[] = [[''], ['FOO'], ['Foobar']] | ||
|
||
// Act + Expect | ||
cases.forEach(([value]) => expect(testSubject(value)).to.be.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* @file Utilities - capitalize | ||
* @module tutils/utils/capitalize | ||
*/ | ||
|
||
import isCapitalized from './is-capitalized' | ||
import uppercase from './uppercase' | ||
|
||
/** | ||
* Capitalizes the first character in `string`. | ||
* | ||
* @template T - String to capitalize | ||
* | ||
* @param {string} string - String to capitalize | ||
* @return {Capitalize<T>} Capitalized `string` | ||
*/ | ||
function capitalize<T extends string>(string: T): Capitalize<T> { | ||
return isCapitalized<T>(string) | ||
? string | ||
: ((uppercase(string.charAt(0)) + string.slice(1)) as Capitalize<T>) | ||
} | ||
|
||
export default capitalize |
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,29 @@ | ||
/** | ||
* @file Utilities - isCapitalized | ||
* @module tutils/utils/isCapitalized | ||
*/ | ||
|
||
import isEmptyString from './is-empty-string' | ||
import isString from './is-string' | ||
import uppercase from './uppercase' | ||
|
||
/** | ||
* Checks if `value` is a capitalized string. | ||
* | ||
* A capitalized string one where the first character is uppercase. | ||
* | ||
* @template T - Capitalized string | ||
* | ||
* @param {unknown} value - Value to check | ||
* @return {value is Capitalize<T>} `true` if `value` is capitalized string | ||
*/ | ||
function isCapitalized<T extends string>( | ||
value: unknown | ||
): value is Capitalize<T> { | ||
return ( | ||
isEmptyString(value) || | ||
(isString(value) && value.startsWith(uppercase(value.charAt(0)))) | ||
) | ||
} | ||
|
||
export default isCapitalized |
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