@scalar/helpers 0.4.1 → 0.4.3

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 (123) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/array/add-to-map-array.js +7 -8
  3. package/dist/array/is-defined.js +12 -5
  4. package/dist/array/sort-by-order.js +60 -18
  5. package/dist/consts/content-types.js +13 -14
  6. package/dist/dom/freeze-element.js +56 -42
  7. package/dist/dom/get-selector.d.ts +7 -0
  8. package/dist/dom/get-selector.d.ts.map +1 -0
  9. package/dist/dom/get-selector.js +31 -0
  10. package/dist/dom/scroll-to-id.js +33 -27
  11. package/dist/file/json2xml.js +79 -61
  12. package/dist/formatters/format-bytes.d.ts +7 -0
  13. package/dist/formatters/format-bytes.d.ts.map +1 -0
  14. package/dist/formatters/format-bytes.js +18 -0
  15. package/dist/formatters/format-milliseconds.d.ts +6 -0
  16. package/dist/formatters/format-milliseconds.d.ts.map +1 -0
  17. package/dist/formatters/format-milliseconds.js +10 -0
  18. package/dist/general/create-limiter.js +47 -31
  19. package/dist/general/debounce.js +86 -66
  20. package/dist/general/extract-config-secrets.js +29 -27
  21. package/dist/general/has-modifier.js +9 -8
  22. package/dist/general/is-mac-os.js +23 -21
  23. package/dist/http/can-method-have-body.js +4 -6
  24. package/dist/http/http-info.js +63 -61
  25. package/dist/http/http-methods.js +4 -7
  26. package/dist/http/http-status-codes.js +316 -320
  27. package/dist/http/is-http-method.js +3 -6
  28. package/dist/http/normalize-http-method.js +19 -19
  29. package/dist/json/escape-json-pointer.js +6 -5
  30. package/dist/json/parse-json-pointer-segments.js +11 -6
  31. package/dist/json/pretty-print-json.d.ts +9 -0
  32. package/dist/json/pretty-print-json.d.ts.map +1 -0
  33. package/dist/json/pretty-print-json.js +42 -0
  34. package/dist/json/unescape-json-pointer.js +7 -5
  35. package/dist/markdown/get-markdown-headings.d.ts +11 -0
  36. package/dist/markdown/get-markdown-headings.d.ts.map +1 -0
  37. package/dist/markdown/get-markdown-headings.js +99 -0
  38. package/dist/node/path.js +168 -138
  39. package/dist/object/get-value-at-path.js +17 -11
  40. package/dist/object/is-object.js +24 -10
  41. package/dist/object/local-storage.js +50 -42
  42. package/dist/object/object-entries.js +2 -5
  43. package/dist/object/object-keys.js +5 -5
  44. package/dist/object/object-replace.js +13 -12
  45. package/dist/object/omit-undefined-values.js +18 -18
  46. package/dist/object/prevent-pollution.js +27 -10
  47. package/dist/object/to-json-compatible.js +71 -62
  48. package/dist/queue/queue.js +103 -84
  49. package/dist/regex/find-variables.js +13 -8
  50. package/dist/regex/regex-helpers.js +17 -17
  51. package/dist/regex/replace-variables.js +21 -19
  52. package/dist/string/camel-to-title.js +10 -5
  53. package/dist/string/capitalize.js +5 -5
  54. package/dist/string/create-hash.js +19 -16
  55. package/dist/string/generate-hash.js +153 -127
  56. package/dist/string/iterate-title.js +13 -11
  57. package/dist/string/truncate.js +16 -9
  58. package/dist/testing/console-spies.js +19 -24
  59. package/dist/testing/measure.js +42 -19
  60. package/dist/testing/measure.test-d.js +12 -9
  61. package/dist/testing/sleep.js +5 -5
  62. package/dist/url/ensure-protocol.js +8 -10
  63. package/dist/url/extract-server-from-path.js +53 -28
  64. package/dist/url/is-local-url.js +24 -18
  65. package/dist/url/is-relative-path.js +15 -13
  66. package/dist/url/is-valid-url.js +17 -10
  67. package/dist/url/make-url-absolute.js +39 -28
  68. package/dist/url/merge-urls.js +74 -52
  69. package/dist/url/redirect-to-proxy.js +68 -39
  70. package/package.json +15 -9
  71. package/dist/array/add-to-map-array.js.map +0 -7
  72. package/dist/array/is-defined.js.map +0 -7
  73. package/dist/array/sort-by-order.js.map +0 -7
  74. package/dist/consts/content-types.js.map +0 -7
  75. package/dist/dom/freeze-element.js.map +0 -7
  76. package/dist/dom/scroll-to-id.js.map +0 -7
  77. package/dist/file/json2xml.js.map +0 -7
  78. package/dist/general/create-limiter.js.map +0 -7
  79. package/dist/general/debounce.js.map +0 -7
  80. package/dist/general/extract-config-secrets.js.map +0 -7
  81. package/dist/general/has-modifier.js.map +0 -7
  82. package/dist/general/is-mac-os.js.map +0 -7
  83. package/dist/http/can-method-have-body.js.map +0 -7
  84. package/dist/http/http-info.js.map +0 -7
  85. package/dist/http/http-methods.js.map +0 -7
  86. package/dist/http/http-status-codes.js.map +0 -7
  87. package/dist/http/is-http-method.js.map +0 -7
  88. package/dist/http/normalize-http-method.js.map +0 -7
  89. package/dist/json/escape-json-pointer.js.map +0 -7
  90. package/dist/json/parse-json-pointer-segments.js.map +0 -7
  91. package/dist/json/unescape-json-pointer.js.map +0 -7
  92. package/dist/node/path.js.map +0 -7
  93. package/dist/object/get-value-at-path.js.map +0 -7
  94. package/dist/object/is-object.js.map +0 -7
  95. package/dist/object/local-storage.js.map +0 -7
  96. package/dist/object/object-entries.js.map +0 -7
  97. package/dist/object/object-keys.js.map +0 -7
  98. package/dist/object/object-replace.js.map +0 -7
  99. package/dist/object/omit-undefined-values.js.map +0 -7
  100. package/dist/object/prevent-pollution.js.map +0 -7
  101. package/dist/object/to-json-compatible.js.map +0 -7
  102. package/dist/queue/queue.js.map +0 -7
  103. package/dist/regex/find-variables.js.map +0 -7
  104. package/dist/regex/regex-helpers.js.map +0 -7
  105. package/dist/regex/replace-variables.js.map +0 -7
  106. package/dist/string/camel-to-title.js.map +0 -7
  107. package/dist/string/capitalize.js.map +0 -7
  108. package/dist/string/create-hash.js.map +0 -7
  109. package/dist/string/generate-hash.js.map +0 -7
  110. package/dist/string/iterate-title.js.map +0 -7
  111. package/dist/string/truncate.js.map +0 -7
  112. package/dist/testing/console-spies.js.map +0 -7
  113. package/dist/testing/measure.js.map +0 -7
  114. package/dist/testing/measure.test-d.js.map +0 -7
  115. package/dist/testing/sleep.js.map +0 -7
  116. package/dist/url/ensure-protocol.js.map +0 -7
  117. package/dist/url/extract-server-from-path.js.map +0 -7
  118. package/dist/url/is-local-url.js.map +0 -7
  119. package/dist/url/is-relative-path.js.map +0 -7
  120. package/dist/url/is-valid-url.js.map +0 -7
  121. package/dist/url/make-url-absolute.js.map +0 -7
  122. package/dist/url/merge-urls.js.map +0 -7
  123. package/dist/url/redirect-to-proxy.js.map +0 -7
@@ -1,6 +1,3 @@
1
- import { httpMethods } from "./http-methods.js";
2
- const isHttpMethod = (method) => method && typeof method === "string" ? httpMethods.has(method.toLowerCase()) : false;
3
- export {
4
- isHttpMethod
5
- };
6
- //# sourceMappingURL=is-http-method.js.map
1
+ import { httpMethods } from './http-methods.js';
2
+ /** Type guard which takes in a string and returns true if it is in fact an HTTPMethod */
3
+ export const isHttpMethod = (method) => method && typeof method === 'string' ? httpMethods.has(method.toLowerCase()) : false;
@@ -1,20 +1,20 @@
1
- import { isHttpMethod } from "./is-http-method.js";
2
- const DEFAULT_REQUEST_METHOD = "get";
3
- const normalizeHttpMethod = (method) => {
4
- if (typeof method !== "string") {
5
- console.warn(`Request method is not a string. Using ${DEFAULT_REQUEST_METHOD} as the default.`);
6
- return DEFAULT_REQUEST_METHOD;
7
- }
8
- const normalizedMethod = method.trim().toLowerCase();
9
- if (!isHttpMethod(normalizedMethod)) {
10
- console.warn(
11
- `${method || "Request method"} is not a valid request method. Using ${DEFAULT_REQUEST_METHOD} as the default.`
12
- );
13
- return DEFAULT_REQUEST_METHOD;
14
- }
15
- return normalizedMethod;
1
+ import { isHttpMethod } from './is-http-method.js';
2
+ const DEFAULT_REQUEST_METHOD = 'get';
3
+ /**
4
+ * Get a normalized request method (e.g. get, post, etc.)
5
+ * Lowercases the method and returns the default if it is not a valid method so you will always have a valid method
6
+ */
7
+ export const normalizeHttpMethod = (method) => {
8
+ // Make sure it's a string
9
+ if (typeof method !== 'string') {
10
+ console.warn(`Request method is not a string. Using ${DEFAULT_REQUEST_METHOD} as the default.`);
11
+ return DEFAULT_REQUEST_METHOD;
12
+ }
13
+ // Normalize the string
14
+ const normalizedMethod = method.trim().toLowerCase();
15
+ if (!isHttpMethod(normalizedMethod)) {
16
+ console.warn(`${method || 'Request method'} is not a valid request method. Using ${DEFAULT_REQUEST_METHOD} as the default.`);
17
+ return DEFAULT_REQUEST_METHOD;
18
+ }
19
+ return normalizedMethod;
16
20
  };
17
- export {
18
- normalizeHttpMethod
19
- };
20
- //# sourceMappingURL=normalize-http-method.js.map
@@ -1,5 +1,6 @@
1
- const escapeJsonPointer = (str) => str.replace(/~/g, "~0").replace(/\//g, "~1");
2
- export {
3
- escapeJsonPointer
4
- };
5
- //# sourceMappingURL=escape-json-pointer.js.map
1
+ /**
2
+ * Escapes a JSON pointer string.
3
+ *
4
+ * Example: `/foo/bar~baz` -> `~1foo~1bar~0baz`
5
+ */
6
+ export const escapeJsonPointer = (str) => str.replace(/~/g, '~0').replace(/\//g, '~1');
@@ -1,6 +1,11 @@
1
- import { unescapeJsonPointer } from "./unescape-json-pointer.js";
2
- const parseJsonPointerSegments = (path) => path.split("/").slice(1).map(unescapeJsonPointer);
3
- export {
4
- parseJsonPointerSegments
5
- };
6
- //# sourceMappingURL=parse-json-pointer-segments.js.map
1
+ import { unescapeJsonPointer } from './unescape-json-pointer.js';
2
+ /**
3
+ * Translate `/paths/~1test` to `['paths', '/test']`
4
+ */
5
+ export const parseJsonPointerSegments = (path) => path
6
+ // ['', 'paths', '~1test']
7
+ .split('/')
8
+ // ['paths', '~1test']
9
+ .slice(1)
10
+ // ['paths', '/test']
11
+ .map(unescapeJsonPointer);
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Takes JSON and formats it.
3
+ */
4
+ export declare const prettyPrintJson: (value: string | number | any[] | Record<any, any>) => string;
5
+ /**
6
+ * JSON.stringify, but with circular dependencies replaced with '[Circular]'
7
+ */
8
+ export declare function replaceCircularDependencies(content: any): string;
9
+ //# sourceMappingURL=pretty-print-json.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pretty-print-json.d.ts","sourceRoot":"","sources":["../../src/json/pretty-print-json.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,eAAO,MAAM,eAAe,GAAI,OAAO,MAAM,GAAG,MAAM,GAAG,GAAG,EAAE,GAAG,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,KAAG,MAyBnF,CAAA;AAED;;GAEG;AACH,wBAAgB,2BAA2B,CAAC,OAAO,EAAE,GAAG,UAiBvD"}
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Takes JSON and formats it.
3
+ */
4
+ export const prettyPrintJson = (value) => {
5
+ // When the values is already a string it should be parsable
6
+ if (typeof value === 'string') {
7
+ try {
8
+ const parsed = JSON.parse(value);
9
+ if (typeof parsed === 'object' && parsed !== null) {
10
+ return JSON.stringify(parsed, null, 2);
11
+ }
12
+ return value;
13
+ }
14
+ catch {
15
+ return value;
16
+ }
17
+ }
18
+ if (typeof value === 'object') {
19
+ try {
20
+ return JSON.stringify(value, null, 2);
21
+ }
22
+ catch {
23
+ return replaceCircularDependencies(value);
24
+ }
25
+ }
26
+ return value?.toString() ?? '';
27
+ };
28
+ /**
29
+ * JSON.stringify, but with circular dependencies replaced with '[Circular]'
30
+ */
31
+ export function replaceCircularDependencies(content) {
32
+ const cache = new Set();
33
+ return JSON.stringify(content, (_key, value) => {
34
+ if (typeof value === 'object' && value !== null) {
35
+ if (cache.has(value)) {
36
+ return '[Circular]';
37
+ }
38
+ cache.add(value);
39
+ }
40
+ return value;
41
+ }, 2);
42
+ }
@@ -1,5 +1,7 @@
1
- const unescapeJsonPointer = (uri) => decodeURI(uri.replace(/~1/g, "/").replace(/~0/g, "~"));
2
- export {
3
- unescapeJsonPointer
4
- };
5
- //# sourceMappingURL=unescape-json-pointer.js.map
1
+ /**
2
+ * Unescape JSON pointer
3
+ *
4
+ * Examples:
5
+ * /foo~1bar~0baz -> /foo/bar~baz
6
+ */
7
+ export const unescapeJsonPointer = (uri) => decodeURI(uri.replace(/~1/g, '/').replace(/~0/g, '~'));
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Extract all headings from a Markdown string without any library dependencies.
3
+ *
4
+ * Correctly skips headings inside fenced code blocks (``` or ~~~) and
5
+ * indented code blocks (4+ spaces or tab).
6
+ */
7
+ export declare function getMarkdownHeadings(markdown: string): {
8
+ depth: number;
9
+ value: string;
10
+ }[];
11
+ //# sourceMappingURL=get-markdown-headings.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-markdown-headings.d.ts","sourceRoot":"","sources":["../../src/markdown/get-markdown-headings.ts"],"names":[],"mappings":"AA8BA;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,GAAG;IACrD,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,EAAE,MAAM,CAAA;CACd,EAAE,CA2EF"}
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Strips inline Markdown formatting to extract plain text.
3
+ *
4
+ * Handles: links, images, bold/italic, inline code, strikethrough, and HTML tags.
5
+ */
6
+ function stripInlineMarkdown(text) {
7
+ return (text
8
+ // Images: ![alt](url) → alt
9
+ .replace(/!\[([^\]]*)\]\([^)]*\)/g, '$1')
10
+ // Links: [text](url) → text
11
+ .replace(/\[([^\]]*)\]\([^)]*\)/g, '$1')
12
+ // Bold/italic combos: ***text*** or ___text___
13
+ .replace(/(\*{3}|_{3})(.+?)\1/g, '$2')
14
+ // Bold: **text** or __text__
15
+ .replace(/(\*{2}|_{2})(.+?)\1/g, '$2')
16
+ // Italic: *text* or _text_
17
+ .replace(/(\*|_)(.+?)\1/g, '$2')
18
+ // Strikethrough: ~~text~~
19
+ .replace(/~~(.+?)~~/g, '$1')
20
+ // Inline code: `code`
21
+ .replace(/`([^`]+)`/g, '$1')
22
+ // HTML tags
23
+ .replace(/<[^>]+>/g, '')
24
+ // Remove any remaining angle brackets to avoid partial/malformed HTML fragments
25
+ .replace(/[<>]/g, '')
26
+ .trim());
27
+ }
28
+ /**
29
+ * Extract all headings from a Markdown string without any library dependencies.
30
+ *
31
+ * Correctly skips headings inside fenced code blocks (``` or ~~~) and
32
+ * indented code blocks (4+ spaces or tab).
33
+ */
34
+ export function getMarkdownHeadings(markdown) {
35
+ const lines = markdown.split(/\r?\n/);
36
+ const headings = [];
37
+ let inFencedBlock = false;
38
+ let fenceChar = '';
39
+ let fenceLength = 0;
40
+ for (let index = 0; index < lines.length; index++) {
41
+ const line = lines[index] ?? '';
42
+ const trimmed = line.trimStart();
43
+ if (!inFencedBlock) {
44
+ const fence = /^(`{3,}|~{3,})/.exec(trimmed)?.[1];
45
+ if (fence) {
46
+ inFencedBlock = true;
47
+ fenceChar = fence.charAt(0);
48
+ fenceLength = fence.length;
49
+ continue;
50
+ }
51
+ }
52
+ else {
53
+ const closeFence = /^(`{3,}|~{3,})\s*$/.exec(trimmed)?.[1];
54
+ if (closeFence && closeFence[0] === fenceChar && closeFence.length >= fenceLength) {
55
+ inFencedBlock = false;
56
+ }
57
+ continue;
58
+ }
59
+ // Skip indented code blocks (4 spaces or 1 tab)
60
+ if (/^(?: {4}|\t)/.test(line)) {
61
+ continue;
62
+ }
63
+ // Match ATX headings: 1-6 # characters followed by a space
64
+ const headingMatch = /^(#{1,6})\s+(.+?)(?:\s+#+\s*)?$/.exec(trimmed);
65
+ const hashes = headingMatch?.[1];
66
+ const rawText = headingMatch?.[2];
67
+ if (hashes && rawText) {
68
+ const value = stripInlineMarkdown(rawText);
69
+ if (value) {
70
+ headings.push({
71
+ depth: hashes.length,
72
+ value,
73
+ });
74
+ }
75
+ continue;
76
+ }
77
+ // Match setext headings:
78
+ // Heading level 1: Text followed by ===
79
+ // Heading level 2: Text followed by ---
80
+ const nextLine = lines[index + 1];
81
+ if (!nextLine || /^(?: {4}|\t)/.test(nextLine)) {
82
+ continue;
83
+ }
84
+ const underline = /^(=+|-+)\s*$/.exec(nextLine.trim())?.[1];
85
+ if (!underline) {
86
+ continue;
87
+ }
88
+ const value = stripInlineMarkdown(trimmed);
89
+ if (!value) {
90
+ continue;
91
+ }
92
+ headings.push({
93
+ depth: underline[0] === '=' ? 1 : 2,
94
+ value,
95
+ });
96
+ index++;
97
+ }
98
+ return headings;
99
+ }
package/dist/node/path.js CHANGED
@@ -1,157 +1,187 @@
1
+ // Copyright Joyent, Inc. and other Node contributors.
2
+ //
3
+ // Permission is hereby granted, free of charge, to any person obtaining a
4
+ // copy of this software and associated documentation files (the
5
+ // "Software"), to deal in the Software without restriction, including
6
+ // without limitation the rights to use, copy, modify, merge, publish,
7
+ // distribute, sublicense, and/or sell copies of the Software, and to permit
8
+ // persons to whom the Software is furnished to do so, subject to the
9
+ // following conditions:
10
+ //
11
+ // The above copyright notice and this permission notice shall be included
12
+ // in all copies or substantial portions of the Software.
13
+ //
14
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
+ // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
+ // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
+ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
+ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
+ // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+ // resolves . and .. elements in a path array with directory names there
22
+ // must be no slashes, empty elements, or device names (c:\) in the array
23
+ // (so also no leading and trailing slashes - it does not distinguish
24
+ // relative and absolute paths)
25
+ // !! WARNING !!
26
+ // The original implementation included ponyfills for `filters` and `substr`.
27
+ // These have been removed in favor of native implementations supported by the current platforms.
28
+ // See: https://github.com/scalar/scalar/pull/7235#discussion_r2484230212
1
29
  function normalizeArray(parts, allowAboveRoot) {
2
- let up = 0;
3
- for (let i = parts.length - 1; i >= 0; i--) {
4
- const last = parts[i];
5
- if (last === ".") {
6
- parts.splice(i, 1);
7
- } else if (last === "..") {
8
- parts.splice(i, 1);
9
- up++;
10
- } else if (up) {
11
- parts.splice(i, 1);
12
- up--;
30
+ // if the path tries to go above the root, `up` ends up > 0
31
+ let up = 0;
32
+ for (let i = parts.length - 1; i >= 0; i--) {
33
+ const last = parts[i];
34
+ if (last === '.') {
35
+ parts.splice(i, 1);
36
+ }
37
+ else if (last === '..') {
38
+ parts.splice(i, 1);
39
+ up++;
40
+ }
41
+ else if (up) {
42
+ parts.splice(i, 1);
43
+ up--;
44
+ }
13
45
  }
14
- }
15
- if (allowAboveRoot) {
16
- for (; up--; up) {
17
- parts.unshift("..");
46
+ // if the path is allowed to go above the root, restore leading ..s
47
+ if (allowAboveRoot) {
48
+ for (; up--; up) {
49
+ parts.unshift('..');
50
+ }
18
51
  }
19
- }
20
- return parts;
52
+ return parts;
21
53
  }
54
+ // Split a filename into [root, dir, basename, ext], unix version
55
+ // 'root' is just a slash, or nothing.
22
56
  const splitPathRe = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^/]+?|)(\.[^./]*|))(?:[/]*)$/;
23
57
  const splitPath = (filename) => splitPathRe.exec(filename).slice(1);
24
- function resolve(...parameters) {
25
- let resolvedPath = "", resolvedAbsolute = false;
26
- for (let i = parameters.length - 1; i >= -1 && !resolvedAbsolute; i--) {
27
- const path2 = i >= 0 ? parameters[i] : "/";
28
- if (typeof path2 !== "string") {
29
- throw new TypeError("Arguments to path.resolve must be strings");
58
+ // path.resolve([from ...], to)
59
+ // posix version
60
+ export function resolve(...parameters) {
61
+ let resolvedPath = '', resolvedAbsolute = false;
62
+ for (let i = parameters.length - 1; i >= -1 && !resolvedAbsolute; i--) {
63
+ const path = i >= 0 ? parameters[i] : '/';
64
+ // Skip empty and invalid entries
65
+ if (typeof path !== 'string') {
66
+ throw new TypeError('Arguments to path.resolve must be strings');
67
+ }
68
+ if (!path) {
69
+ continue;
70
+ }
71
+ resolvedPath = path + '/' + resolvedPath;
72
+ resolvedAbsolute = path.charAt(0) === '/';
30
73
  }
31
- if (!path2) {
32
- continue;
33
- }
34
- resolvedPath = path2 + "/" + resolvedPath;
35
- resolvedAbsolute = path2.charAt(0) === "/";
36
- }
37
- resolvedPath = normalizeArray(
38
- resolvedPath.split("/").filter((p) => !!p),
39
- !resolvedAbsolute
40
- ).join("/");
41
- return (resolvedAbsolute ? "/" : "") + resolvedPath || ".";
74
+ // At this point the path should be resolved to a full absolute path, but
75
+ // handle relative paths to be safe (might happen when process.cwd() fails)
76
+ // Normalize the path
77
+ resolvedPath = normalizeArray(resolvedPath.split('/').filter((p) => !!p), !resolvedAbsolute).join('/');
78
+ return (resolvedAbsolute ? '/' : '') + resolvedPath || '.';
42
79
  }
43
- function normalize(inputPath) {
44
- const isPathAbsolute = isAbsolute(inputPath), trailingSlash = inputPath.slice(-1) === "/";
45
- let path2 = normalizeArray(
46
- inputPath.split("/").filter((p) => !!p),
47
- !isPathAbsolute
48
- ).join("/");
49
- if (!path2 && !isPathAbsolute) {
50
- path2 = ".";
51
- }
52
- if (path2 && trailingSlash) {
53
- path2 += "/";
54
- }
55
- return (isPathAbsolute ? "/" : "") + path2;
80
+ // path.normalize(path)
81
+ // posix version
82
+ export function normalize(inputPath) {
83
+ const isPathAbsolute = isAbsolute(inputPath), trailingSlash = inputPath.slice(-1) === '/';
84
+ // Normalize the path
85
+ let path = normalizeArray(inputPath.split('/').filter((p) => !!p), !isPathAbsolute).join('/');
86
+ if (!path && !isPathAbsolute) {
87
+ path = '.';
88
+ }
89
+ if (path && trailingSlash) {
90
+ path += '/';
91
+ }
92
+ return (isPathAbsolute ? '/' : '') + path;
56
93
  }
57
- function isAbsolute(path2) {
58
- return path2.charAt(0) === "/";
94
+ // posix version
95
+ export function isAbsolute(path) {
96
+ return path.charAt(0) === '/';
59
97
  }
60
- function join(...paths) {
61
- return normalize(
62
- paths.filter((p) => {
63
- if (typeof p !== "string") {
64
- throw new TypeError("Arguments to path.join must be strings");
65
- }
66
- return p;
67
- }).join("/")
68
- );
98
+ // posix version
99
+ export function join(...paths) {
100
+ return normalize(paths
101
+ .filter((p) => {
102
+ if (typeof p !== 'string') {
103
+ throw new TypeError('Arguments to path.join must be strings');
104
+ }
105
+ return p;
106
+ })
107
+ .join('/'));
69
108
  }
70
- function relative(from, to) {
71
- const fromResolved = resolve(from).substring(1);
72
- const toResolved = resolve(to).substring(1);
73
- function trim(arr) {
74
- let start = 0;
75
- for (; start < arr.length; start++) {
76
- if (arr[start] !== "") {
77
- break;
78
- }
79
- }
80
- let end = arr.length - 1;
81
- for (; end >= 0; end--) {
82
- if (arr[end] !== "") {
83
- break;
84
- }
109
+ // path.relative(from, to)
110
+ // posix version
111
+ export function relative(from, to) {
112
+ const fromResolved = resolve(from).substring(1);
113
+ const toResolved = resolve(to).substring(1);
114
+ function trim(arr) {
115
+ let start = 0;
116
+ for (; start < arr.length; start++) {
117
+ if (arr[start] !== '') {
118
+ break;
119
+ }
120
+ }
121
+ let end = arr.length - 1;
122
+ for (; end >= 0; end--) {
123
+ if (arr[end] !== '') {
124
+ break;
125
+ }
126
+ }
127
+ if (start > end) {
128
+ return [];
129
+ }
130
+ return arr.slice(start, end - start + 1);
85
131
  }
86
- if (start > end) {
87
- return [];
132
+ const fromParts = trim(fromResolved.split('/'));
133
+ const toParts = trim(toResolved.split('/'));
134
+ const length = Math.min(fromParts.length, toParts.length);
135
+ let samePartsLength = length;
136
+ for (let i = 0; i < length; i++) {
137
+ if (fromParts[i] !== toParts[i]) {
138
+ samePartsLength = i;
139
+ break;
140
+ }
88
141
  }
89
- return arr.slice(start, end - start + 1);
90
- }
91
- const fromParts = trim(fromResolved.split("/"));
92
- const toParts = trim(toResolved.split("/"));
93
- const length = Math.min(fromParts.length, toParts.length);
94
- let samePartsLength = length;
95
- for (let i = 0; i < length; i++) {
96
- if (fromParts[i] !== toParts[i]) {
97
- samePartsLength = i;
98
- break;
142
+ let outputParts = [];
143
+ for (let i = samePartsLength; i < fromParts.length; i++) {
144
+ outputParts.push('..');
99
145
  }
100
- }
101
- let outputParts = [];
102
- for (let i = samePartsLength; i < fromParts.length; i++) {
103
- outputParts.push("..");
104
- }
105
- outputParts = outputParts.concat(toParts.slice(samePartsLength));
106
- return outputParts.join("/");
146
+ outputParts = outputParts.concat(toParts.slice(samePartsLength));
147
+ return outputParts.join('/');
107
148
  }
108
- const sep = "/";
109
- const delimiter = ":";
110
- function dirname(path2) {
111
- const result = splitPath(path2);
112
- const root = result[0];
113
- let dir = result[1];
114
- if (!root && !dir) {
115
- return ".";
116
- }
117
- if (dir) {
118
- dir = dir.slice(0, -1);
119
- }
120
- return root + dir;
149
+ export const sep = '/';
150
+ export const delimiter = ':';
151
+ export function dirname(path) {
152
+ const result = splitPath(path);
153
+ const root = result[0];
154
+ let dir = result[1];
155
+ if (!root && !dir) {
156
+ // No dirname whatsoever
157
+ return '.';
158
+ }
159
+ if (dir) {
160
+ // It has a dirname, strip trailing slash
161
+ dir = dir.slice(0, -1);
162
+ }
163
+ return root + dir;
121
164
  }
122
- function basename(path2, ext) {
123
- let f = splitPath(path2)[2];
124
- if (ext && f.slice(-ext.length) === ext) {
125
- f = f.slice(0, -ext.length);
126
- }
127
- return f;
165
+ export function basename(path, ext) {
166
+ let f = splitPath(path)[2];
167
+ // TODO: make this comparison case-insensitive on windows?
168
+ if (ext && f.slice(-ext.length) === ext) {
169
+ f = f.slice(0, -ext.length);
170
+ }
171
+ return f;
128
172
  }
129
- function extname(path2) {
130
- return splitPath(path2)[3];
173
+ export function extname(path) {
174
+ return splitPath(path)[3];
131
175
  }
132
- const path = {
133
- extname,
134
- basename,
135
- dirname,
136
- sep,
137
- delimiter,
138
- relative,
139
- join,
140
- isAbsolute,
141
- normalize,
142
- resolve
143
- };
144
- export {
145
- basename,
146
- delimiter,
147
- dirname,
148
- extname,
149
- isAbsolute,
150
- join,
151
- normalize,
152
- path,
153
- relative,
154
- resolve,
155
- sep
176
+ export const path = {
177
+ extname,
178
+ basename,
179
+ dirname,
180
+ sep,
181
+ delimiter,
182
+ relative,
183
+ join,
184
+ isAbsolute,
185
+ normalize,
186
+ resolve,
156
187
  };
157
- //# sourceMappingURL=path.js.map
@@ -1,12 +1,18 @@
1
- function getValueAtPath(obj, pointer) {
2
- return pointer.reduce((acc, part) => {
3
- if (acc === void 0 || acc === null) {
4
- return void 0;
5
- }
6
- return acc[part];
7
- }, obj);
1
+ /**
2
+ * Retrieves a nested value from the source document using a path array
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * getValueByPath(document, ['components', 'schemas', 'User'])
7
+ *
8
+ * { id: '123', name: 'John Doe' }
9
+ * ```
10
+ */
11
+ export function getValueAtPath(obj, pointer) {
12
+ return pointer.reduce((acc, part) => {
13
+ if (acc === undefined || acc === null) {
14
+ return undefined;
15
+ }
16
+ return acc[part];
17
+ }, obj);
8
18
  }
9
- export {
10
- getValueAtPath
11
- };
12
- //# sourceMappingURL=get-value-at-path.js.map