Skip to content

Commit

Permalink
Add dropPrefix and dropSuffix to String
Browse files Browse the repository at this point in the history
  • Loading branch information
samhh committed Oct 26, 2023
1 parent fb098af commit a23cf67
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This project adheres to semantic versioning.
- Add an `Eq` instance to `URLPath`.
- Add `singleton`, `appendAt`, `upsertAt`, `deleteAt`, `lookup`, `lookupFirst`, `toLeadingString`, `keys`, `values`, `size`, `concatBy`, `fromMap` and `toMap`, and `Eq`, `Semigroup` and `Monoid` instances to `URLSearchParams`.
- Add `integerFromString` to `Number`.
- Add `fromBool` to `String`.
- Add `dropPrefix`, `dropSuffix`, and `fromBool` to `String`.
- Fix `fromString` in `Number` not parsing floats.
- Deprecate `getParam`, `getAllForParam`, and `setParam` in `URLSearchParams`.

Expand Down
58 changes: 58 additions & 0 deletions docs/modules/String.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ Added in v0.1.0
- [append](#append)
- [dropLeft](#dropleft)
- [dropLeftWhile](#dropleftwhile)
- [dropPrefix](#dropprefix)
- [dropRight](#dropright)
- [dropRightWhile](#droprightwhile)
- [dropSuffix](#dropsuffix)
- [fromBool](#frombool)
- [fromNumber](#fromnumber)
- [head](#head)
Expand Down Expand Up @@ -142,6 +144,34 @@ assert.strictEqual(dropFilename('File.hs'), '.hs')

Added in v0.6.0

## dropPrefix

Drop a prefix if present, else return the input string unmodified.

**Signature**

```ts
export declare const dropPrefix: (prefix: string) => Endomorphism<string>
```
```hs
dropPrefix :: string -> Endomorphism string
```

**Example**

```ts
import { dropPrefix } from 'fp-ts-std/String'

const f = dropPrefix('foo')

assert.strictEqual(f('foobar'), 'bar')
assert.strictEqual(f('barfoo'), 'barfoo')
assert.strictEqual(f('foofoo'), 'foo')
```

Added in v0.18.0

## dropRight

Drop a number of characters from the end of a string, returning a new
Expand Down Expand Up @@ -204,6 +234,34 @@ assert.deepStrictEqual(dropRightVowels('hellooo'), 'hell')

Added in v0.7.0

## dropSuffix

Drop a suffix if present, else return the input string unmodified.

**Signature**

```ts
export declare const dropSuffix: (suffix: string) => Endomorphism<string>
```
```hs
dropSuffix :: string -> Endomorphism string
```

**Example**

```ts
import { dropSuffix } from 'fp-ts-std/String'

const f = dropSuffix('bar')

assert.strictEqual(f('foobar'), 'foo')
assert.strictEqual(f('barfoo'), 'barfoo')
assert.strictEqual(f('barbar'), 'bar')
```

Added in v0.18.0

## fromBool

Convert a boolean to a string.
Expand Down
36 changes: 36 additions & 0 deletions src/String.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,3 +683,39 @@ export const words = S.split(/\s/)
* @since 0.14.0
*/
export const unwords = join(" ")

/**
* Drop a prefix if present, else return the input string unmodified.
*
* @example
* import { dropPrefix } from 'fp-ts-std/String'
*
* const f = dropPrefix('foo')
*
* assert.strictEqual(f('foobar'), 'bar')
* assert.strictEqual(f('barfoo'), 'barfoo')
* assert.strictEqual(f('foofoo'), 'foo')
*
* @category 3 Functions
* @since 0.18.0
*/
export const dropPrefix = (prefix: string): Endomorphism<string> =>
when(S.startsWith(prefix))(dropLeft(S.size(prefix)))

/**
* Drop a suffix if present, else return the input string unmodified.
*
* @example
* import { dropSuffix } from 'fp-ts-std/String'
*
* const f = dropSuffix('bar')
*
* assert.strictEqual(f('foobar'), 'foo')
* assert.strictEqual(f('barfoo'), 'barfoo')
* assert.strictEqual(f('barbar'), 'bar')
*
* @category 3 Functions
* @since 0.18.0
*/
export const dropSuffix = (suffix: string): Endomorphism<string> =>
when(S.endsWith(suffix))(dropRight(S.size(suffix)))
42 changes: 42 additions & 0 deletions test/String.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import {
isSpace,
words,
unwords,
dropPrefix,
dropSuffix,
} from "../src/String"
import * as O from "fp-ts/Option"
import * as NEA from "fp-ts/NonEmptyArray"
Expand Down Expand Up @@ -796,4 +798,44 @@ describe("String", () => {
expect(f(["a", "b", "c"])).toBe("a b c")
})
})

describe("dropPrefix", () => {
const f = dropPrefix

it("removes prefix if present", () => {
expect(f("foo")("foobar")).toBe("bar")

fc.assert(
fc.property(fc.string(), fc.string(), (x, y) => f(y)(y + x) === x),
)
})

it("does not modify string without prefix", () => {
expect(f("bar")("foobar")).toBe("foobar")
})

it("does not modify string given empty prefix", () => {
fc.assert(fc.property(fc.string(), x => f("")(x) === x))
})
})

describe("dropSuffix", () => {
const f = dropSuffix

it("removes suffix if present", () => {
expect(f("bar")("foobar")).toBe("foo")

fc.assert(
fc.property(fc.string(), fc.string(), (x, y) => f(y)(x + y) === x),
)
})

it("does not modify string without suffix", () => {
expect(f("foo")("foobar")).toBe("foobar")
})

it("does not modify string given empty suffix", () => {
fc.assert(fc.property(fc.string(), x => f("")(x) === x))
})
})
})

0 comments on commit a23cf67

Please sign in to comment.