Skip to content

Commit

Permalink
Add support for WHATWG URL
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
aduh95 authored Jun 11, 2021
1 parent 243d8ea commit be7687f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
* @typedef {BufferEncoding|{encoding?: null|BufferEncoding, mode: Mode?, flag?: string}} WriteOptions
*
* @typedef {string|Uint8Array} Path Path of the file.
* @typedef {Path|Options|VFile} Compatible Things that can be
* @typedef {typeof import('url').URL} URL WHATWG URL
* @typedef {Path|URL|Options|VFile} Compatible Things that can be
* passed to the function.
*/

Expand All @@ -20,6 +21,7 @@

import fs from 'fs'
import path from 'path'
import {fileURLToPath} from 'url'
import buffer from 'is-buffer'
import {VFile} from 'vfile'

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

return options instanceof VFile ? options : new VFile(options)
Expand Down
10 changes: 9 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ npm install to-vfile
import {toVFile} from 'to-vfile'

console.log(toVFile('readme.md'))
console.log(toVFile(new URL('./readme.md', import.meta.url)))
console.log(toVFile.readSync('.git/HEAD'))
console.log(toVFile.readSync('.git/HEAD', 'utf8'))
```
Expand All @@ -41,6 +42,12 @@ VFile {
history: ['readme.md'],
cwd: '/Users/tilde/projects/oss/to-vfile'
}
VFile {
data: {},
messages: [],
history: ['readme.md'],
cwd: '/Users/tilde/projects/oss/to-vfile'
}
VFile {
data: {},
messages: [],
Expand All @@ -67,7 +74,8 @@ There is no default export.
Create a virtual file.
Works like the [vfile][] constructor, except when `options` is `string` or
`Buffer`, in which case it’s treated as `{path: options}` instead of
`{value: options}`.
`{value: options}`, or when `options` is a WHATWG `URL` object, in which case
it’s treated as `{path: fileURLToPath(options)}`.
### `toVFile.read(options[, encoding][, callback])`
Expand Down
14 changes: 14 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs'
import path from 'path'
import {fileURLToPath, URL} from 'url'
import test from 'tape'
import buffer from 'is-buffer'
import {toVFile} from './index.js'
Expand Down Expand Up @@ -52,6 +53,19 @@ test('toVFile()', function (t) {
st.equal(first, second)
st.end()
})

t.test('should accept a WHATWG URL object', function (st) {
const dir = fileURLToPath(new URL('./', import.meta.url))
var file = toVFile(new URL('./baz.qux', import.meta.url))

st.equal(file.path, join(dir, 'baz.qux'))
st.equal(file.basename, 'baz.qux')
st.equal(file.stem, 'baz')
st.equal(file.extname, '.qux')
st.equal(file.dirname, dir.replace(/[/\\]$/, ''))
st.equal(file.value, undefined)
st.end()
})
})

test('toVFile.readSync', function (t) {
Expand Down

0 comments on commit be7687f

Please sign in to comment.