Skip to content

Commit

Permalink
Merge branch 'main' into client-file
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope authored May 21, 2024
2 parents 3617255 + 90a11d9 commit 7dd7cf0
Show file tree
Hide file tree
Showing 22 changed files with 59 additions and 57 deletions.
19 changes: 7 additions & 12 deletions packages/core/src/app/prepare/prepareRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,15 @@ const resolvePageRedirects = ({ path, pathInferred }: Page): string[] => {
// paths that should redirect to this page, use set to dedupe
const redirectsSet = new Set<string>()

// add redirect to the set when the redirect could not be normalized & encoded to the page path
const addRedirect = (redirect: string): void => {
const normalizedPath = normalizeRoutePath(redirect)
if (normalizedPath === path) return

const encodedPath = encodeURI(normalizedPath)
if (encodedPath === path) return

redirectsSet.add(redirect)
}

// redirect from inferred path, notice that the inferred path is not uri-encoded
if (pathInferred !== null) {
addRedirect(encodeURI(pathInferred))
const normalizedPathInferred = normalizeRoutePath(pathInferred)
const encodedPathInferred = encodeURI(normalizedPathInferred)

// add redirect to the set when the redirect could not be normalized & encoded to the page path
if (normalizedPathInferred !== path && encodedPathInferred !== path) {
redirectsSet.add(encodedPathInferred)
}
}

return Array.from(redirectsSet)
Expand Down
12 changes: 3 additions & 9 deletions packages/shared/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -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'
6 changes: 0 additions & 6 deletions packages/shared/src/utils/isPlainObject.ts

This file was deleted.

3 changes: 3 additions & 0 deletions packages/shared/src/utils/links/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './isLinkExternal.js'
export * from './isLinkHttp.js'
export * from './isLinkWithProtocol.js'
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions packages/shared/src/utils/routes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './inferRoutePath'
export * from './normalizeRoutePath.js'
export * from './resolveLocalePath.js'
export * from './resolveRoutePathFromUrl.js'
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 7 additions & 0 deletions packages/shared/src/utils/typeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <T extends Record<any, any> = Record<any, any>>(
val: unknown,
): val is T => Object.prototype.toString.call(val) === '[object Object]'

/**
* Check if a value is a string
*/
Expand Down
21 changes: 0 additions & 21 deletions packages/shared/tests/isPlainObject.spec.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, it } from 'vitest'
import { isLinkExternal } from '../src/index.js'
import { isLinkExternal } from '../../src/index.js'

const testCases: [
Parameters<typeof isLinkExternal>,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<typeof isLinkHttp>][] = [
['https://foobar.com', true],
Expand Down
Original file line number Diff line number Diff line change
@@ -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<typeof isLinkWithProtocol>][] = [
// with protocol
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = {
'/': {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, it } from 'vitest'
import { resolveRoutePathFromUrl } from '../src/index.js'
import { resolveRoutePathFromUrl } from '../../src/index.js'

const testCases: [
Parameters<typeof resolveRoutePathFromUrl>,
Expand Down
23 changes: 23 additions & 0 deletions packages/shared/tests/typeGuards.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
})

0 comments on commit 7dd7cf0

Please sign in to comment.