Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for WHATWG URL #17

Merged
merged 3 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
* 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(/[/\\]$/, ''))
wooorm marked this conversation as resolved.
Show resolved Hide resolved
st.equal(file.value, undefined)
st.end()
})
})

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