@zokugun/fs-path 0.1.0 → 0.2.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 (50) hide show
  1. package/README.md +11 -16
  2. package/lib/cjs/absolute.d.cts +2 -2
  3. package/lib/cjs/index.cjs +10 -8
  4. package/lib/cjs/index.d.cts +8 -7
  5. package/lib/cjs/is-in-dir.d.cts +2 -2
  6. package/lib/cjs/is-in-dirs.d.cts +2 -2
  7. package/lib/cjs/is-safe-path.d.cts +2 -2
  8. package/lib/cjs/is-safe-segment.d.cts +2 -2
  9. package/lib/cjs/join.d.cts +2 -2
  10. package/lib/cjs/leaf-ext.cjs +31 -0
  11. package/lib/cjs/leaf-ext.d.cts +3 -0
  12. package/lib/cjs/leaf-name.cjs +33 -0
  13. package/lib/cjs/leaf-name.d.cts +3 -0
  14. package/lib/cjs/parent-name.cjs +14 -0
  15. package/lib/cjs/parent-name.d.cts +3 -0
  16. package/lib/cjs/{parent.cjs → parent-path.cjs} +4 -4
  17. package/lib/cjs/parent-path.d.cts +3 -0
  18. package/lib/cjs/platform.cjs +10 -7
  19. package/lib/cjs/resolve.d.cts +2 -2
  20. package/lib/cjs/sanitize.d.cts +2 -2
  21. package/lib/cjs/types.d.cts +6 -6
  22. package/lib/cjs/untildify.d.cts +2 -2
  23. package/lib/esm/absolute.d.mts +2 -2
  24. package/lib/esm/index.d.mts +8 -7
  25. package/lib/esm/index.mjs +6 -5
  26. package/lib/esm/is-in-dir.d.mts +2 -2
  27. package/lib/esm/is-in-dirs.d.mts +2 -2
  28. package/lib/esm/is-safe-path.d.mts +2 -2
  29. package/lib/esm/is-safe-segment.d.mts +2 -2
  30. package/lib/esm/join.d.mts +2 -2
  31. package/lib/esm/leaf-ext.d.mts +3 -0
  32. package/lib/esm/leaf-ext.mjs +28 -0
  33. package/lib/esm/leaf-name.d.mts +3 -0
  34. package/lib/esm/leaf-name.mjs +30 -0
  35. package/lib/esm/parent-name.d.mts +3 -0
  36. package/lib/esm/parent-name.mjs +11 -0
  37. package/lib/esm/parent-path.d.mts +3 -0
  38. package/lib/esm/{parent.mjs → parent-path.mjs} +3 -3
  39. package/lib/esm/platform.mjs +14 -11
  40. package/lib/esm/resolve.d.mts +2 -2
  41. package/lib/esm/sanitize.d.mts +2 -2
  42. package/lib/esm/types.d.mts +6 -6
  43. package/lib/esm/untildify.d.mts +2 -2
  44. package/package.json +1 -1
  45. package/lib/cjs/dir.cjs +0 -6
  46. package/lib/cjs/dir.d.cts +0 -3
  47. package/lib/cjs/parent.d.cts +0 -3
  48. package/lib/esm/dir.d.mts +0 -3
  49. package/lib/esm/dir.mjs +0 -3
  50. package/lib/esm/parent.d.mts +0 -3
package/README.md CHANGED
@@ -44,19 +44,19 @@ This example expands the current user's home directory, joins paths safely, and
44
44
  API reference
45
45
  -------------
46
46
 
47
- - `absolute(...paths: string[]): string`: Return an absolute path for the given segments. Uses the current platform resolution behavior.
48
- - `dir(path: string): string`: Return the directory portion of `path` (empty string when the result is `.`).
49
- - `extension(path: string): string`: Return the file extension (same as `path.extname`).
47
+ - `absolute(...paths: string[]): string`: Returns a normalized absolute path.
50
48
  - `isAbsolute(path: string): boolean`: Return `true` when `path` is absolute for the current platform.
51
49
  - `isInDir(path: string, parent: string): boolean`: Return `true` when `path` is equal to or contained within `parent`.
52
50
  - `isInDirs(path: string, parents: string[]): boolean`: Return `true` when `path` is equal to or contained within in one of the `parents`.
53
51
  - `isSafePath(path: string, options?): boolean`: Return `true` when `path` is considered safe for use (guards against suspicious segments such as `..` when appropriate for the platform).
54
52
  - `isSafeSegment(segment: string, target?: Target | 'auto'): boolean`: Return `true` when a single path segment is safe (doesn't contain path separators or traversal sequences).
55
- - `join(...paths: string[]): string`: Join path segments; returns empty string when the join result is `.`.
56
- - `leaf(path: string, suffix?: string): string`: Return the basename of `path` (alias of `path.basename`).
53
+ - `join(...paths: string[]): string`: Join path segments.
54
+ - `leafExt(path: string, length?: number): string`: Return the extensions.
55
+ - `leafName(path: string, extension?: number | string): string`: Return the basename of `path` with/without the extensions.
57
56
  - `matchesGlob?(path: string, pattern: string): boolean`: Optional platform-provided glob matcher when available.
58
57
  - `normalize(path: string): string`: Normalize the path (same as `path.normalize`).
59
- - `parent(path: string): string`: Return the parent directory of `file` (empty string when the result is `.`).
58
+ - `parentName(path: string): string`: Return the parent name of `path`.
59
+ - `parentPath(path: string): string`: Return the parent directory of `path`.
60
60
  - `relative(from: string, to: string): string`: Compute the relative path from `from` to `to`.
61
61
  - `resolve(...paths: string[]): string`: Resolve path segments to an absolute path. Supports tilde expansion:
62
62
  - `~` expands to the current user's home directory.
@@ -73,8 +73,6 @@ export type PlatformPath = {
73
73
  readonly posix: PlatformPath;
74
74
  readonly win32: PlatformPath;
75
75
  absolute: (...paths: string[]) => string;
76
- dir: (path: string) => string;
77
- extension: (path: string) => string;
78
76
  isAbsolute: (path: string) => boolean;
79
77
  isInDir(path: string, parent: string): boolean;
80
78
  isInDirs(path: string, parents: string[]): boolean;
@@ -85,10 +83,12 @@ export type PlatformPath = {
85
83
  }) => boolean;
86
84
  isSafeSegment: (segment: string, target?: Target | 'auto') => boolean;
87
85
  join: (...paths: string[]) => string;
88
- leaf: (path: string, suffix?: string) => string;
86
+ leafExt: (path: string, length?: number) => string;
87
+ leafName: (path: string, extension?: number | string) => string;
89
88
  matchesGlob?: (path: string, pattern: string) => boolean;
90
89
  normalize: (path: string) => string;
91
- parent: (path: string) => string;
90
+ parentName: (path: string) => string;
91
+ parentPath: (path: string) => string;
92
92
  relative: (from: string, to: string) => string;
93
93
  resolve: (...paths: string[]) => string;
94
94
  sanitize(path: string, options?: {
@@ -106,15 +106,10 @@ Comparison with Node's `path`
106
106
 
107
107
  This library intentionally mirrors many of Node's `path` APIs but provides small ergonomic differences and extra features where useful:
108
108
 
109
- - `absolute(...paths)`: resolves using the runtime platform and always returns a normalized absolute path.
110
- - `dir(path)`: returns `path.parse(path).dir` (similar to `path.dirname`, kept for API symmetry).
111
- - `extension(path)`: alias of `path.extname`.
112
109
  - `join(...paths)`: wraps `path.join`. Returns `''` instead of `.` when the joined result is the current directory.
113
110
  - `isAbsolute(path)`, `normalize(path)`, `relative(from, to)`: direct mappings to `path.isAbsolute`, `path.normalize`, and `path.relative` respectively.
114
- - `leaf(path, suffix?)`: alias of `path.basename`.
115
- - `parent(file)`: wraps `path.dirname`. Returns `''` instead of `.` for consistency with `join`.
111
+ - `parentPath(file)`: wraps `path.dirname`. Returns `''` instead of `.` for consistency with `join`.
116
112
  - `resolve(...paths)`: wraps `path.resolve` but adds tilde expansion (`~` and `~username`) before resolving.
117
- - `untildify(path)`: purely handles `~` expansion; no direct equivalent in Node core.
118
113
  - `delimiter`: same value as `path.delimiter` on the current platform.
119
114
  - `separator`: same value as `path.sep` on the current platform.
120
115
 
@@ -1,3 +1,3 @@
1
- import type path from 'node:path';
1
+ import type opath from 'node:path';
2
2
  import { type PlatformPath } from './types.cjs';
3
- export declare function absolute(nodePath: path.PlatformPath, localPath: PlatformPath): (...paths: string[]) => string;
3
+ export declare function absolute(nodePath: opath.PlatformPath, localPath: PlatformPath): (...paths: string[]) => string;
package/lib/cjs/index.cjs CHANGED
@@ -1,17 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.win32 = exports.untildify = exports.separator = exports.sanitize = exports.resolve = exports.relative = exports.posix = exports.isSafeSegment = exports.isSafePath = exports.isInDirs = exports.isInDir = exports.parent = exports.normalize = exports.matchesGlob = exports.leaf = exports.join = exports.isAbsolute = exports.extension = exports.delimiter = exports.absolute = void 0;
3
+ exports.win32 = exports.untildify = exports.separator = exports.sanitize = exports.resolve = exports.relative = exports.posix = exports.isSafeSegment = exports.isSafePath = exports.isInDirs = exports.isInDir = exports.parentPath = exports.parentName = exports.normalize = exports.matchesGlob = exports.leafName = exports.leafExt = exports.join = exports.isAbsolute = exports.delimiter = exports.absolute = void 0;
4
4
  const platform_js_1 = require("./platform.cjs");
5
- const { absolute, delimiter, extension, isAbsolute, join, leaf, matchesGlob, normalize, parent, isInDir, isInDirs, isSafePath, isSafeSegment, posix, relative, resolve, sanitize, separator, untildify, win32, } = platform_js_1.DEFAULT_PLATFORM;
5
+ const { absolute, delimiter, isAbsolute, join, leafExt, leafName, matchesGlob, normalize, parentName, parentPath, isInDir, isInDirs, isSafePath, isSafeSegment, posix, relative, resolve, sanitize, separator, untildify, win32, } = platform_js_1.DEFAULT_PLATFORM;
6
6
  exports.absolute = absolute;
7
7
  exports.delimiter = delimiter;
8
- exports.extension = extension;
9
8
  exports.isAbsolute = isAbsolute;
10
9
  exports.join = join;
11
- exports.leaf = leaf;
10
+ exports.leafExt = leafExt;
11
+ exports.leafName = leafName;
12
12
  exports.matchesGlob = matchesGlob;
13
13
  exports.normalize = normalize;
14
- exports.parent = parent;
14
+ exports.parentName = parentName;
15
+ exports.parentPath = parentPath;
15
16
  exports.isInDir = isInDir;
16
17
  exports.isInDirs = isInDirs;
17
18
  exports.isSafePath = isSafePath;
@@ -29,13 +30,14 @@ exports.default = {
29
30
  PlatformPath: undefined,
30
31
  absolute,
31
32
  delimiter,
32
- extension,
33
33
  isAbsolute,
34
34
  join,
35
- leaf,
35
+ leafExt,
36
+ leafName,
36
37
  matchesGlob,
37
38
  normalize,
38
- parent,
39
+ parentName,
40
+ parentPath,
39
41
  isInDir,
40
42
  isInDirs,
41
43
  isSafePath,
@@ -1,5 +1,5 @@
1
1
  import { type PlatformPath } from './types.cjs';
2
- declare const absolute: (...paths: string[]) => string, delimiter: ";" | ":", extension: (path: string) => string, isAbsolute: (path: string) => boolean, join: (...paths: string[]) => string, leaf: (path: string, suffix?: string) => string, matchesGlob: ((path: string, pattern: string) => boolean) | undefined, normalize: (path: string) => string, parent: (file: string) => string, isInDir: (file: string, dir: string) => boolean, isInDirs: (file: string, directories: string[]) => boolean, isSafePath: (path: string, options?: {
2
+ declare const absolute: (...paths: string[]) => string, delimiter: ";" | ":", isAbsolute: (path: string) => boolean, join: (...paths: string[]) => string, leafExt: (path: string, length?: number) => string, leafName: (path: string, extension?: number | string) => string, matchesGlob: ((path: string, pattern: string) => boolean) | undefined, normalize: (path: string) => string, parentName: (path: string) => string, parentPath: (path: string) => string, isInDir: (path: string, dir: string) => boolean, isInDirs: (path: string, directories: string[]) => boolean, isSafePath: (path: string, options?: {
3
3
  parent?: string | null;
4
4
  parents?: string | string[] | null;
5
5
  target?: Target | "auto";
@@ -9,22 +9,23 @@ declare const absolute: (...paths: string[]) => string, delimiter: ";" | ":", ex
9
9
  replacement?: string | null;
10
10
  target?: Target | "auto";
11
11
  }) => DResult<string>, separator: "\\" | "/", untildify: (path: string) => string, win32: PlatformPath;
12
- export { type PlatformPath, absolute, delimiter, extension, isAbsolute, join, leaf, matchesGlob, normalize, parent, isInDir, isInDirs, isSafePath, isSafeSegment, posix, relative, resolve, sanitize, separator, untildify, win32, };
12
+ export { type PlatformPath, absolute, delimiter, isAbsolute, join, leafExt, leafName, matchesGlob, normalize, parentName, parentPath, isInDir, isInDirs, isSafePath, isSafeSegment, posix, relative, resolve, sanitize, separator, untildify, win32, };
13
13
  import { type Target } from "@zokugun/fs-path-sanitize";
14
14
  import { type DResult } from "@zokugun/xtry/result";
15
15
  declare const _default: {
16
16
  PlatformPath: PlatformPath;
17
17
  absolute: (...paths: string[]) => string;
18
18
  delimiter: ";" | ":";
19
- extension: (path: string) => string;
20
19
  isAbsolute: (path: string) => boolean;
21
20
  join: (...paths: string[]) => string;
22
- leaf: (path: string, suffix?: string) => string;
21
+ leafExt: (path: string, length?: number) => string;
22
+ leafName: (path: string, extension?: number | string) => string;
23
23
  matchesGlob: ((path: string, pattern: string) => boolean) | undefined;
24
24
  normalize: (path: string) => string;
25
- parent: (file: string) => string;
26
- isInDir: (file: string, dir: string) => boolean;
27
- isInDirs: (file: string, directories: string[]) => boolean;
25
+ parentName: (path: string) => string;
26
+ parentPath: (path: string) => string;
27
+ isInDir: (path: string, dir: string) => boolean;
28
+ isInDirs: (path: string, directories: string[]) => boolean;
28
29
  isSafePath: (path: string, options?: {
29
30
  parent?: string | null;
30
31
  parents?: string | string[] | null;
@@ -1,3 +1,3 @@
1
- import type path from 'node:path';
1
+ import type opath from 'node:path';
2
2
  import { type PlatformPath } from './types.cjs';
3
- export declare function isInDir(_nodePath: path.PlatformPath, localPath: PlatformPath): (path: string, parent: string) => boolean;
3
+ export declare function isInDir(_nodePath: opath.PlatformPath, localPath: PlatformPath): (path: string, parent: string) => boolean;
@@ -1,3 +1,3 @@
1
- import type path from 'node:path';
1
+ import type opath from 'node:path';
2
2
  import { type PlatformPath } from './types.cjs';
3
- export declare function isInDirs(_nodePath: path.PlatformPath, localPath: PlatformPath): (path: string, parents: string[]) => boolean;
3
+ export declare function isInDirs(_nodePath: opath.PlatformPath, localPath: PlatformPath): (path: string, parents: string[]) => boolean;
@@ -1,7 +1,7 @@
1
- import type path from 'node:path';
1
+ import type opath from 'node:path';
2
2
  import { type Target } from '@zokugun/fs-path-sanitize';
3
3
  import { type PlatformPath } from './types.cjs';
4
- export declare function isSafePath(_nodePath: path.PlatformPath, localPath: PlatformPath): (path: string, options?: {
4
+ export declare function isSafePath(_nodePath: opath.PlatformPath, localPath: PlatformPath): (path: string, options?: {
5
5
  parent?: string | null;
6
6
  parents?: string | string[] | null;
7
7
  target?: Target | 'auto';
@@ -1,4 +1,4 @@
1
- import type path from 'node:path';
1
+ import type opath from 'node:path';
2
2
  import { type Target } from '@zokugun/fs-path-sanitize';
3
3
  import { type PlatformPath } from './types.cjs';
4
- export declare function isSafeSegment(_nodePath: path.PlatformPath, localPath: PlatformPath): (segment: string, target?: Target | 'auto') => boolean;
4
+ export declare function isSafeSegment(_nodePath: opath.PlatformPath, localPath: PlatformPath): (segment: string, target?: Target | 'auto') => boolean;
@@ -1,3 +1,3 @@
1
- import type path from 'path';
1
+ import type opath from 'path';
2
2
  import { type PlatformPath } from './types.cjs';
3
- export declare function join(nodePath: path.PlatformPath, _localPath: PlatformPath): (...paths: string[]) => string;
3
+ export declare function join(nodePath: opath.PlatformPath, _localPath: PlatformPath): (...paths: string[]) => string;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.leafExt = leafExt;
4
+ const is_it_type_1 = require("@zokugun/is-it-type");
5
+ // eslint-disable-next-line unicorn/prevent-abbreviations
6
+ function leafExt(nodePath, _localPath) {
7
+ return (path, length) => {
8
+ if ((0, is_it_type_1.isUndefined)(length)) {
9
+ return nodePath.extname(path);
10
+ }
11
+ const name = nodePath.basename(path);
12
+ if (name.length === 0 || name.startsWith('.')) {
13
+ return '';
14
+ }
15
+ if (length <= 0) {
16
+ const index = name.indexOf('.');
17
+ return index === -1 ? '' : name.slice(index);
18
+ }
19
+ else if (length === 1) {
20
+ const index = name.lastIndexOf('.');
21
+ return index === -1 ? '' : name.slice(index);
22
+ }
23
+ else {
24
+ const parts = name.split('.');
25
+ if (parts.length === 1) {
26
+ return '';
27
+ }
28
+ return '.' + parts.slice(parts.length <= length ? 1 : -length).join('.');
29
+ }
30
+ };
31
+ }
@@ -0,0 +1,3 @@
1
+ import type path from 'node:path';
2
+ import { type PlatformPath } from './types.cjs';
3
+ export declare function leafExt(nodePath: path.PlatformPath, _localPath: PlatformPath): (path: string, length?: number) => string;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.leafName = leafName;
4
+ const is_it_type_1 = require("@zokugun/is-it-type");
5
+ function leafName(nodePath, _localPath) {
6
+ return (path, extension) => {
7
+ if ((0, is_it_type_1.isUndefined)(extension)) {
8
+ return nodePath.basename(path);
9
+ }
10
+ if ((0, is_it_type_1.isString)(extension)) {
11
+ return nodePath.basename(path, extension);
12
+ }
13
+ const name = nodePath.basename(path);
14
+ if (name.length === 0 || name.startsWith('.')) {
15
+ return name;
16
+ }
17
+ if (extension <= 0) {
18
+ const index = name.indexOf('.');
19
+ return index === -1 ? name : name.slice(0, index);
20
+ }
21
+ else if (extension === 1) {
22
+ const index = name.lastIndexOf('.');
23
+ return index === -1 ? name : name.slice(0, index);
24
+ }
25
+ else {
26
+ const parts = name.split('.');
27
+ if (parts.length <= extension) {
28
+ return parts[0];
29
+ }
30
+ return parts.slice(0, -extension).join('.');
31
+ }
32
+ };
33
+ }
@@ -0,0 +1,3 @@
1
+ import type path from 'node:path';
2
+ import { type PlatformPath } from './types.cjs';
3
+ export declare function leafName(nodePath: path.PlatformPath, _localPath: PlatformPath): (path: string, extension?: number | string) => string;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parentName = parentName;
4
+ function parentName(nodePath, _localPath) {
5
+ return (path) => {
6
+ const parent = nodePath.dirname(path);
7
+ if (parent === '.') {
8
+ return '';
9
+ }
10
+ else {
11
+ return nodePath.basename(parent);
12
+ }
13
+ };
14
+ }
@@ -0,0 +1,3 @@
1
+ import type path from 'node:path';
2
+ import { type PlatformPath } from './types.cjs';
3
+ export declare function parentName(nodePath: path.PlatformPath, _localPath: PlatformPath): (path: string) => string;
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parent = parent;
4
- function parent(nodePath, _localPath) {
5
- return (file) => {
6
- const parent = nodePath.dirname(file);
3
+ exports.parentPath = parentPath;
4
+ function parentPath(nodePath, _localPath) {
5
+ return (path) => {
6
+ const parent = nodePath.dirname(path);
7
7
  if (parent === '.') {
8
8
  return '';
9
9
  }
@@ -0,0 +1,3 @@
1
+ import type opath from 'node:path';
2
+ import { type PlatformPath } from './types.cjs';
3
+ export declare function parentPath(nodePath: opath.PlatformPath, _localPath: PlatformPath): (path: string) => string;
@@ -6,37 +6,40 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.DEFAULT_PLATFORM = void 0;
7
7
  const node_path_1 = __importDefault(require("node:path"));
8
8
  const absolute_js_1 = require("./absolute.cjs");
9
- const dir_js_1 = require("./dir.cjs");
10
9
  const is_in_dir_js_1 = require("./is-in-dir.cjs");
11
10
  // eslint-disable-next-line unicorn/prevent-abbreviations
12
11
  const is_in_dirs_js_1 = require("./is-in-dirs.cjs");
13
12
  const is_safe_path_js_1 = require("./is-safe-path.cjs");
14
13
  const is_safe_segment_js_1 = require("./is-safe-segment.cjs");
15
14
  const join_js_1 = require("./join.cjs");
16
- const parent_js_1 = require("./parent.cjs");
15
+ // eslint-disable-next-line unicorn/prevent-abbreviations
16
+ const leaf_ext_js_1 = require("./leaf-ext.cjs");
17
+ const leaf_name_js_1 = require("./leaf-name.cjs");
18
+ const parent_name_js_1 = require("./parent-name.cjs");
19
+ const parent_path_js_1 = require("./parent-path.cjs");
17
20
  const resolve_js_1 = require("./resolve.cjs");
18
21
  const sanitize_js_1 = require("./sanitize.cjs");
19
22
  const untildify_js_1 = require("./untildify.cjs");
20
23
  function buildPlatform(path) {
21
- const { basename: leaf, delimiter, extname: extension, isAbsolute, normalize, relative, sep: separator, } = path;
24
+ const { delimiter, isAbsolute, normalize, relative, sep: separator, } = path;
22
25
  const result = {
23
26
  delimiter,
24
27
  separator,
25
- extension,
26
28
  isAbsolute,
27
- leaf,
28
29
  matchesGlob: path.matchesGlob ?? undefined,
29
30
  normalize,
30
31
  relative,
31
32
  };
32
33
  result.absolute = (0, absolute_js_1.absolute)(path, result);
33
- result.dir = (0, dir_js_1.dir)(path, result);
34
34
  result.isInDir = (0, is_in_dir_js_1.isInDir)(path, result);
35
35
  result.isInDirs = (0, is_in_dirs_js_1.isInDirs)(path, result);
36
36
  result.isSafePath = (0, is_safe_path_js_1.isSafePath)(path, result);
37
37
  result.isSafeSegment = (0, is_safe_segment_js_1.isSafeSegment)(path, result);
38
38
  result.join = (0, join_js_1.join)(path, result);
39
- result.parent = (0, parent_js_1.parent)(path, result);
39
+ result.leafExt = (0, leaf_ext_js_1.leafExt)(path, result);
40
+ result.leafName = (0, leaf_name_js_1.leafName)(path, result);
41
+ result.parentName = (0, parent_name_js_1.parentName)(path, result);
42
+ result.parentPath = (0, parent_path_js_1.parentPath)(path, result);
40
43
  result.resolve = (0, resolve_js_1.resolve)(path, result);
41
44
  result.sanitize = (0, sanitize_js_1.sanitize)(path, result);
42
45
  result.untildify = (0, untildify_js_1.untildify)(path, result);
@@ -1,3 +1,3 @@
1
- import type path from 'node:path';
1
+ import type opath from 'node:path';
2
2
  import { type PlatformPath } from './types.cjs';
3
- export declare function resolve(nodePath: path.PlatformPath, _localPath: PlatformPath): (...paths: string[]) => string;
3
+ export declare function resolve(nodePath: opath.PlatformPath, _localPath: PlatformPath): (...paths: string[]) => string;
@@ -1,8 +1,8 @@
1
- import type path from 'node:path';
1
+ import type opath from 'node:path';
2
2
  import { type Target } from '@zokugun/fs-path-sanitize';
3
3
  import { type DResult } from '@zokugun/xtry';
4
4
  import { type PlatformPath } from './types.cjs';
5
- export declare function sanitize(_nodePath: path.PlatformPath, localPath: PlatformPath): (path: string, options?: {
5
+ export declare function sanitize(_nodePath: opath.PlatformPath, localPath: PlatformPath): (path: string, options?: {
6
6
  parent?: string | null;
7
7
  parents?: string | string[] | null;
8
8
  replacement?: string | null;
@@ -6,11 +6,9 @@ export type PlatformPath = {
6
6
  readonly posix: PlatformPath;
7
7
  readonly win32: PlatformPath;
8
8
  absolute: (...paths: string[]) => string;
9
- dir: (path: string) => string;
10
- extension: (path: string) => string;
11
9
  isAbsolute: (path: string) => boolean;
12
- isInDir: (file: string, dir: string) => boolean;
13
- isInDirs: (file: string, directories: string[]) => boolean;
10
+ isInDir: (path: string, dir: string) => boolean;
11
+ isInDirs: (path: string, directories: string[]) => boolean;
14
12
  isSafePath: (path: string, options?: {
15
13
  parent?: string | null;
16
14
  parents?: string | string[] | null;
@@ -18,10 +16,12 @@ export type PlatformPath = {
18
16
  }) => boolean;
19
17
  isSafeSegment: (segment: string, target?: Target | 'auto') => boolean;
20
18
  join: (...paths: string[]) => string;
21
- leaf: (path: string, suffix?: string) => string;
19
+ leafExt: (path: string, length?: number) => string;
20
+ leafName: (path: string, extension?: number | string) => string;
22
21
  matchesGlob?: (path: string, pattern: string) => boolean;
23
22
  normalize: (path: string) => string;
24
- parent: (file: string) => string;
23
+ parentName: (path: string) => string;
24
+ parentPath: (path: string) => string;
25
25
  relative: (from: string, to: string) => string;
26
26
  resolve: (...paths: string[]) => string;
27
27
  sanitize: (path: string, options?: {
@@ -1,3 +1,3 @@
1
- import type path from 'node:path';
1
+ import type opath from 'node:path';
2
2
  import { type PlatformPath } from './types.cjs';
3
- export declare function untildify(_nodePath: path.PlatformPath, _localPath: PlatformPath): (path: string) => string;
3
+ export declare function untildify(_nodePath: opath.PlatformPath, _localPath: PlatformPath): (path: string) => string;
@@ -1,3 +1,3 @@
1
- import type path from 'node:path';
1
+ import type opath from 'node:path';
2
2
  import { type PlatformPath } from './types.mjs';
3
- export declare function absolute(nodePath: path.PlatformPath, localPath: PlatformPath): (...paths: string[]) => string;
3
+ export declare function absolute(nodePath: opath.PlatformPath, localPath: PlatformPath): (...paths: string[]) => string;
@@ -1,5 +1,5 @@
1
1
  import { type PlatformPath } from './types.mjs';
2
- declare const absolute: (...paths: string[]) => string, delimiter: ";" | ":", extension: (path: string) => string, isAbsolute: (path: string) => boolean, join: (...paths: string[]) => string, leaf: (path: string, suffix?: string) => string, matchesGlob: ((path: string, pattern: string) => boolean) | undefined, normalize: (path: string) => string, parent: (file: string) => string, isInDir: (file: string, dir: string) => boolean, isInDirs: (file: string, directories: string[]) => boolean, isSafePath: (path: string, options?: {
2
+ declare const absolute: (...paths: string[]) => string, delimiter: ";" | ":", isAbsolute: (path: string) => boolean, join: (...paths: string[]) => string, leafExt: (path: string, length?: number) => string, leafName: (path: string, extension?: number | string) => string, matchesGlob: ((path: string, pattern: string) => boolean) | undefined, normalize: (path: string) => string, parentName: (path: string) => string, parentPath: (path: string) => string, isInDir: (path: string, dir: string) => boolean, isInDirs: (path: string, directories: string[]) => boolean, isSafePath: (path: string, options?: {
3
3
  parent?: string | null;
4
4
  parents?: string | string[] | null;
5
5
  target?: Target | "auto";
@@ -9,22 +9,23 @@ declare const absolute: (...paths: string[]) => string, delimiter: ";" | ":", ex
9
9
  replacement?: string | null;
10
10
  target?: Target | "auto";
11
11
  }) => DResult<string>, separator: "\\" | "/", untildify: (path: string) => string, win32: PlatformPath;
12
- export { type PlatformPath, absolute, delimiter, extension, isAbsolute, join, leaf, matchesGlob, normalize, parent, isInDir, isInDirs, isSafePath, isSafeSegment, posix, relative, resolve, sanitize, separator, untildify, win32, };
12
+ export { type PlatformPath, absolute, delimiter, isAbsolute, join, leafExt, leafName, matchesGlob, normalize, parentName, parentPath, isInDir, isInDirs, isSafePath, isSafeSegment, posix, relative, resolve, sanitize, separator, untildify, win32, };
13
13
  import { type Target } from "@zokugun/fs-path-sanitize";
14
14
  import { type DResult } from "@zokugun/xtry";
15
15
  declare const _default: {
16
16
  PlatformPath: PlatformPath;
17
17
  absolute: (...paths: string[]) => string;
18
18
  delimiter: ";" | ":";
19
- extension: (path: string) => string;
20
19
  isAbsolute: (path: string) => boolean;
21
20
  join: (...paths: string[]) => string;
22
- leaf: (path: string, suffix?: string) => string;
21
+ leafExt: (path: string, length?: number) => string;
22
+ leafName: (path: string, extension?: number | string) => string;
23
23
  matchesGlob: ((path: string, pattern: string) => boolean) | undefined;
24
24
  normalize: (path: string) => string;
25
- parent: (file: string) => string;
26
- isInDir: (file: string, dir: string) => boolean;
27
- isInDirs: (file: string, directories: string[]) => boolean;
25
+ parentName: (path: string) => string;
26
+ parentPath: (path: string) => string;
27
+ isInDir: (path: string, dir: string) => boolean;
28
+ isInDirs: (path: string, directories: string[]) => boolean;
28
29
  isSafePath: (path: string, options?: {
29
30
  parent?: string | null;
30
31
  parents?: string | string[] | null;
package/lib/esm/index.mjs CHANGED
@@ -1,20 +1,21 @@
1
1
  import { DEFAULT_PLATFORM } from './platform.mjs';
2
- const { absolute, delimiter, extension, isAbsolute, join, leaf, matchesGlob, normalize, parent, isInDir, isInDirs, isSafePath, isSafeSegment, posix, relative, resolve, sanitize, separator, untildify, win32, } = DEFAULT_PLATFORM;
2
+ const { absolute, delimiter, isAbsolute, join, leafExt, leafName, matchesGlob, normalize, parentName, parentPath, isInDir, isInDirs, isSafePath, isSafeSegment, posix, relative, resolve, sanitize, separator, untildify, win32, } = DEFAULT_PLATFORM;
3
3
  /* eslint-disable unicorn/prefer-export-from */
4
- export { absolute, delimiter, extension, isAbsolute, join, leaf, matchesGlob, normalize, parent, isInDir, isInDirs, isSafePath, isSafeSegment, posix, relative, resolve, sanitize, separator, untildify, win32, };
4
+ export { absolute, delimiter, isAbsolute, join, leafExt, leafName, matchesGlob, normalize, parentName, parentPath, isInDir, isInDirs, isSafePath, isSafeSegment, posix, relative, resolve, sanitize, separator, untildify, win32, };
5
5
  /* eslint-enable unicorn/prefer-export-from */
6
6
  // eslint-disable-next-line import/no-anonymous-default-export
7
7
  export default {
8
8
  PlatformPath: undefined,
9
9
  absolute,
10
10
  delimiter,
11
- extension,
12
11
  isAbsolute,
13
12
  join,
14
- leaf,
13
+ leafExt,
14
+ leafName,
15
15
  matchesGlob,
16
16
  normalize,
17
- parent,
17
+ parentName,
18
+ parentPath,
18
19
  isInDir,
19
20
  isInDirs,
20
21
  isSafePath,
@@ -1,3 +1,3 @@
1
- import type path from 'node:path';
1
+ import type opath from 'node:path';
2
2
  import { type PlatformPath } from './types.mjs';
3
- export declare function isInDir(_nodePath: path.PlatformPath, localPath: PlatformPath): (path: string, parent: string) => boolean;
3
+ export declare function isInDir(_nodePath: opath.PlatformPath, localPath: PlatformPath): (path: string, parent: string) => boolean;
@@ -1,3 +1,3 @@
1
- import type path from 'node:path';
1
+ import type opath from 'node:path';
2
2
  import { type PlatformPath } from './types.mjs';
3
- export declare function isInDirs(_nodePath: path.PlatformPath, localPath: PlatformPath): (path: string, parents: string[]) => boolean;
3
+ export declare function isInDirs(_nodePath: opath.PlatformPath, localPath: PlatformPath): (path: string, parents: string[]) => boolean;
@@ -1,7 +1,7 @@
1
- import type path from 'node:path';
1
+ import type opath from 'node:path';
2
2
  import { type Target } from '@zokugun/fs-path-sanitize';
3
3
  import { type PlatformPath } from './types.mjs';
4
- export declare function isSafePath(_nodePath: path.PlatformPath, localPath: PlatformPath): (path: string, options?: {
4
+ export declare function isSafePath(_nodePath: opath.PlatformPath, localPath: PlatformPath): (path: string, options?: {
5
5
  parent?: string | null;
6
6
  parents?: string | string[] | null;
7
7
  target?: Target | 'auto';
@@ -1,4 +1,4 @@
1
- import type path from 'node:path';
1
+ import type opath from 'node:path';
2
2
  import { type Target } from '@zokugun/fs-path-sanitize';
3
3
  import { type PlatformPath } from './types.mjs';
4
- export declare function isSafeSegment(_nodePath: path.PlatformPath, localPath: PlatformPath): (segment: string, target?: Target | 'auto') => boolean;
4
+ export declare function isSafeSegment(_nodePath: opath.PlatformPath, localPath: PlatformPath): (segment: string, target?: Target | 'auto') => boolean;
@@ -1,3 +1,3 @@
1
- import type path from 'path';
1
+ import type opath from 'path';
2
2
  import { type PlatformPath } from './types.mjs';
3
- export declare function join(nodePath: path.PlatformPath, _localPath: PlatformPath): (...paths: string[]) => string;
3
+ export declare function join(nodePath: opath.PlatformPath, _localPath: PlatformPath): (...paths: string[]) => string;
@@ -0,0 +1,3 @@
1
+ import type path from 'node:path';
2
+ import { type PlatformPath } from './types.mjs';
3
+ export declare function leafExt(nodePath: path.PlatformPath, _localPath: PlatformPath): (path: string, length?: number) => string;
@@ -0,0 +1,28 @@
1
+ import { isUndefined } from '@zokugun/is-it-type';
2
+ // eslint-disable-next-line unicorn/prevent-abbreviations
3
+ export function leafExt(nodePath, _localPath) {
4
+ return (path, length) => {
5
+ if (isUndefined(length)) {
6
+ return nodePath.extname(path);
7
+ }
8
+ const name = nodePath.basename(path);
9
+ if (name.length === 0 || name.startsWith('.')) {
10
+ return '';
11
+ }
12
+ if (length <= 0) {
13
+ const index = name.indexOf('.');
14
+ return index === -1 ? '' : name.slice(index);
15
+ }
16
+ else if (length === 1) {
17
+ const index = name.lastIndexOf('.');
18
+ return index === -1 ? '' : name.slice(index);
19
+ }
20
+ else {
21
+ const parts = name.split('.');
22
+ if (parts.length === 1) {
23
+ return '';
24
+ }
25
+ return '.' + parts.slice(parts.length <= length ? 1 : -length).join('.');
26
+ }
27
+ };
28
+ }
@@ -0,0 +1,3 @@
1
+ import type path from 'node:path';
2
+ import { type PlatformPath } from './types.mjs';
3
+ export declare function leafName(nodePath: path.PlatformPath, _localPath: PlatformPath): (path: string, extension?: number | string) => string;
@@ -0,0 +1,30 @@
1
+ import { isString, isUndefined } from '@zokugun/is-it-type';
2
+ export function leafName(nodePath, _localPath) {
3
+ return (path, extension) => {
4
+ if (isUndefined(extension)) {
5
+ return nodePath.basename(path);
6
+ }
7
+ if (isString(extension)) {
8
+ return nodePath.basename(path, extension);
9
+ }
10
+ const name = nodePath.basename(path);
11
+ if (name.length === 0 || name.startsWith('.')) {
12
+ return name;
13
+ }
14
+ if (extension <= 0) {
15
+ const index = name.indexOf('.');
16
+ return index === -1 ? name : name.slice(0, index);
17
+ }
18
+ else if (extension === 1) {
19
+ const index = name.lastIndexOf('.');
20
+ return index === -1 ? name : name.slice(0, index);
21
+ }
22
+ else {
23
+ const parts = name.split('.');
24
+ if (parts.length <= extension) {
25
+ return parts[0];
26
+ }
27
+ return parts.slice(0, -extension).join('.');
28
+ }
29
+ };
30
+ }
@@ -0,0 +1,3 @@
1
+ import type path from 'node:path';
2
+ import { type PlatformPath } from './types.mjs';
3
+ export declare function parentName(nodePath: path.PlatformPath, _localPath: PlatformPath): (path: string) => string;
@@ -0,0 +1,11 @@
1
+ export function parentName(nodePath, _localPath) {
2
+ return (path) => {
3
+ const parent = nodePath.dirname(path);
4
+ if (parent === '.') {
5
+ return '';
6
+ }
7
+ else {
8
+ return nodePath.basename(parent);
9
+ }
10
+ };
11
+ }
@@ -0,0 +1,3 @@
1
+ import type opath from 'node:path';
2
+ import { type PlatformPath } from './types.mjs';
3
+ export declare function parentPath(nodePath: opath.PlatformPath, _localPath: PlatformPath): (path: string) => string;
@@ -1,6 +1,6 @@
1
- export function parent(nodePath, _localPath) {
2
- return (file) => {
3
- const parent = nodePath.dirname(file);
1
+ export function parentPath(nodePath, _localPath) {
2
+ return (path) => {
3
+ const parent = nodePath.dirname(path);
4
4
  if (parent === '.') {
5
5
  return '';
6
6
  }
@@ -1,43 +1,46 @@
1
- import path from 'node:path';
1
+ import opath from 'node:path';
2
2
  import { absolute } from './absolute.mjs';
3
- import { dir } from './dir.mjs';
4
3
  import { isInDir } from './is-in-dir.mjs';
5
4
  // eslint-disable-next-line unicorn/prevent-abbreviations
6
5
  import { isInDirs } from './is-in-dirs.mjs';
7
6
  import { isSafePath } from './is-safe-path.mjs';
8
7
  import { isSafeSegment } from './is-safe-segment.mjs';
9
8
  import { join } from './join.mjs';
10
- import { parent } from './parent.mjs';
9
+ // eslint-disable-next-line unicorn/prevent-abbreviations
10
+ import { leafExt } from './leaf-ext.mjs';
11
+ import { leafName } from './leaf-name.mjs';
12
+ import { parentName } from './parent-name.mjs';
13
+ import { parentPath } from './parent-path.mjs';
11
14
  import { resolve } from './resolve.mjs';
12
15
  import { sanitize } from './sanitize.mjs';
13
16
  import { untildify } from './untildify.mjs';
14
17
  function buildPlatform(path) {
15
- const { basename: leaf, delimiter, extname: extension, isAbsolute, normalize, relative, sep: separator, } = path;
18
+ const { delimiter, isAbsolute, normalize, relative, sep: separator, } = path;
16
19
  const result = {
17
20
  delimiter,
18
21
  separator,
19
- extension,
20
22
  isAbsolute,
21
- leaf,
22
23
  matchesGlob: path.matchesGlob ?? undefined,
23
24
  normalize,
24
25
  relative,
25
26
  };
26
27
  result.absolute = absolute(path, result);
27
- result.dir = dir(path, result);
28
28
  result.isInDir = isInDir(path, result);
29
29
  result.isInDirs = isInDirs(path, result);
30
30
  result.isSafePath = isSafePath(path, result);
31
31
  result.isSafeSegment = isSafeSegment(path, result);
32
32
  result.join = join(path, result);
33
- result.parent = parent(path, result);
33
+ result.leafExt = leafExt(path, result);
34
+ result.leafName = leafName(path, result);
35
+ result.parentName = parentName(path, result);
36
+ result.parentPath = parentPath(path, result);
34
37
  result.resolve = resolve(path, result);
35
38
  result.sanitize = sanitize(path, result);
36
39
  result.untildify = untildify(path, result);
37
40
  return result;
38
41
  }
39
- const POSIX_PLATFORM = buildPlatform(path.posix);
40
- const WIN32_PLATFORM = buildPlatform(path.win32);
42
+ const POSIX_PLATFORM = buildPlatform(opath.posix);
43
+ const WIN32_PLATFORM = buildPlatform(opath.win32);
41
44
  Object.defineProperties(POSIX_PLATFORM, {
42
45
  posix: {
43
46
  value: POSIX_PLATFORM,
@@ -58,4 +61,4 @@ Object.defineProperties(WIN32_PLATFORM, {
58
61
  writable: false,
59
62
  },
60
63
  });
61
- export const DEFAULT_PLATFORM = (path === path.posix ? POSIX_PLATFORM : WIN32_PLATFORM);
64
+ export const DEFAULT_PLATFORM = (opath === opath.posix ? POSIX_PLATFORM : WIN32_PLATFORM);
@@ -1,3 +1,3 @@
1
- import type path from 'node:path';
1
+ import type opath from 'node:path';
2
2
  import { type PlatformPath } from './types.mjs';
3
- export declare function resolve(nodePath: path.PlatformPath, _localPath: PlatformPath): (...paths: string[]) => string;
3
+ export declare function resolve(nodePath: opath.PlatformPath, _localPath: PlatformPath): (...paths: string[]) => string;
@@ -1,8 +1,8 @@
1
- import type path from 'node:path';
1
+ import type opath from 'node:path';
2
2
  import { type Target } from '@zokugun/fs-path-sanitize';
3
3
  import { type DResult } from '@zokugun/xtry';
4
4
  import { type PlatformPath } from './types.mjs';
5
- export declare function sanitize(_nodePath: path.PlatformPath, localPath: PlatformPath): (path: string, options?: {
5
+ export declare function sanitize(_nodePath: opath.PlatformPath, localPath: PlatformPath): (path: string, options?: {
6
6
  parent?: string | null;
7
7
  parents?: string | string[] | null;
8
8
  replacement?: string | null;
@@ -6,11 +6,9 @@ export type PlatformPath = {
6
6
  readonly posix: PlatformPath;
7
7
  readonly win32: PlatformPath;
8
8
  absolute: (...paths: string[]) => string;
9
- dir: (path: string) => string;
10
- extension: (path: string) => string;
11
9
  isAbsolute: (path: string) => boolean;
12
- isInDir: (file: string, dir: string) => boolean;
13
- isInDirs: (file: string, directories: string[]) => boolean;
10
+ isInDir: (path: string, dir: string) => boolean;
11
+ isInDirs: (path: string, directories: string[]) => boolean;
14
12
  isSafePath: (path: string, options?: {
15
13
  parent?: string | null;
16
14
  parents?: string | string[] | null;
@@ -18,10 +16,12 @@ export type PlatformPath = {
18
16
  }) => boolean;
19
17
  isSafeSegment: (segment: string, target?: Target | 'auto') => boolean;
20
18
  join: (...paths: string[]) => string;
21
- leaf: (path: string, suffix?: string) => string;
19
+ leafExt: (path: string, length?: number) => string;
20
+ leafName: (path: string, extension?: number | string) => string;
22
21
  matchesGlob?: (path: string, pattern: string) => boolean;
23
22
  normalize: (path: string) => string;
24
- parent: (file: string) => string;
23
+ parentName: (path: string) => string;
24
+ parentPath: (path: string) => string;
25
25
  relative: (from: string, to: string) => string;
26
26
  resolve: (...paths: string[]) => string;
27
27
  sanitize: (path: string, options?: {
@@ -1,3 +1,3 @@
1
- import type path from 'node:path';
1
+ import type opath from 'node:path';
2
2
  import { type PlatformPath } from './types.mjs';
3
- export declare function untildify(_nodePath: path.PlatformPath, _localPath: PlatformPath): (path: string) => string;
3
+ export declare function untildify(_nodePath: opath.PlatformPath, _localPath: PlatformPath): (path: string) => string;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@zokugun/fs-path",
3
3
  "description": "Path utilities complementing the builtin path module",
4
- "version": "0.1.0",
4
+ "version": "0.2.0",
5
5
  "author": {
6
6
  "name": "Baptiste Augrain",
7
7
  "email": "daiyam@zokugun.org"
package/lib/cjs/dir.cjs DELETED
@@ -1,6 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.dir = dir;
4
- function dir(nodePath, _localPath) {
5
- return (path) => nodePath.parse(path).dir;
6
- }
package/lib/cjs/dir.d.cts DELETED
@@ -1,3 +0,0 @@
1
- import type path from 'node:path';
2
- import { type PlatformPath } from './types.cjs';
3
- export declare function dir(nodePath: path.PlatformPath, _localPath: PlatformPath): (path: string) => string;
@@ -1,3 +0,0 @@
1
- import type path from 'node:path';
2
- import { type PlatformPath } from './types.cjs';
3
- export declare function parent(nodePath: path.PlatformPath, _localPath: PlatformPath): (file: string) => string;
package/lib/esm/dir.d.mts DELETED
@@ -1,3 +0,0 @@
1
- import type path from 'node:path';
2
- import { type PlatformPath } from './types.mjs';
3
- export declare function dir(nodePath: path.PlatformPath, _localPath: PlatformPath): (path: string) => string;
package/lib/esm/dir.mjs DELETED
@@ -1,3 +0,0 @@
1
- export function dir(nodePath, _localPath) {
2
- return (path) => nodePath.parse(path).dir;
3
- }
@@ -1,3 +0,0 @@
1
- import type path from 'node:path';
2
- import { type PlatformPath } from './types.mjs';
3
- export declare function parent(nodePath: path.PlatformPath, _localPath: PlatformPath): (file: string) => string;