@duplojs/utils 1.4.47 → 1.4.49

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.
@@ -0,0 +1,114 @@
1
+ 'use strict';
2
+
3
+ var success = require('../either/right/success.cjs');
4
+ var fail = require('../either/left/fail.cjs');
5
+
6
+ exports.Path = void 0;
7
+ (function (Path) {
8
+ Path.baseNameRegex = /\/?([^/]+)$/;
9
+ Path.folderNameRegex = /([^]+)\/[^/]+\/?$/;
10
+ Path.extensionNameRegex = /\.([^./]+)$/;
11
+ Path.isRelativeRegex = /(^|\/)\.\.(?=\/|$)/;
12
+ Path.segmentTrailingRegex = /\/$/;
13
+ Path.segmentRelativeRegex = /^(.\/)/;
14
+ /**
15
+ * {@include common/path/getBaseName/index.md}
16
+ */
17
+ function getBaseName(path, params) {
18
+ const match = Path.baseNameRegex.exec(path);
19
+ if (!match) {
20
+ return null;
21
+ }
22
+ if (params?.removeExtension) {
23
+ return match[1].replace(Path.extensionNameRegex, "");
24
+ }
25
+ return match[1];
26
+ }
27
+ Path.getBaseName = getBaseName;
28
+ /**
29
+ * {@include common/path/getExtensionName/index.md}
30
+ */
31
+ function getExtensionName(path) {
32
+ const match = Path.extensionNameRegex.exec(path);
33
+ if (match) {
34
+ return match[1];
35
+ }
36
+ return null;
37
+ }
38
+ Path.getExtensionName = getExtensionName;
39
+ /**
40
+ * {@include common/path/getParentFolderPath/index.md}
41
+ */
42
+ function getParentFolderPath(path) {
43
+ const match = path.match(Path.folderNameRegex);
44
+ if (!match) {
45
+ return null;
46
+ }
47
+ return match[1];
48
+ }
49
+ Path.getParentFolderPath = getParentFolderPath;
50
+ /**
51
+ * {@include common/path/isAbsolute/index.md}
52
+ */
53
+ function isAbsolute(path) {
54
+ return path.startsWith("/")
55
+ && !Path.isRelativeRegex.test(path);
56
+ }
57
+ Path.isAbsolute = isAbsolute;
58
+ /**
59
+ * {@include common/path/resolveFrom/index.md}
60
+ */
61
+ function resolveFrom(origin, segments) {
62
+ const result = resolveRelative([origin, ...segments]);
63
+ return isAbsolute(result)
64
+ ? success.success(result)
65
+ : fail.fail();
66
+ }
67
+ Path.resolveFrom = resolveFrom;
68
+ /**
69
+ * {@include common/path/resolveRelative/index.md}
70
+ */
71
+ function resolveRelative(segments) {
72
+ let clearedPath = "";
73
+ for (const segment of segments) {
74
+ if (segment.length === 0) {
75
+ continue;
76
+ }
77
+ if (segment === "/") {
78
+ clearedPath = segment;
79
+ continue;
80
+ }
81
+ const formattedSegment = segment
82
+ .replace(Path.segmentTrailingRegex, "")
83
+ .replace(Path.segmentRelativeRegex, "");
84
+ if (formattedSegment.startsWith("/")) {
85
+ clearedPath = formattedSegment;
86
+ continue;
87
+ }
88
+ if (clearedPath === "/") {
89
+ clearedPath += formattedSegment;
90
+ }
91
+ else {
92
+ clearedPath += `/${formattedSegment}`;
93
+ }
94
+ }
95
+ const dotResult = [];
96
+ const result = [];
97
+ for (const element of clearedPath.split("/")) {
98
+ if (element === "..") {
99
+ const deletedElement = result.pop();
100
+ if (!deletedElement) {
101
+ dotResult.push(element);
102
+ }
103
+ }
104
+ else {
105
+ result.push(element);
106
+ }
107
+ }
108
+ if (dotResult.length === 0) {
109
+ return result.join("/");
110
+ }
111
+ return `${dotResult.join("/")}/${result.join("/")}`;
112
+ }
113
+ Path.resolveRelative = resolveRelative;
114
+ })(exports.Path || (exports.Path = {}));
@@ -0,0 +1,145 @@
1
+ import * as DEither from "../either";
2
+ import type { AnyTuple } from "./types";
3
+ export declare namespace Path {
4
+ const baseNameRegex: RegExp;
5
+ const folderNameRegex: RegExp;
6
+ const extensionNameRegex: RegExp;
7
+ const isRelativeRegex: RegExp;
8
+ const segmentTrailingRegex: RegExp;
9
+ const segmentRelativeRegex: RegExp;
10
+ interface GetBaseNameParams {
11
+ removeExtension?: boolean;
12
+ }
13
+ /**
14
+ * Returns the last segment of a path (after the final slash), with optional extension removal.
15
+ *
16
+ * **Supported call styles:**
17
+ * - Classic: `getBaseName(path, params?)` -> returns the base name or null
18
+ *
19
+ * The path must contain at least one `/` to match. It returns null when no segment is found.
20
+ * When `removeExtension` is true, the file extension is removed from the base name.
21
+ *
22
+ * ```ts
23
+ * const defaultResult = Path.getBaseName("/foo/bar.txt");
24
+ * // defaultResult: "bar.txt"
25
+ * const withoutExtResult = Path.getBaseName("/foo/bar.txt", { removeExtension: true });
26
+ * // withoutExtResult: "bar"
27
+ * const nullResult = Path.getBaseName("bar.txt");
28
+ * // nullResult: null
29
+ * ```
30
+ *
31
+ * @see https://utils.duplojs.dev/en/v1/api/common/path/getBaseName
32
+ *
33
+ */
34
+ function getBaseName<GenericPath extends string>(path: GenericPath, params?: GetBaseNameParams): string | null;
35
+ /**
36
+ * Returns the last extension of a path, without the leading dot.
37
+ *
38
+ * **Supported call styles:**
39
+ * - Classic: `getExtensionName(path)` -> returns the extension or null
40
+ *
41
+ * It returns null when no extension is found, when the path ends with a dot, or when the path is `..`.
42
+ *
43
+ * ```ts
44
+ * const txtResult = Path.getExtensionName("/foo/bar.txt");
45
+ * // txtResult: "txt"
46
+ * const tarResult = Path.getExtensionName("archive.tar.gz");
47
+ * // tarResult: "gz"
48
+ * const dotResult = Path.getExtensionName("file.");
49
+ * // dotResult: null
50
+ * ```
51
+ *
52
+ * @see https://utils.duplojs.dev/en/v1/api/common/path/getExtensionName
53
+ *
54
+ */
55
+ function getExtensionName<GenericPath extends string>(path: GenericPath): string | null;
56
+ /**
57
+ * Returns the parent folder path of a POSIX path.
58
+ *
59
+ * **Supported call styles:**
60
+ * - Classic: `getParentFolderPath(path)` -> returns the parent folder or null
61
+ *
62
+ * The path must contain at least one `/` to match. It removes a trailing slash and drops the last segment.
63
+ *
64
+ * ```ts
65
+ * const result = Path.getParentFolderPath("/foo/bar/baz");
66
+ * // result: "/foo/bar"
67
+ * const trailingResult = Path.getParentFolderPath("/foo/bar/");
68
+ * // trailingResult: "/foo"
69
+ * const relativeResult = Path.getParentFolderPath("foo");
70
+ * // relativeResult: null
71
+ * ```
72
+ *
73
+ * @see https://utils.duplojs.dev/en/v1/api/common/path/getParentFolderPath
74
+ *
75
+ */
76
+ function getParentFolderPath<GenericPath extends string>(path: GenericPath): string | null;
77
+ /**
78
+ * Checks whether a path is absolute (POSIX).
79
+ *
80
+ * **Supported call styles:**
81
+ * - Classic: `isAbsolute(path)` -> returns a boolean
82
+ *
83
+ * It returns true when the path starts with `/` and does not contain `..` segments.
84
+ *
85
+ * ```ts
86
+ * const absolutePath = Path.isAbsolute("/var/log");
87
+ * // absolutePath: true
88
+ * const parentTraversal = Path.isAbsolute("/var/../log");
89
+ * // parentTraversal: false
90
+ * const relativePath = Path.isAbsolute("var/log");
91
+ * // relativePath: false
92
+ * ```
93
+ *
94
+ * @see https://utils.duplojs.dev/en/v1/api/common/path/isAbsolute
95
+ *
96
+ */
97
+ function isAbsolute<GenericPath extends string>(path: GenericPath): boolean;
98
+ /**
99
+ * Resolves a list of path segments from an origin and returns an Either.
100
+ *
101
+ * **Supported call styles:**
102
+ * - Classic: `resolveFrom(origin, segments)` -> returns an Either
103
+ *
104
+ * Segments are resolved in order using `resolveRelative`.
105
+ * The result is an `Either` that is `success` only when the resolved path is absolute; otherwise it returns `fail`.
106
+ *
107
+ * ```ts
108
+ * const absoluteResult = Path.resolveFrom("/root", ["alpha", "beta"]);
109
+ * // absoluteResult: DEither.success<"/root/alpha/beta">
110
+ * const result = unwrap(absoluteResult);
111
+ * // result: "/root/alpha/beta"
112
+ *
113
+ * const overrideResult = Path.resolveFrom("gamma", ["alpha", "/root", "beta"]);
114
+ * // overrideResult: DEither.success<"/root/beta">
115
+ * const relativeResult = Path.resolveFrom("alpha", ["..", ".."]);
116
+ * // relativeResult: DEither.fail
117
+ * ```
118
+ *
119
+ * @see https://utils.duplojs.dev/en/v1/api/common/path/resolveFrom
120
+ *
121
+ */
122
+ function resolveFrom<GenericSegment extends string>(origin: string, segments: AnyTuple<GenericSegment>): DEither.Fail | DEither.Success<string>;
123
+ /**
124
+ * Resolves path segments into a single POSIX-like path.
125
+ *
126
+ * **Supported call styles:**
127
+ * - Classic: `resolveRelative(segments)` -> returns the resolved path
128
+ *
129
+ * Empty segments are ignored, trailing slashes and leading `./` are removed, and absolute segments reset the base.
130
+ * `..` segments remove previous segments and may remain leading when resolving above root.
131
+ *
132
+ * ```ts
133
+ * const basicResult = Path.resolveRelative(["alpha", "beta"]);
134
+ * // basicResult: "/alpha/beta"
135
+ * const overrideResult = Path.resolveRelative(["alpha", "/root", "beta"]);
136
+ * // overrideResult: "/root/beta"
137
+ * const parentResult = Path.resolveRelative(["alpha", "..", "..", "beta"]);
138
+ * // parentResult: "../beta"
139
+ * ```
140
+ *
141
+ * @see https://utils.duplojs.dev/en/v1/api/common/path/resolveRelative
142
+ *
143
+ */
144
+ function resolveRelative<GenericSegment extends string>(segments: AnyTuple<GenericSegment>): string;
145
+ }
@@ -0,0 +1,114 @@
1
+ import { success } from '../either/right/success.mjs';
2
+ import { fail } from '../either/left/fail.mjs';
3
+
4
+ var Path;
5
+ (function (Path) {
6
+ Path.baseNameRegex = /\/?([^/]+)$/;
7
+ Path.folderNameRegex = /([^]+)\/[^/]+\/?$/;
8
+ Path.extensionNameRegex = /\.([^./]+)$/;
9
+ Path.isRelativeRegex = /(^|\/)\.\.(?=\/|$)/;
10
+ Path.segmentTrailingRegex = /\/$/;
11
+ Path.segmentRelativeRegex = /^(.\/)/;
12
+ /**
13
+ * {@include common/path/getBaseName/index.md}
14
+ */
15
+ function getBaseName(path, params) {
16
+ const match = Path.baseNameRegex.exec(path);
17
+ if (!match) {
18
+ return null;
19
+ }
20
+ if (params?.removeExtension) {
21
+ return match[1].replace(Path.extensionNameRegex, "");
22
+ }
23
+ return match[1];
24
+ }
25
+ Path.getBaseName = getBaseName;
26
+ /**
27
+ * {@include common/path/getExtensionName/index.md}
28
+ */
29
+ function getExtensionName(path) {
30
+ const match = Path.extensionNameRegex.exec(path);
31
+ if (match) {
32
+ return match[1];
33
+ }
34
+ return null;
35
+ }
36
+ Path.getExtensionName = getExtensionName;
37
+ /**
38
+ * {@include common/path/getParentFolderPath/index.md}
39
+ */
40
+ function getParentFolderPath(path) {
41
+ const match = path.match(Path.folderNameRegex);
42
+ if (!match) {
43
+ return null;
44
+ }
45
+ return match[1];
46
+ }
47
+ Path.getParentFolderPath = getParentFolderPath;
48
+ /**
49
+ * {@include common/path/isAbsolute/index.md}
50
+ */
51
+ function isAbsolute(path) {
52
+ return path.startsWith("/")
53
+ && !Path.isRelativeRegex.test(path);
54
+ }
55
+ Path.isAbsolute = isAbsolute;
56
+ /**
57
+ * {@include common/path/resolveFrom/index.md}
58
+ */
59
+ function resolveFrom(origin, segments) {
60
+ const result = resolveRelative([origin, ...segments]);
61
+ return isAbsolute(result)
62
+ ? success(result)
63
+ : fail();
64
+ }
65
+ Path.resolveFrom = resolveFrom;
66
+ /**
67
+ * {@include common/path/resolveRelative/index.md}
68
+ */
69
+ function resolveRelative(segments) {
70
+ let clearedPath = "";
71
+ for (const segment of segments) {
72
+ if (segment.length === 0) {
73
+ continue;
74
+ }
75
+ if (segment === "/") {
76
+ clearedPath = segment;
77
+ continue;
78
+ }
79
+ const formattedSegment = segment
80
+ .replace(Path.segmentTrailingRegex, "")
81
+ .replace(Path.segmentRelativeRegex, "");
82
+ if (formattedSegment.startsWith("/")) {
83
+ clearedPath = formattedSegment;
84
+ continue;
85
+ }
86
+ if (clearedPath === "/") {
87
+ clearedPath += formattedSegment;
88
+ }
89
+ else {
90
+ clearedPath += `/${formattedSegment}`;
91
+ }
92
+ }
93
+ const dotResult = [];
94
+ const result = [];
95
+ for (const element of clearedPath.split("/")) {
96
+ if (element === "..") {
97
+ const deletedElement = result.pop();
98
+ if (!deletedElement) {
99
+ dotResult.push(element);
100
+ }
101
+ }
102
+ else {
103
+ result.push(element);
104
+ }
105
+ }
106
+ if (dotResult.length === 0) {
107
+ return result.join("/");
108
+ }
109
+ return `${dotResult.join("/")}/${result.join("/")}`;
110
+ }
111
+ Path.resolveRelative = resolveRelative;
112
+ })(Path || (Path = {}));
113
+
114
+ export { Path };
package/dist/index.cjs CHANGED
@@ -12,7 +12,6 @@ var index$8 = require('./dataParser/parsers/coerce/index.cjs');
12
12
  var index$9 = require('./dataParser/extended/index.cjs');
13
13
  var index$a = require('./date/index.cjs');
14
14
  var index$b = require('./clean/index.cjs');
15
- var index$c = require('./common/path/index.cjs');
16
15
  var addWrappedProperties = require('./common/addWrappedProperties.cjs');
17
16
  var asyncPipe = require('./common/asyncPipe.cjs');
18
17
  var clone = require('./common/clone.cjs');
@@ -26,6 +25,7 @@ var promiseObject = require('./common/promiseObject.cjs');
26
25
  var simpleClone = require('./common/simpleClone.cjs');
27
26
  var sleep = require('./common/sleep.cjs');
28
27
  var stringToBytes = require('./common/stringToBytes.cjs');
28
+ var mimeType = require('./common/mimeType.cjs');
29
29
  var stringToMillisecond = require('./common/stringToMillisecond.cjs');
30
30
  var toJSON = require('./common/toJSON.cjs');
31
31
  var toTransform = require('./common/toTransform.cjs');
@@ -62,6 +62,7 @@ var hasKinds = require('./common/hasKinds.cjs');
62
62
  var toCurriedPredicate = require('./common/toCurriedPredicate.cjs');
63
63
  var pipeCall = require('./common/pipeCall.cjs');
64
64
  var asserts = require('./common/asserts.cjs');
65
+ var path = require('./common/path.cjs');
65
66
 
66
67
 
67
68
 
@@ -89,7 +90,6 @@ exports.D = index$a;
89
90
  exports.DDate = index$a;
90
91
  exports.C = index$b;
91
92
  exports.DClean = index$b;
92
- exports.Path = index$c;
93
93
  exports.addWrappedProperties = addWrappedProperties.addWrappedProperties;
94
94
  exports.asyncPipe = asyncPipe.asyncPipe;
95
95
  exports.clone = clone.clone;
@@ -108,6 +108,7 @@ exports.simpleClone = simpleClone.simpleClone;
108
108
  exports.sleep = sleep.sleep;
109
109
  exports.InvalidBytesInStringError = stringToBytes.InvalidBytesInStringError;
110
110
  exports.stringToBytes = stringToBytes.stringToBytes;
111
+ exports.mimeType = mimeType.mimeType;
111
112
  exports.InvalidMillisecondInStringError = stringToMillisecond.InvalidMillisecondInStringError;
112
113
  exports.stringToMillisecond = stringToMillisecond.stringToMillisecond;
113
114
  exports.toJSON = toJSON.toJSON;
@@ -152,3 +153,7 @@ exports.toCurriedPredicate = toCurriedPredicate.toCurriedPredicate;
152
153
  exports.pipeCall = pipeCall.pipeCall;
153
154
  exports.AssertsError = asserts.AssertsError;
154
155
  exports.asserts = asserts.asserts;
156
+ Object.defineProperty(exports, "Path", {
157
+ enumerable: true,
158
+ get: function () { return path.Path; }
159
+ });
package/dist/index.mjs CHANGED
@@ -34,8 +34,6 @@ export { index$a as DDate };
34
34
  import * as index$b from './clean/index.mjs';
35
35
  export { index$b as C };
36
36
  export { index$b as DClean };
37
- import * as index$c from './common/path/index.mjs';
38
- export { index$c as Path };
39
37
  export { addWrappedProperties } from './common/addWrappedProperties.mjs';
40
38
  export { asyncPipe } from './common/asyncPipe.mjs';
41
39
  export { clone } from './common/clone.mjs';
@@ -49,6 +47,7 @@ export { promiseObject } from './common/promiseObject.mjs';
49
47
  export { simpleClone } from './common/simpleClone.mjs';
50
48
  export { sleep } from './common/sleep.mjs';
51
49
  export { InvalidBytesInStringError, stringToBytes } from './common/stringToBytes.mjs';
50
+ export { mimeType } from './common/mimeType.mjs';
52
51
  export { InvalidMillisecondInStringError, stringToMillisecond } from './common/stringToMillisecond.mjs';
53
52
  export { toJSON } from './common/toJSON.mjs';
54
53
  export { toTransform } from './common/toTransform.mjs';
@@ -85,3 +84,4 @@ export { hasKinds } from './common/hasKinds.mjs';
85
84
  export { toCurriedPredicate } from './common/toCurriedPredicate.mjs';
86
85
  export { pipeCall } from './common/pipeCall.mjs';
87
86
  export { AssertsError, asserts } from './common/asserts.mjs';
87
+ export { Path } from './common/path.mjs';
@@ -999,74 +999,6 @@
999
999
  {
1000
1000
  "name": "common",
1001
1001
  "files": [
1002
- {
1003
- "name": "path",
1004
- "files": [
1005
- {
1006
- "name": "getBaseName.cjs"
1007
- },
1008
- {
1009
- "name": "getBaseName.d.ts"
1010
- },
1011
- {
1012
- "name": "getBaseName.mjs"
1013
- },
1014
- {
1015
- "name": "getExtensionName.cjs"
1016
- },
1017
- {
1018
- "name": "getExtensionName.d.ts"
1019
- },
1020
- {
1021
- "name": "getExtensionName.mjs"
1022
- },
1023
- {
1024
- "name": "getParentFolderPath.cjs"
1025
- },
1026
- {
1027
- "name": "getParentFolderPath.d.ts"
1028
- },
1029
- {
1030
- "name": "getParentFolderPath.mjs"
1031
- },
1032
- {
1033
- "name": "index.cjs"
1034
- },
1035
- {
1036
- "name": "index.d.ts"
1037
- },
1038
- {
1039
- "name": "index.mjs"
1040
- },
1041
- {
1042
- "name": "isAbsolute.cjs"
1043
- },
1044
- {
1045
- "name": "isAbsolute.d.ts"
1046
- },
1047
- {
1048
- "name": "isAbsolute.mjs"
1049
- },
1050
- {
1051
- "name": "resolveFrom.cjs"
1052
- },
1053
- {
1054
- "name": "resolveFrom.d.ts"
1055
- },
1056
- {
1057
- "name": "resolveFrom.mjs"
1058
- },
1059
- {
1060
- "name": "resolveRelative.cjs"
1061
- },
1062
- {
1063
- "name": "resolveRelative.d.ts"
1064
- },
1065
- {
1066
- "name": "resolveRelative.mjs"
1067
- }
1068
- ]
1069
- },
1070
1002
  {
1071
1003
  "name": "types",
1072
1004
  "files": [
@@ -1483,6 +1415,15 @@
1483
1415
  {
1484
1416
  "name": "memo.mjs"
1485
1417
  },
1418
+ {
1419
+ "name": "mimeType.cjs"
1420
+ },
1421
+ {
1422
+ "name": "mimeType.d.ts"
1423
+ },
1424
+ {
1425
+ "name": "mimeType.mjs"
1426
+ },
1486
1427
  {
1487
1428
  "name": "or.cjs"
1488
1429
  },
@@ -1501,6 +1442,15 @@
1501
1442
  {
1502
1443
  "name": "override.mjs"
1503
1444
  },
1445
+ {
1446
+ "name": "path.cjs"
1447
+ },
1448
+ {
1449
+ "name": "path.d.ts"
1450
+ },
1451
+ {
1452
+ "name": "path.mjs"
1453
+ },
1504
1454
  {
1505
1455
  "name": "pipe.cjs"
1506
1456
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@duplojs/utils",
3
- "version": "1.4.47",
3
+ "version": "1.4.49",
4
4
  "author": {
5
5
  "name": "mathcovax",
6
6
  "url": "https://github.com/mathcovax"
@@ -1,25 +0,0 @@
1
- 'use strict';
2
-
3
- var split = require('../../string/split.cjs');
4
- var findLast = require('../../array/findLast.cjs');
5
- var length = require('../../string/length.cjs');
6
- var endsWith = require('../../string/endsWith.cjs');
7
- var slice = require('../../string/slice.cjs');
8
-
9
- /**
10
- * {@include common/path/getBaseName/index.md}
11
- */
12
- function getBaseName(path, params) {
13
- const segments = split.split(path, "/");
14
- const lastSegment = findLast.findLast(segments, (value) => length.length(value) > 0) ?? null;
15
- if (!lastSegment || lastSegment === "..") {
16
- return null;
17
- }
18
- if (params?.extension && endsWith.endsWith(lastSegment, params?.extension)) {
19
- const extensionLength = length.length(params.extension);
20
- return slice.slice(lastSegment, 0, -extensionLength);
21
- }
22
- return lastSegment;
23
- }
24
-
25
- exports.getBaseName = getBaseName;
@@ -1,26 +0,0 @@
1
- interface GetBaseNameParams {
2
- extension?: string;
3
- }
4
- /**
5
- * Returns the last non-empty segment of a path, optionally without an extension.
6
- *
7
- * **Supported call styles:**
8
- * - Classic: `getBaseName(path, params?)` -> returns the base name or null
9
- *
10
- * It ignores trailing slashes and returns null when the path has no segment or when the last segment is `..`.
11
- * When an extension is provided, it is removed only if it matches the end of the base name.
12
- *
13
- * ```ts
14
- * const defaultResult = Path.getBaseName("/foo/bar.txt");
15
- * // defaultResult: "bar.txt"
16
- * const withoutExtResult = Path.getBaseName("/foo/bar.txt", { extension: ".txt" });
17
- * // withoutExtResult: "bar"
18
- * const nullResult = Path.getBaseName("..");
19
- * // nullResult: null
20
- * ```
21
- *
22
- * @see https://utils.duplojs.dev/en/v1/api/common/path/getBaseName
23
- *
24
- */
25
- export declare function getBaseName<GenericPath extends string>(path: GenericPath, params?: GetBaseNameParams): string | null;
26
- export {};
@@ -1,23 +0,0 @@
1
- import { split } from '../../string/split.mjs';
2
- import { findLast } from '../../array/findLast.mjs';
3
- import { length } from '../../string/length.mjs';
4
- import { endsWith } from '../../string/endsWith.mjs';
5
- import { slice } from '../../string/slice.mjs';
6
-
7
- /**
8
- * {@include common/path/getBaseName/index.md}
9
- */
10
- function getBaseName(path, params) {
11
- const segments = split(path, "/");
12
- const lastSegment = findLast(segments, (value) => length(value) > 0) ?? null;
13
- if (!lastSegment || lastSegment === "..") {
14
- return null;
15
- }
16
- if (params?.extension && endsWith(lastSegment, params?.extension)) {
17
- const extensionLength = length(params.extension);
18
- return slice(lastSegment, 0, -extensionLength);
19
- }
20
- return lastSegment;
21
- }
22
-
23
- export { getBaseName };
@@ -1,17 +0,0 @@
1
- 'use strict';
2
-
3
- var minElements = require('../../array/minElements.cjs');
4
-
5
- const extensionNameRegex = /\.([^./]+)$/;
6
- /**
7
- * {@include common/path/getExtensionName/index.md}
8
- */
9
- function getExtensionName(path) {
10
- const match = extensionNameRegex.exec(path);
11
- if (!!match && minElements.minElements(match, 2)) {
12
- return match[1];
13
- }
14
- return null;
15
- }
16
-
17
- exports.getExtensionName = getExtensionName;
@@ -1,21 +0,0 @@
1
- /**
2
- * Returns the last extension of a path, without the leading dot.
3
- *
4
- * **Supported call styles:**
5
- * - Classic: `getExtensionName(path)` -> returns the extension or null
6
- *
7
- * It returns null when no extension is found, when the path ends with a dot, or when the path is `..`.
8
- *
9
- * ```ts
10
- * const txtResult = Path.getExtensionName("/foo/bar.txt");
11
- * // txtResult: "txt"
12
- * const tarResult = Path.getExtensionName("archive.tar.gz");
13
- * // tarResult: "gz"
14
- * const dotResult = Path.getExtensionName("file.");
15
- * // dotResult: null
16
- * ```
17
- *
18
- * @see https://utils.duplojs.dev/en/v1/api/common/path/getExtensionName
19
- *
20
- */
21
- export declare function getExtensionName<GenericPath extends string>(path: GenericPath): string | null;