path-serializer 0.1.2 → 0.1.4

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 CHANGED
@@ -1,5 +1,7 @@
1
1
  # path-serializer
2
2
 
3
+ <a href="https://www.npmjs.com/package/path-serializer"><img src="https://img.shields.io/npm/v/path-serializer?style=flat-square&color=98c379" alt="latest version" /></a>
4
+
3
5
  ```ts
4
6
  // __snapshots__/index.test.ts.snap
5
7
  {
@@ -849,13 +849,25 @@ function __webpack_require__(moduleId) {
849
849
  });
850
850
  return ret;
851
851
  };
852
+ const createPnpmInnerMatchers = ()=>[
853
+ // posix
854
+ {
855
+ match: /(?<=\/)(\.pnpm\/.+?\/node_modules)(?=\/)/g,
856
+ mark: 'pnpmInner'
857
+ },
858
+ // win32
859
+ {
860
+ match: /(?<=\\)(\.pnpm\\.+?\\node_modules)(?=\\)/g,
861
+ mark: 'pnpmInner'
862
+ }
863
+ ];
852
864
  function transformCodeToPosixPath(code) {
853
865
  return code.replace(// ignore http, https, file
854
866
  /(?<![a-zA-Z])([a-zA-Z]:[\\/])([-\u4e00-\u9fa5\w\s.()~!@#$%^&()\[\]{}+=]+[\\/])*/g, (match, _diskName)=>normalizeToPosixPath(match));
855
867
  }
856
868
  function createSnapshotSerializer(options) {
857
869
  const { cwd = process.cwd(), workspace = process.cwd(), replace: customMatchers = [], features = {} } = options || {};
858
- const { replaceRoot = true, replaceWorkspace = true, replacePnpmInner = true, replaceTmpDir = true, ansiDoubleQuotes = true, addDoubleQuotes = true } = features;
870
+ const { replaceRoot = true, replaceWorkspace = true, replacePnpmInner = true, replaceTmpDir = true, ansiDoubleQuotes = true, addDoubleQuotes = true, transformWin32Path = true } = features;
859
871
  function createPathMatchers() {
860
872
  const pathMatchers = [];
861
873
  if (replaceRoot) pathMatchers.push({
@@ -866,12 +878,9 @@ function __webpack_require__(moduleId) {
866
878
  mark: 'workspace',
867
879
  match: workspace
868
880
  });
869
- if (replacePnpmInner) pathMatchers.push({
870
- match: /(?<=\/)(\.pnpm\/.+?\/node_modules)(?=\/)/g,
871
- mark: 'pnpmInner'
872
- });
873
- if (replaceTmpDir) pathMatchers.push(...createTmpDirMatchers());
881
+ if (replacePnpmInner) pathMatchers.push(...createPnpmInnerMatchers());
874
882
  pathMatchers.push(...customMatchers);
883
+ if (replaceTmpDir) pathMatchers.push(...createTmpDirMatchers());
875
884
  return pathMatchers;
876
885
  }
877
886
  const pathMatchers = createPathMatchers();
@@ -881,9 +890,10 @@ function __webpack_require__(moduleId) {
881
890
  test (val) {
882
891
  return 'string' == typeof val;
883
892
  },
884
- serialize (val, _config, _indentation, _depth, _refs, _printer) {
885
- const normalized = transformCodeToPosixPath(val);
886
- let replaced = applyMatcherReplacement(pathMatchers, normalized);
893
+ serialize (val) {
894
+ let replaced = val;
895
+ if (transformWin32Path) replaced = transformCodeToPosixPath(replaced);
896
+ replaced = applyMatcherReplacement(pathMatchers, replaced);
887
897
  if (ansiDoubleQuotes) replaced = replaced.replace(/"/g, '\\"');
888
898
  if (addDoubleQuotes) replaced = `"${replaced}"`;
889
899
  return replaced;
@@ -1,5 +1,3 @@
1
- import { SnapshotSerializer } from '@vitest/snapshot';
2
-
3
1
  export declare function createSnapshotSerializer(options?: SnapshotSerializerOptions): SnapshotSerializer;
4
2
 
5
3
  declare interface Features {
@@ -27,6 +25,10 @@ declare interface Features {
27
25
  * @default true
28
26
  */
29
27
  addDoubleQuotes?: boolean;
28
+ /**
29
+ * @default true
30
+ */
31
+ transformWin32Path?: boolean;
30
32
  }
31
33
 
32
34
  declare interface PathMatcher {
@@ -36,6 +38,11 @@ declare interface PathMatcher {
36
38
 
37
39
  declare type PathMatchExpression = string | RegExp;
38
40
 
41
+ declare interface SnapshotSerializer {
42
+ serialize: (val: any) => string;
43
+ test: (arg0: any) => boolean;
44
+ }
45
+
39
46
  declare interface SnapshotSerializerOptions {
40
47
  cwd?: string;
41
48
  workspace?: string;
@@ -1,5 +1,3 @@
1
- import { SnapshotSerializer } from '@vitest/snapshot';
2
-
3
1
  export declare function createSnapshotSerializer(options?: SnapshotSerializerOptions): SnapshotSerializer;
4
2
 
5
3
  declare interface Features {
@@ -27,6 +25,10 @@ declare interface Features {
27
25
  * @default true
28
26
  */
29
27
  addDoubleQuotes?: boolean;
28
+ /**
29
+ * @default true
30
+ */
31
+ transformWin32Path?: boolean;
30
32
  }
31
33
 
32
34
  declare interface PathMatcher {
@@ -36,6 +38,11 @@ declare interface PathMatcher {
36
38
 
37
39
  declare type PathMatchExpression = string | RegExp;
38
40
 
41
+ declare interface SnapshotSerializer {
42
+ serialize: (val: any) => string;
43
+ test: (arg0: any) => boolean;
44
+ }
45
+
39
46
  declare interface SnapshotSerializerOptions {
40
47
  cwd?: string;
41
48
  workspace?: string;
package/dist/esm/index.js CHANGED
@@ -824,13 +824,25 @@ const createTmpDirMatchers = ()=>{
824
824
  });
825
825
  return ret;
826
826
  };
827
+ const createPnpmInnerMatchers = ()=>[
828
+ // posix
829
+ {
830
+ match: /(?<=\/)(\.pnpm\/.+?\/node_modules)(?=\/)/g,
831
+ mark: 'pnpmInner'
832
+ },
833
+ // win32
834
+ {
835
+ match: /(?<=\\)(\.pnpm\\.+?\\node_modules)(?=\\)/g,
836
+ mark: 'pnpmInner'
837
+ }
838
+ ];
827
839
  function transformCodeToPosixPath(code) {
828
840
  return code.replace(// ignore http, https, file
829
841
  /(?<![a-zA-Z])([a-zA-Z]:[\\/])([-\u4e00-\u9fa5\w\s.()~!@#$%^&()\[\]{}+=]+[\\/])*/g, (match, _diskName)=>normalizeToPosixPath(match));
830
842
  }
831
843
  function createSnapshotSerializer(options) {
832
844
  const { cwd = process.cwd(), workspace = process.cwd(), replace: customMatchers = [], features = {} } = options || {};
833
- const { replaceRoot = true, replaceWorkspace = true, replacePnpmInner = true, replaceTmpDir = true, ansiDoubleQuotes = true, addDoubleQuotes = true } = features;
845
+ const { replaceRoot = true, replaceWorkspace = true, replacePnpmInner = true, replaceTmpDir = true, ansiDoubleQuotes = true, addDoubleQuotes = true, transformWin32Path = true } = features;
834
846
  function createPathMatchers() {
835
847
  const pathMatchers = [];
836
848
  if (replaceRoot) pathMatchers.push({
@@ -841,12 +853,9 @@ function createSnapshotSerializer(options) {
841
853
  mark: 'workspace',
842
854
  match: workspace
843
855
  });
844
- if (replacePnpmInner) pathMatchers.push({
845
- match: /(?<=\/)(\.pnpm\/.+?\/node_modules)(?=\/)/g,
846
- mark: 'pnpmInner'
847
- });
848
- if (replaceTmpDir) pathMatchers.push(...createTmpDirMatchers());
856
+ if (replacePnpmInner) pathMatchers.push(...createPnpmInnerMatchers());
849
857
  pathMatchers.push(...customMatchers);
858
+ if (replaceTmpDir) pathMatchers.push(...createTmpDirMatchers());
850
859
  return pathMatchers;
851
860
  }
852
861
  const pathMatchers = createPathMatchers();
@@ -856,9 +865,10 @@ function createSnapshotSerializer(options) {
856
865
  test (val) {
857
866
  return 'string' == typeof val;
858
867
  },
859
- serialize (val, _config, _indentation, _depth, _refs, _printer) {
860
- const normalized = transformCodeToPosixPath(val);
861
- let replaced = applyMatcherReplacement(pathMatchers, normalized);
868
+ serialize (val) {
869
+ let replaced = val;
870
+ if (transformWin32Path) replaced = transformCodeToPosixPath(replaced);
871
+ replaced = applyMatcherReplacement(pathMatchers, replaced);
862
872
  if (ansiDoubleQuotes) replaced = replaced.replace(/"/g, '\\"');
863
873
  if (addDoubleQuotes) replaced = `"${replaced}"`;
864
874
  return replaced;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "path-serializer",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "path-serializer",
5
5
  "keywords": [
6
6
  "snapshot",