diff --git a/packages/shared/src/utils/index.ts b/packages/shared/src/utils/index.ts index 2d329ba2c3..a73e96e89e 100644 --- a/packages/shared/src/utils/index.ts +++ b/packages/shared/src/utils/index.ts @@ -1,18 +1,12 @@ +export * from './links/index.js' +export * from './routes/index.js' + export * from './dedupeHead.js' export * from './ensureLeadingSlash.js' export * from './ensureEndingSlash.js' export * from './formatDateString.js' -export * from './inferRoutePath.js' -export * from './isLinkExternal.js' -export * from './isLinkHttp.js' -export * from './isLinkWithProtocol.js' -export * from './isPlainObject.js' -export * from './inferRoutePath.js' -export * from './normalizeRoutePath.js' export * from './omit.js' export * from './removeEndingSlash.js' export * from './removeLeadingSlash.js' export * from './resolveHeadIdentifier.js' -export * from './resolveLocalePath.js' -export * from './resolveRoutePathFromUrl.js' export * from './typeGuards.js' diff --git a/packages/shared/src/utils/isPlainObject.ts b/packages/shared/src/utils/isPlainObject.ts deleted file mode 100644 index 754f948983..0000000000 --- a/packages/shared/src/utils/isPlainObject.ts +++ /dev/null @@ -1,6 +0,0 @@ -/** - * Check if a value is plain object, with generic type support - */ -export const isPlainObject = = Record>( - val: unknown, -): val is T => Object.prototype.toString.call(val) === '[object Object]' diff --git a/packages/shared/src/utils/links/index.ts b/packages/shared/src/utils/links/index.ts new file mode 100644 index 0000000000..b99aa456d4 --- /dev/null +++ b/packages/shared/src/utils/links/index.ts @@ -0,0 +1,3 @@ +export * from './isLinkExternal.js' +export * from './isLinkHttp.js' +export * from './isLinkWithProtocol.js' diff --git a/packages/shared/src/utils/isLinkExternal.ts b/packages/shared/src/utils/links/isLinkExternal.ts similarity index 100% rename from packages/shared/src/utils/isLinkExternal.ts rename to packages/shared/src/utils/links/isLinkExternal.ts diff --git a/packages/shared/src/utils/isLinkHttp.ts b/packages/shared/src/utils/links/isLinkHttp.ts similarity index 100% rename from packages/shared/src/utils/isLinkHttp.ts rename to packages/shared/src/utils/links/isLinkHttp.ts diff --git a/packages/shared/src/utils/isLinkWithProtocol.ts b/packages/shared/src/utils/links/isLinkWithProtocol.ts similarity index 100% rename from packages/shared/src/utils/isLinkWithProtocol.ts rename to packages/shared/src/utils/links/isLinkWithProtocol.ts diff --git a/packages/shared/src/utils/routes/index.ts b/packages/shared/src/utils/routes/index.ts new file mode 100644 index 0000000000..a83b4caa70 --- /dev/null +++ b/packages/shared/src/utils/routes/index.ts @@ -0,0 +1,4 @@ +export * from './inferRoutePath' +export * from './normalizeRoutePath.js' +export * from './resolveLocalePath.js' +export * from './resolveRoutePathFromUrl.js' diff --git a/packages/shared/src/utils/inferRoutePath.ts b/packages/shared/src/utils/routes/inferRoutePath.ts similarity index 100% rename from packages/shared/src/utils/inferRoutePath.ts rename to packages/shared/src/utils/routes/inferRoutePath.ts diff --git a/packages/shared/src/utils/normalizeRoutePath.ts b/packages/shared/src/utils/routes/normalizeRoutePath.ts similarity index 100% rename from packages/shared/src/utils/normalizeRoutePath.ts rename to packages/shared/src/utils/routes/normalizeRoutePath.ts diff --git a/packages/shared/src/utils/resolveLocalePath.ts b/packages/shared/src/utils/routes/resolveLocalePath.ts similarity index 89% rename from packages/shared/src/utils/resolveLocalePath.ts rename to packages/shared/src/utils/routes/resolveLocalePath.ts index 7019dad148..3717a003ee 100644 --- a/packages/shared/src/utils/resolveLocalePath.ts +++ b/packages/shared/src/utils/routes/resolveLocalePath.ts @@ -1,4 +1,4 @@ -import type { LocaleConfig } from '../types/index.js' +import type { LocaleConfig } from '../../types/index.js' /** * Resolve the matched locale path of route path diff --git a/packages/shared/src/utils/resolveRoutePathFromUrl.ts b/packages/shared/src/utils/routes/resolveRoutePathFromUrl.ts similarity index 76% rename from packages/shared/src/utils/resolveRoutePathFromUrl.ts rename to packages/shared/src/utils/routes/resolveRoutePathFromUrl.ts index e2db6f6701..8d1cd587e2 100644 --- a/packages/shared/src/utils/resolveRoutePathFromUrl.ts +++ b/packages/shared/src/utils/routes/resolveRoutePathFromUrl.ts @@ -1,3 +1,6 @@ +/** + * For a give URL, remove the origin and the site base to get the route path + */ export const resolveRoutePathFromUrl = (url: string, base = '/'): string => { const pathname = url // remove url origin diff --git a/packages/shared/src/utils/typeGuards.ts b/packages/shared/src/utils/typeGuards.ts index 38c60344f9..6ff96310bf 100644 --- a/packages/shared/src/utils/typeGuards.ts +++ b/packages/shared/src/utils/typeGuards.ts @@ -5,6 +5,13 @@ export const isFunction = (val: unknown): val is Function => typeof val === 'function' +/** + * Check if a value is plain object, with generic type support + */ +export const isPlainObject = = Record>( + val: unknown, +): val is T => Object.prototype.toString.call(val) === '[object Object]' + /** * Check if a value is a string */ diff --git a/packages/shared/tests/isPlainObject.spec.ts b/packages/shared/tests/isPlainObject.spec.ts deleted file mode 100644 index 3b75e37ef3..0000000000 --- a/packages/shared/tests/isPlainObject.spec.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { expect, it } from 'vitest' -import { isPlainObject } from '../src/index.js' - -const testCases: [unknown, boolean][] = [ - [true, false], - [false, false], - ['', false], - ['foobar', false], - [0, false], - [1, false], - [[], false], - [{}, true], - [{ foo: 'bar' }, true], - [Object.create(null), true], -] - -it('should determine plain object correctly', () => { - testCases.forEach(([source, expected]) => { - expect(isPlainObject(source)).toBe(expected) - }) -}) diff --git a/packages/shared/tests/isLinkExternal.spec.ts b/packages/shared/tests/links/isLinkExternal.spec.ts similarity index 97% rename from packages/shared/tests/isLinkExternal.spec.ts rename to packages/shared/tests/links/isLinkExternal.spec.ts index 40ca463e70..d875503ee1 100644 --- a/packages/shared/tests/isLinkExternal.spec.ts +++ b/packages/shared/tests/links/isLinkExternal.spec.ts @@ -1,5 +1,5 @@ import { expect, it } from 'vitest' -import { isLinkExternal } from '../src/index.js' +import { isLinkExternal } from '../../src/index.js' const testCases: [ Parameters, diff --git a/packages/shared/tests/isLinkHttp.spec.ts b/packages/shared/tests/links/isLinkHttp.spec.ts similarity index 88% rename from packages/shared/tests/isLinkHttp.spec.ts rename to packages/shared/tests/links/isLinkHttp.spec.ts index af54449acd..c897ab8680 100644 --- a/packages/shared/tests/isLinkHttp.spec.ts +++ b/packages/shared/tests/links/isLinkHttp.spec.ts @@ -1,5 +1,5 @@ import { expect, it } from 'vitest' -import { isLinkHttp } from '../src/index.js' +import { isLinkHttp } from '../../src/index.js' const testCases: [string, ReturnType][] = [ ['https://foobar.com', true], diff --git a/packages/shared/tests/isLinkWithProtocol.spec.ts b/packages/shared/tests/links/isLinkWithProtocol.spec.ts similarity index 92% rename from packages/shared/tests/isLinkWithProtocol.spec.ts rename to packages/shared/tests/links/isLinkWithProtocol.spec.ts index a8563351c6..85679f31ec 100644 --- a/packages/shared/tests/isLinkWithProtocol.spec.ts +++ b/packages/shared/tests/links/isLinkWithProtocol.spec.ts @@ -1,5 +1,5 @@ import { expect, it } from 'vitest' -import { isLinkWithProtocol } from '../src/index.js' +import { isLinkWithProtocol } from '../../src/index.js' const testCases: [string, ReturnType][] = [ // with protocol diff --git a/packages/shared/tests/inferRoutePath.spec.ts b/packages/shared/tests/routes/inferRoutePath.spec.ts similarity index 96% rename from packages/shared/tests/inferRoutePath.spec.ts rename to packages/shared/tests/routes/inferRoutePath.spec.ts index 36bd910b96..053fa22a89 100644 --- a/packages/shared/tests/inferRoutePath.spec.ts +++ b/packages/shared/tests/routes/inferRoutePath.spec.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest' -import { inferRoutePath } from '../src/index.js' +import { inferRoutePath } from '../../src/index.js' const testCases = [ // absolute index diff --git a/packages/shared/tests/normalizeRoutePath.spec.ts b/packages/shared/tests/routes/normalizeRoutePath.spec.ts similarity index 99% rename from packages/shared/tests/normalizeRoutePath.spec.ts rename to packages/shared/tests/routes/normalizeRoutePath.spec.ts index 6543ab24e2..d18a00b035 100644 --- a/packages/shared/tests/normalizeRoutePath.spec.ts +++ b/packages/shared/tests/routes/normalizeRoutePath.spec.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest' -import { normalizeRoutePath } from '../src/index.js' +import { normalizeRoutePath } from '../../src/index.js' const testCases = [ // absolute index diff --git a/packages/shared/tests/resolveLocalePath.spec.ts b/packages/shared/tests/routes/resolveLocalePath.spec.ts similarity index 84% rename from packages/shared/tests/resolveLocalePath.spec.ts rename to packages/shared/tests/routes/resolveLocalePath.spec.ts index dc956e8607..375be0d473 100644 --- a/packages/shared/tests/resolveLocalePath.spec.ts +++ b/packages/shared/tests/routes/resolveLocalePath.spec.ts @@ -1,6 +1,6 @@ import { expect, it } from 'vitest' -import { resolveLocalePath } from '../src/index.js' -import type { LocaleConfig } from '../src/index.js' +import type { LocaleConfig } from '../../src/index.js' +import { resolveLocalePath } from '../../src/index.js' const locales: LocaleConfig = { '/': { diff --git a/packages/shared/tests/resolveRoutePathFromUrl.spec.ts b/packages/shared/tests/routes/resolveRoutePathFromUrl.spec.ts similarity index 94% rename from packages/shared/tests/resolveRoutePathFromUrl.spec.ts rename to packages/shared/tests/routes/resolveRoutePathFromUrl.spec.ts index e360c89a9a..f7a9b892fd 100644 --- a/packages/shared/tests/resolveRoutePathFromUrl.spec.ts +++ b/packages/shared/tests/routes/resolveRoutePathFromUrl.spec.ts @@ -1,5 +1,5 @@ import { expect, it } from 'vitest' -import { resolveRoutePathFromUrl } from '../src/index.js' +import { resolveRoutePathFromUrl } from '../../src/index.js' const testCases: [ Parameters, diff --git a/packages/shared/tests/typeGuards.spec.ts b/packages/shared/tests/typeGuards.spec.ts new file mode 100644 index 0000000000..f75beeaa11 --- /dev/null +++ b/packages/shared/tests/typeGuards.spec.ts @@ -0,0 +1,23 @@ +import { describe, expect, it } from 'vitest' +import { isPlainObject } from '../src/index.js' + +describe('isPlainObject', () => { + const testCases: [unknown, boolean][] = [ + [true, false], + [false, false], + ['', false], + ['foobar', false], + [0, false], + [1, false], + [[], false], + [{}, true], + [{ foo: 'bar' }, true], + [Object.create(null), true], + ] + + it('should determine plain object correctly', () => { + testCases.forEach(([source, expected]) => { + expect(isPlainObject(source)).toBe(expected) + }) + }) +})