Skip to content

Commit

Permalink
feat(utils): lowercase, uppercase
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 c8b806a commit 95cb9b9
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/types/or-lowercase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
* @see https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html#lowercasestringtype
*
* @template T - String type
* @template T - String type to evaluate
*/
type OrLowercase<T extends string> = Lowercase<T> | T

Expand Down
2 changes: 1 addition & 1 deletion src/types/or-uppercase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
* @see https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html#uppercasestringtype
*
* @template T - String type
* @template T - String type to evaluate
*/
type OrUppercase<T extends string> = T | Uppercase<T>

Expand Down
16 changes: 16 additions & 0 deletions src/utils/__tests__/lowercase.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @file Type Tests - lowercase
* @module tutils/utils/tests/unit-d/lowercase
*/

import type testSubject from '../lowercase'

describe('unit-d:utils/lowercase', () => {
it('should return Lowercase<T>', () => {
// Arrange
type T = 'STRING'

// Expect
expectTypeOf<typeof testSubject<T>>().returns.toEqualTypeOf<Lowercase<T>>()
})
})
16 changes: 16 additions & 0 deletions src/utils/__tests__/lowercase.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @file Unit Tests - lowercase
* @module tutils/utils/tests/unit/lowercase
*/

import testSubject from '../lowercase'

describe('unit:utils/lowercase', () => {
it('should return lowercase string', () => {
// Arrange
const string: string = faker.internet.email().toLowerCase()

// Act + Expect
expect(testSubject(string)).to.equal(string.toLocaleLowerCase())
})
})
16 changes: 16 additions & 0 deletions src/utils/__tests__/uppercase.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @file Type Tests - uppercase
* @module tutils/utils/tests/unit-d/uppercase
*/

import type testSubject from '../uppercase'

describe('unit-d:utils/uppercase', () => {
it('should return Uppercase<T>', () => {
// Arrange
type T = 'string'

// Expect
expectTypeOf<typeof testSubject<T>>().returns.toEqualTypeOf<Uppercase<T>>()
})
})
16 changes: 16 additions & 0 deletions src/utils/__tests__/uppercase.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @file Unit Tests - uppercase
* @module tutils/utils/tests/unit/uppercase
*/

import testSubject from '../uppercase'

describe('unit:utils/uppercase', () => {
it('should return uppercase string', () => {
// Arrange
const string: string = faker.person.firstName().toLowerCase()

// Act + Expect
expect(testSubject(string)).to.equal(string.toLocaleUpperCase())
})
})
2 changes: 2 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ export { default as isPrimitive } from './is-primitive'
export { default as isString } from './is-string'
export { default as isSymbol } from './is-symbol'
export { default as isUndefined } from './is-undefined'
export { default as lowercase } from './lowercase'
export { default as uppercase } from './uppercase'
27 changes: 27 additions & 0 deletions src/utils/lowercase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @file Utilities - lowercase
* @module tutils/utils/lowercase
*/

import type { OneOrMany } from '#src/types'

/**
* Converts all alphabetic characters in `string` to lowercase.
*
* @see https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleLowerCase
* @see https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase
*
* @template T - String to convert
*
* @param {T} string - String to convert
* @param {OneOrMany<string> | undefined} [locales] - Locales to consider
* @return {Lowercase<T>} Lowercase `string`
*/
function lowercase<T extends string>(
string: T,
locales?: OneOrMany<string> | undefined
): Lowercase<T> {
return string.toLocaleLowerCase<T>(locales)
}

export default lowercase
27 changes: 27 additions & 0 deletions src/utils/uppercase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @file Utilities - uppercase
* @module tutils/utils/uppercase
*/

import type { OneOrMany } from '#src/types'

/**
* Converts all alphabetic characters in `string` to uppercase.
*
* @see https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleUpperCase
* @see https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase
*
* @template T - String to convert
*
* @param {T} string - String to convert
* @param {OneOrMany<string> | undefined} [locales] - Locales to consider
* @return {Uppercase<T>} Uppercase `string`
*/
function uppercase<T extends string>(
string: T,
locales?: OneOrMany<string> | undefined
): Uppercase<T> {
return string.toLocaleUpperCase<T>(locales)
}

export default uppercase
62 changes: 47 additions & 15 deletions typings/typescript/lib/lib.es5.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,49 @@
interface String {
/**
* Converts all the alphabetic characters in a string to lowercase.
*
* @return {string} Lowercase string
*/
toLowerCase(): Lowercase<string>
import type { OneOrMany } from '@flex-development/tutils'

/**
* Converts all alphabetic characters to lowercase, taking into account the
* host environment's current locale.
*
* @param {string | string[]} [locales] - Locales to consider
* @return {string} Lowercase string
*/
toLocaleLowerCase(): Lowercase<string>
declare global {
interface String {
/**
* Converts all alphabetic characters to lowercase, taking into account the
* host environment's current locale.
*
* @template T - String being converted
*
* @param {OneOrMany<string> | undefined} [locales] - Locales to consider
* @return {T} Lowercase string
*/
toLocaleLowerCase<T extends string>(
locales?: OneOrMany<string> | undefined
): Lowercase<T>

/**
* Converts all alphabetic characters to uppercase, taking into account the
* host environment's current locale.
*
* @template T - String being converted
*
* @param {OneOrMany<string> | undefined} [locales] - Locales to consider
* @return {T} Lowercase string
*/
toLocaleUpperCase<T extends string>(
locales?: OneOrMany<string> | undefined
): Uppercase<T>

/**
* Converts all the alphabetic characters in a string to lowercase.
*
* @template T - String being converted
*
* @return {Lowercase<T>} Lowercase string
*/
toLowerCase<T extends string>(): Lowercase<T>

/**
* Converts all the alphabetic characters in a string to uppercase.
*
* @template T - String being converted
*
* @return {Uppercase<T>} Uppercase string
*/
toUpperCase<T extends string>(): Uppercase<T>
}
}

0 comments on commit 95cb9b9

Please sign in to comment.