-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: marked all manifests that are loaded as frozen
- Loading branch information
Showing
8 changed files
with
199 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Recursively freezes an object and all of its properties. This prevents the | ||
* object from being modified at runtime. When the JS runtime is running in | ||
* strict mode, any attempts to modify a frozen object will throw an error. | ||
* | ||
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze | ||
* @param obj The object to freeze. | ||
*/ | ||
export function freeze(obj: object): void { | ||
// `null` is an object, if we get this, we should just return it. | ||
if (obj === null) return | ||
|
||
// An array is an object, but we also want to freeze each element in the array | ||
// as well. | ||
if (Array.isArray(obj)) { | ||
for (const item of obj) { | ||
if (!item || typeof item !== 'object') continue | ||
freeze(item) | ||
} | ||
|
||
Object.freeze(obj) | ||
return | ||
} | ||
|
||
for (const name of Object.keys(obj)) { | ||
const value = obj[name as keyof typeof obj] | ||
|
||
if (!value || typeof value !== 'object') continue | ||
freeze(value) | ||
} | ||
|
||
Object.freeze(obj) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { toRoute } from './to-route' | ||
|
||
describe('toRoute Function', () => { | ||
it('should remove trailing slash', () => { | ||
const result = toRoute('/example/') | ||
expect(result).toBe('/example') | ||
}) | ||
|
||
it('should remove trailing `/index`', () => { | ||
const result = toRoute('/example/index') | ||
expect(result).toBe('/example') | ||
}) | ||
|
||
it('should return `/` when input is `/index`', () => { | ||
const result = toRoute('/index') | ||
expect(result).toBe('/') | ||
}) | ||
|
||
it('should return `/` when input is `/index/`', () => { | ||
const result = toRoute('/index/') | ||
expect(result).toBe('/') | ||
}) | ||
|
||
it('should return `/` when input is only a slash', () => { | ||
const result = toRoute('/') | ||
expect(result).toBe('/') | ||
}) | ||
|
||
it('should return `/` when input is empty', () => { | ||
const result = toRoute('') | ||
expect(result).toBe('/') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* This transforms a URL pathname into a route. It removes any trailing slashes | ||
* and the `/index` suffix. | ||
* | ||
* @param {string} pathname - The URL path that needs to be optimized. | ||
* @returns {string} - The route | ||
* | ||
* @example | ||
* // returns '/example' | ||
* toRoute('/example/index/'); | ||
* | ||
* @example | ||
* // returns '/example' | ||
* toRoute('/example/'); | ||
* | ||
* @example | ||
* // returns '/' | ||
* toRoute('/index/'); | ||
* | ||
* @example | ||
* // returns '/' | ||
* toRoute('/'); | ||
*/ | ||
export function toRoute(pathname: string): string { | ||
return pathname.replace(/(?:\/index)?\/?$/, '') || '/' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters