Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added position to the truncate function #14

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# deverything

## 0.47.2

### Patch Changes

- added position to truncate

## 0.47.1

### Patch Changes
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": "deverything",
"version": "0.47.1",
"version": "0.47.2",
"description": "Everything you need for Dev",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
25 changes: 24 additions & 1 deletion src/helpers/truncate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, expect, test } from "@jest/globals";
import { truncate } from "./truncate";

describe("truncate", () => {
test("works", async () => {
test("basic functionality", async () => {
expect(truncate("asd", -Infinity)).toBe("asd");
expect(truncate("asd", Infinity)).toBe("asd");
expect(truncate("asd", -1)).toBe("asd");
Expand All @@ -12,4 +12,27 @@ describe("truncate", () => {
expect(truncate("1 ", 1)).toBe("1...");
expect(truncate("😴😄😃⛔🎠🚓🚇", 4)).toBe("😴😄😃⛔...");
});

test("truncation at the start", () => {
expect(truncate("Hello, world!", 5, "...", "start")).toBe("...orld!");
expect(truncate("😴😄😃⛔🎠🚓🚇", 3, "...", "start")).toBe("...🎠🚓🚇");
});

test("truncation in the middle", () => {
expect(truncate("Hello, world!", 8, "...", "middle")).toBe("Hell...rld!");
expect(truncate("😴😄😃⛔🎠🚓🚇", 5, "...", "middle")).toBe("😴😄...🚓🚇");
});

test("truncation at the end", () => {
expect(truncate("Hello, world!", 10, "...", "end")).toBe("Hello, wor...");
expect(truncate("😴😄😃⛔🎠🚓🚇", 5, "...", "end")).toBe("😴😄😃⛔🎠...");
});

test("custom ellipsis", () => {
const originalUrl =
"https://very-long-url.com/path?query=string&anotherParam=value";
const expectedUrl =
"https://very-long-url.com[...]string&anotherParam=value";
expect(truncate(originalUrl, 50, "[...]", "middle")).toBe(expectedUrl);
});
});
24 changes: 22 additions & 2 deletions src/helpers/truncate.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
import { isPositiveInt } from "../validators/isNumber";

export const truncate = (arg: string, limit: number, ellipsis = "...") => {
export const truncate = (
arg: string,
limit: number,
ellipsis = "...",
position: "start" | "middle" | "end" = "end"
tokenosopher marked this conversation as resolved.
Show resolved Hide resolved
) => {
if (!isPositiveInt(limit)) return arg;

const argArray = [...arg]; // convert string to array, emoji and unicode safe

if (argArray.length <= limit) return arg;

return argArray.slice(0, limit).join("") + ellipsis;
switch (position) {
case "start":
return ellipsis + argArray.slice(-limit).join("");
case "middle": {
const startSliceLength = Math.floor(limit / 2);
tokenosopher marked this conversation as resolved.
Show resolved Hide resolved
const endSliceStart = argArray.length - Math.floor(limit / 2);
return (
argArray.slice(0, startSliceLength).join("") +
ellipsis +
argArray.slice(endSliceStart).join("")
);
}
case "end":
default:
return argArray.slice(0, limit).join("") + ellipsis;
}
};
Loading