@jsenv/urls 1.0.0 → 1.1.0

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.
Files changed (63) hide show
  1. package/main.js +17 -0
  2. package/package.json +13 -5
  3. package/src/caller_position.js +1 -1
  4. package/src/common_pathname.js +42 -0
  5. package/src/ensure_url_trailing_slash.js +3 -0
  6. package/src/filesystem_path_to_url.js +10 -0
  7. package/src/inline_content_url_generator.js +1 -1
  8. package/src/is_filesystem_path.js +21 -0
  9. package/src/move_url.js +10 -0
  10. package/src/resolve_directory_url.js +7 -0
  11. package/src/resolve_url.js +6 -0
  12. package/src/url_is_inside_of.js +17 -0
  13. package/src/url_to_basename.js +9 -0
  14. package/src/url_to_extension.js +19 -0
  15. package/src/url_to_filename.js +14 -0
  16. package/src/url_to_filesystem_path.js +12 -0
  17. package/src/url_to_origin.js +7 -0
  18. package/src/url_to_parent_url.js +31 -0
  19. package/src/url_to_pathname.js +19 -0
  20. package/src/url_to_relative_url.js +56 -0
  21. package/src/url_to_ressource.js +21 -0
  22. package/src/url_to_scheme.js +10 -0
  23. package/src/url_trace.js +1 -1
  24. package/src/url_utils.js +3 -1
  25. package/docs/url_meta/advanced.md +0 -28
  26. package/docs/url_meta/demo/apply_associations.mjs +0 -27
  27. package/docs/url_meta/demo/full_match.mjs +0 -7
  28. package/docs/url_meta/demo/partial_match.mjs +0 -7
  29. package/docs/url_meta/demo/resolve_associations.mjs +0 -12
  30. package/docs/url_meta/notes.md +0 -26
  31. package/docs/url_meta/pattern_matching.md +0 -71
  32. package/docs/url_meta/readme.md +0 -87
  33. package/test/data_url.test.mjs +0 -57
  34. package/test/meta/aliasing.test.mjs +0 -47
  35. package/test/meta/as_flat_associations/basic.test.mjs +0 -64
  36. package/test/meta/assertions.test.mjs +0 -23
  37. package/test/meta/get_url_meta/any.test.mjs +0 -94
  38. package/test/meta/get_url_meta/basic.test.mjs +0 -226
  39. package/test/meta/get_url_meta/complex.test.mjs +0 -203
  40. package/test/meta/get_url_meta/double_wildcard_after.test.mjs +0 -68
  41. package/test/meta/get_url_meta/double_wildcard_around.test.mjs +0 -42
  42. package/test/meta/get_url_meta/double_wildcard_before.test.mjs +0 -55
  43. package/test/meta/get_url_meta/inheritance.test.mjs +0 -59
  44. package/test/meta/is_plain_object.test.mjs +0 -27
  45. package/test/meta/pattern_matching/basic.test.mjs +0 -100
  46. package/test/meta/pattern_matching/common.test.mjs +0 -145
  47. package/test/meta/pattern_matching/complex.test.mjs +0 -159
  48. package/test/meta/pattern_matching/contains.test.mjs +0 -46
  49. package/test/meta/pattern_matching/doublewildcard_after.test.mjs +0 -81
  50. package/test/meta/pattern_matching/doublewildcard_around.test.mjs +0 -42
  51. package/test/meta/pattern_matching/doublewildcard_before.test.mjs +0 -68
  52. package/test/meta/pattern_matching/doublewildcard_inside.test.mjs +0 -42
  53. package/test/meta/pattern_matching/trailing_slash.test.mjs +0 -55
  54. package/test/meta/pattern_matching/wildcard_after.test.mjs +0 -68
  55. package/test/meta/pattern_matching/wildcard_around.test.mjs +0 -94
  56. package/test/meta/pattern_matching/wildcard_before.test.mjs +0 -55
  57. package/test/meta/pattern_matching/wildcard_inside.test.mjs +0 -68
  58. package/test/meta/resolve_associations/basic.test.mjs +0 -42
  59. package/test/meta/resolve_associations/http_pattern.test.mjs +0 -35
  60. package/test/meta/url_child_may_match/inheritance.test.mjs +0 -71
  61. package/test/meta/url_child_may_match/url_child_may_match.test.mjs +0 -166
  62. package/test/url_trace.test.mjs +0 -224
  63. package/test.mjs +0 -13
@@ -1,55 +0,0 @@
1
- import { assert } from "@jsenv/assert"
2
-
3
- import { URL_META } from "@jsenv/urls"
4
-
5
- {
6
- const pattern = "file:///*a"
7
- const url = "file:///a"
8
- const actual = URL_META.applyPatternMatching({ pattern, url })
9
- const expected = {
10
- matched: false,
11
- patternIndex: pattern.indexOf("*"),
12
- urlIndex: url.indexOf("a"),
13
- matchGroups: [""],
14
- }
15
- assert({ actual, expected })
16
- }
17
-
18
- {
19
- const pattern = "file:///*a"
20
- const url = "file:///Za"
21
- const actual = URL_META.applyPatternMatching({ pattern, url })
22
- const expected = {
23
- matched: true,
24
- patternIndex: pattern.length,
25
- urlIndex: url.length,
26
- matchGroups: ["Z"],
27
- }
28
- assert({ actual, expected })
29
- }
30
-
31
- {
32
- const pattern = "file:///*a"
33
- const url = "file:///ZZZa"
34
- const actual = URL_META.applyPatternMatching({ pattern, url })
35
- const expected = {
36
- matched: true,
37
- patternIndex: pattern.length,
38
- urlIndex: url.length,
39
- matchGroups: ["ZZZ"],
40
- }
41
- assert({ actual, expected })
42
- }
43
-
44
- {
45
- const pattern = "file:///*a"
46
- const url = "file:///aZ"
47
- const actual = URL_META.applyPatternMatching({ pattern, url })
48
- const expected = {
49
- matched: false,
50
- patternIndex: pattern.indexOf("*"),
51
- urlIndex: url.indexOf("a"),
52
- matchGroups: [""],
53
- }
54
- assert({ actual, expected })
55
- }
@@ -1,68 +0,0 @@
1
- import { assert } from "@jsenv/assert"
2
-
3
- import { URL_META } from "@jsenv/urls"
4
-
5
- {
6
- const pattern = "file:///a*bc"
7
- const url = "file:///abc"
8
- const actual = URL_META.applyPatternMatching({ pattern, url })
9
- const expected = {
10
- matched: false,
11
- patternIndex: pattern.indexOf("*"),
12
- urlIndex: url.indexOf("b"),
13
- matchGroups: [""],
14
- }
15
- assert({ actual, expected })
16
- }
17
-
18
- {
19
- const pattern = "file:///a*bc"
20
- const url = "file:///aZZbc"
21
- const actual = URL_META.applyPatternMatching({ pattern, url })
22
- const expected = {
23
- matched: true,
24
- patternIndex: pattern.length,
25
- urlIndex: url.length,
26
- matchGroups: ["ZZ"],
27
- }
28
- assert({ actual, expected })
29
- }
30
-
31
- {
32
- const pattern = "file:///a*bc"
33
- const url = "file:///aZZbd"
34
- const actual = URL_META.applyPatternMatching({ pattern, url })
35
- const expected = {
36
- matched: false,
37
- patternIndex: pattern.lastIndexOf("c"),
38
- urlIndex: url.lastIndexOf("d"),
39
- matchGroups: ["ZZ"],
40
- }
41
- assert({ actual, expected })
42
- }
43
-
44
- {
45
- const pattern = "file:///a/b*/c"
46
- const url = "file:///a/bZ/c"
47
- const actual = URL_META.applyPatternMatching({ pattern, url })
48
- const expected = {
49
- matched: true,
50
- patternIndex: pattern.length,
51
- urlIndex: url.length,
52
- matchGroups: ["Z"],
53
- }
54
- assert({ actual, expected })
55
- }
56
-
57
- {
58
- const pattern = "file:///a/b*/c"
59
- const url = "file:///a/b/c"
60
- const actual = URL_META.applyPatternMatching({ pattern, url })
61
- const expected = {
62
- matched: false,
63
- patternIndex: pattern.indexOf("*"),
64
- urlIndex: url.indexOf("/c"),
65
- matchGroups: [""],
66
- }
67
- assert({ actual, expected })
68
- }
@@ -1,42 +0,0 @@
1
- import { assert } from "@jsenv/assert"
2
-
3
- import { URL_META } from "@jsenv/urls"
4
-
5
- {
6
- const actual = URL_META.resolveAssociations(
7
- {
8
- whatever: {
9
- "./a.js": true,
10
- "http://example.com/file.js": true,
11
- },
12
- },
13
- "file:///User/name/directory/",
14
- )
15
- const expected = {
16
- whatever: {
17
- "file:///User/name/directory/a.js": true,
18
- "http://example.com/file.js": true,
19
- },
20
- }
21
- assert({ actual, expected })
22
- }
23
-
24
- // ensure resolveAssociations does not sort by length
25
- {
26
- const actual = URL_META.resolveAssociations(
27
- {
28
- whatever: {
29
- "./a.js": 42,
30
- "./long.js": 42,
31
- },
32
- },
33
- "file:///",
34
- )
35
- const expected = {
36
- whatever: {
37
- "file:///a.js": 42,
38
- "file:///long.js": 42,
39
- },
40
- }
41
- assert({ actual, expected })
42
- }
@@ -1,35 +0,0 @@
1
- import { assert } from "@jsenv/assert"
2
-
3
- import { URL_META } from "@jsenv/urls"
4
-
5
- const associations = URL_META.resolveAssociations(
6
- {
7
- whatever: {
8
- "file://": false,
9
- "http://": true,
10
- },
11
- },
12
- "file:///User/name/directory/",
13
- )
14
- const fileUrlMeta = URL_META.applyAssociations({
15
- url: "file:///dir/file.js",
16
- associations,
17
- })
18
- const httpUrlMeta = URL_META.applyAssociations({
19
- url: "http://example.com",
20
- associations,
21
- })
22
-
23
- const actual = {
24
- fileUrlMeta,
25
- httpUrlMeta,
26
- }
27
- const expected = {
28
- fileUrlMeta: {
29
- whatever: false,
30
- },
31
- httpUrlMeta: {
32
- whatever: true,
33
- },
34
- }
35
- assert({ actual, expected })
@@ -1,71 +0,0 @@
1
- import { assert } from "@jsenv/assert"
2
-
3
- import { URL_META } from "@jsenv/urls"
4
-
5
- {
6
- const actual = URL_META.urlChildMayMatch({
7
- url: "file:///src/",
8
- associations: {
9
- whatever: {
10
- "file:///**/*": 42,
11
- "file:///.git/": 43,
12
- },
13
- },
14
- predicate: ({ whatever }) => whatever === 42,
15
- })
16
- const expected = true
17
- assert({ actual, expected })
18
- }
19
-
20
- {
21
- const actual = URL_META.urlChildMayMatch({
22
- url: "file:///.git/",
23
- associations: {
24
- whatever: {
25
- "file:///**/*": 42,
26
- "file:///.git/": 43,
27
- },
28
- },
29
- predicate: ({ whatever }) => whatever === 42,
30
- })
31
- const expected = false
32
- assert({ actual, expected })
33
- }
34
-
35
- try {
36
- URL_META.urlChildMayMatch({
37
- url: "file:///.git",
38
- associations: {
39
- whatever: {
40
- "file:///**/*": 42,
41
- "file:///.git/": 43,
42
- },
43
- },
44
- predicate: ({ whatever }) => whatever === 42,
45
- })
46
- throw new Error("shoud crash")
47
- } catch (error) {
48
- const actual = error
49
- const expected = new Error(`url should end with /, got file:///.git`)
50
- assert({ actual, expected })
51
- }
52
-
53
- try {
54
- URL_META.urlChildMayMatch({
55
- url: "file:///.git/",
56
- associations: {
57
- whatever: {
58
- "file:///**/*": 42,
59
- "file:///.git/": 43,
60
- },
61
- },
62
- predicate: "I'm a string",
63
- })
64
- throw new Error("shoud crash")
65
- } catch (error) {
66
- const actual = error
67
- const expected = new TypeError(
68
- `predicate must be a function, got I'm a string`,
69
- )
70
- assert({ actual, expected })
71
- }
@@ -1,166 +0,0 @@
1
- import { assert } from "@jsenv/assert"
2
-
3
- import { URL_META } from "@jsenv/urls"
4
-
5
- {
6
- const actual = URL_META.urlChildMayMatch({
7
- url: "file:///.github/",
8
- associations: {
9
- source: {
10
- "file:///**/.github/": true,
11
- "file:///**/.git/": false,
12
- },
13
- },
14
- predicate: ({ source }) => source,
15
- })
16
- const expected = true
17
- assert({ actual, expected })
18
- }
19
-
20
- {
21
- const associations = {
22
- whatever: {
23
- "file:///a/b/": 42,
24
- },
25
- }
26
-
27
- {
28
- const actual = URL_META.urlChildMayMatch({
29
- url: "file:///a/",
30
- associations,
31
- predicate: ({ whatever }) => whatever === 42,
32
- })
33
- const expected = true
34
- assert({ actual, expected })
35
- }
36
- {
37
- const actual = URL_META.urlChildMayMatch({
38
- url: "file:///a/b/",
39
- associations,
40
- predicate: ({ whatever }) => whatever === 42,
41
- })
42
- const expected = true
43
- assert({ actual, expected })
44
- }
45
- {
46
- const actual = URL_META.urlChildMayMatch({
47
- url: "file:///a/c/",
48
- associations,
49
- predicate: ({ whatever }) => whatever === 42,
50
- })
51
- const expected = false
52
- assert({ actual, expected })
53
- }
54
- }
55
-
56
- {
57
- const associations = {
58
- whatever: {
59
- "file:///a/b*/c/": 42,
60
- },
61
- }
62
-
63
- {
64
- const actual = URL_META.urlChildMayMatch({
65
- url: "file:///a/bZ/",
66
- associations,
67
- predicate: ({ whatever }) => whatever === 42,
68
- })
69
- const expected = true
70
- assert({ actual, expected })
71
- }
72
- {
73
- const actual = URL_META.urlChildMayMatch({
74
- url: "file:///a/bZ/c/",
75
- associations,
76
- predicate: ({ whatever }) => whatever === 42,
77
- })
78
- const expected = true
79
- assert({ actual, expected })
80
- }
81
- }
82
-
83
- {
84
- const actual = URL_META.urlChildMayMatch({
85
- url: "file:///a/b/c/",
86
- associations: {
87
- whatever: {
88
- "file:///a/**/b.js": 42,
89
- },
90
- },
91
- predicate: ({ whatever }) => whatever === 42,
92
- })
93
- const expected = true
94
- assert({ actual, expected })
95
- }
96
-
97
- {
98
- const actual = URL_META.urlChildMayMatch({
99
- url: "file:///node_modules/",
100
- associations: {
101
- whatever: {
102
- "file:///**/*": 42,
103
- "file:///node_modules/": 43,
104
- },
105
- },
106
- predicate: ({ whatever }) => whatever === 42,
107
- })
108
- const expected = false
109
- assert({ actual, expected })
110
- }
111
-
112
- {
113
- const actual = URL_META.urlChildMayMatch({
114
- url: "file:///src/",
115
- associations: {
116
- whatever: {
117
- "file:///**/*.js": 42,
118
- "file:///**/*.md": 43,
119
- },
120
- },
121
- predicate: ({ whatever }) => whatever === 42,
122
- })
123
- const expected = true
124
- assert({ actual, expected })
125
- }
126
-
127
- {
128
- const associations = {
129
- whatever: {
130
- "file:///**/*.js": 42,
131
- },
132
- }
133
-
134
- {
135
- const actual = URL_META.urlChildMayMatch({
136
- url: "file:///src/folder/",
137
- associations,
138
- predicate: ({ whatever }) => whatever === 42,
139
- })
140
- const expected = true
141
- assert({ actual, expected })
142
- }
143
- {
144
- const actual = URL_META.urlChildMayMatch({
145
- url: "file:///src/folder/subfolder/",
146
- associations,
147
- predicate: ({ whatever }) => whatever === 42,
148
- })
149
- const expected = true
150
- assert({ actual, expected })
151
- }
152
- }
153
-
154
- {
155
- const actual = URL_META.urlChildMayMatch({
156
- url: "file:///src/jsCreateCompileService/compile/",
157
- associations: {
158
- whatever: {
159
- "file:///src/**/*.js": 42,
160
- },
161
- },
162
- predicate: ({ whatever }) => whatever === 42,
163
- })
164
- const expected = true
165
- assert({ actual, expected })
166
- }
@@ -1,224 +0,0 @@
1
- /**
2
- * ".slice(1)" is used to be able to write "expected" like that:
3
- *
4
- * ```js
5
- * const expected = `
6
- * > 1 | const a = true;
7
- * ^
8
- * 2 | const b = true;`.slice(1)
9
- * ```
10
- *
11
- * Because it's more readable than
12
- *
13
- * ```js
14
- * const expected = `> 1 | const a = true;
15
- * ^
16
- * 2 | const b = true;`
17
- * ```
18
- *
19
- */
20
-
21
- import { assert } from "@jsenv/assert"
22
-
23
- import { showSourceLocation } from "@jsenv/urls"
24
-
25
- {
26
- const actual = showSourceLocation({
27
- content: `const a = false;
28
- const b = true;
29
- const c = true;
30
- const d = true;
31
- const e = false;
32
- `,
33
- column: 1,
34
- line: 3,
35
- })
36
- const expected = `
37
- 2 | const b = true;
38
- > 3 | const c = true;
39
- ^
40
- 4 | const d = true;`.slice(1)
41
- assert({ actual, expected })
42
- }
43
-
44
- {
45
- const actual = showSourceLocation({
46
- content: `const a = false;
47
- const b = true;
48
- const c = true;
49
- const d = true;
50
- const e = false;`,
51
- column: 7,
52
- line: 3,
53
- numberOfSurroundingLinesToShow: 10,
54
- })
55
- const expected = `
56
- 1 | const a = false;
57
- 2 | const b = true;
58
- > 3 | const c = true;
59
- ^
60
- 4 | const d = true;
61
- 5 | const e = false;`.slice(1)
62
- assert({ actual, expected })
63
- }
64
-
65
- // empty last line is shown
66
- {
67
- const actual = showSourceLocation({
68
- content: `const a = false;
69
- const b = true;
70
- const c = true;
71
- const d = true;
72
- const e = false;
73
- `,
74
- column: 1,
75
- line: 3,
76
- numberOfSurroundingLinesToShow: 10,
77
- })
78
- const expected = `
79
- 1 | const a = false;
80
- 2 | const b = true;
81
- > 3 | const c = true;
82
- ^
83
- 4 | const d = true;
84
- 5 | const e = false;
85
- 6 | `.slice(1)
86
- assert({ actual, expected })
87
- }
88
-
89
- // no column given
90
- {
91
- const actual = showSourceLocation({
92
- content: `const a = false;
93
- const b = true;
94
- const c = true;
95
- const d = true;
96
- const e = false;
97
- `,
98
- line: 3,
99
- })
100
- const expected = `
101
- 2 | const b = true;
102
- > 3 | const c = true;
103
- 4 | const d = true;`.slice(1)
104
- assert({ actual, expected })
105
- }
106
-
107
- // line number goes from 1 digit to 2 digits
108
- {
109
- const actual = showSourceLocation({
110
- content: `const a = false;
111
- const b = false;
112
- const c = false;
113
- const d = false;
114
- const e = false;
115
- const f = false;
116
- const g = false;
117
- const h = false;
118
- const i = true;
119
- const j = true;
120
- const k = true;
121
- const l = true;`,
122
- line: 10,
123
- })
124
- const expected = `
125
- 9 | const i = true;
126
- > 10 | const j = true;
127
- 11 | const k = true;`.slice(1)
128
- assert({ actual, expected })
129
- }
130
-
131
- // line is too long and column is undefined
132
- {
133
- const actual = showSourceLocation({
134
- content: `const a = false;
135
- const b = true;
136
- const thisVariableNameisQuiteLong = true;
137
- const d = true;
138
- const e = false;
139
- `,
140
- line: 3,
141
- lineMaxLength: 15,
142
- })
143
- const expected = `
144
- 2 | const b = true;
145
- > 3 | const thisVari…
146
- 4 | const d = true;`.slice(1)
147
- assert({ actual, expected })
148
- }
149
-
150
- // line is too long and column is near beginning
151
- {
152
- const actual = showSourceLocation({
153
- content: `const a = false;
154
- const b = true;
155
- const thisVariableNameisQuiteLong = true;
156
- const d = true;
157
- const e = false;
158
- `,
159
- line: 3,
160
- column: 4,
161
- lineMaxLength: 15,
162
- })
163
- const expected = `
164
- 2 | const b = true;
165
- > 3 | const thisVari…
166
- ^
167
- 4 | const d = true;`.slice(1)
168
- assert({ actual, expected })
169
- }
170
-
171
- // line is too long and column is near middle
172
- {
173
- const actual = showSourceLocation({
174
- content: `const a = false;
175
- const b = true
176
- const thisVariableNameisQuiteLong = true;
177
- const d = tru
178
- const e = false;
179
- `,
180
- line: 3,
181
- column: 20,
182
- lineMaxLength: 15,
183
- })
184
- const expected = `
185
- 2 |${" "}
186
- > 3 | …ableNameisQui…
187
- ^
188
- 4 |${" "}`.slice(1)
189
- assert({ actual, expected })
190
- }
191
-
192
- // line is too long and column is near end
193
- {
194
- const actual = showSourceLocation({
195
- content: `const a = false;
196
-
197
- const thisVariableNameisQuiteLong = true;
198
- const d = tru
199
- const e = false;
200
- `,
201
- line: 3,
202
- column: 35,
203
- lineMaxLength: 15,
204
- })
205
- const expected = `
206
- 2 |${" "}
207
- > 3 | …Long = true;
208
- ^
209
- 4 |${" "}`.slice(1)
210
- assert({ actual, expected })
211
- }
212
-
213
- // line and column being 0
214
- {
215
- const actual = showSourceLocation({
216
- content: `const a = false;`,
217
- line: 0,
218
- column: 0,
219
- })
220
- const expected = `
221
- > 1 | const a = false;
222
- ^`.slice(1)
223
- assert({ actual, expected })
224
- }
package/test.mjs DELETED
@@ -1,13 +0,0 @@
1
- import { executeTestPlan, nodeProcess } from "@jsenv/core"
2
-
3
- await executeTestPlan({
4
- rootDirectoryUrl: new URL("./", import.meta.url),
5
- testPlan: {
6
- "test/**/*.test.mjs": {
7
- node: {
8
- runtime: nodeProcess,
9
- },
10
- },
11
- },
12
- coverage: process.argv.includes("--coverage"),
13
- })