Skip to content

Commit

Permalink
feat(utils): capitalize, isCapitalized
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed May 16, 2023
1 parent 188633c commit 4bda650
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 4 deletions.
16 changes: 16 additions & 0 deletions src/utils/__tests__/capitalize.spec-d.ts
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>>()
})
})
22 changes: 22 additions & 0 deletions src/utils/__tests__/capitalize.spec.ts
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)
})
})
})
14 changes: 14 additions & 0 deletions src/utils/__tests__/is-capitalized.spec-d.ts
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>
>()
})
})
27 changes: 27 additions & 0 deletions src/utils/__tests__/is-capitalized.spec.ts
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)
})
})
23 changes: 23 additions & 0 deletions src/utils/capitalize.ts
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
2 changes: 2 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
* @module tutils/utils
*/

export { default as capitalize } from './capitalize'
export { default as isAppEnv } from './is-app-env'
export { default as isArray } from './is-array'
export { default as isBigInt } from './is-big-int'
export { default as isBoolean } from './is-boolean'
export { default as isBooleanish } from './is-booleanish'
export { default as isCapitalized } from './is-capitalized'
export { default as isDate } from './is-date'
export { default as isEmptyString } from './is-empty-string'
export { default as isEmptyValue } from './is-empty-value'
Expand Down
29 changes: 29 additions & 0 deletions src/utils/is-capitalized.ts
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
6 changes: 4 additions & 2 deletions src/utils/is-lowercase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import lowercase from './lowercase'
/**
* Checks if `value` is a lowercase string.
*
* @template T - Lowercase string
*
* @param {unknown} value - Value to check
* @return {value is Lowercase<string>} `true` if `value` is lowercase string
* @return {value is Lowercase<T>} `true` if `value` is lowercase string
*/
const isLowercase = (value: unknown): value is Lowercase<string> => {
function isLowercase<T extends string>(value: unknown): value is Lowercase<T> {
return isString(value) && value === lowercase(value)
}

Expand Down
6 changes: 4 additions & 2 deletions src/utils/is-uppercase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import uppercase from './uppercase'
/**
* Checks if `value` is an uppercase string.
*
* @template T - Uppercase string
*
* @param {unknown} value - Value to check
* @return {value is Uppercase<string>} `true` if `value` is uppercase string
* @return {value is Uppercase<T>} `true` if `value` is uppercase string
*/
const isUppercase = (value: unknown): value is Uppercase<string> => {
function isUppercase<T extends string>(value: unknown): value is Uppercase<T> {
return isString(value) && value === uppercase(value)
}

Expand Down

0 comments on commit 4bda650

Please sign in to comment.