Skip to content

Commit

Permalink
feat(utils): trim, trimEnd, trimStart
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 45bccc7 commit 4a6f2bc
Show file tree
Hide file tree
Showing 14 changed files with 206 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/types/__tests__/trim-end.spec-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import type TestSubject from '../trim-end'

describe('unit-d:types/TrimEnd', () => {
it('should remove trailing whitespaces from T', () => {
it('should return T without trailing whitespaces', () => {
expectTypeOf<TestSubject<' foo '>>().toEqualTypeOf<' foo'>()
})
})
4 changes: 2 additions & 2 deletions src/types/__tests__/trim-start.spec-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import type TestSubject from '../trim-start'

describe('unit-d:types/TrimStart', () => {
it('should remove leading whitespaces from T', () => {
expectTypeOf<TestSubject<' foo '>>().toEqualTypeOf<'foo '>()
it('should return T without leading whitespaces', () => {
expectTypeOf<TestSubject<' bar '>>().toEqualTypeOf<'bar '>()
})
})
4 changes: 2 additions & 2 deletions src/types/__tests__/trim.spec-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import type TestSubject from '../trim'

describe('unit-d:types/Trim', () => {
it('should remove leading and trailing whitespaces from T', () => {
expectTypeOf<TestSubject<' foo '>>().toEqualTypeOf<'foo'>()
it('should return T without leading and trailing whitespaces', () => {
expectTypeOf<TestSubject<' baz '>>().toEqualTypeOf<'baz'>()
})
})
17 changes: 17 additions & 0 deletions src/utils/__tests__/trim-end.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @file Type Tests - trimEnd
* @module tutils/utils/tests/unit-d/trimEnd
*/

import type { TrimEnd } from '#src/types'
import type testSubject from '../trim-end'

describe('unit-d:utils/trimEnd', () => {
it('should return TrimEnd<T>', () => {
// Arrange
type T = 'foo '

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

import testSubject from '../trim-end'

describe('unit:utils/trimEnd', () => {
it('should return string without trailing whitespaces', () => {
// Arrange
const string: string = 'foo '

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

import type { TrimStart } from '#src/types'
import type testSubject from '../trim-start'

describe('unit-d:utils/trimStart', () => {
it('should return TrimStart<T>', () => {
// Arrange
type T = ' bar'

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

import testSubject from '../trim-start'

describe('unit:utils/trimStart', () => {
it('should return string without leading whitespaces', () => {
// Arrange
const string: string = ' bar'

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

import type { Trim } from '#src/types'
import type testSubject from '../trim'

describe('unit-d:utils/trim', () => {
it('should return Trim<T>', () => {
// Arrange
type T = ' baz '

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

import testSubject from '../trim'

describe('unit:utils/trim', () => {
it('should return string without leading and trailing whitespaces', () => {
// Arrange
const string: string = ' baz '

// Act + Expect
expect(testSubject(string)).to.equal(string.trim())
})
})
3 changes: 3 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ export { default as isSymbol } from './is-symbol'
export { default as isUndefined } from './is-undefined'
export { default as isUppercase } from './is-uppercase'
export { default as lowercase } from './lowercase'
export { default as trim } from './trim'
export { default as trimEnd } from './trim-end'
export { default as trimStart } from './trim-start'
export { default as uppercase } from './uppercase'
22 changes: 22 additions & 0 deletions src/utils/trim-end.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @file Utilities - trimEnd
* @module tutils/utils/trimEnd
*/

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

/**
* Removes trailing whitespace characters from `string`.
*
* @see {@linkcode TrimEnd}
*
* @template T - String to trim
*
* @param {T} string - String to trim
* @return {TrimEnd<T>} `string` with trailing whitespaces removed
*/
function trimEnd<T extends string>(string: T): TrimEnd<T> {
return string.trimEnd<T>()
}

export default trimEnd
22 changes: 22 additions & 0 deletions src/utils/trim-start.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @file Utilities - trimStart
* @module tutils/utils/trimStart
*/

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

/**
* Removes leading whitespace characters from `string`.
*
* @see {@linkcode TrimStart}
*
* @template T - String to trim
*
* @param {T} string - String to trim
* @return {TrimStart<T>} `string` with leading whitespaces removed
*/
function trimStart<T extends string>(string: T): TrimStart<T> {
return string.trimStart<T>()
}

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

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

/**
* Removes leading and trailing whitespace characters from `string`.
*
* @see {@linkcode Trim}
*
* @template T - String to trim
*
* @param {T} string - String to trim
* @return {Trim<T>} `string` with leading and trailing whitespaces removed
*/
function trim<T extends string>(string: T): Trim<T> {
return string.trim<T>()
}

export default trim
34 changes: 33 additions & 1 deletion typings/typescript/lib/lib.es5.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { OneOrMany } from '@flex-development/tutils'
import type {
OneOrMany,
Trim,
TrimEnd,
TrimStart
} from '@flex-development/tutils'

declare global {
interface String {
Expand Down Expand Up @@ -45,5 +50,32 @@ declare global {
* @return {Uppercase<T>} Uppercase string
*/
toUpperCase<T extends string>(): Uppercase<T>

/**
* Removes leading and trailing whitespace characters from a string.
*
* @template T - String being trimmed
*
* @return {Trim<T>} String with leading and trailing whitespaces removed
*/
trim<T extends string>(): Trim<T>

/**
* Removes trailing whitespace characters from a string.
*
* @template T - String being trimmed
*
* @return {TrimEnd<T>} String with trailing whitespaces removed
*/
trimEnd<T extends string>(): TrimEnd<T>

/**
* Removes leading whitespace characters from a string.
*
* @template T - String being trimmed
*
* @return {TrimStart<T>} String with leading whitespaces removed
*/
trimStart<T extends string>(): TrimStart<T>
}
}

0 comments on commit 4a6f2bc

Please sign in to comment.