Skip to content

Commit f255a20

Browse files
committed
1.0.1
1 parent 8d742bb commit f255a20

File tree

5 files changed

+112
-1
lines changed

5 files changed

+112
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

.npmignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.DS_Store
2+
3+
.git/
4+
.gitignore
5+
6+
README.md
7+
8+
node_modules/
9+
npm-debug.log

README.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,48 @@
1-
# true-case-path
1+
# true-case-path
2+
3+
## Usage
4+
5+
`trueCasePathSync(<fileSystemPath>)`
6+
7+
## Description
8+
9+
Given a possibly case-variant version of an existing filesystem path, returns
10+
the case-exact, normalized version as stored in the filesystem.
11+
12+
If the input path is a globbing *pattern* as defined by the 'glob' npm
13+
package, only the 1st match, if any, is returned.
14+
Only a literal input path guarantees an unambiguous result.
15+
16+
If no matching path exists, undefined is returned.
17+
On case-SENSITIVE filesystems, a match will also be found, but if case
18+
variations of a given path exist, it is undefined which match is returned.
19+
20+
## Platforms
21+
22+
Windows, OSX, and Linux (though note the limitations with case-insensitive filesystems).
23+
24+
## Limitations
25+
26+
- Paths starting with `'./'` are acceptable, but paths starting with `'../'`
27+
are not - when in doubt, resolve with `fs.realPathSync()` first.
28+
An initial `'.'` and *interior* `'..'` instances are normalized, but a relative
29+
input path still results in a relative output path. If you want to ensure
30+
an absolute output path, apply `fs.realPathSync()` to the result.
31+
- On Windows, no attempt is made to case-correct the drive letter or UNC-share
32+
component of the path.
33+
- Unicode support:
34+
- Be sure to use UTF8 source-code files (with a BOM on Windows)
35+
- On OSX, the input path is automatically converted to NFD Unicode form
36+
to match how the filesystem stores names, but note that the result will
37+
invariably be NFD too (which makes no difference for ASCII-characters-only
38+
names).
39+
40+
## Examples
41+
42+
```
43+
const trueCasePathSync = require('true-case-path')
44+
45+
trueCasePathSync('/users/guest') // OSX: -> '/Users/Guest'
46+
47+
trueCasePathSync('c:\\users\\all users') // Windows: -> 'c:\Users\All Users'
48+
```

index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict'
2+
3+
var glob = require('glob')
4+
var path = require('path')
5+
6+
function trueCasePathSync(fsPath) {
7+
8+
// Normalize the path so as to resolve . and .. components.
9+
// !! As of Node v4.1.1, a path starting with ../ is NOT resolved relative
10+
// !! to the current dir, and glob.sync() below then fails.
11+
// !! When in doubt, resolve with fs.realPathSync() *beforehand*.
12+
var fsPathNormalized = path.normalize(fsPath)
13+
14+
// OSX: HFS+ stores filenames in NFD (decomposed normal form) Unicode format,
15+
// so we must ensure that the input path is in that format first.
16+
if (process.platform === 'darwin') fsPathNormalized = fsPathNormalized.normalize('NFD')
17+
18+
// !! Windows: Curiously, the drive component mustn't be part of a glob,
19+
// !! otherwise glob.sync() will invariably match nothing.
20+
// !! Thus, we remove the drive component and instead pass it in as the 'cwd'
21+
// !! (working dir.) property below.
22+
var pathRoot = path.parse(fsPathNormalized).root
23+
var noDrivePath = fsPathNormalized.slice(Math.max(pathRoot.length - 1, 0))
24+
25+
// Perform case-insensitive globbing (on Windows, relative to the drive /
26+
// network share) and return the 1st match, if any.
27+
// Fortunately, glob() with nocase case-corrects the input even if it is
28+
// a *literal* path.
29+
return glob.sync(noDrivePath, { nocase: true, cwd: pathRoot })[0]
30+
}
31+
32+
module.exports = trueCasePathSync

package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "true-case-path",
3+
"version": "1.0.1",
4+
"description": "Given a possibly case-variant version of an existing filesystem path, returns the case-exact, normalized version as stored in the filesystem.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/barsh/true-case-path.git"
12+
},
13+
"author": "barsh",
14+
"license": "Apache version 2.0",
15+
"bugs": {
16+
"url": "https://github.com/barsh/true-case-path/issues"
17+
},
18+
"homepage": "https://github.com/barsh/true-case-path#readme",
19+
"dependencies": {
20+
"glob": "^6.0.4"
21+
}
22+
}

0 commit comments

Comments
 (0)