Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: unjs/ufo
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.8.2
Choose a base ref
...
head repository: unjs/ufo
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.8.3
Choose a head ref
  • 2 commits
  • 4 files changed
  • 1 contributor

Commits on Mar 31, 2022

  1. fix(withProtocol): handle input without protocol (resolves #52)

    side effect: hasProtocol allows [protocol]:$
    pi0 committed Mar 31, 2022

    Verified

    This commit was signed with the committer’s verified signature.
    kurtmckee Kurt McKee
    Copy the full SHA
    4f66249 View commit details
  2. chore(release): 0.8.3

    pi0 committed Mar 31, 2022

    Verified

    This commit was signed with the committer’s verified signature.
    kurtmckee Kurt McKee
    Copy the full SHA
    1566047 View commit details
Showing with 22 additions and 6 deletions.
  1. +7 −0 CHANGELOG.md
  2. +1 −1 package.json
  3. +9 −2 src/utils.ts
  4. +5 −3 test/utilities.test.ts
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [0.8.3](https://github.com/unjs/ufo/compare/v0.8.2...v0.8.3) (2022-03-31)


### Bug Fixes

* **withProtocol:** handle input without protocol (resolves [#52](https://github.com/unjs/ufo/issues/52)) ([4f66249](https://github.com/unjs/ufo/commit/4f66249c2a5946483554ead832bb68db724b612e))

### [0.8.2](https://github.com/unjs/ufo/compare/v0.8.1...v0.8.2) (2022-03-31)


2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ufo",
"version": "0.8.2",
"version": "0.8.3",
"description": "URL utils for humans",
"repository": "unjs/ufo",
"license": "MIT",
11 changes: 9 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -7,8 +7,11 @@ export function isRelative (inputStr: string) {
return ['./', '../'].some(str => inputStr.startsWith(str))
}

const PROTOCOL_REGEX = /^\w+:(\/\/)?/
const PROTOCOL_RELATIVE_REGEX = /^\/\/[^/]+/

export function hasProtocol (inputStr: string, acceptProtocolRelative = false): boolean {
return /^\w+:(\/\/)?.+/.test(inputStr) || (acceptProtocolRelative && /^\/\/[^/]+/.test(inputStr))
return PROTOCOL_REGEX.test(inputStr) || (acceptProtocolRelative && PROTOCOL_RELATIVE_REGEX.test(inputStr))
}

const TRAILING_SLASH_RE = /\/$|\/\?/
@@ -122,7 +125,11 @@ export function withoutProtocol (input: string): string {
}

export function withProtocol (input: string, protocol: string): string {
return input.replace(/^\w+:(\/\/)?/, protocol)
const match = input.match(PROTOCOL_REGEX)
if (!match) {
return protocol + input
}
return protocol + input.substring(match[0].length)
}

// $URL based utils
8 changes: 5 additions & 3 deletions test/utilities.test.ts
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ describe('hasProtocol', () => {
{ input: 'https://test.com', out: [true, true] },
{ input: '/test', out: [false, false] },
{ input: 'file:///home/user', out: [true, true] },
{ input: 'tel:', out: [false, false] },
{ input: 'tel:', out: [true, true] },
{ input: 'tel:123456', out: [true, true] },
{ input: 'mailto:support@example.com', out: [true, true] }
]
@@ -75,7 +75,8 @@ describe('withHttp', () => {
{ input: 'https://example.com', out: 'http://example.com' },
{ input: 'ftp://example.com/test?foo', out: 'http://example.com/test?foo' },
{ input: 'https://foo.com/test?query=123#hash', out: 'http://foo.com/test?query=123#hash' },
{ input: 'file:///home/user', out: 'http:///home/user' }
{ input: 'file:///home/user', out: 'http:///home/user' },
{ input: 'foo.bar.com', out: 'http://foo.bar.com' }
]

for (const t of tests) {
@@ -90,7 +91,8 @@ describe('withHttps', () => {
{ input: 'http://example.com', out: 'https://example.com' },
{ input: 'ftp://example.com/test?foo', out: 'https://example.com/test?foo' },
{ input: 'http://foo.com/test?query=123#hash', out: 'https://foo.com/test?query=123#hash' },
{ input: 'file:///home/user', out: 'https:///home/user' }
{ input: 'file:///home/user', out: 'https:///home/user' },
{ input: 'foo.bar.com', out: 'https://foo.bar.com' }
]

for (const t of tests) {