@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,87 +0,0 @@
1
- # url meta
2
-
3
- Associate value to urls using pattern matching.
4
-
5
- ```js
6
- import { URL_META } from "@jsenv/urls"
7
-
8
- // conditionally associates url and values
9
- const associations = {
10
- color: {
11
- "file:///*": "black",
12
- "file:///*.js": "red",
13
- },
14
- }
15
- const getUrlColor = (url) => {
16
- return URL_META.applyAssociations({ url, associations }).color
17
- }
18
- console.log(`file.json color is ${getUrlColor("file:///file.json")}`)
19
- console.log(`file.js color is ${getUrlColor("file:///file.js")}`)
20
- ```
21
-
22
- _Code above logs_
23
-
24
- ```console
25
- file.json color is black
26
- file.js color is red
27
- ```
28
-
29
- # Common pattern example
30
-
31
- | pattern | Description |
32
- | ------------------ | ------------------------------------ |
33
- | `**/` | Everything |
34
- | `*/**/` | Inside a directory |
35
- | `**/.*/` | Inside directory starting with a dot |
36
- | `**/node_modules/` | Inside any `node_modules` directory |
37
- | `node_modules/` | Inside root `node_modules` directory |
38
- | `**/*.map` | Ending with `.map` |
39
- | `**/*.test.*` | Contains `.test.` |
40
- | `*` | Inside the root directory only |
41
- | `*/*` | Inside a directory of depth 1 |
42
-
43
- Read more at [./pattern_matching.md](./pattern_matching.md)
44
-
45
- # associations
46
-
47
- _associations_ below translates into: "files are visible except thoose in .git/ directory"
48
-
49
- ```js
50
- const associations = {
51
- visible: {
52
- "**/*/": true,
53
- "**/.git/": false,
54
- },
55
- }
56
- ```
57
-
58
- _associations_ allows to group patterns per property which are easy to read and compose.
59
- All keys in _associations_ must be absolute urls, this can be done with [resolveAssociations](#resolveAssociations).
60
-
61
- # resolveAssociations
62
-
63
- _resolveAssociations_ is a function resolving _associations_ keys that may contain relative urls against an _url_.
64
-
65
- ```js
66
- import { URL_META } from "@jsenv/urls"
67
-
68
- const associations = URL_META.resolveAssociations(
69
- {
70
- visible: {
71
- "**/*/": true,
72
- "**/.git/": false,
73
- },
74
- },
75
- "file:///Users/directory/",
76
- )
77
- console.log(JSON.stringify(associations, null, " "))
78
- ```
79
-
80
- ```json
81
- {
82
- "visible": {
83
- "file:///Users/directory/**/*/": true,
84
- "file:///Users/directory/**/.git/": false
85
- }
86
- }
87
- ```
@@ -1,57 +0,0 @@
1
- import { assert } from "@jsenv/assert"
2
-
3
- import { DataUrl } from "@jsenv/urls"
4
-
5
- {
6
- const actual = DataUrl.parse("data:,")
7
- const expected = {
8
- contentType: "text/plain;charset=US-ASCII",
9
- base64Flag: false,
10
- data: "",
11
- }
12
- assert({ actual, expected })
13
- }
14
-
15
- {
16
- const actual = DataUrl.parse("data:,Hello%2C%20World!")
17
- const expected = {
18
- contentType: "text/plain;charset=US-ASCII",
19
- base64Flag: false,
20
- data: "Hello%2C%20World!",
21
- }
22
- assert({ actual, expected })
23
- }
24
-
25
- {
26
- const actual = DataUrl.parse(
27
- "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D",
28
- )
29
- const expected = {
30
- contentType: "text/plain",
31
- base64Flag: true,
32
- data: "SGVsbG8sIFdvcmxkIQ%3D%3D",
33
- }
34
- assert({ actual, expected })
35
- }
36
-
37
- {
38
- const actual = DataUrl.parse(
39
- "data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E",
40
- )
41
- const expected = {
42
- contentType: "text/html",
43
- base64Flag: false,
44
- data: "%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E",
45
- }
46
- assert({ actual, expected })
47
- }
48
-
49
- {
50
- const actual = DataUrl.parse("data:text/html,<script>alert('hi');</script>")
51
- const expected = {
52
- contentType: "text/html",
53
- base64Flag: false,
54
- data: "<script>alert('hi');</script>",
55
- }
56
- assert({ actual, expected })
57
- }
@@ -1,47 +0,0 @@
1
- import { assert } from "@jsenv/assert"
2
-
3
- import { URL_META } from "@jsenv/urls"
4
-
5
- {
6
- const actual = URL_META.applyAliases({
7
- url: "file:///alias.json",
8
- aliases: {
9
- "file:///alias.json": "file:///data.json",
10
- },
11
- })
12
- const expected = "file:///data.json"
13
- assert({ actual, expected })
14
- }
15
-
16
- {
17
- const actual = URL_META.applyAliases({
18
- url: "file:///a.js",
19
- aliases: {
20
- "file:///*.js": "file:///file.js",
21
- },
22
- })
23
- const expected = "file:///file.js"
24
- assert({ actual, expected })
25
- }
26
-
27
- {
28
- const actual = URL_META.applyAliases({
29
- url: "file:///dir/b.txt",
30
- aliases: {
31
- "file:///dir/*": "file:///dir/a.txt",
32
- },
33
- })
34
- const expected = "file:///dir/a.txt"
35
- assert({ actual, expected })
36
- }
37
-
38
- {
39
- const actual = URL_META.applyAliases({
40
- url: "file:///foo/deep/4fhj.js",
41
- aliases: {
42
- "file:///*/deep/*.js": "file:///*/deep/file.js",
43
- },
44
- })
45
- const expected = "file:///foo/deep/file.js"
46
- assert({ actual, expected })
47
- }
@@ -1,64 +0,0 @@
1
- import { assert } from "@jsenv/assert"
2
-
3
- import { asFlatAssociations } from "@jsenv/urls/src/meta/as_flat_associations.js"
4
-
5
- {
6
- const actual = asFlatAssociations({
7
- visible: {
8
- "file:///a.js": true,
9
- "file:///b.js": false,
10
- },
11
- })
12
- const expected = {
13
- "file:///a.js": {
14
- visible: true,
15
- },
16
- "file:///b.js": {
17
- visible: false,
18
- },
19
- }
20
- assert({ actual, expected })
21
- }
22
-
23
- {
24
- const actual = asFlatAssociations({
25
- visible: {
26
- "file:///a.js": true,
27
- },
28
- whatever: {
29
- "file:///a.js": true,
30
- },
31
- })
32
- const expected = {
33
- "file:///a.js": {
34
- visible: true,
35
- whatever: true,
36
- },
37
- }
38
- assert({ actual, expected })
39
- }
40
-
41
- try {
42
- asFlatAssociations("foo")
43
- throw new Error("shoud crash")
44
- } catch (error) {
45
- const actual = error
46
- const expected = new TypeError(`associations must be a plain object, got foo`)
47
- assert({ actual, expected })
48
- }
49
-
50
- try {
51
- asFlatAssociations({
52
- visible: "foo",
53
- whatever: {
54
- "file:///a.js": true,
55
- },
56
- })
57
- throw new Error("shoud crash")
58
- } catch (error) {
59
- const actual = error
60
- const expected = new TypeError(
61
- `all associations value must be objects, found "visible": foo`,
62
- )
63
- assert({ actual, expected })
64
- }
@@ -1,23 +0,0 @@
1
- import { assert } from "@jsenv/assert"
2
-
3
- import { assertUrlLike } from "@jsenv/urls/src/meta/assertions.js"
4
-
5
- try {
6
- assertUrlLike(["123"])
7
- throw new Error("shoud crash")
8
- } catch (error) {
9
- const actual = error
10
- const expected = new TypeError(`url must be a url string, got 123`)
11
- assert({ actual, expected })
12
- }
13
-
14
- try {
15
- assertUrlLike("*$^=")
16
- throw new Error("shoud crash")
17
- } catch (error) {
18
- const actual = error
19
- const expected = new TypeError(
20
- `url must be a url and no scheme found, got *$^=`,
21
- )
22
- assert({ actual, expected })
23
- }
@@ -1,94 +0,0 @@
1
- import { assert } from "@jsenv/assert"
2
-
3
- import { URL_META } from "@jsenv/urls"
4
-
5
- {
6
- const actual = URL_META.applyAssociations({
7
- url: "file:///a",
8
- associations: {
9
- a: {
10
- "file:///**/*": true,
11
- },
12
- },
13
- })
14
- const expected = { a: true }
15
- assert({ actual, expected })
16
- }
17
-
18
- {
19
- const actual = URL_META.applyAssociations({
20
- url: "file:///node_modules",
21
- associations: {
22
- a: {
23
- "file:///**/*": true,
24
- },
25
- },
26
- })
27
- const expected = { a: true }
28
- assert({ actual, expected })
29
- }
30
-
31
- {
32
- const actual = URL_META.applyAssociations({
33
- url: "file:///a",
34
- associations: {
35
- a: {
36
- "file:///a/**/*.test.js": true,
37
- },
38
- },
39
- })
40
- const expected = {}
41
- assert({ actual, expected })
42
- }
43
-
44
- {
45
- const actual = URL_META.applyAssociations({
46
- url: "file:///a/b.test.js",
47
- associations: {
48
- a: {
49
- "file:///a/**/*.test.js": true,
50
- },
51
- },
52
- })
53
- const expected = { a: true }
54
- assert({ actual, expected })
55
- }
56
-
57
- {
58
- const actual = URL_META.applyAssociations({
59
- url: "file:///a/b.js",
60
- associations: {
61
- a: {
62
- "file:///a/**/*.test.js": true,
63
- },
64
- },
65
- })
66
- const expected = {}
67
- assert({ actual, expected })
68
- }
69
-
70
- {
71
- const actual = URL_META.applyAssociations({
72
- url: "file:///a/b/c.test.js",
73
- associations: {
74
- a: {
75
- "file:///a/**/*.test.js": true,
76
- },
77
- },
78
- })
79
- const expected = { a: true }
80
- assert({ actual, expected })
81
- }
82
-
83
- {
84
- const actual = URL_META.applyAssociations({
85
- url: "file:///index.test.js",
86
- associations: {
87
- a: {
88
- "file:///**/*.js": true,
89
- },
90
- },
91
- })
92
- const expected = { a: true }
93
- assert({ actual, expected })
94
- }
@@ -1,226 +0,0 @@
1
- import { assert } from "@jsenv/assert"
2
-
3
- import { URL_META } from "@jsenv/urls"
4
-
5
- {
6
- const url = "file:///file"
7
- const associations = {
8
- "/file": true,
9
- }
10
- try {
11
- URL_META.applyAssociations({
12
- url,
13
- associations,
14
- })
15
- } catch (actual) {
16
- const expected = new TypeError(
17
- `all associations value must be objects, found "/file": true`,
18
- )
19
- assert({ actual, expected })
20
- }
21
- }
22
-
23
- {
24
- const url = "file:///"
25
- const associations = {
26
- "file:///foo": true,
27
- }
28
- try {
29
- URL_META.applyAssociations({
30
- url,
31
- associations,
32
- })
33
- } catch (actual) {
34
- const expected = new TypeError(
35
- `all associations value must be objects, found "file:///foo": true`,
36
- )
37
- assert({ actual, expected })
38
- }
39
- }
40
-
41
- {
42
- const url = "file:///file"
43
- const associations = {
44
- whatever: {
45
- "file:///*.js": true,
46
- "file:///file.js": null,
47
- },
48
- }
49
- const actual = URL_META.applyAssociations({
50
- url,
51
- associations,
52
- })
53
- const expected = {}
54
- assert({ actual, expected })
55
- }
56
-
57
- {
58
- const actual = URL_META.applyAssociations({
59
- url: "file:///",
60
- associations: {
61
- a: {
62
- "file:///foo": true,
63
- },
64
- },
65
- })
66
- const expected = {}
67
- assert({ actual, expected })
68
- }
69
-
70
- {
71
- const actual = URL_META.applyAssociations({
72
- url: "file:///foo",
73
- associations: {
74
- a: {
75
- "file:///foo": true,
76
- },
77
- },
78
- })
79
- const expected = { a: true }
80
- assert({ actual, expected })
81
- }
82
-
83
- {
84
- const actual = URL_META.applyAssociations({
85
- url: "file:///a",
86
- associations: {
87
- a: {
88
- "file:///a": true,
89
- },
90
- },
91
- })
92
- const expected = { a: true }
93
- assert({ actual, expected })
94
- }
95
-
96
- {
97
- const actual = URL_META.applyAssociations({
98
- url: "file:///a.js",
99
- associations: {
100
- a: {
101
- "file:///a": true,
102
- },
103
- },
104
- })
105
- const expected = {}
106
- assert({ actual, expected })
107
- }
108
-
109
- {
110
- const actual = URL_META.applyAssociations({
111
- url: "file:///a/b",
112
- associations: {
113
- a: {
114
- "file:///a": true,
115
- },
116
- },
117
- })
118
- const expected = {}
119
- assert({ actual, expected })
120
- }
121
-
122
- {
123
- const actual = URL_META.applyAssociations({
124
- url: "file:///a/b.js",
125
- associations: {
126
- a: {
127
- "file:///a": true,
128
- },
129
- },
130
- })
131
- const expected = {}
132
- assert({ actual, expected })
133
- }
134
-
135
- {
136
- const actual = URL_META.applyAssociations({
137
- url: "file:///b/a",
138
- associations: {
139
- a: {
140
- "file:///b/a": true,
141
- },
142
- },
143
- })
144
- const expected = { a: true }
145
- assert({ actual, expected })
146
- }
147
-
148
- {
149
- const actual = URL_META.applyAssociations({
150
- url: "file:///b/a.js",
151
- associations: {
152
- a: {
153
- "file:///b/a": true,
154
- },
155
- },
156
- })
157
- const expected = {}
158
- assert({ actual, expected })
159
- }
160
-
161
- {
162
- const actual = URL_META.applyAssociations({
163
- url: "file:///b/c",
164
- associations: {
165
- a: {
166
- "file:///b/a": true,
167
- },
168
- },
169
- })
170
- const expected = {}
171
- assert({ actual, expected })
172
- }
173
-
174
- {
175
- const actual = URL_META.applyAssociations({
176
- url: "file:///b/a/c",
177
- associations: {
178
- a: {
179
- "file:///b/a": true,
180
- },
181
- },
182
- })
183
- const expected = {}
184
- assert({ actual, expected })
185
- }
186
-
187
- {
188
- const actual = URL_META.applyAssociations({
189
- url: "file:///dist",
190
- associations: {
191
- a: {
192
- "file:///dist": 0,
193
- },
194
- },
195
- })
196
- const expected = { a: 0 }
197
- assert({ actual, expected })
198
- }
199
-
200
- {
201
- const actual = URL_META.applyAssociations({
202
- url: "file:///a/dist",
203
- associations: {
204
- a: {
205
- "file:///dist": 0,
206
- },
207
- },
208
- })
209
- const expected = {}
210
- assert({ actual, expected })
211
- }
212
-
213
- // ensure getUrlMeta overrides in order (without sorting specifier keys by length)
214
- {
215
- const actual = URL_META.applyAssociations({
216
- url: "file:///abcd/",
217
- associations: {
218
- whatever: {
219
- "file:///a*/": 41,
220
- "file:///abcd/": 42,
221
- },
222
- },
223
- })
224
- const expected = { whatever: 42 }
225
- assert({ actual, expected })
226
- }