From 369d1aec054b1254f285ae396473aea7efe19c76 Mon Sep 17 00:00:00 2001 From: tokenosopher Date: Tue, 19 Mar 2024 16:12:24 +0000 Subject: [PATCH 1/2] truncation --- CHANGELOG.md | 6 ++++++ package.json | 2 +- src/helpers/truncate.test.ts | 25 ++++++++++++++++++++++++- src/helpers/truncate.ts | 24 ++++++++++++++++++++++-- 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4481b74..effaeaf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # deverything +## 0.47.2 + +### Patch Changes + +- added position to truncate + ## 0.47.1 ### Patch Changes diff --git a/package.json b/package.json index 94fa6e6..0e63bda 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/helpers/truncate.test.ts b/src/helpers/truncate.test.ts index 5f607d0..ffebbe2 100644 --- a/src/helpers/truncate.test.ts +++ b/src/helpers/truncate.test.ts @@ -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"); @@ -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); + }); }); diff --git a/src/helpers/truncate.ts b/src/helpers/truncate.ts index 3088968..af64f5b 100644 --- a/src/helpers/truncate.ts +++ b/src/helpers/truncate.ts @@ -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" +) => { 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); + 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; + } }; From 4bb1b889c39ed02796e9f606396508c30d017bfb Mon Sep 17 00:00:00 2001 From: tokenosopher Date: Wed, 20 Mar 2024 15:14:44 +0000 Subject: [PATCH 2/2] fix length added object as optional arguments --- src/helpers/truncate.test.ts | 48 ++++++++++++++++++++++++++++-------- src/helpers/truncate.ts | 21 +++++++++++----- 2 files changed, 53 insertions(+), 16 deletions(-) diff --git a/src/helpers/truncate.test.ts b/src/helpers/truncate.test.ts index ffebbe2..3e586d8 100644 --- a/src/helpers/truncate.test.ts +++ b/src/helpers/truncate.test.ts @@ -9,23 +9,46 @@ describe("truncate", () => { 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!", 5, "...", "start")).toBe("...orld!"); - expect(truncate("😴😄😃⛔🎠🚓🚇", 3, "...", "start")).toBe("...🎠🚓🚇"); + expect( + truncate("Hello, world!", 8, { + position: "start", + }) + ).toBe("...orld!"); + expect( + truncate("😴😄😃⛔🎠🚓🚇", 6, { + position: "start", + }) + ).toBe("...🎠🚓🚇"); }); test("truncation in the middle", () => { - expect(truncate("Hello, world!", 8, "...", "middle")).toBe("Hell...rld!"); - expect(truncate("😴😄😃⛔🎠🚓🚇", 5, "...", "middle")).toBe("😴😄...🚓🚇"); + 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, "...", "end")).toBe("Hello, wor..."); - expect(truncate("😴😄😃⛔🎠🚓🚇", 5, "...", "end")).toBe("😴😄😃⛔🎠..."); + expect( + truncate("Hello, world!", 10, { + position: "end", + }) + ).toBe("Hello, ..."); + expect( + truncate("😴😄😃⛔🎠🚓🚇", 5, { + position: "end", + }) + ).toBe("😴😄..."); }); test("custom ellipsis", () => { @@ -33,6 +56,11 @@ describe("truncate", () => { "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); + expect( + truncate(originalUrl, 55, { + ellipsis: "[...]", + position: "middle", + }) + ).toBe(expectedUrl); }); }); diff --git a/src/helpers/truncate.ts b/src/helpers/truncate.ts index af64f5b..34b560c 100644 --- a/src/helpers/truncate.ts +++ b/src/helpers/truncate.ts @@ -3,21 +3,30 @@ import { isPositiveInt } from "../validators/isNumber"; export const truncate = ( arg: string, limit: number, - ellipsis = "...", - position: "start" | "middle" | "end" = "end" + { + 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; switch (position) { case "start": - return ellipsis + argArray.slice(-limit).join(""); + const startSliceLength = limit - ellipsisLength; + return ellipsis + argArray.slice(-startSliceLength).join(""); case "middle": { - const startSliceLength = Math.floor(limit / 2); - const endSliceStart = argArray.length - Math.floor(limit / 2); + const startSliceLength = Math.ceil((limit - ellipsisLength) / 2); + const endSliceStart = + argArray.length - Math.floor((limit - ellipsisLength) / 2); return ( argArray.slice(0, startSliceLength).join("") + ellipsis + @@ -26,6 +35,6 @@ export const truncate = ( } case "end": default: - return argArray.slice(0, limit).join("") + ellipsis; + return argArray.slice(0, limit - ellipsisLength).join("") + ellipsis; } };