Skip to content

Commit

Permalink
fix(helper/ssg): fix bug of joinPaths (#2809)
Browse files Browse the repository at this point in the history
* fix bug of joinPaths

* chore: fomat & lint

* fix
  • Loading branch information
EdamAme-x authored May 28, 2024
1 parent 366f760 commit 7875a61
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/helper/ssg/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ describe('joinPath', () => {
expect(joinPaths('test', '...', 'test2')).toBe('test/.../test2') // single triple dot and single
expect(joinPaths('test', './test2', '.test3.')).toBe('test/test2/.test3.') // single and single with slash and single with dot
expect(joinPaths('test', '../', '.test2')).toBe('.test2') // single and parent and single with dot
expect(joinPaths('..', '..', 'test')).toBe('../../test') // parent and parent and single
expect(joinPaths('..', '..')).toBe('../..') // parent and parent
expect(joinPaths('.test../test2/../')).toBe('.test..') //shuffle
expect(joinPaths('.test./.test2/../')).toBe('.test.') //shuffle2
})
Expand Down
13 changes: 8 additions & 5 deletions src/helper/ssg/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const normalizePath = (path: string): string => {
return path.replace(/(\\)/g, '/').replace(/\/$/g, '')
}

const handleDotDot = (resultPaths: string[]): void => {
if (resultPaths.length === 0) {
const handleParent = (resultPaths: string[], beforeParentFlag: boolean): void => {
if (resultPaths.length === 0 || beforeParentFlag) {
resultPaths.push('..')
} else {
resultPaths.pop()
Expand All @@ -33,13 +33,16 @@ const handleNonDot = (path: string, resultPaths: string[]): void => {
}

const handleSegments = (paths: string[], resultPaths: string[]): void => {
let beforeParentFlag = false
for (const path of paths) {
// Handle `..` or `../`
// Handle `..`
if (path === '..') {
handleDotDot(resultPaths)
handleParent(resultPaths, beforeParentFlag)
beforeParentFlag = true
} else {
// Handle `.` or `./`
// Handle `.` or `abc`
handleNonDot(path, resultPaths)
beforeParentFlag = false
}
}
}
Expand Down

0 comments on commit 7875a61

Please sign in to comment.