@gesslar/toolkit 0.0.9 → 0.0.12
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/package.json +4 -4
- package/src/lib/File.js +71 -0
- package/src/lib/FileObject.js +1 -1
- package/src/types/File.d.ts +6 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gesslar/toolkit",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.12",
|
|
4
4
|
"description": "Get in, bitches, we're going toolkitting.",
|
|
5
5
|
"main": "./src/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -63,10 +63,10 @@
|
|
|
63
63
|
"devDependencies": {
|
|
64
64
|
"@stylistic/eslint-plugin": "^5.4.0",
|
|
65
65
|
"@types/node": "^24.5.2",
|
|
66
|
-
"@typescript-eslint/eslint-plugin": "^8.44.
|
|
67
|
-
"@typescript-eslint/parser": "^8.44.
|
|
66
|
+
"@typescript-eslint/eslint-plugin": "^8.44.1",
|
|
67
|
+
"@typescript-eslint/parser": "^8.44.1",
|
|
68
68
|
"eslint": "^9.36.0",
|
|
69
|
-
"eslint-plugin-jsdoc": "^60.
|
|
69
|
+
"eslint-plugin-jsdoc": "^60.3.0",
|
|
70
70
|
"typedoc": "^0.28.13",
|
|
71
71
|
"typedoc-plugin-markdown": "^4.9.0"
|
|
72
72
|
}
|
package/src/lib/File.js
CHANGED
|
@@ -340,4 +340,75 @@ export default class File {
|
|
|
340
340
|
? to.path
|
|
341
341
|
: relative
|
|
342
342
|
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Merge two paths by finding overlapping segments and combining them efficiently
|
|
346
|
+
*
|
|
347
|
+
* @param {string} path1 - The first path
|
|
348
|
+
* @param {string} path2 - The second path to merge with the first
|
|
349
|
+
* @param {string} [sep] - The path separator to use (defaults to system separator)
|
|
350
|
+
* @returns {string} The merged path
|
|
351
|
+
*/
|
|
352
|
+
static mergeOverlappingPaths(path1, path2, sep=path.sep) {
|
|
353
|
+
const from = path1.split(sep).filter(Boolean)
|
|
354
|
+
const to = path2.split(sep).filter(Boolean)
|
|
355
|
+
|
|
356
|
+
// If they're the same, just return path1
|
|
357
|
+
if(to.length === from.length && from.every((f, i) => to[i] === f)) {
|
|
358
|
+
return path1
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
const overlapIndex = from.findLastIndex((curr, index) => {
|
|
362
|
+
const left = from.at(index)
|
|
363
|
+
const right = to.at(0)
|
|
364
|
+
|
|
365
|
+
return left === right
|
|
366
|
+
})
|
|
367
|
+
|
|
368
|
+
// If overlap is found, slice and join
|
|
369
|
+
if(overlapIndex !== -1) {
|
|
370
|
+
const prefix = from.slice(0, overlapIndex)
|
|
371
|
+
|
|
372
|
+
return path.join(...prefix, ...to)
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// If no overlap, just join the paths
|
|
376
|
+
return path.join(path1, path2)
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Resolve a path relative to another path using various strategies
|
|
381
|
+
* Handles absolute paths, relative navigation, and overlap-based merging
|
|
382
|
+
*
|
|
383
|
+
* @param {string} fromPath - The base path to resolve from
|
|
384
|
+
* @param {string} toPath - The target path to resolve
|
|
385
|
+
* @returns {string} The resolved path
|
|
386
|
+
*/
|
|
387
|
+
static resolvePath(fromPath, toPath) {
|
|
388
|
+
// Normalize inputs
|
|
389
|
+
const from = fromPath.trim()
|
|
390
|
+
const to = toPath.trim()
|
|
391
|
+
|
|
392
|
+
// Handle empty cases
|
|
393
|
+
if(!from && !to)
|
|
394
|
+
return ""
|
|
395
|
+
|
|
396
|
+
if(!from)
|
|
397
|
+
return to
|
|
398
|
+
|
|
399
|
+
if(!to)
|
|
400
|
+
return from
|
|
401
|
+
|
|
402
|
+
// Strategy 1: If 'to' is absolute, it's standalone
|
|
403
|
+
if(path.isAbsolute(to))
|
|
404
|
+
return to
|
|
405
|
+
|
|
406
|
+
// Strategy 2: If 'to' contains relative navigation (./ or ../)
|
|
407
|
+
if(to.includes("./") || to.includes("../") || to.startsWith(".") || to.startsWith(".."))
|
|
408
|
+
return path.resolve(from, to)
|
|
409
|
+
|
|
410
|
+
// Strategy 3: Try overlap-based merging, which will default to a basic
|
|
411
|
+
// join if no overlap
|
|
412
|
+
return File.mergeOverlappingPaths(from, to)
|
|
413
|
+
}
|
|
343
414
|
}
|
package/src/lib/FileObject.js
CHANGED
|
@@ -68,7 +68,7 @@ export default class FileObject {
|
|
|
68
68
|
|
|
69
69
|
const final = path.isAbsolute(fixedFile)
|
|
70
70
|
? fixedFile
|
|
71
|
-
: path.resolve(directory.
|
|
71
|
+
: path.resolve(directory?.path ?? ".", fixedFile)
|
|
72
72
|
|
|
73
73
|
const resolved = final
|
|
74
74
|
const fileUri = File.pathToUri(resolved)
|
package/src/types/File.d.ts
CHANGED
|
@@ -74,4 +74,10 @@ export default class File {
|
|
|
74
74
|
|
|
75
75
|
/** Compute relative path between two file system objects */
|
|
76
76
|
static relativeOrAbsolutePath(from: FileObject | DirectoryObject, to: FileObject | DirectoryObject): string
|
|
77
|
+
|
|
78
|
+
/** Merge two paths by finding overlapping segments and combining them efficiently */
|
|
79
|
+
static mergeOverlappingPaths(path1: string, path2: string, sep?: string): string
|
|
80
|
+
|
|
81
|
+
/** Resolve a path relative to another path using various strategies. Handles absolute paths, relative navigation, and overlap-based merging */
|
|
82
|
+
static resolvePath(fromPath: string, toPath: string): string
|
|
77
83
|
}
|