@code-pushup/utils 0.6.3 → 0.6.5

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
@@ -537,6 +537,12 @@ var reportSchema = packageVersionSchema({
537
537
  )
538
538
  );
539
539
 
540
+ // packages/utils/src/lib/constants.ts
541
+ var SCORE_COLOR_RANGE = {
542
+ GREEN_MIN: 0.9,
543
+ YELLOW_MIN: 0.5
544
+ };
545
+
540
546
  // packages/utils/src/lib/file-system.ts
541
547
  import { bundleRequire } from "bundle-require";
542
548
  import chalk from "chalk";
@@ -761,19 +767,19 @@ function formatReportScore(score) {
761
767
  return Math.round(score * 100).toString();
762
768
  }
763
769
  function getRoundScoreMarker(score) {
764
- if (score >= 0.9) {
770
+ if (score >= SCORE_COLOR_RANGE.GREEN_MIN) {
765
771
  return "\u{1F7E2}";
766
772
  }
767
- if (score >= 0.5) {
773
+ if (score >= SCORE_COLOR_RANGE.YELLOW_MIN) {
768
774
  return "\u{1F7E1}";
769
775
  }
770
776
  return "\u{1F534}";
771
777
  }
772
778
  function getSquaredScoreMarker(score) {
773
- if (score >= 0.9) {
779
+ if (score >= SCORE_COLOR_RANGE.GREEN_MIN) {
774
780
  return "\u{1F7E9}";
775
781
  }
776
- if (score >= 0.5) {
782
+ if (score >= SCORE_COLOR_RANGE.YELLOW_MIN) {
777
783
  return "\u{1F7E8}";
778
784
  }
779
785
  return "\u{1F7E5}";
@@ -978,38 +984,6 @@ function executeProcess(cfg) {
978
984
  });
979
985
  });
980
986
  }
981
- function objectToCliArgs(params) {
982
- if (!params) {
983
- return [];
984
- }
985
- return Object.entries(params).flatMap(([key, value]) => {
986
- if (key === "_") {
987
- if (Array.isArray(value)) {
988
- return value;
989
- } else {
990
- return [value + ""];
991
- }
992
- }
993
- const prefix = key.length === 1 ? "-" : "--";
994
- if (Array.isArray(value)) {
995
- return value.map((v) => `${prefix}${key}="${v}"`);
996
- }
997
- if (Array.isArray(value)) {
998
- return value.map((v) => `${prefix}${key}="${v}"`);
999
- }
1000
- if (typeof value === "string") {
1001
- return [`${prefix}${key}="${value}"`];
1002
- }
1003
- if (typeof value === "number") {
1004
- return [`${prefix}${key}=${value}`];
1005
- }
1006
- if (typeof value === "boolean") {
1007
- return [`${prefix}${value ? "" : "no-"}${key}`];
1008
- }
1009
- throw new Error(`Unsupported type ${typeof value} for key ${key}`);
1010
- });
1011
- }
1012
- objectToCliArgs({ z: 5 });
1013
987
 
1014
988
  // packages/utils/src/lib/git.ts
1015
989
  import simpleGit from "simple-git";
@@ -1415,19 +1389,18 @@ function reportToDetailSection(report) {
1415
1389
  return output;
1416
1390
  }
1417
1391
  function withColor({ score, text }) {
1418
- let str = text ?? formatReportScore(score);
1392
+ const formattedScore = text ?? formatReportScore(score);
1419
1393
  const style2 = text ? chalk3 : chalk3.bold;
1420
- if (score < 0.5) {
1421
- str = style2.red(str);
1422
- } else if (score < 0.9) {
1423
- str = style2.yellow(str);
1424
- } else {
1425
- str = style2.green(str);
1394
+ if (score >= SCORE_COLOR_RANGE.GREEN_MIN) {
1395
+ return style2.green(formattedScore);
1426
1396
  }
1427
- return str;
1397
+ if (score >= SCORE_COLOR_RANGE.YELLOW_MIN) {
1398
+ return style2.yellow(formattedScore);
1399
+ }
1400
+ return style2.red(formattedScore);
1428
1401
  }
1429
1402
 
1430
- // packages/utils/src/lib/transformation.ts
1403
+ // packages/utils/src/lib/transform.ts
1431
1404
  function toArray(val) {
1432
1405
  return Array.isArray(val) ? val : [val];
1433
1406
  }
@@ -1466,6 +1439,37 @@ function factorOf(items, filterFn) {
1466
1439
  const filterCount = items.filter(filterFn).length;
1467
1440
  return filterCount === 0 ? 1 : (itemCount - filterCount) / itemCount;
1468
1441
  }
1442
+ function objectToCliArgs(params) {
1443
+ if (!params) {
1444
+ return [];
1445
+ }
1446
+ return Object.entries(params).flatMap(([key, value]) => {
1447
+ if (key === "_") {
1448
+ if (Array.isArray(value)) {
1449
+ return value;
1450
+ } else {
1451
+ return [value + ""];
1452
+ }
1453
+ }
1454
+ const prefix = key.length === 1 ? "-" : "--";
1455
+ if (Array.isArray(value)) {
1456
+ return value.map((v) => `${prefix}${key}="${v}"`);
1457
+ }
1458
+ if (Array.isArray(value)) {
1459
+ return value.map((v) => `${prefix}${key}="${v}"`);
1460
+ }
1461
+ if (typeof value === "string") {
1462
+ return [`${prefix}${key}="${value}"`];
1463
+ }
1464
+ if (typeof value === "number") {
1465
+ return [`${prefix}${key}=${value}`];
1466
+ }
1467
+ if (typeof value === "boolean") {
1468
+ return [`${prefix}${value ? "" : "no-"}${key}`];
1469
+ }
1470
+ throw new Error(`Unsupported type ${typeof value} for key ${key}`);
1471
+ });
1472
+ }
1469
1473
 
1470
1474
  // packages/utils/src/lib/scoring.ts
1471
1475
  function calculateScore(refs, scoreFn) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@code-pushup/utils",
3
- "version": "0.6.3",
3
+ "version": "0.6.5",
4
4
  "dependencies": {
5
5
  "@code-pushup/models": "*",
6
6
  "bundle-require": "^4.0.1",
package/src/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- export { CliArgsObject, ProcessConfig, ProcessError, ProcessObserver, ProcessResult, executeProcess, objectToCliArgs, } from './lib/execute-process';
2
- export { FileResult, MultipleFileResults, CrawlFileSystemOptions, crawlFileSystem, ensureDirectoryExists, fileExists, findLineNumberInText, importEsmModule, logMultipleFileResults, pluginWorkDir, readJsonFile, readTextFile, toUnixPath, } from './lib/file-system';
1
+ export { ProcessConfig, ProcessError, ProcessObserver, ProcessResult, executeProcess, } from './lib/execute-process';
2
+ export { CrawlFileSystemOptions, FileResult, MultipleFileResults, crawlFileSystem, ensureDirectoryExists, fileExists, findLineNumberInText, importEsmModule, logMultipleFileResults, pluginWorkDir, readJsonFile, readTextFile, toUnixPath, } from './lib/file-system';
3
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';
@@ -10,5 +10,5 @@ export { CODE_PUSHUP_DOMAIN, FOOTER_PREFIX, README_LINK, calcDuration, compareIs
10
10
  export { reportToMd } from './lib/report-to-md';
11
11
  export { reportToStdout } from './lib/report-to-stdout';
12
12
  export { ScoredReport, scoreReport } from './lib/scoring';
13
- export { countOccurrences, distinct, factorOf, objectToEntries, objectToKeys, toArray, } from './lib/transformation';
13
+ export { CliArgsObject, countOccurrences, distinct, factorOf, objectToCliArgs, objectToEntries, objectToKeys, toArray, } from './lib/transform';
14
14
  export { verboseUtils } from './lib/verbose-utils';
@@ -0,0 +1,4 @@
1
+ export declare const SCORE_COLOR_RANGE: {
2
+ GREEN_MIN: number;
3
+ YELLOW_MIN: number;
4
+ };
@@ -119,19 +119,3 @@ export type ProcessObserver = {
119
119
  * @param cfg - see {@link ProcessConfig}
120
120
  */
121
121
  export declare function executeProcess(cfg: ProcessConfig): Promise<ProcessResult>;
122
- type ArgumentValue = number | string | boolean | string[];
123
- export type CliArgsObject<T extends object = Record<string, ArgumentValue>> = T extends never ? Record<string, ArgumentValue | undefined> | {
124
- _: string;
125
- } : T;
126
- /**
127
- * Converts an object with different types of values into an array of command-line arguments.
128
- *
129
- * @example
130
- * const args = objectToProcessArgs({
131
- * _: ['node', 'index.js'], // node index.js
132
- * name: 'Juanita', // --name=Juanita
133
- * formats: ['json', 'md'] // --format=json --format=md
134
- * });
135
- */
136
- export declare function objectToCliArgs<T extends object = Record<string, ArgumentValue>>(params?: CliArgsObject<T>): string[];
137
- export {};
@@ -0,0 +1,23 @@
1
+ export declare function toArray<T>(val: T | T[]): T[];
2
+ export declare function objectToKeys<T extends object>(obj: T): (keyof T)[];
3
+ export declare function objectToEntries<T extends object>(obj: T): [keyof T, T[keyof T]][];
4
+ export declare function countOccurrences<T extends PropertyKey>(values: T[]): Partial<Record<T, number>>;
5
+ export declare function distinct<T extends string | number | boolean>(array: T[]): T[];
6
+ export declare function deepClone<T>(obj: T): T;
7
+ export declare function factorOf<T>(items: T[], filterFn: (i: T) => boolean): number;
8
+ type ArgumentValue = number | string | boolean | string[];
9
+ export type CliArgsObject<T extends object = Record<string, ArgumentValue>> = T extends never ? Record<string, ArgumentValue | undefined> | {
10
+ _: string;
11
+ } : T;
12
+ /**
13
+ * Converts an object with different types of values into an array of command-line arguments.
14
+ *
15
+ * @example
16
+ * const args = objectToProcessArgs({
17
+ * _: ['node', 'index.js'], // node index.js
18
+ * name: 'Juanita', // --name=Juanita
19
+ * formats: ['json', 'md'] // --format=json --format=md
20
+ * });
21
+ */
22
+ export declare function objectToCliArgs<T extends object = Record<string, ArgumentValue>>(params?: CliArgsObject<T>): string[];
23
+ export {};
@@ -1,7 +0,0 @@
1
- export declare function toArray<T>(val: T | T[]): T[];
2
- export declare function objectToKeys<T extends object>(obj: T): (keyof T)[];
3
- export declare function objectToEntries<T extends object>(obj: T): [keyof T, T[keyof T]][];
4
- export declare function countOccurrences<T extends PropertyKey>(values: T[]): Partial<Record<T, number>>;
5
- export declare function distinct<T extends string | number | boolean>(array: T[]): T[];
6
- export declare function deepClone<T>(obj: T): T;
7
- export declare function factorOf<T>(items: T[], filterFn: (i: T) => boolean): number;