Skip to content

Commit

Permalink
add isCWD, correct relative() when cwd is root
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed May 9, 2024
1 parent 9cdd8e9 commit 7b26587
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,11 @@ pathname matches, due to unicode normalization mismatches.
Always use this method instead of testing the `path.name`
property directly.

#### `path.isCWD`

Set to true if this `Path` object is the current working
directory of the `PathScurry` collection that contains it.

#### `path.getType()`

Returns the type of the Path object, `'File'`, `'Directory'`,
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
"typedoc": "^0.25.12",
"typescript": "^5.4.3"
},
"tap": {
"typecheck": true
},
"engines": {
"node": ">=16 || 14 >=14.17"
},
Expand Down
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,12 @@ export abstract class PathBase implements Dirent {
*/
nocase: boolean

/**
* boolean indicating that this path is the current working directory
* of the PathScurry collection that contains it.
*/
isCWD: boolean = false

/**
* the string or regexp used to split paths. On posix, it is `'/'`, and on
* windows it is a RegExp matching either `'/'` or `'\\'`
Expand Down Expand Up @@ -581,6 +587,7 @@ export abstract class PathBase implements Dirent {
* the cwd, then this ends up being equivalent to the fullpath()
*/
relative(): string {
if (this.isCWD) return ''
if (this.#relative !== undefined) {
return this.#relative
}
Expand All @@ -601,6 +608,7 @@ export abstract class PathBase implements Dirent {
*/
relativePosix(): string {
if (this.sep === '/') return this.relative()
if (this.isCWD) return ''
if (this.#relativePosix !== undefined) return this.#relativePosix
const name = this.name
const p = this.parent
Expand Down Expand Up @@ -1348,6 +1356,8 @@ export abstract class PathBase implements Dirent {
*/
[setAsCwd](oldCwd: PathBase): void {
if (oldCwd === this) return
oldCwd.isCWD = false
this.isCWD = true

const changed = new Set<PathBase>([])
let rp = []
Expand Down
12 changes: 12 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ t.test('platform-specific', t => {
t.equal(pw.resolve('//prp1/'), 'C:\\prp1')
t.equal(pw.resolvePosix('//prp1/'), '//?/C:/prp1')
t.equal(pw.cwd.resolve('../../../../../../../'), pw.root)
pw.chdir(pw.root)
t.equal(pw.relativePosix('C:/some'), 'some')
t.equal(pw.relative('C:/some/PATH'), 'some\\path')
t.equal(pw.relativePosix('C:/some/PATH'), 'some/path')
t.equal(new PathScurry('/prp2').cwd.fullpath(), 'C:\\prp2')
t.equal(new PathScurry('\\prp3').resolve(), 'C:\\prp3')
t.equal(new PathScurry('\\prp3').resolvePosix(), '//?/C:/prp3')
Expand Down Expand Up @@ -179,6 +183,10 @@ t.test('platform-specific', t => {
'/x/y/some/absolute/path',
)
t.equal(pw.cwd.resolve('foo'), pw.cwd.resolve('x/../FOO'))
pw.chdir(pw.root)
t.equal(pw.relativePosix('/some'), 'some')
t.equal(pw.relative('/some/PATH'), 'some/path')
t.equal(pw.relativePosix('/some/PATH'), 'some/path')
t.end()
})

Expand Down Expand Up @@ -206,6 +214,10 @@ t.test('platform-specific', t => {
t.equal(pw.cwd.resolve('../../../../../../../'), pw.root)
t.equal(pw.cwd.resolve('foo'), pw.cwd.resolve('x/../foo'))
t.not(pw.cwd.resolve('foo'), pw.cwd.resolve('x/../FOO'))
pw.chdir(pw.root)
t.equal(pw.relativePosix('/some'), 'some')
t.equal(pw.relative('/some/PATH'), 'some/PATH')
t.equal(pw.relativePosix('/some/PATH'), 'some/PATH')
t.end()
})

Expand Down

0 comments on commit 7b26587

Please sign in to comment.