diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..41fe626 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,21 @@ +name: CI +on: + - push + - pull_request +jobs: + test: + name: Node.js ${{ matrix.node-version }} + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + node-version: + - 14 + - 12 + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + - run: npm install + - run: npm test diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index f3fa8cd..0000000 --- a/.travis.yml +++ /dev/null @@ -1,4 +0,0 @@ -language: node_js -node_js: - - '10' - - '8' diff --git a/index.d.ts b/index.d.ts index c7950f0..e2d24bf 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,12 +1,10 @@ -declare namespace fileUrl { - interface Options { - /** - Passing `false` will make it not call `path.resolve()` on the path. +export interface Options { + /** + Passing `false` will make it not call `path.resolve()` on the path. - @default true - */ - readonly resolve?: boolean; - } + @default true + */ + readonly resolve?: boolean; } /** @@ -17,7 +15,7 @@ Convert a file path to a file URL. @example ``` -import fileUrl = require('file-url'); +import fileUrl from 'file-url'; fileUrl('unicorn.jpg'); //=> 'file:///Users/sindresorhus/dev/file-url/unicorn.jpg' @@ -29,6 +27,4 @@ fileUrl('unicorn.jpg', {resolve: false}); //=> 'file:///unicorn.jpg' ``` */ -declare function fileUrl(filePath: string, options?: fileUrl.Options): string; - -export = fileUrl; +export default function fileUrl(filePath: string, options?: Options): string; diff --git a/index.js b/index.js index e543787..340376b 100644 --- a/index.js +++ b/index.js @@ -1,30 +1,25 @@ -'use strict'; -const path = require('path'); +import path from 'path'; -module.exports = (filePath, options) => { +export default function fileUrl(filePath, options = {}) { if (typeof filePath !== 'string') { throw new TypeError(`Expected a string, got ${typeof filePath}`); } - options = { - resolve: true, - ...options - }; + const {resolve = true} = options; let pathName = filePath; - - if (options.resolve) { + if (resolve) { pathName = path.resolve(filePath); } pathName = pathName.replace(/\\/g, '/'); - // Windows drive letter must be prefixed with a slash + // Windows drive letter must be prefixed with a slash. if (pathName[0] !== '/') { pathName = `/${pathName}`; } - // Escape required characters for path components + // Escape required characters for path components. // See: https://tools.ietf.org/html/rfc3986#section-3.3 return encodeURI(`file://${pathName}`).replace(/[?#]/g, encodeURIComponent); -}; +} diff --git a/index.test-d.ts b/index.test-d.ts index 4929c98..f939094 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,5 +1,5 @@ import {expectType} from 'tsd'; -import fileUrl = require('.'); +import fileUrl from './index.js'; expectType(fileUrl('unicorn.jpg')); expectType(fileUrl('unicorn.jpg', {resolve: false})); diff --git a/license b/license index e7af2f7..fa7ceba 100644 --- a/license +++ b/license @@ -1,6 +1,6 @@ MIT License -Copyright (c) Sindre Sorhus (sindresorhus.com) +Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/package.json b/package.json index 2e0785c..2835984 100644 --- a/package.json +++ b/package.json @@ -1,16 +1,19 @@ { "name": "file-url", - "version": "3.0.0", + "version": "4.0.0", "description": "Convert a file path to a file url: `unicorn.jpg` → `file:///Users/sindresorhus/unicorn.jpg`", "license": "MIT", "repository": "sindresorhus/file-url", + "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", - "url": "sindresorhus.com" + "url": "https://sindresorhus.com" }, + "type": "module", + "exports": "./index.js", "engines": { - "node": ">=8" + "node": ">=12" }, "scripts": { "test": "xo && ava && tsd" @@ -28,8 +31,8 @@ "slash" ], "devDependencies": { - "ava": "^1.4.1", - "tsd": "^0.7.2", - "xo": "^0.24.0" + "ava": "^3.15.0", + "tsd": "^0.14.0", + "xo": "^0.38.2" } } diff --git a/readme.md b/readme.md index 43604c1..685191c 100644 --- a/readme.md +++ b/readme.md @@ -1,19 +1,17 @@ -# file-url [![Build Status](https://travis-ci.org/sindresorhus/file-url.svg?branch=master)](https://travis-ci.org/sindresorhus/file-url) +# file-url > Convert a file path to a file url: `unicorn.jpg` → `file:///Users/sindresorhus/unicorn.jpg` - ## Install ``` $ npm install file-url ``` - ## Usage ```js -const fileUrl = require('file-url'); +import fileUrl from 'file-url'; fileUrl('unicorn.jpg'); //=> 'file:///Users/sindresorhus/dev/file-url/unicorn.jpg' @@ -27,7 +25,7 @@ fileUrl('unicorn.jpg', {resolve: false}); ## API -### fileUrl(filePath, [options]) +### fileUrl(filePath, options?) Returns the `filePath` converted to a file URL. @@ -39,21 +37,15 @@ File path to convert. #### options -Type: `Object` +Type: `object` ##### resolve -Type: `boolean`
+Type: `boolean`\ Default: `true` Passing `false` will make it not call `path.resolve()` on the path. - ## Related - [file-url-cli](https://github.com/sindresorhus/file-url-cli) - CLI for this module - - -## License - -MIT © [Sindre Sorhus](https://sindresorhus.com) diff --git a/test.js b/test.js index fae5310..b2369ec 100644 --- a/test.js +++ b/test.js @@ -1,8 +1,8 @@ import test from 'ava'; -import fileUrl from '.'; +import fileUrl from './index.js'; test('converts path to file url', t => { - t.regex(fileUrl('test.jpg'), /file:\/\/\/.*\/test\.jpg/); + t.regex(fileUrl('test.jpg'), /file:\/{3}.*\/test\.jpg/); if (process.platform === 'win32') { t.is(fileUrl('C:\\Users\\sindresorhus\\dev\\te^st.jpg'), 'file:///C:/Users/sindresorhus/dev/te%5Est.jpg'); @@ -12,7 +12,7 @@ test('converts path to file url', t => { }); test('escapes reserved characters in path', t => { - t.regex(fileUrl('Bad?/A#1.jpg'), /^file:\/\/\/.*\/Bad%3F\/A%231\.jpg$/); + t.regex(fileUrl('Bad?/A#1.jpg'), /^file:\/{3}.*\/Bad%3F\/A%231\.jpg$/); }); test('accepts resolve parameter', t => {