MCPcopy Create free account
hub / github.com/atomicdata-dev/atomic-data-browser / truncateUrl

Function truncateUrl

lib/src/truncate.ts:5–27  ·  view source on GitHub ↗
(
  url: string,
  num: number,
  truncateBack?: boolean,
)

Source from the content-addressed store, hash-verified

3 * to the window hostname
4 */
5export function truncateUrl(
6 url: string,
7 num: number,
8 truncateBack?: boolean,
9): string {
10 // Remove the schema, the https:// part
11 let noSchema = url.replace(/(^\w+:|^)\/\//, '');
12 if (
13 typeof window !== 'undefined' &&
14 window?.location &&
15 noSchema.startsWith(window.location.hostname)
16 ) {
17 noSchema = noSchema.slice(window.location.hostname.length);
18 }
19 if (noSchema.length <= num) {
20 return noSchema;
21 }
22 if (truncateBack) {
23 const tooMuch = noSchema.length - num;
24 return '...' + noSchema.slice(tooMuch);
25 }
26 return noSchema.slice(0, num) + '...';
27}

Callers 2

useTitleFunction · 0.90
PropValFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected