@code-pushup/utils 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -11,6 +11,11 @@ import { z as z2 } from "zod";
11
11
  import { z } from "zod";
12
12
  import { MATERIAL_ICONS } from "@code-pushup/portal-client";
13
13
 
14
+ // packages/models/src/lib/implementation/limits.ts
15
+ var MAX_SLUG_LENGTH = 128;
16
+ var MAX_TITLE_LENGTH = 256;
17
+ var MAX_DESCRIPTION_LENGTH = 65536;
18
+
14
19
  // packages/models/src/lib/implementation/utils.ts
15
20
  var slugRegex = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
16
21
  var filenameRegex = /^(?!.*[ \\/:*?"<>|]).+$/;
@@ -46,12 +51,12 @@ function executionMetaSchema(options = {
46
51
  function slugSchema(description = "Unique ID (human-readable, URL-safe)") {
47
52
  return z.string({ description }).regex(slugRegex, {
48
53
  message: "The slug has to follow the pattern [0-9a-z] followed by multiple optional groups of -[0-9a-z]. e.g. my-slug"
49
- }).max(128, {
50
- message: "slug can be max 128 characters long"
54
+ }).max(MAX_SLUG_LENGTH, {
55
+ message: `slug can be max ${MAX_SLUG_LENGTH} characters long`
51
56
  });
52
57
  }
53
58
  function descriptionSchema(description = "Description (markdown)") {
54
- return z.string({ description }).max(65536).optional();
59
+ return z.string({ description }).max(MAX_DESCRIPTION_LENGTH).optional();
55
60
  }
56
61
  function docsUrlSchema(description = "Documentation site") {
57
62
  return urlSchema(description).optional().or(z.string().max(0));
@@ -60,7 +65,7 @@ function urlSchema(description) {
60
65
  return z.string({ description }).url();
61
66
  }
62
67
  function titleSchema(description = "Descriptive name") {
63
- return z.string({ description }).max(256);
68
+ return z.string({ description }).max(MAX_TITLE_LENGTH);
64
69
  }
65
70
  function metaSchema(options) {
66
71
  const {
@@ -566,6 +571,19 @@ function formatDuration(duration) {
566
571
  }
567
572
  return `${(duration / 1e3).toFixed(2)} s`;
568
573
  }
574
+ function truncateText(text, maxChars) {
575
+ if (text.length <= maxChars) {
576
+ return text;
577
+ }
578
+ const ellipsis = "...";
579
+ return text.slice(0, maxChars - ellipsis.length) + ellipsis;
580
+ }
581
+ function truncateTitle(text) {
582
+ return truncateText(text, MAX_TITLE_LENGTH);
583
+ }
584
+ function truncateDescription(text) {
585
+ return truncateText(text, MAX_DESCRIPTION_LENGTH);
586
+ }
569
587
 
570
588
  // packages/utils/src/lib/guards.ts
571
589
  function isPromiseFulfilledResult(result) {
@@ -1544,5 +1562,8 @@ export {
1544
1562
  slugify,
1545
1563
  toArray,
1546
1564
  toUnixPath,
1565
+ truncateDescription,
1566
+ truncateText,
1567
+ truncateTitle,
1547
1568
  verboseUtils
1548
1569
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@code-pushup/utils",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "dependencies": {
5
5
  "@code-pushup/models": "*",
6
6
  "bundle-require": "^4.0.1",
package/src/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export { CliArgsObject, ProcessConfig, ProcessError, ProcessObserver, ProcessResult, executeProcess, objectToCliArgs, } from './lib/execute-process';
2
2
  export { FileResult, MultipleFileResults, crawlFileSystem, ensureDirectoryExists, fileExists, findLineNumberInText, importEsmModule, logMultipleFileResults, pluginWorkDir, readJsonFile, readTextFile, toUnixPath, } from './lib/file-system';
3
- export { formatBytes, formatDuration, pluralize, pluralizeToken, slugify, } from './lib/formatting';
3
+ export { formatBytes, formatDuration, pluralize, pluralizeToken, slugify, truncateDescription, truncateText, truncateTitle, } from './lib/formatting';
4
4
  export { getLatestCommit, git } from './lib/git';
5
5
  export { isPromiseFulfilledResult, isPromiseRejectedResult, } from './lib/guards';
6
6
  export { logMultipleResults } from './lib/log-results';
@@ -3,3 +3,6 @@ export declare function pluralize(text: string): string;
3
3
  export declare function formatBytes(bytes: number, decimals?: number): string;
4
4
  export declare function pluralizeToken(token: string, times?: number): string;
5
5
  export declare function formatDuration(duration: number): string;
6
+ export declare function truncateText(text: string, maxChars: number): string;
7
+ export declare function truncateTitle(text: string): string;
8
+ export declare function truncateDescription(text: string): string;