Skip to content

Commit

Permalink
+ trim, trimStart, and trimEnd methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dzek69 committed May 16, 2024
1 parent c68d3ef commit a7fcba0
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 6 deletions.
16 changes: 11 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@ The format is based on [EZEZ Changelog](https://ezez.dev/changelog/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [UNRELEASED]
- (nothing yet)

## [4.0.0] - 2024-05-16
### Breaking
- `replaceDeep` will not mutate anything by default — previously it was wrongly documented as always mutating,
but it was not mutating objects. Mutating behavior can be enabled and additionally controlled by defining if instances
properties are allowed to be mutated.
- `serialize` to avoid excessive calls to serializer functions - now they are only called with non-plain objects or unknown data types, this breaks some rare use cases
- `replaceDeep` will not mutate anything by default anymore — previously it was wrongly documented as always mutating,
but it was not mutating objects. Mutating behavior can now be enabled and additionally controlled by defining if
instances properties are allowed to be mutated.
- [`serialize`] to avoid excessive calls to serializer functions - they are now only called with non-plain objects or
unknown data types, this breaks some rare use cases
### Changed
- `serialize` custom serializers now allow things like `Date` (that defines .toJSON) to be supported as the user would expect
- `serialize` custom serializers now allow things like `Date` (that defines .toJSON) to be supported as the user would
expect
### Added
- `replaceDeepByFn` method for more granular control over replacing values. `replaceDeep` is a simplified wrapper over
the new function
- `trim`, `trimStart`, and `trimEnd` methods
### Dev
- upgraded some jsdocs

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ezez/utils",
"version": "3.0.0+",
"version": "4.0.0",
"repository": "https://github.com/dzek69/bottom-line.git",
"author": "Jacek Nowacki @dzek69 <git-public@dzek.eu>",
"license": "MIT",
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export * from "./stripPrefix.js";
export * from "./stripSuffix.js";
export * from "./throttle.js";
export * from "./toggle.js";
export * from "./trim.js";
export * from "./trimEnd.js";
export * from "./trimStart.js";
export * from "./truthy.js";
export * from "./unique.js";
export * from "./wait.js";
Expand Down
22 changes: 22 additions & 0 deletions src/trim.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { trim } from "./trim";

describe("trim", () => {
it("should trim single character from both sides of the string", async () => {
must(trim("aaacataaa", "a")).equal("cat");
must(trim("abacteria", "a")).equal("bacteri");
must(trim("abc", "a")).equal("bc");
must(trim("abc", "c")).equal("ab");
must(trim("aajjja", "a")).equal("jjj");
});

it("should trim multiple characters as a whole", async () => {
must(trim("abopopab", "ab")).equal("opop");
must(trim("atitleb", "ab")).equal("atitleb");
must(trim("abtitleab", "ab")).equal("title");
});

it("can trim into empty string", async () => {
must(trim("abc", "abc")).equal("");
must(trim("a", "a")).equal("");
});
});
23 changes: 23 additions & 0 deletions src/trim.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { trimStart } from "./trimStart.js";
import { trimEnd } from "./trimEnd.js";

/**
* Removes given characters from both sides of the string.
* If you want to remove from one side of the string see {@link trimStart} and {@link trimEnd}.
*
* @param source - Source string.
* @param characters - Characters to remove, taken as a whole.
*
* @example
* trim("abcb", "ab"); // "cb"
* trim("!aaa!", "!"); // "aaa"
* trim("!aaa!!!", "!"); // "aaa"
* trim("!aaa!!!", "!!!"); // "!aaa"
*/
const trim = (source: string, characters: string) => {
return trimStart(trimEnd(source, characters), characters);
};

export {
trim,
};
20 changes: 20 additions & 0 deletions src/trimEnd.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { trimEnd } from "./trimEnd";

describe("trimEnd", () => {
it("should trim single character from the end of the string", async () => {
must(trimEnd("abc?", "?")).equal("abc");
must(trimEnd("a? b?c???", "?")).equal("a? b?c");
});

it("should trim multiple characters as a whole", async () => {
must(trimEnd("abc", "bc")).equal("a");

must(trimEnd("abcc", "bc")).equal("abcc");
must(trimEnd("abcb", "bc")).equal("abcb");
});

it("can trim into empty string", async () => {
must(trimEnd("abc", "abc")).equal("");
must(trimEnd("a", "a")).equal("");
});
});
22 changes: 22 additions & 0 deletions src/trimEnd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Removes given characters from the end of the string.
* See also: {@link trim} and {@link trimStart}.
*
* @param source - Source string.
* @param characters - Characters to remove, taken as a whole.
*
* @example
* trimEnd("abcxzyz", "yz"); // "abcxz"
* trimEnd("!aaa!!", "!"); // "!aaa"
*/
const trimEnd = (source: string, characters: string) => {
let s = source;
while (s.endsWith(characters)) {
s = s.slice(0, -characters.length);
}
return s;
};

export {
trimEnd,
};
20 changes: 20 additions & 0 deletions src/trimStart.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { trimStart } from "./trimStart";

describe("trimStart", () => {
it("should trim single character from the start of the string", async () => {
must(trimStart("?abc", "?")).equal("abc");
must(trimStart("aaabc", "a")).equal("bc");
});

it("should trim multiple characters as a whole", async () => {
must(trimStart("abc", "ab")).equal("c");

must(trimStart("aabcc", "ab")).equal("aabcc");
must(trimStart("babcb", "ab")).equal("babcb");
});

it("should trim into empty string if needed", async () => {
must(trimStart("abc", "abc")).equal("");
must(trimStart("aaa", "a")).equal("");
});
});
22 changes: 22 additions & 0 deletions src/trimStart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Removes given characters from the start of the string.
* See also: {@link trim} and {@link trimEnd}.
*
* @param source - Source string.
* @param characters - Characters to remove, taken as a whole.
*
* @example
* trimStart("abbcb", "ab"); // "bcb"
* trimStart("!!aaa!", "!"); // "aaa!"
*/
const trimStart = (source: string, characters: string) => {
let s = source;
while (s.startsWith(characters)) {
s = s.slice(characters.length);
}
return s;
};

export {
trimStart,
};

0 comments on commit a7fcba0

Please sign in to comment.