Skip to content

Commit

Permalink
feat(string): add squish
Browse files Browse the repository at this point in the history
  • Loading branch information
innocenzi committed Aug 26, 2023
1 parent 0c455ba commit 8a3bb3f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/string.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, it } from 'vitest'
import { capitalize, ensureStartsWith, ensureEndsWith, toForwardSlashes, template, toBackSlashes, replaceFirst, replaceLast, before, beforeLast, after, afterLast, between, betweenShrink } from './string'
import { capitalize, ensureStartsWith, ensureEndsWith, toForwardSlashes, template, toBackSlashes, replaceFirst, replaceLast, before, beforeLast, after, afterLast, between, betweenShrink, squish } from './string'

it('template', () => {
expect(
Expand Down Expand Up @@ -217,3 +217,9 @@ it('betweenShrink', () => {
expect(betweenShrink('foofoobar', 'foo', 'bar')).toEqual('foo')
expect(betweenShrink('foobarbar', 'foo', 'bar')).toEqual('')
})

it('squish', () => {
expect(squish(' hello world ')).toEqual('hello world')
expect(squish(' \t hello \t world \t ')).toEqual('hello world')
expect(squish(' \t hello\n \t world \t\n ')).toEqual('hello world')
})
7 changes: 7 additions & 0 deletions src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ export function ensureEndsWith(suffix: string, str: string) {
return str
}

/**
* Removes duplicate spaces from a string.
*/
export function squish(str: string) {
return str.replace(/\s+/g, ' ').trim()
}

/**
* Dead simple template engine, just like Python's `.format()`
* Support passing variables as either in index based or object/name based approach
Expand Down

0 comments on commit 8a3bb3f

Please sign in to comment.