Skip to content

Commit

Permalink
add unit test byDateModified
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanimir committed Jun 19, 2024
1 parent 7b01995 commit d4bda0b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 63 deletions.
132 changes: 73 additions & 59 deletions __tests__/common/unit/misc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,88 +132,102 @@ describe("Common misc unit tests", () => {
});
test("misc.sortByCriteria", () => {
const items = [
{ name: "b", isFile: true, size: 30 },
{ name: "a", isFile: true, size: 20 },
{ name: "c", isFile: true, size: 10 },
{ name: "b", isFile: true, size: 30, lmdt: new Date('2024-06-30T00:00:00Z').getTime() },
{ name: "a", isFile: true, size: 20, lmdt: new Date('2024-06-20T00:00:00Z').getTime() },
{ name: "c", isFile: true, size: 10, lmdt: new Date('2024-06-10T00:00:00Z').getTime() },
{ name: "folder", isFile: false },
{ name: "0", isFile: true, size: 11 },
{ name: "2", isFile: true, size: 12 },
{ name: "1", isFile: true, size: 13 },
{ name: "02", isFile: true, size: 14 },
{ name: "01", isFile: true, size: 15 },
{ name: "10", isFile: true, size: 16 },
{ name: "0", isFile: true, size: 11, lmdt: new Date('2024-06-11T00:00:00Z').getTime() },
{ name: "2", isFile: true, size: 12, lmdt: new Date('2024-06-12T00:00:00Z').getTime() },
{ name: "1", isFile: true, size: 13, lmdt: new Date('2024-06-13T00:00:00Z').getTime() },
{ name: "02", isFile: true, size: 14, lmdt: new Date('2024-06-14T00:00:00Z').getTime() },
{ name: "01", isFile: true, size: 15, lmdt: new Date('2024-06-15T00:00:00Z').getTime() },
{ name: "10", isFile: true, size: 16, lmdt: new Date('2024-06-16T00:00:00Z').getTime() },
];
const reversedItems = [...items].reverse();
//asc
let output = misc.sortByCriteria(items, "byName", true);
expect(output).toEqual([
{ name: "folder", isFile: false },
{ name: "a", isFile: true, size: 20 },
{ name: "b", isFile: true, size: 30 },
{ name: "c", isFile: true, size: 10 },
{ name: "0", isFile: true, size: 11 },
{ name: "1", isFile: true, size: 13 },
{ name: "01", isFile: true, size: 15 },
{ name: "2", isFile: true, size: 12 },
{ name: "02", isFile: true, size: 14 },
{ name: "10", isFile: true, size: 16 },
items.find(i => i.name === 'folder'),
items.find(i => i.name === 'a'),
items.find(i => i.name === 'b'),
items.find(i => i.name === 'c'),
items.find(i => i.name === '0'),
items.find(i => i.name === '1'),
items.find(i => i.name === '01'),
items.find(i => i.name === '2'),
items.find(i => i.name === '02'),
items.find(i => i.name === '10'),
]);
//asc reverse
output = misc.sortByCriteria(reversedItems, "byName", true);
expect(output).toEqual([
{ name: "folder", isFile: false },
{ name: "0", isFile: true, size: 11 },
{ name: "01", isFile: true, size: 15 },
{ name: "1", isFile: true, size: 13 },
{ name: "02", isFile: true, size: 14 },
{ name: "2", isFile: true, size: 12 },
{ name: "10", isFile: true, size: 16 },
{ name: "a", isFile: true, size: 20 },
{ name: "b", isFile: true, size: 30 },
{ name: "c", isFile: true, size: 10 },
items.find(i => i.name === 'folder'),
items.find(i => i.name === '0'),
items.find(i => i.name === '01'),
items.find(i => i.name === '1'),
items.find(i => i.name === '02'),
items.find(i => i.name === '2'),
items.find(i => i.name === '10'),
items.find(i => i.name === 'a'),
items.find(i => i.name === 'b'),
items.find(i => i.name === 'c'),
]);
//desc
output = misc.sortByCriteria(items, "byName", false);
expect(output).toEqual([
{ name: "10", isFile: true, size: 16 },
{ name: "02", isFile: true, size: 14 },
{ name: "2", isFile: true, size: 12 },
{ name: "01", isFile: true, size: 15 },
{ name: "1", isFile: true, size: 13 },
{ name: "0", isFile: true, size: 11 },
{ name: "c", isFile: true, size: 10 },
{ name: "b", isFile: true, size: 30 },
{ name: "a", isFile: true, size: 20 },
{ name: "folder", isFile: false },
items.find(i => i.name === '10'),
items.find(i => i.name === '02'),
items.find(i => i.name === '2'),
items.find(i => i.name === '01'),
items.find(i => i.name === '1'),
items.find(i => i.name === '0'),
items.find(i => i.name === 'c'),
items.find(i => i.name === 'b'),
items.find(i => i.name === 'a'),
items.find(i => i.name === 'folder'),
]);

//desc reversed
output = misc.sortByCriteria(reversedItems, "byName", false);
expect(output).toEqual([
{ name: "c", isFile: true, size: 10 },
{ name: "b", isFile: true, size: 30 },
{ name: "a", isFile: true, size: 20 },
{ name: "10", isFile: true, size: 16 },
{ name: "2", isFile: true, size: 12 },
{ name: "02", isFile: true, size: 14 },
{ name: "1", isFile: true, size: 13 },
{ name: "01", isFile: true, size: 15 },
{ name: "0", isFile: true, size: 11 },
{ name: "folder", isFile: false },
items.find(i => i.name === 'c'),
items.find(i => i.name === 'b'),
items.find(i => i.name === 'a'),
items.find(i => i.name === '10'),
items.find(i => i.name === '2'),
items.find(i => i.name === '02'),
items.find(i => i.name === '1'),
items.find(i => i.name === '01'),
items.find(i => i.name === '0'),
items.find(i => i.name === 'folder'),
]);

output = misc.sortByCriteria(items, "byFileSize", true);
expect(output).toEqual([
{ name: "folder", isFile: false },
{ name: "c", isFile: true, size: 10 },
{ name: "0", isFile: true, size: 11 },
{ name: "2", isFile: true, size: 12 },
{ name: "1", isFile: true, size: 13 },
{ name: "02", isFile: true, size: 14 },
{ name: "01", isFile: true, size: 15 },
{ name: "10", isFile: true, size: 16 },
{ name: "a", isFile: true, size: 20 },
{ name: "b", isFile: true, size: 30 },
items.find(i => i.name === 'folder'),
items.find(i => i.name === 'c'),
items.find(i => i.name === '0'),
items.find(i => i.name === '2'),
items.find(i => i.name === '1'),
items.find(i => i.name === '02'),
items.find(i => i.name === '01'),
items.find(i => i.name === '10'),
items.find(i => i.name === 'a'),
items.find(i => i.name === 'b'),
]);

output = misc.sortByCriteria(items, "byDateModified", true);
expect(output).toEqual([
items.find(i => i.name === 'folder'),
items.find(i => i.name === 'c'),
items.find(i => i.name === '0'),
items.find(i => i.name === '2'),
items.find(i => i.name === '1'),
items.find(i => i.name === '02'),
items.find(i => i.name === '01'),
items.find(i => i.name === '10'),
items.find(i => i.name === 'a'),
items.find(i => i.name === 'b'),
]);
});
});
12 changes: 10 additions & 2 deletions packages/common/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,14 +514,22 @@ function sortBySize(a, b) {
return aSize - bSize;
}

function getTimestamp(value) {
if (value === undefined) {
return 0; // Default to epoch start if undefined
}
return value; // Already a timestamp
}

/**
* @param a: TS.FileSystemEntry
* @param b: TS.FileSystemEntry
* @returns {number}
*/
function sortByDateModified(a, b) {
const aLmdt = a.lmdt | 0;
const bLmdt = b.lmdt | 0;

const aLmdt = getTimestamp(a.lmdt);
const bLmdt = getTimestamp(b.lmdt);
return aLmdt - bLmdt;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"license": "MIT",
"scripts": {
"clean-install": "npx rimraf package-lock.json && npx rimraf node_modules && npm install",
"npmpack-ts": "npm run clean-install && npm pack && tar -xvf tagspaces-tagspaces-common-4.3.3.tgz && npx shx cp -rL ./package/** ../../../tagspaces/node_modules/@tagspaces/tagspaces-common && npx rimraf package",
"npmpack-ts-main": "npm run clean-install && npm pack && tar -xvf tagspaces-tagspaces-common-4.3.3.tgz && npx shx cp -rL ./package/** ../../../tagspaces/release/app/node_modules/@tagspaces/tagspaces-common && npx rimraf package",
"npmpack-ts": "npm run clean-install && npm pack && tar -xvf tagspaces-tagspaces-common-4.3.23.tgz && npx shx cp -rL ./package/** ../../../tagspaces/node_modules/@tagspaces/tagspaces-common && npx rimraf package",
"npmpack-ts-main": "npm run clean-install && npm pack && tar -xvf tagspaces-tagspaces-common-4.3.23.tgz && npx shx cp -rL ./package/** ../../../tagspaces/release/app/node_modules/@tagspaces/tagspaces-common && npx rimraf package",
"npmpack-webdav": "npm run clean-install && npm pack && tar -xvf tagspaces-tagspaces-common-1.3.13.tgz && npx shx cp -rL ./package/** ../common-webdav/node_modules/@tagspaces/tagspaces-common && npx rimraf package",
"pkg-publish": "npm run clean-install && npm publish --access public"
},
Expand Down

0 comments on commit d4bda0b

Please sign in to comment.