From 2b48855e19712cf7c5fa5b8af2dca9dc17a0f83d Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Tue, 28 May 2019 17:28:20 +0700 Subject: [PATCH 1/4] Create funding.yml --- .github/funding.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .github/funding.yml diff --git a/.github/funding.yml b/.github/funding.yml new file mode 100644 index 0000000..1a630e9 --- /dev/null +++ b/.github/funding.yml @@ -0,0 +1,3 @@ +github: sindresorhus +open_collective: sindresorhus +custom: https://sindresorhus.com/donate From 079488f404fe610b13e89241eca99f9534f9d155 Mon Sep 17 00:00:00 2001 From: Richie Bendall Date: Sat, 2 Jan 2021 02:47:05 +1300 Subject: [PATCH 2/4] Move to GitHub Actions (#11) --- .github/workflows/main.yml | 23 +++++++++++++++++++++++ .travis.yml | 4 ---- readme.md | 2 +- 3 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/main.yml delete mode 100644 .travis.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..18531b3 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,23 @@ +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 + - 10 + - 8 + 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/readme.md b/readme.md index 43604c1..d6d3392 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,4 @@ -# 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` From 52b906de5d2f9d1289d9522d7e5032853ae801cf Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Thu, 8 Apr 2021 02:04:19 +0700 Subject: [PATCH 3/4] Require Node.js 12 and move to ESM --- .github/funding.yml | 3 --- .github/workflows/main.yml | 2 -- index.d.ts | 20 ++++++++------------ index.js | 19 +++++++------------ index.test-d.ts | 2 +- license | 2 +- package.json | 13 ++++++++----- readme.md | 16 ++++------------ test.js | 6 +++--- 9 files changed, 32 insertions(+), 51 deletions(-) delete mode 100644 .github/funding.yml diff --git a/.github/funding.yml b/.github/funding.yml deleted file mode 100644 index 1a630e9..0000000 --- a/.github/funding.yml +++ /dev/null @@ -1,3 +0,0 @@ -github: sindresorhus -open_collective: sindresorhus -custom: https://sindresorhus.com/donate diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 18531b3..41fe626 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -12,8 +12,6 @@ jobs: node-version: - 14 - 12 - - 10 - - 8 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v1 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..8699d42 100644 --- a/package.json +++ b/package.json @@ -4,13 +4,16 @@ "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 d6d3392..685191c 100644 --- a/readme.md +++ b/readme.md @@ -2,18 +2,16 @@ > 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 => { From 186c35b83a0728c5cef65b88899b862bb6783603 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Thu, 8 Apr 2021 02:05:05 +0700 Subject: [PATCH 4/4] 4.0.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8699d42..2835984 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "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",