Skip to content

Commit

Permalink
refactor(utils): [capitalize] use at
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Aug 14, 2023
1 parent 078a3e5 commit cea32d9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/utils/__tests__/capitalize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ describe('unit:utils/capitalize', () => {
// Arrange
const cases: [...Parameters<typeof testSubject>, Capitalize<string>][] = [
['', ''],
[' ', ' '],
['foo', 'Foo'],
['Foobar', 'Foobar']
]

// Act + Expect
cases.forEach(([string, expected]) => {
expect(testSubject(string)).to.eql(expected)
cases.forEach(([str, expected]) => {
expect(testSubject(str)).to.equal(expected)
})
})
})
7 changes: 6 additions & 1 deletion src/utils/__tests__/is-capitalized.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ describe('unit:utils/isCapitalized', () => {

it('should return true if value is capitalized string', () => {
// Arrange
const cases: Parameters<typeof testSubject>[] = [[''], ['FOO'], ['Foobar']]
const cases: Parameters<typeof testSubject>[] = [
[''],
[' '],
['FOO'],
['Foobar']
]

// Act + Expect
cases.forEach(([value]) => expect(testSubject(value)).to.be.true)
Expand Down
3 changes: 2 additions & 1 deletion src/utils/capitalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @module tutils/utils/capitalize
*/

import at from './at'
import cast from './cast'
import isCapitalized from './is-capitalized'
import uppercase from './uppercase'
Expand All @@ -20,7 +21,7 @@ import uppercase from './uppercase'
const capitalize = <T extends string>(str: T): Capitalize<T> => {
return isCapitalized<T>(str)
? str
: cast(uppercase(str.charAt(0)) + str.slice(1))
: cast(uppercase(at(str, 0, '')) + str.slice(1))
}

export default capitalize
7 changes: 6 additions & 1 deletion src/utils/is-capitalized.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
*/

import at from './at'
import cast from './cast'
import isString from './is-string'
import trim from './trim'
import uppercase from './uppercase'

/**
Expand All @@ -22,7 +24,10 @@ import uppercase from './uppercase'
const isCapitalized = <T extends string>(
value: unknown
): value is Capitalize<T> => {
return isString(value) && value.startsWith(uppercase(at(value, 0, '')))
return (
isString(value) &&
(value = trim(value)).startsWith(uppercase(at(cast<string>(value), 0, '')))
)
}

export default isCapitalized

0 comments on commit cea32d9

Please sign in to comment.