@vitest/utils 4.1.0-beta.4 → 4.1.0-beta.6

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/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # @vitest/utils
2
+
3
+ [![NPM version](https://img.shields.io/npm/v/@vitest/utils?color=a1b858&label=)](https://npmx.dev/package/@vitest/utils)
4
+
5
+ Internal shared utilities used by other Vitest packages.
6
+
7
+ [GitHub](https://github.com/vitest-dev/vitest/tree/main/packages/utils) | [Documentation](https://vitest.dev/)
package/dist/diff.js CHANGED
@@ -2012,8 +2012,8 @@ function diff(a, b, options) {
2012
2012
  }
2013
2013
  aDisplay = truncate(aDisplay);
2014
2014
  bDisplay = truncate(bDisplay);
2015
- const aDiff = `${aColor(`${aIndicator} ${aAnnotation}:`)} \n${aDisplay}`;
2016
- const bDiff = `${bColor(`${bIndicator} ${bAnnotation}:`)} \n${bDisplay}`;
2015
+ const aDiff = `${aColor(`${aIndicator} ${aAnnotation}:`)}\n${aDisplay}`;
2016
+ const bDiff = `${bColor(`${bIndicator} ${bAnnotation}:`)}\n${bDisplay}`;
2017
2017
  return `${aDiff}\n\n${bDiff}`;
2018
2018
  }
2019
2019
  if (omitDifference) {
package/dist/helpers.d.ts CHANGED
@@ -61,6 +61,7 @@ declare function createDefer<T>(): DeferPromise<T>;
61
61
  */
62
62
  declare function getCallLastIndex(code: string): number | null;
63
63
  declare function isNegativeNaN(val: number): boolean;
64
+ declare function ordinal(i: number): string;
64
65
  /**
65
66
  * Deep merge :P
66
67
  *
@@ -71,5 +72,5 @@ declare function isNegativeNaN(val: number): boolean;
71
72
  declare function deepMerge<T extends object = object>(target: T, ...sources: any[]): T;
72
73
  declare function unique<T>(array: T[]): T[];
73
74
 
74
- export { assertTypes, cleanUrl, clone, createDefer, createSimpleStackTrace, deepClone, deepMerge, filterOutComments, getCallLastIndex, getOwnProperties, getType, isBareImport, isExternalUrl, isNegativeNaN, isObject, isPrimitive, nanoid, noop, notNullish, objectAttr, shuffle, slash, toArray, unique, unwrapId, withTrailingSlash, wrapId };
75
+ export { assertTypes, cleanUrl, clone, createDefer, createSimpleStackTrace, deepClone, deepMerge, filterOutComments, getCallLastIndex, getOwnProperties, getType, isBareImport, isExternalUrl, isNegativeNaN, isObject, isPrimitive, nanoid, noop, notNullish, objectAttr, ordinal, shuffle, slash, toArray, unique, unwrapId, withTrailingSlash, wrapId };
75
76
  export type { DeferPromise };
package/dist/helpers.js CHANGED
@@ -286,6 +286,20 @@ function isPlainObject(val) {
286
286
  function isMergeableObject(item) {
287
287
  return isPlainObject(item) && !Array.isArray(item);
288
288
  }
289
+ function ordinal(i) {
290
+ const j = i % 10;
291
+ const k = i % 100;
292
+ if (j === 1 && k !== 11) {
293
+ return `${i}st`;
294
+ }
295
+ if (j === 2 && k !== 12) {
296
+ return `${i}nd`;
297
+ }
298
+ if (j === 3 && k !== 13) {
299
+ return `${i}rd`;
300
+ }
301
+ return `${i}th`;
302
+ }
289
303
  /**
290
304
  * Deep merge :P
291
305
  *
@@ -320,4 +334,4 @@ function unique(array) {
320
334
  return Array.from(new Set(array));
321
335
  }
322
336
 
323
- export { assertTypes, cleanUrl, clone, createDefer, createSimpleStackTrace, deepClone, deepMerge, filterOutComments, getCallLastIndex, getOwnProperties, getType, isBareImport, isExternalUrl, isNegativeNaN, isObject, isPrimitive, nanoid, noop, notNullish, objectAttr, shuffle, slash, toArray, unique, unwrapId, withTrailingSlash, wrapId };
337
+ export { assertTypes, cleanUrl, clone, createDefer, createSimpleStackTrace, deepClone, deepMerge, filterOutComments, getCallLastIndex, getOwnProperties, getType, isBareImport, isExternalUrl, isNegativeNaN, isObject, isPrimitive, nanoid, noop, notNullish, objectAttr, ordinal, shuffle, slash, toArray, unique, unwrapId, withTrailingSlash, wrapId };
@@ -0,0 +1,6 @@
1
+ interface ExtractedSourceMap {
2
+ map: any;
3
+ }
4
+ declare function extractSourcemapFromFile(code: string, filePath: string): ExtractedSourceMap | undefined;
5
+
6
+ export { extractSourcemapFromFile };
@@ -0,0 +1,23 @@
1
+ import { existsSync, readFileSync } from 'node:fs';
2
+ import path from 'node:path';
3
+ import convertSourceMap from 'convert-source-map';
4
+
5
+ // based on vite
6
+ // https://github.com/vitejs/vite/blob/84079a84ad94de4c1ef4f1bdb2ab448ff2c01196/packages/vite/src/node/server/sourcemap.ts#L149
7
+ function extractSourcemapFromFile(code, filePath) {
8
+ const map = (convertSourceMap.fromSource(code) || convertSourceMap.fromMapFileSource(code, createConvertSourceMapReadMap(filePath)))?.toObject();
9
+ return map ? { map } : undefined;
10
+ }
11
+ function createConvertSourceMapReadMap(originalFileName) {
12
+ return (filename) => {
13
+ // convertSourceMap can detect invalid filename from comments.
14
+ // fallback to empty source map to avoid errors.
15
+ const targetPath = path.resolve(path.dirname(originalFileName), filename);
16
+ if (existsSync(targetPath)) {
17
+ return readFileSync(targetPath, "utf-8");
18
+ }
19
+ return "{}";
20
+ };
21
+ }
22
+
23
+ export { extractSourcemapFromFile };
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@vitest/utils",
3
3
  "type": "module",
4
- "version": "4.1.0-beta.4",
4
+ "version": "4.1.0-beta.6",
5
5
  "description": "Shared Vitest utility functions",
6
6
  "license": "MIT",
7
7
  "funding": "https://opencollective.com/vitest",
8
- "homepage": "https://github.com/vitest-dev/vitest/tree/main/packages/utils#readme",
8
+ "homepage": "https://github.com/vitest-dev/vitest/tree/main/packages/utils",
9
9
  "repository": {
10
10
  "type": "git",
11
11
  "url": "git+https://github.com/vitest-dev/vitest.git",
@@ -56,6 +56,10 @@
56
56
  "types": "./dist/source-map.d.ts",
57
57
  "default": "./dist/source-map.js"
58
58
  },
59
+ "./source-map/node": {
60
+ "types": "./dist/source-map/node.d.ts",
61
+ "default": "./dist/source-map/node.js"
62
+ },
59
63
  "./serialize": {
60
64
  "types": "./dist/serialize.d.ts",
61
65
  "default": "./dist/serialize.js"
@@ -69,6 +73,9 @@
69
73
  "*": {
70
74
  "source-map": [
71
75
  "dist/source-map.d.ts"
76
+ ],
77
+ "source-map/node": [
78
+ "dist/source-map/node.d.ts"
72
79
  ]
73
80
  }
74
81
  },
@@ -77,11 +84,13 @@
77
84
  "dist"
78
85
  ],
79
86
  "dependencies": {
87
+ "convert-source-map": "^2.0.0",
80
88
  "tinyrainbow": "^3.0.3",
81
- "@vitest/pretty-format": "4.1.0-beta.4"
89
+ "@vitest/pretty-format": "4.1.0-beta.6"
82
90
  },
83
91
  "devDependencies": {
84
92
  "@jridgewell/trace-mapping": "0.3.31",
93
+ "@types/convert-source-map": "^2.0.3",
85
94
  "@types/estree": "^1.0.8",
86
95
  "diff-sequences": "^29.6.3",
87
96
  "loupe": "^3.2.1"