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 all commits
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
57 changes: 54 additions & 3 deletions src/helpers/truncate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,65 @@ 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");
expect(truncate("", 0)).toBe("");
expect(truncate("", 1)).toBe("");
expect(truncate("1", 1)).toBe("1");
expect(truncate("1 ", 1)).toBe("1...");
expect(truncate("😴😄😃⛔🎠🚓🚇", 4)).toBe("😴😄😃⛔...");
expect(truncate("12345", 4)).toBe("1...");
expect(truncate("😴😄😃⛔🎠🚓🚇", 4)).toBe("😴...");
});
test("truncation at the start", () => {
expect(
truncate("Hello, world!", 8, {
position: "start",
})
).toBe("...orld!");
expect(
truncate("😴😄😃⛔🎠🚓🚇", 6, {
position: "start",
})
).toBe("...🎠🚓🚇");
});

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

test("truncation at the end", () => {
expect(
truncate("Hello, world!", 10, {
position: "end",
})
).toBe("Hello, ...");
expect(
truncate("😴😄😃⛔🎠🚓🚇", 5, {
position: "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, 55, {
ellipsis: "[...]",
position: "middle",
})
).toBe(expectedUrl);
});
});
33 changes: 31 additions & 2 deletions src/helpers/truncate.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
import { isPositiveInt } from "../validators/isNumber";

export const truncate = (arg: string, limit: number, ellipsis = "...") => {
export const truncate = (
arg: string,
limit: number,
{
ellipsis,
position,
}: { ellipsis?: string; position?: "start" | "middle" | "end" } = {}
) => {
if (!ellipsis) ellipsis = "...";
if (!position) position = "end";

if (!isPositiveInt(limit)) return arg;

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

const ellipsisLength = ellipsis.length;

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

return argArray.slice(0, limit).join("") + ellipsis;
switch (position) {
case "start":
const startSliceLength = limit - ellipsisLength;
return ellipsis + argArray.slice(-startSliceLength).join("");
case "middle": {
const startSliceLength = Math.ceil((limit - ellipsisLength) / 2);
const endSliceStart =
argArray.length - Math.floor((limit - ellipsisLength) / 2);
return (
argArray.slice(0, startSliceLength).join("") +
ellipsis +
argArray.slice(endSliceStart).join("")
);
}
case "end":
default:
return argArray.slice(0, limit - ellipsisLength).join("") + ellipsis;
}
};
Loading