bare-url 0.3.3 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +24 -0
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -221,3 +221,27 @@ exports.fileURLToPath = function fileURLToPath (url) {
221
221
 
222
222
  return pathname
223
223
  }
224
+
225
+ exports.pathToFileURL = function pathToFileURL (pathname) {
226
+ let resolved = path.resolve(pathname)
227
+
228
+ if (pathname[pathname.length - 1] === '/') {
229
+ resolved += '/'
230
+ } else if (os.platform() === 'win32' && pathname[pathname.length - 1 === '\\']) {
231
+ resolved += '/'
232
+ }
233
+
234
+ resolved = resolved
235
+ .replaceAll('%', '%25') // Must be first
236
+ .replaceAll('#', '%23')
237
+ .replaceAll('?', '%3f')
238
+ .replaceAll('\n', '%0a')
239
+ .replaceAll('\r', '%0d')
240
+ .replaceAll('\t', '%09')
241
+
242
+ if (os.platform() !== 'win32') {
243
+ resolved = resolved.replaceAll('\\', '%5c')
244
+ }
245
+
246
+ return new URL('file:' + resolved)
247
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-url",
3
- "version": "0.3.3",
3
+ "version": "0.3.4",
4
4
  "description": "URL parser for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [