Skip to content

Commit

Permalink
Decouple getCreationDates & getModificationDates from Obsidian datatypes
Browse files Browse the repository at this point in the history
Make it easier to maintain and test
  • Loading branch information
saertna committed Jun 12, 2024
1 parent 0babd04 commit 2515a60
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/creatmodchartcalculation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export function findEarliestModifiedFile<T extends FileInterface>(files: T[]): T
}


export function findEarliestDateFile(files: TFile[]): TFile {
let earliestCreatedFile: TFile = files[0];
export function findEarliestDateFile<T extends FileInterface>(files: T[]): T {
let earliestCreatedFile: T = files[0];
for (const file of files) {
if (file.stat.ctime < earliestCreatedFile.stat.ctime) {
earliestCreatedFile = file;
Expand All @@ -53,25 +53,21 @@ export function monthsBetween(startMonth: Date, endMonth: Date): number {
}


export function getCreationDates(files: TFile[]): Array<Date> {
export function getCreationDates<T extends FileInterface>(files: T[]): Array<Date> {
const creationDates: Array<Date> = [];

for (const file of files) {
creationDates.push(new Date(file.stat.ctime));
}

return creationDates;
}


export function getModificationDates(files: TFile[]): Array<Date> {
const creationDates: Array<Date> = [];

export function getModificationDates<T extends FileInterface>(files: T[]): Array<Date> {
const modificationDates: Array<Date> = [];
for (const file of files) {
creationDates.push(new Date(file.stat.mtime));
modificationDates.push(new Date(file.stat.mtime));
}

return creationDates;
return modificationDates;
}


Expand Down
62 changes: 62 additions & 0 deletions tests/creatmodchartcalculation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ describe('findEarliestModifiedFile', () => {
});
});

describe('findEarliestDateFile', () => {
it('should return the file with the earliest date for creation or modification time', () => {
const result = findEarliestDateFile(files);
expect(result).toEqual(files[1]);
});

it('should handle an empty array', () => {
const result = findEarliestDateFile([]);
expect(result).toBeUndefined();
});

it('should handle a single element array', () => {
const singleFile = [files[0]];
const result = findEarliestDateFile(singleFile);
expect(result).toEqual(files[0]);
});
});

describe('monthsBetween', () => {
it('should return how many month are between March 22 and July 23', () => {
const actual = monthsBetween(new Date(2022,3), new Date(2023,6));
Expand All @@ -73,6 +91,50 @@ describe('monthsBetween', () => {
});


describe('getCreationDates', () => {
it('should return an array of creation dates', () => {
const result = getCreationDates(files);
expect(result).toEqual([
new Date(1625234672000),
new Date(1625233672000),
new Date(1625235672000)
]);
});

it('should handle an empty array', () => {
const result = getCreationDates([]);
expect(result).toEqual([]);
});

it('should handle a single element array', () => {
const singleFile = [files[0]];
const result = getCreationDates(singleFile);
expect(result).toEqual([new Date(1625234672000)]);
});
});

describe('getModificationDates', () => {
it('should return an array of modification dates', () => {
const result = getModificationDates(files);
expect(result).toEqual([
new Date(1625234672000),
new Date(1625233672000),
new Date(1625235672000)
]);
});

it('should handle an empty array', () => {
const result = getModificationDates([]);
expect(result).toEqual([]);
});

it('should handle a single element array', () => {
const singleFile = [files[0]];
const result = getModificationDates(singleFile);
expect(result).toEqual([new Date(1625234672000)]);
});
});

describe('createChartFormat', () => {
it('should return chart full length', () => {
const actual = createChartFormat("Jan 22, Feb 22, Mar 22, April 22", "0, 1, 2, 3", 0);
Expand Down

0 comments on commit 2515a60

Please sign in to comment.