Skip to content

Commit be7687f

Browse files
authored
Add support for WHATWG URL
Closes GH-16. Closes GH-17. Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com> Reviewed-by: Remco Haszing <remcohaszing@gmail.com> Reviewed-by: Merlijn Vos <merlijn@soverin.net> Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent 243d8ea commit be7687f

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

lib/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
* @typedef {BufferEncoding|{encoding?: null|BufferEncoding, mode: Mode?, flag?: string}} WriteOptions
99
*
1010
* @typedef {string|Uint8Array} Path Path of the file.
11-
* @typedef {Path|Options|VFile} Compatible Things that can be
11+
* @typedef {typeof import('url').URL} URL WHATWG URL
12+
* @typedef {Path|URL|Options|VFile} Compatible Things that can be
1213
* passed to the function.
1314
*/
1415

@@ -20,6 +21,7 @@
2021

2122
import fs from 'fs'
2223
import path from 'path'
24+
import {fileURLToPath} from 'url'
2325
import buffer from 'is-buffer'
2426
import {VFile} from 'vfile'
2527

@@ -35,6 +37,8 @@ import {VFile} from 'vfile'
3537
export function toVFile(options) {
3638
if (typeof options === 'string' || buffer(options)) {
3739
options = {path: String(options)}
40+
} else if (options && options.href && options.origin) {
41+
options = {path: fileURLToPath(options)}
3842
}
3943

4044
return options instanceof VFile ? options : new VFile(options)

readme.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ npm install to-vfile
2828
import {toVFile} from 'to-vfile'
2929

3030
console.log(toVFile('readme.md'))
31+
console.log(toVFile(new URL('./readme.md', import.meta.url)))
3132
console.log(toVFile.readSync('.git/HEAD'))
3233
console.log(toVFile.readSync('.git/HEAD', 'utf8'))
3334
```
@@ -41,6 +42,12 @@ VFile {
4142
history: ['readme.md'],
4243
cwd: '/Users/tilde/projects/oss/to-vfile'
4344
}
45+
VFile {
46+
data: {},
47+
messages: [],
48+
history: ['readme.md'],
49+
cwd: '/Users/tilde/projects/oss/to-vfile'
50+
}
4451
VFile {
4552
data: {},
4653
messages: [],
@@ -67,7 +74,8 @@ There is no default export.
6774
Create a virtual file.
6875
Works like the [vfile][] constructor, except when `options` is `string` or
6976
`Buffer`, in which case it’s treated as `{path: options}` instead of
70-
`{value: options}`.
77+
`{value: options}`, or when `options` is a WHATWG `URL` object, in which case
78+
it’s treated as `{path: fileURLToPath(options)}`.
7179
7280
### `toVFile.read(options[, encoding][, callback])`
7381

test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from 'fs'
22
import path from 'path'
3+
import {fileURLToPath, URL} from 'url'
34
import test from 'tape'
45
import buffer from 'is-buffer'
56
import {toVFile} from './index.js'
@@ -52,6 +53,19 @@ test('toVFile()', function (t) {
5253
st.equal(first, second)
5354
st.end()
5455
})
56+
57+
t.test('should accept a WHATWG URL object', function (st) {
58+
const dir = fileURLToPath(new URL('./', import.meta.url))
59+
var file = toVFile(new URL('./baz.qux', import.meta.url))
60+
61+
st.equal(file.path, join(dir, 'baz.qux'))
62+
st.equal(file.basename, 'baz.qux')
63+
st.equal(file.stem, 'baz')
64+
st.equal(file.extname, '.qux')
65+
st.equal(file.dirname, dir.replace(/[/\\]$/, ''))
66+
st.equal(file.value, undefined)
67+
st.end()
68+
})
5569
})
5670

5771
test('toVFile.readSync', function (t) {

0 commit comments

Comments
 (0)