Skip to content

Commit

Permalink
refactor(lib): [relative] drop partial url support
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Dec 16, 2024
1 parent 6b8a3be commit 4dbc19d
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 37 deletions.
16 changes: 6 additions & 10 deletions src/interfaces/platform-path.mts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import type dot from '#lib/dot'
import type resolveWith from '#lib/resolve-with'
import type toPath from '#lib/to-path'
import type {
Delimiter,
EmptyString,
Expand Down Expand Up @@ -245,29 +244,26 @@ interface PlatformPath {
* If a zero-length string is passed as `from` or `to`, the current working
* directory will be used instead of the zero-length strings.
*
* > 👉 **Note**: If `from` or `to` is a {@linkcode URL}, or can be parsed to
* > a `URL`, they'll be converted to paths using {@linkcode toPath}.
*
* @see {@linkcode RelativeOptions}
*
* @category
* core
*
* @this {void}
*
* @param {URL | string[] | string} from
* Start path, path segments, or URL
* @param {URL | string[] | string} to
* Destination path, path segments, or URL
* @param {string[] | string} from
* Start path or path segments
* @param {string[] | string} to
* Destination path or path segments
* @param {RelativeOptions | null | undefined} [options]
* Relative path generation options
* @return {string}
* Relative path from `from` to `to`
*/
relative(
this: void,
from: URL | string[] | string,
to: URL | string[] | string,
from: string[] | string,
to: string[] | string,
options?: RelativeOptions | null | undefined
): string

Expand Down
7 changes: 3 additions & 4 deletions src/lib/__tests__/relative.spec.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @file Unit Tests - relative
* @module pathe/lib/tests/unit/relative
* @see https://github.com/nodejs/node/blob/v23.2.0/test/parallel/test-path-relative.js
* @see https://github.com/nodejs/node/blob/v23.4.0/test/parallel/test-path-relative.js
*/

import process from '#internal/process'
Expand All @@ -13,7 +13,7 @@ import { posix, win32 } from 'node:path'

describe('unit:lib/relative', () => {
describe('posix', () => {
it.each<[URL | string, URL | string]>([
it.each<[string, string]>([
['', ''],
['/Users/a/web/b/test/mails', '/Users/a/web/b'],
['/baz', '/baz-quux'],
Expand All @@ -30,7 +30,6 @@ describe('unit:lib/relative', () => {
['/var/lib', '/var'],
['/var/lib', '/var/apache'],
['/var/lib', '/var/lib'],
['file:///package.json', new URL('file:///test/index.mjs')],
[posix.sep, '/foo'],
[posix.sep, '/var/lib']
])('should return relative path (%j, %j)', (from, to) => {
Expand All @@ -47,7 +46,7 @@ describe('unit:lib/relative', () => {
vi.spyOn(process, 'cwd').mockImplementation(cwdWindows)
})

it.each<[URL | string, URL | string]>([
it.each<[string, string]>([
['C:\\', 'C:\\foo'],
['C:\\baz', 'C:\\baz-quux'],
['C:\\baz', '\\\\foo\\bar\\baz'],
Expand Down
2 changes: 1 addition & 1 deletion src/lib/__tests__/resolve-with.spec.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @file Unit Tests - resolveWith
* @module pathe/lib/tests/unit/resolveWith
* @see https://github.com/nodejs/node/blob/v23.2.0/test/parallel/test-path-resolve.js
* @see https://github.com/nodejs/node/blob/v23.4.0/test/parallel/test-path-resolve.js
*/

import DRIVE from '#fixtures/drive'
Expand Down
31 changes: 11 additions & 20 deletions src/lib/relative.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
*/

import { DRIVE_PATH_REGEX } from '#internal/constants'
import validateURLString from '#internal/validate-url-string'
import validateString from '#internal/validate-string'
import dot from '#lib/dot'
import isSep from '#lib/is-sep'
import resolveWith from '#lib/resolve-with'
import sep from '#lib/sep'
import toPath from '#lib/to-path'
import type { RelativeOptions } from '@flex-development/pathe'

export default relative
Expand All @@ -24,40 +23,32 @@ export default relative
* If a zero-length string is passed as `from` or `to`, the current working
* directory will be used instead of the zero-length strings.
*
* > 👉 **Note**: If `from` or `to` is a {@linkcode URL}, or can be parsed to a
* > `URL`, they'll be converted to paths using {@linkcode toPath}.
*
* @see {@linkcode RelativeOptions}
*
* @todo url support
*
* @category
* core
*
* @this {void}
*
* @param {URL | string[] | string} from
* Start path, path segments, or URL
* @param {URL | string[] | string} to
* Destination path, path segments, or URL
* @param {string[] | string} from
* Start path or path segments
* @param {string[] | string} to
* Destination path or path segments
* @param {RelativeOptions | null | undefined} [options]
* Relative path generation options
* @return {string}
* Relative path from `from` to `to`
*/
function relative(
this: void,
from: URL | string[] | string,
to: URL | string[] | string,
from: string[] | string,
to: string[] | string,
options?: RelativeOptions | null | undefined
): string {
if (!Array.isArray(from)) {
validateURLString(from, 'from')
from = toPath(from)
}

if (!Array.isArray(to)) {
validateURLString(to, 'to')
to = toPath(to)
}
if (!Array.isArray(from)) validateString(from, 'from')
if (!Array.isArray(to)) validateString(to, 'to')

if (from === to) return ''

Expand Down
2 changes: 2 additions & 0 deletions src/lib/resolve-with.mts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import type { Cwd, ResolveWithOptions } from '@flex-development/pathe'
*
* @see {@linkcode ResolveWithOptions}
*
* @todo url support
*
* @category
* utils
*
Expand Down
2 changes: 1 addition & 1 deletion src/types/__tests__/ext.spec-d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* @module pathe/types/tests/unit-d/Ext
*/

import type TestSubject from '#types/ext'
import type { Dot } from '@flex-development/pathe'
import type TestSubject from '../ext'

describe('unit-d:types/Ext', () => {
it('should equal `${Dot}${string}`', () => {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.typecheck.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"include": [
"__fixtures__/**/*",
"__tests__/**/*",
"src/**.mts",
"src/**/**.mts",
"typings/**/*",
"vitest-env.d.mts"
]
Expand Down

0 comments on commit 4dbc19d

Please sign in to comment.