Skip to content

Commit

Permalink
🎉 NEW: add midTrim (#714)
Browse files Browse the repository at this point in the history
  • Loading branch information
jycouet authored Aug 22, 2024
1 parent 4b28005 commit 53804e4
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-schools-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@kitql/helpers': patch
---

add midTrim function
2 changes: 1 addition & 1 deletion packages/helpers/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type { Prettify } from './Prettify.js'
export { sleep } from './sleep.js'
export { stry, stry0, stryEq } from './stry/stry.js'
export { stry, stry0, stryEq, midTrim } from './stry/stry.js'

export { Log } from './Log.js'
export {
Expand Down
46 changes: 45 additions & 1 deletion packages/helpers/src/lib/stry/stry.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest'

import { stry, stry0, stryEq } from './stry.js'
import { midTrim, stry, stry0, stryEq } from './stry.js'

describe('kitql - helper - stry', () => {
it('space 2', () => {
Expand Down Expand Up @@ -196,3 +196,47 @@ describe('kitql - helper - stryEq', () => {
expect(result).toBe(false)
})
})

describe('kitql - helper - midTrim', () => {
it('midTrim 0', () => {
const x = midTrim('A')
expect(x).toBe(x)
})

it('midTrim 1', () => {
const x = midTrim('Auth - Users sessions')
expect(x).toBe(x)
})

it('midTrim 2', () => {
const i = '#######0123456789#######'
expect(i.length).toBe(24)
const x = midTrim(i)
expect(x.length).toBe(21)
expect(x).toBe('#######01...89#######')
})

it('midTrim 3', () => {
const i = '#######01234567890#######'
expect(i.length).toBe(25)
const x = midTrim(i)
expect(x.length).toBe(21)
expect(x).toBe('#######01...90#######')
})

it('midTrim 4', () => {
const i = '#######01234567890#######'
expect(i.length).toBe(25)
const x = midTrim(i, { midStr: '_' })
expect(x.length).toBe(21)
expect(x).toBe('#######012_890#######')
})

it('midTrim 5', () => {
const i = '0123456789'
expect(i.length).toBe(10)
const x = midTrim(i, { len: 3, midStr: '_' })
expect(x.length).toBe(3)
expect(x).toBe('0_9')
})
})
12 changes: 12 additions & 0 deletions packages/helpers/src/lib/stry/stry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,15 @@ export function stry(
export function stryEq(obj1: object | null | undefined, obj2: object | null | undefined): boolean {
return stry0(obj1) === stry0(obj2)
}

export function midTrim(str: string, o?: { len?: number; midStr?: string }): string {
const len = o?.len || 21
if (str.length > len) {
const midStr = o?.midStr || '...'
const reducedLen = len - midStr.length
const trimLength = Math.floor(reducedLen / 2)
return `${str.slice(0, trimLength).trim()}${midStr}${str.slice(-trimLength).trim()}`
}

return str
}

0 comments on commit 53804e4

Please sign in to comment.