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

feat: Add sortNodes and generic orderBy #961

Merged
merged 2 commits into from
May 23, 2024
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest'

import { formatFileSize, parseFileSize } from '../lib/humanfilesize'
import { formatFileSize, parseFileSize } from '../../lib/index'

describe('humanFileSize', () => {
describe('formatFileSize', () => {
Expand Down
196 changes: 196 additions & 0 deletions __tests__/utils/fileSorting.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { ArgumentsType, describe, expect, test } from 'vitest'
import { File, FilesSortingMode, Folder, sortNodes as originalSortNodes } from '../../lib'

const file = (name: string, size?: number, modified?: number, favorite = false) => new File({
source: `https://cloud.domain.com/remote.php/dav/${name}`,
mime: 'text/plain',
owner: 'jdoe',
mtime: new Date(modified ?? Date.now()),
size,
attributes: favorite
? {
favorite: 1,
}
: undefined,
})

const folder = (name: string, size?: number, modified?: number, favorite = false) => new Folder({
source: `https://cloud.domain.com/remote.php/dav/${name}`,
owner: 'jdoe',
mtime: new Date(modified ?? Date.now()),
size,
attributes: favorite
? {
favorite: 1,
}
: undefined,
})

const sortNodes = (...args: ArgumentsType<typeof originalSortNodes>) => originalSortNodes(...args).map((node) => node.basename)

describe('sortNodes', () => {
test('By default files are sorted by name', () => {
const array = [
file('a', 500, 100),
file('b', 100, 100),
file('c', 100, 500),
]

expect(sortNodes(array)).toEqual(['a', 'b', 'c'])
})

test('By default folders are handled like files', () => {
const array = [
file('a', 500, 100),
folder('b', 100, 100),
file('c', 100, 500),
]

expect(sortNodes(array)).toEqual(['a', 'b', 'c'])
})

test('By default favorites are not handled special', () => {
const array = [
file('a', 500, 100),
file('b', 100, 100, true),
file('c', 100, 500),
]

expect(sortNodes(array)).toEqual(['a', 'b', 'c'])
})

test('If a display name is available the displayname is prefered over the basename', () => {
const array = [
// File with basename "d" but displayname "a"
new File({
owner: 'jdoe',
mime: 'text/plain',
// Resulting in name "d"
source: 'https://cloud.domain.com/remote.php/dav/d',
mtime: new Date(100),
size: 100,
attributes: {
displayName: 'a',
},
}),
file('b', 100, 100),
file('c', 100, 500),
]

expect(sortNodes(array)).toEqual(['d', 'b', 'c'])
})

test('Can sort favorites first', () => {
const array = [
file('a', 500, 100),
file('b', 100, 100, true),
file('c', 100, 500),
]

expect(sortNodes(array, { sortFavoritesFirst: true })).toEqual(['b', 'a', 'c'])
})

test('Can sort folders first', () => {
const array = [
file('a', 500, 100),
folder('b', 100, 100),
file('c', 100, 500),
]

expect(sortNodes(array, { sortFoldersFirst: true })).toEqual(['b', 'a', 'c'])
})

test('Can sort folders first when sorting descending', () => {
const array = [
file('a', 500, 100),
folder('b', 100, 100),
file('c', 100, 500),
]

expect(sortNodes(array, { sortFoldersFirst: true, sortingOrder: 'desc' })).toEqual(['b', 'c', 'a'])
})

test('Can sort favorite folders first', () => {
const array = [
file('a', 500, 100),
folder('b', 100, 100),
folder('c', 100, 500, true),
]

expect(sortNodes(array, { sortFoldersFirst: true, sortFavoritesFirst: true })).toEqual(['c', 'b', 'a'])
})

test('Can sort favorites before folders', () => {
const array = [
file('a', 500, 100),
folder('b', 100, 100),
file('c', 100, 500, true),
]

expect(sortNodes(array, { sortFoldersFirst: true, sortFavoritesFirst: true })).toEqual(['c', 'b', 'a'])
})

test('Sort nodes (with default mode) descending', () => {
const array = [
file('a', 500, 100),
file('b', 100, 100),
file('c', 100, 500),
]

expect(sortNodes(array, { sortingOrder: 'desc' })).toEqual(['c', 'b', 'a'])
})

test('Sort nodes by name descending', () => {
const array = [
file('a', 500, 100),
file('b', 100, 100),
file('c', 100, 500),
]

expect(sortNodes(array, { sortingOrder: 'desc', sortingMode: FilesSortingMode.Name })).toEqual(['c', 'b', 'a'])
})

test('Sort nodes by size', () => {
const array = [
file('a', 500, 100),
folder('b', 150, 100),
file('c', 100, 500),
]

expect(sortNodes(array, { sortingMode: FilesSortingMode.Size })).toEqual(['c', 'b', 'a'])
})

test('Sort nodes by size descending', () => {
const array = [
file('a', 500, 100),
folder('b', 150, 100),
file('c', 100, 500),
]

expect(sortNodes(array, { sortingMode: FilesSortingMode.Size, sortingOrder: 'desc' })).toEqual(['a', 'b', 'c'])
})

test('Sort nodes by mtime', () => {
const array = [
file('a', 500, 100),
folder('b', 100, 250),
file('c', 100, 500),
]

expect(sortNodes(array, { sortingMode: FilesSortingMode.Modified })).toEqual(['c', 'b', 'a'])
})

test('Sort nodes by mtime descending', () => {
const array = [
file('a', 500, 100),
folder('b', 100, 250),
file('c', 100, 500),
]

expect(sortNodes(array, { sortingMode: FilesSortingMode.Modified, sortingOrder: 'desc' })).toEqual(['a', 'b', 'c'])
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ describe('isFilenameValid', () => {
})

it('works for valid filenames', async () => {
const { isFilenameValid } = await import('../lib/index')
const { isFilenameValid } = await import('../../lib/index')

expect(isFilenameValid('foo.bar')).toBe(true)
})

it('has fallback invalid characters', async () => {
const { isFilenameValid } = await import('../lib/index')
const { isFilenameValid } = await import('../../lib/index')

expect(isFilenameValid('foo\\bar')).toBe(false)
expect(isFilenameValid('foo/bar')).toBe(false)
})

it('reads invalid characters from oc config', async () => {
window._oc_config = { forbidden_filenames_characters: ['=', '?'] }
const { isFilenameValid } = await import('../lib/index')
const { isFilenameValid } = await import('../../lib/index')

expect(isFilenameValid('foo.bar')).toBe(true)
expect(isFilenameValid('foo=bar')).toBe(false)
Expand All @@ -35,7 +35,7 @@ describe('isFilenameValid', () => {

it('supports invalid filename regex', async () => {
window._oc_config = { forbidden_filenames_characters: ['/'], blacklist_files_regex: '\\.(part|filepart)$' }
const { isFilenameValid } = await import('../lib/index')
const { isFilenameValid } = await import('../../lib/index')

expect(isFilenameValid('foo.bar')).toBe(true)
expect(isFilenameValid('foo.part')).toBe(false)
Expand Down
115 changes: 115 additions & 0 deletions __tests__/utils/sorting.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { describe, expect, test } from 'vitest'
import { orderBy } from '../../lib/utils/sorting'

describe('orderBy', () => {
test('By default the identify and ascending order is used', () => {
const array = ['a', 'z', 'b']
expect(orderBy(array)).toEqual(['a', 'b', 'z'])
})

test('Use identifiy but descending', () => {
const array = ['a', 'z', 'b']
expect(orderBy(array, undefined, ['desc'])).toEqual(['z', 'b', 'a'])
})

test('Can set identifier function', () => {
const array = [
{ text: 'a', order: 2 },
{ text: 'z', order: 1 },
{ text: 'b', order: 3 },
] as const
expect(orderBy(array, [(v) => v.order]).map((v) => v.text)).toEqual(['z', 'a', 'b'])
})

test('Can set multiple identifier functions', () => {
const array = [
{ text: 'a', order: 2, secondOrder: 2 },
{ text: 'z', order: 1, secondOrder: 3 },
{ text: 'b', order: 2, secondOrder: 1 },
] as const
expect(orderBy(array, [(v) => v.order, (v) => v.secondOrder]).map((v) => v.text)).toEqual(['z', 'b', 'a'])
})

test('Can set order partially', () => {
const array = [
{ text: 'a', order: 2, secondOrder: 2 },
{ text: 'z', order: 1, secondOrder: 3 },
{ text: 'b', order: 2, secondOrder: 1 },
] as const

expect(
orderBy(
array,
[(v) => v.order, (v) => v.secondOrder],
['desc'],
).map((v) => v.text),
).toEqual(['b', 'a', 'z'])
})

test('Can set order array', () => {
const array = [
{ text: 'a', order: 2, secondOrder: 2 },
{ text: 'z', order: 1, secondOrder: 3 },
{ text: 'b', order: 2, secondOrder: 1 },
] as const

expect(
orderBy(
array,
[(v) => v.order, (v) => v.secondOrder],
['desc', 'desc'],
).map((v) => v.text),
).toEqual(['a', 'b', 'z'])
})

test('Numbers are handled correctly', () => {
const array = [
{ text: '2.3' },
{ text: '2.10' },
{ text: '2.0' },
{ text: '2.2' },
] as const

expect(
orderBy(
array,
[(v) => v.text],
).map((v) => v.text),
).toEqual(['2.0', '2.2', '2.3', '2.10'])
})

test('Numbers with suffixes are handled correctly', () => {
const array = [
{ text: '2024-01-05' },
{ text: '2024-05-01' },
{ text: '2024-01-10' },
{ text: '2024-01-05 Foo' },
] as const

expect(
orderBy(
array,
[(v) => v.text],
).map((v) => v.text),
).toEqual(['2024-01-05', '2024-01-05 Foo', '2024-01-10', '2024-05-01'])
})

test('Dates are handled correctly', () => {
const array = [
{ text: 'monday', date: new Date(1716212366 * 1000) },
{ text: 'wednesday', date: new Date(1716385166 * 1000) },
{ text: 'tuesday', date: new Date(1716298766 * 1000) },
]

expect(
orderBy(
array,
[(v) => v.date],
).map((v) => v.text),
).toEqual(['monday', 'tuesday', 'wednesday'])
})
})
7 changes: 5 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import { type Entry, getNewFileMenu } from './newFileMenu'
import { type Folder } from './files/folder'

export { formatFileSize, parseFileSize } from './humanfilesize'
export { FileAction, getFileActions, registerFileAction, DefaultType } from './fileAction'
export { Header, getFileListHeaders, registerFileListHeaders } from './fileListHeaders'
export { type Entry } from './newFileMenu'
Expand All @@ -38,7 +37,11 @@ export { FileType } from './files/fileType'
export { File } from './files/file'
export { Folder } from './files/folder'
export { Node, NodeStatus } from './files/node'
export { isFilenameValid } from './filename'

export { isFilenameValid } from './utils/filename'
export { formatFileSize, parseFileSize } from './utils/fileSize'
export { orderBy } from './utils/sorting'
export { sortNodes, FilesSortingMode, type FilesSortingOptions } from './utils/fileSorting'

export * from './navigation/navigation'
export * from './navigation/column'
Expand Down
File renamed without changes.
Loading
Loading