Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix getParentPath with content name like Test(1) #949

Merged
merged 7 commits into from
Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/sn-client-utils/src/path-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class PathHelper {
* @param path The path to be splitted
*/
public static getSegments(path: string) {
if (!path) {
if (!path || path.startsWith('content') || path.startsWith('/content')) {
return []
}
// Split path at / and remove empty strings
Expand All @@ -34,7 +34,7 @@ export class PathHelper {
throw new Error(`Couldn't get the segments for ${path}`)
}
// Match if last item is Root/content(123) or Root/Example('content')
const matches = lastItem.match(/(\('\w+'\)|\(\d+\)$)/g)
const matches = lastItem.match(/(\('\w+'\)$)/g)
if (!matches) {
return [...splitted, lastItem]
}
Expand All @@ -44,11 +44,11 @@ export class PathHelper {
}

/**
* Checks if a specific segment is an Item segment or not (like "('Content1')"" or "(654)")
* Checks if a specific segment is an Item segment or not (like "('Content1')" or "Test(1)")
* @param segment The segment to be examined
*/
public static isItemSegment(segment: string): boolean {
return RegExp(/\('+[\s\S]+'\)$/).test(segment) || RegExp(/\(+\d+\)$/).test(segment)
return RegExp(/\('+[\s\S]+'\)$/).test(segment) || RegExp(/\(+\w+\d+\)$/).test(segment)
}

/**
Expand Down
17 changes: 16 additions & 1 deletion packages/sn-client-utils/test/path-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,14 @@ export const pathHelperTests = describe('PathHelper', () => {
expect(PathHelper.getSegments("/Root/Example('Content1')")).toEqual(['Root', 'Example', "('Content1')"])
})
it('Should split the path to segments', () => {
expect(PathHelper.getSegments('/Root/Example(123)')).toEqual(['Root', 'Example', '(123)'])
expect(PathHelper.getSegments('/Root/Example(123)')).toEqual(['Root', 'Example(123)'])
})
it('Should split the path to segments', () => {
expect(PathHelper.getSegments('/Root/Example/100pages(3).pdf')).toEqual(['Root', 'Example', '100pages(3).pdf'])
})
it('Should split the path to segments', () => {
expect(PathHelper.getSegments('/content(123)')).toEqual([])
})
it('should throw an error if the path is /', () => {
expect(() => PathHelper.getSegments('/')).toThrow()
})
Expand All @@ -195,8 +198,20 @@ export const pathHelperTests = describe('PathHelper', () => {
expect(PathHelper.getParentPath("Root/Example('Content')")).toBe('Root/Example')
})

it('Should return the parent path in case of more than 1 segments with item path', () => {
expect(PathHelper.getParentPath('Root/Memo/Test(1)')).toBe('Root/Memo')
})

it('Should return the path in case of 1 segments', () => {
expect(PathHelper.getParentPath('Root')).toBe('Root')
})

it('Should return the path in case of 1 segments', () => {
expect(PathHelper.getParentPath('content(123)')).toBe('')
})

it('Should return the path in case of 1 segments', () => {
expect(PathHelper.getParentPath('/content(123)')).toBe('')
})
})
})