Skip to content

Commit

Permalink
fix: Optimize and modernize code
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed Jan 29, 2024
1 parent d55dbc6 commit e0becaf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
14 changes: 14 additions & 0 deletions __tests__/paths.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ describe('Path generation', () => {
beforeEach(() => {
window._oc_webroot = ''
window._oc_appswebroots = { forms: '/apps-extra/forms' }

window.OC = {
coreApps: ['', 'admin', 'log', 'core/search', 'core', '3rdparty'],
}
})

test('missing core apps global variable', () => {
delete window.OC.coreApps
expect(generateFilePath('forms', '', 'file.js')).toBe('/apps-extra/forms/file.js')
})

test('missing OC global variable (unit tests)', () => {
delete window.OC
expect(generateFilePath('forms', '', 'file.js')).toBe('/apps-extra/forms/file.js')
})

test('non core PHP index file', () => {
Expand Down
19 changes: 8 additions & 11 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export const imagePath = (app: string, file: string) => {
* @return {string} URL with webroot for a file in an app
*/
export const generateFilePath = (app: string, type: string, file: string) => {
const isCore = window?.OC?.coreApps?.indexOf(app) !== -1
const isCore = window?.OC?.coreApps?.includes(app) ?? false
const isPHP = file.slice(-3) === 'php'
let link = getRootUrl()
if (isPHP && !isCore) {
Expand All @@ -176,26 +176,23 @@ export const generateFilePath = (app: string, type: string, file: string) => {
} else if (!isPHP && !isCore) {
link = getAppRootUrl(app)
if (type) {
link += '/' + type + '/'
link += `/${type}/`
}
if (link.substring(link.length - 1) !== '/') {
if (link.at(-1) !== '/') {
link += '/'
}
link += file
} else {
if ((app === 'settings' || app === 'core' || app === 'search') && type === 'ajax') {
link += '/index.php/'
} else {
link += '/'
link += '/index.php'
}
if (app !== '') {
app += '/'
link += app
if (app) {
link += `/${app}`
}
if (type) {
link += type + '/'
link += `/${type}`
}
link += file
link += `/${file}`
}
return link
}
Expand Down

0 comments on commit e0becaf

Please sign in to comment.