@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.
- package/dist/common/index.d.ts +2 -1
- package/dist/common/mimeType.cjs +1081 -0
- package/dist/common/mimeType.d.ts +22 -0
- package/dist/common/mimeType.mjs +1079 -0
- package/dist/common/path.cjs +114 -0
- package/dist/common/path.d.ts +145 -0
- package/dist/common/path.mjs +114 -0
- package/dist/index.cjs +7 -2
- package/dist/index.mjs +2 -2
- package/dist/metadata.json +18 -68
- package/package.json +1 -1
- package/dist/common/path/getBaseName.cjs +0 -25
- package/dist/common/path/getBaseName.d.ts +0 -26
- package/dist/common/path/getBaseName.mjs +0 -23
- package/dist/common/path/getExtensionName.cjs +0 -17
- package/dist/common/path/getExtensionName.d.ts +0 -21
- package/dist/common/path/getExtensionName.mjs +0 -15
- package/dist/common/path/getParentFolderPath.cjs +0 -21
- package/dist/common/path/getParentFolderPath.d.ts +0 -23
- package/dist/common/path/getParentFolderPath.mjs +0 -19
- package/dist/common/path/index.cjs +0 -19
- package/dist/common/path/index.d.ts +0 -27
- package/dist/common/path/index.mjs +0 -10
- package/dist/common/path/isAbsolute.cjs +0 -15
- package/dist/common/path/isAbsolute.d.ts +0 -21
- package/dist/common/path/isAbsolute.mjs +0 -13
- package/dist/common/path/resolveFrom.cjs +0 -18
- package/dist/common/path/resolveFrom.d.ts +0 -27
- package/dist/common/path/resolveFrom.mjs +0 -16
- package/dist/common/path/resolveRelative.cjs +0 -51
- package/dist/common/path/resolveRelative.d.ts +0 -23
- package/dist/common/path/resolveRelative.mjs +0 -49
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { minElements } from '../../array/minElements.mjs';
|
|
2
|
-
|
|
3
|
-
const extensionNameRegex = /\.([^./]+)$/;
|
|
4
|
-
/**
|
|
5
|
-
* {@include common/path/getExtensionName/index.md}
|
|
6
|
-
*/
|
|
7
|
-
function getExtensionName(path) {
|
|
8
|
-
const match = extensionNameRegex.exec(path);
|
|
9
|
-
if (!!match && minElements(match, 2)) {
|
|
10
|
-
return match[1];
|
|
11
|
-
}
|
|
12
|
-
return null;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export { getExtensionName };
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var pipe = require('../pipe.cjs');
|
|
4
|
-
var isAbsolute = require('./isAbsolute.cjs');
|
|
5
|
-
var slice = require('../../array/slice.cjs');
|
|
6
|
-
var split = require('../../string/split.cjs');
|
|
7
|
-
var replace = require('../../string/replace.cjs');
|
|
8
|
-
var join = require('../../array/join.cjs');
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* {@include common/path/getParentFolderPath/index.md}
|
|
12
|
-
*/
|
|
13
|
-
function getParentFolderPath(path) {
|
|
14
|
-
const segments = pipe.pipe(path, replace.replace(/\/$/, ""), split.split("/"), slice.slice(0, -1));
|
|
15
|
-
return join.join(segments, "/")
|
|
16
|
-
|| (isAbsolute.isAbsolute(path)
|
|
17
|
-
? "/"
|
|
18
|
-
: ".");
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
exports.getParentFolderPath = getParentFolderPath;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Returns the parent folder path of a POSIX path.
|
|
3
|
-
*
|
|
4
|
-
* **Supported call styles:**
|
|
5
|
-
* - Classic: `getParentFolderPath(path)` -> returns the parent folder
|
|
6
|
-
*
|
|
7
|
-
* It removes a trailing slash, drops the last segment, and falls back to `/` for absolute paths or `.` for relative paths when needed.
|
|
8
|
-
*
|
|
9
|
-
* ```ts
|
|
10
|
-
* const result = Path.getParentFolderPath("/foo/bar/baz");
|
|
11
|
-
* // result: "/foo/bar"
|
|
12
|
-
* const trailingResult = Path.getParentFolderPath("/foo/bar/");
|
|
13
|
-
* // trailingResult: "/foo"
|
|
14
|
-
* const relativeResult = Path.getParentFolderPath("foo");
|
|
15
|
-
* // relativeResult: "."
|
|
16
|
-
* const absoluteResult = Path.getParentFolderPath("/foo");
|
|
17
|
-
* // absoluteResult: "/"
|
|
18
|
-
* ```
|
|
19
|
-
*
|
|
20
|
-
* @see https://utils.duplojs.dev/en/v1/api/common/path/getParentFolderPath
|
|
21
|
-
*
|
|
22
|
-
*/
|
|
23
|
-
export declare function getParentFolderPath<GenericPath extends string>(path: GenericPath): string;
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { pipe } from '../pipe.mjs';
|
|
2
|
-
import { isAbsolute } from './isAbsolute.mjs';
|
|
3
|
-
import { slice } from '../../array/slice.mjs';
|
|
4
|
-
import { split } from '../../string/split.mjs';
|
|
5
|
-
import { replace } from '../../string/replace.mjs';
|
|
6
|
-
import { join } from '../../array/join.mjs';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* {@include common/path/getParentFolderPath/index.md}
|
|
10
|
-
*/
|
|
11
|
-
function getParentFolderPath(path) {
|
|
12
|
-
const segments = pipe(path, replace(/\/$/, ""), split("/"), slice(0, -1));
|
|
13
|
-
return join(segments, "/")
|
|
14
|
-
|| (isAbsolute(path)
|
|
15
|
-
? "/"
|
|
16
|
-
: ".");
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export { getParentFolderPath };
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var isAbsolute = require('./isAbsolute.cjs');
|
|
4
|
-
var getParentFolderPath = require('./getParentFolderPath.cjs');
|
|
5
|
-
var getBaseName = require('./getBaseName.cjs');
|
|
6
|
-
var getExtensionName = require('./getExtensionName.cjs');
|
|
7
|
-
var resolveFrom = require('./resolveFrom.cjs');
|
|
8
|
-
var resolveRelative = require('./resolveRelative.cjs');
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* {@include common/path/index.md}
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
exports.isAbsolute = isAbsolute.isAbsolute;
|
|
15
|
-
exports.getParentFolderPath = getParentFolderPath.getParentFolderPath;
|
|
16
|
-
exports.getBaseName = getBaseName.getBaseName;
|
|
17
|
-
exports.getExtensionName = getExtensionName.getExtensionName;
|
|
18
|
-
exports.resolveFrom = resolveFrom.resolveFrom;
|
|
19
|
-
exports.resolveRelative = resolveRelative.resolveRelative;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Path utilities to resolve and inspect POSIX paths.
|
|
3
|
-
* They preserve input strings and return new values.
|
|
4
|
-
*
|
|
5
|
-
* **How to import:**
|
|
6
|
-
* - From the main common namespace
|
|
7
|
-
* - Via direct import for tree-shaking
|
|
8
|
-
*
|
|
9
|
-
* ```ts
|
|
10
|
-
* import { Path } from "@duplojs/utils";
|
|
11
|
-
* import * as Path from "@duplojs/utils/common/path";
|
|
12
|
-
* ```
|
|
13
|
-
*
|
|
14
|
-
* What you will find in this namespace:
|
|
15
|
-
* - checks (`Path.isAbsolute`)
|
|
16
|
-
* - resolution (`Path.resolveFrom`, `Path.resolveRelative`)
|
|
17
|
-
* - path parsing (`Path.getParentFolderPath`, `Path.getBaseName`, `Path.getExtensionName`)
|
|
18
|
-
*
|
|
19
|
-
* @see https://utils.duplojs.dev/en/v1/api/common/path
|
|
20
|
-
*
|
|
21
|
-
*/
|
|
22
|
-
export * from "./isAbsolute";
|
|
23
|
-
export * from "./getParentFolderPath";
|
|
24
|
-
export * from "./getBaseName";
|
|
25
|
-
export * from "./getExtensionName";
|
|
26
|
-
export * from "./resolveFrom";
|
|
27
|
-
export * from "./resolveRelative";
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export { isAbsolute } from './isAbsolute.mjs';
|
|
2
|
-
export { getParentFolderPath } from './getParentFolderPath.mjs';
|
|
3
|
-
export { getBaseName } from './getBaseName.mjs';
|
|
4
|
-
export { getExtensionName } from './getExtensionName.mjs';
|
|
5
|
-
export { resolveFrom } from './resolveFrom.mjs';
|
|
6
|
-
export { resolveRelative } from './resolveRelative.mjs';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* {@include common/path/index.md}
|
|
10
|
-
*/
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var startsWith = require('../../string/startsWith.cjs');
|
|
4
|
-
var test = require('../../string/test.cjs');
|
|
5
|
-
|
|
6
|
-
const isRelativeRegex = /(^|\/)\.\.(?=\/|$)/;
|
|
7
|
-
/**
|
|
8
|
-
* {@include common/path/isAbsolute/index.md}
|
|
9
|
-
*/
|
|
10
|
-
function isAbsolute(path) {
|
|
11
|
-
return startsWith.startsWith(path, "/")
|
|
12
|
-
&& !test.test(path, isRelativeRegex);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
exports.isAbsolute = isAbsolute;
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Checks whether a path is absolute (POSIX).
|
|
3
|
-
*
|
|
4
|
-
* **Supported call styles:**
|
|
5
|
-
* - Classic: `isAbsolute(path)` -> returns a boolean
|
|
6
|
-
*
|
|
7
|
-
* It returns true when the path starts with `/` and does not contain `..` segments.
|
|
8
|
-
*
|
|
9
|
-
* ```ts
|
|
10
|
-
* const absolutePath = Path.isAbsolute("/var/log");
|
|
11
|
-
* // absolutePath: true
|
|
12
|
-
* const parentTraversal = Path.isAbsolute("/var/../log");
|
|
13
|
-
* // parentTraversal: false
|
|
14
|
-
* const relativePath = Path.isAbsolute("var/log");
|
|
15
|
-
* // relativePath: false
|
|
16
|
-
* ```
|
|
17
|
-
*
|
|
18
|
-
* @see https://utils.duplojs.dev/en/v1/api/common/path/isAbsolute
|
|
19
|
-
*
|
|
20
|
-
*/
|
|
21
|
-
export declare function isAbsolute<GenericPath extends string>(path: GenericPath): boolean;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { startsWith } from '../../string/startsWith.mjs';
|
|
2
|
-
import { test } from '../../string/test.mjs';
|
|
3
|
-
|
|
4
|
-
const isRelativeRegex = /(^|\/)\.\.(?=\/|$)/;
|
|
5
|
-
/**
|
|
6
|
-
* {@include common/path/isAbsolute/index.md}
|
|
7
|
-
*/
|
|
8
|
-
function isAbsolute(path) {
|
|
9
|
-
return startsWith(path, "/")
|
|
10
|
-
&& !test(path, isRelativeRegex);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export { isAbsolute };
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var isAbsolute = require('./isAbsolute.cjs');
|
|
4
|
-
var resolveRelative = require('./resolveRelative.cjs');
|
|
5
|
-
var success = require('../../either/right/success.cjs');
|
|
6
|
-
var fail = require('../../either/left/fail.cjs');
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* {@include common/path/resolveFrom/index.md}
|
|
10
|
-
*/
|
|
11
|
-
function resolveFrom(origin, segments) {
|
|
12
|
-
const result = resolveRelative.resolveRelative([origin, ...segments]);
|
|
13
|
-
return isAbsolute.isAbsolute(result)
|
|
14
|
-
? success.success(result)
|
|
15
|
-
: fail.fail();
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
exports.resolveFrom = resolveFrom;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import * as DEither from "../../either";
|
|
2
|
-
import type { AnyTuple } from "../types";
|
|
3
|
-
/**
|
|
4
|
-
* Resolves a list of path segments from an origin and returns an Either.
|
|
5
|
-
*
|
|
6
|
-
* **Supported call styles:**
|
|
7
|
-
* - Classic: `resolveFrom(origin, segments)` -> returns an Either
|
|
8
|
-
*
|
|
9
|
-
* Segments are resolved in order using `resolveRelative`.
|
|
10
|
-
* The result is an `Either` that is `success` only when the resolved path is absolute; otherwise it returns `fail`.
|
|
11
|
-
*
|
|
12
|
-
* ```ts
|
|
13
|
-
* const absoluteResult = Path.resolveFrom("/root", ["alpha", "beta"]);
|
|
14
|
-
* // absoluteResult: DEither.success<"/root/alpha/beta">
|
|
15
|
-
* const result = unwrap(absoluteResult);
|
|
16
|
-
* // result: "/root/alpha/beta"
|
|
17
|
-
*
|
|
18
|
-
* const overrideResult = Path.resolveFrom("gamma", ["alpha", "/root", "beta"]);
|
|
19
|
-
* // overrideResult: DEither.success<"/root/beta">
|
|
20
|
-
* const relativeResult = Path.resolveFrom("alpha", ["..", ".."]);
|
|
21
|
-
* // relativeResult: DEither.fail
|
|
22
|
-
* ```
|
|
23
|
-
*
|
|
24
|
-
* @see https://utils.duplojs.dev/en/v1/api/common/path/resolveFrom
|
|
25
|
-
*
|
|
26
|
-
*/
|
|
27
|
-
export declare function resolveFrom<GenericSegment extends string>(origin: string, segments: AnyTuple<GenericSegment>): DEither.Fail | DEither.Success<string>;
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { isAbsolute } from './isAbsolute.mjs';
|
|
2
|
-
import { resolveRelative } from './resolveRelative.mjs';
|
|
3
|
-
import { success } from '../../either/right/success.mjs';
|
|
4
|
-
import { fail } from '../../either/left/fail.mjs';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* {@include common/path/resolveFrom/index.md}
|
|
8
|
-
*/
|
|
9
|
-
function resolveFrom(origin, segments) {
|
|
10
|
-
const result = resolveRelative([origin, ...segments]);
|
|
11
|
-
return isAbsolute(result)
|
|
12
|
-
? success(result)
|
|
13
|
-
: fail();
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export { resolveFrom };
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const segmentTrailingRegex = /\/$/;
|
|
4
|
-
const segmentRelativeRegex = /^(.\/)/;
|
|
5
|
-
/**
|
|
6
|
-
* {@include common/path/resolveRelative/index.md}
|
|
7
|
-
*/
|
|
8
|
-
function resolveRelative(segments) {
|
|
9
|
-
let clearedPath = "";
|
|
10
|
-
for (const segment of segments) {
|
|
11
|
-
if (segment.length === 0) {
|
|
12
|
-
continue;
|
|
13
|
-
}
|
|
14
|
-
if (segment === "/") {
|
|
15
|
-
clearedPath = segment;
|
|
16
|
-
continue;
|
|
17
|
-
}
|
|
18
|
-
const formattedSegment = segment
|
|
19
|
-
.replace(segmentTrailingRegex, "")
|
|
20
|
-
.replace(segmentRelativeRegex, "");
|
|
21
|
-
if (formattedSegment.startsWith("/")) {
|
|
22
|
-
clearedPath = formattedSegment;
|
|
23
|
-
continue;
|
|
24
|
-
}
|
|
25
|
-
if (clearedPath === "/") {
|
|
26
|
-
clearedPath += formattedSegment;
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
29
|
-
clearedPath += `/${formattedSegment}`;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
const dotResult = [];
|
|
33
|
-
const result = [];
|
|
34
|
-
for (const element of clearedPath.split("/")) {
|
|
35
|
-
if (element === "..") {
|
|
36
|
-
const deletedElement = result.pop();
|
|
37
|
-
if (!deletedElement) {
|
|
38
|
-
dotResult.push(element);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
result.push(element);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
if (dotResult.length === 0) {
|
|
46
|
-
return result.join("/");
|
|
47
|
-
}
|
|
48
|
-
return `${dotResult.join("/")}/${result.join("/")}`;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
exports.resolveRelative = resolveRelative;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import type { AnyTuple } from "../types";
|
|
2
|
-
/**
|
|
3
|
-
* Resolves path segments into a single POSIX-like path.
|
|
4
|
-
*
|
|
5
|
-
* **Supported call styles:**
|
|
6
|
-
* - Classic: `resolveRelative(segments)` -> returns the resolved path
|
|
7
|
-
*
|
|
8
|
-
* Empty segments are ignored, trailing slashes and leading `./` are removed, and absolute segments reset the base.
|
|
9
|
-
* `..` segments remove previous segments and may remain leading when resolving above root.
|
|
10
|
-
*
|
|
11
|
-
* ```ts
|
|
12
|
-
* const basicResult = Path.resolveRelative(["alpha", "beta"]);
|
|
13
|
-
* // basicResult: "/alpha/beta"
|
|
14
|
-
* const overrideResult = Path.resolveRelative(["alpha", "/root", "beta"]);
|
|
15
|
-
* // overrideResult: "/root/beta"
|
|
16
|
-
* const parentResult = Path.resolveRelative(["alpha", "..", "..", "beta"]);
|
|
17
|
-
* // parentResult: "../beta"
|
|
18
|
-
* ```
|
|
19
|
-
*
|
|
20
|
-
* @see https://utils.duplojs.dev/en/v1/api/common/path/resolveRelative
|
|
21
|
-
*
|
|
22
|
-
*/
|
|
23
|
-
export declare function resolveRelative<GenericSegment extends string>(segments: AnyTuple<GenericSegment>): string;
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
const segmentTrailingRegex = /\/$/;
|
|
2
|
-
const segmentRelativeRegex = /^(.\/)/;
|
|
3
|
-
/**
|
|
4
|
-
* {@include common/path/resolveRelative/index.md}
|
|
5
|
-
*/
|
|
6
|
-
function resolveRelative(segments) {
|
|
7
|
-
let clearedPath = "";
|
|
8
|
-
for (const segment of segments) {
|
|
9
|
-
if (segment.length === 0) {
|
|
10
|
-
continue;
|
|
11
|
-
}
|
|
12
|
-
if (segment === "/") {
|
|
13
|
-
clearedPath = segment;
|
|
14
|
-
continue;
|
|
15
|
-
}
|
|
16
|
-
const formattedSegment = segment
|
|
17
|
-
.replace(segmentTrailingRegex, "")
|
|
18
|
-
.replace(segmentRelativeRegex, "");
|
|
19
|
-
if (formattedSegment.startsWith("/")) {
|
|
20
|
-
clearedPath = formattedSegment;
|
|
21
|
-
continue;
|
|
22
|
-
}
|
|
23
|
-
if (clearedPath === "/") {
|
|
24
|
-
clearedPath += formattedSegment;
|
|
25
|
-
}
|
|
26
|
-
else {
|
|
27
|
-
clearedPath += `/${formattedSegment}`;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
const dotResult = [];
|
|
31
|
-
const result = [];
|
|
32
|
-
for (const element of clearedPath.split("/")) {
|
|
33
|
-
if (element === "..") {
|
|
34
|
-
const deletedElement = result.pop();
|
|
35
|
-
if (!deletedElement) {
|
|
36
|
-
dotResult.push(element);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
result.push(element);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
if (dotResult.length === 0) {
|
|
44
|
-
return result.join("/");
|
|
45
|
-
}
|
|
46
|
-
return `${dotResult.join("/")}/${result.join("/")}`;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export { resolveRelative };
|