-
-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,3 +63,4 @@ typings/ | |
# dotenv environment variables file | ||
.env | ||
|
||
ueli.countstorage.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as fs from "fs"; | ||
import { CountManager } from "./../../ts/count-manager"; | ||
import { CountLoader } from "./../../ts/count-loader"; | ||
|
||
const testStorage = { | ||
"C:\\Path\\To\\Exe.exe": 10, | ||
"D:\\Test\\Path": 40, | ||
"E:\\Invalid\\Value": "lol", | ||
}; | ||
|
||
const testLoader = { | ||
loadCountFromCountFile: (): any => testStorage, | ||
writeCountFile: (): void => {/* do nothing */}, | ||
} as CountLoader; | ||
|
||
describe("CountManager", (): void => { | ||
const count = new CountManager(testLoader); | ||
|
||
it("Should set value of new path to 1", (): void => { | ||
count.addCount("F:\\New\\Path"); | ||
expect(count.getCount("F:\\New\\Path")).toBe(1); | ||
}); | ||
|
||
it("Should increase score of path to 11", (): void => { | ||
count.addCount("C:\\Path\\To\\Exe.exe"); | ||
expect(count.getCount("C:\\Path\\To\\Exe.exe")).toBe(11); | ||
}); | ||
|
||
it("Should keep score of path at 40", (): void => { | ||
count.addCount("D:\\Test\\Path"); | ||
expect(count.getCount("D:\\Test\\Path")).toBe(40); | ||
}); | ||
|
||
it("Should return an empty object after clearStorage", (): void => { | ||
count.clearStorage(); | ||
expect(Object.keys(count.getStorage()).length).toBe(0); | ||
}); | ||
}); | ||
|
||
describe("CountLoader", (): void => { | ||
const testCountFilePath = "./test.countstorage.json"; | ||
const loader = new CountLoader(testCountFilePath); | ||
|
||
it("Should return an empty object", (): void => { | ||
const storage = loader.loadCountFromCountFile(); | ||
expect(Object.keys(storage).length).toBe(0); | ||
}); | ||
|
||
afterAll((): void => { | ||
fs.unlink(testCountFilePath); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import * as fs from "fs"; | ||
import * as os from "os"; | ||
import * as path from "path"; | ||
|
||
export interface Count { | ||
[key: string]: number; | ||
} | ||
|
||
let countFilePath = path.join(process.cwd(), "ueli.countstorage.json"); | ||
|
||
export class CountLoader { | ||
public constructor(filePath?: string) { | ||
if (filePath !== undefined) { | ||
countFilePath = filePath; | ||
} | ||
} | ||
|
||
public loadCountFromCountFile(): Count { | ||
try { | ||
const fileContent = fs.readFileSync(countFilePath, "utf-8"); | ||
const parsed = JSON.parse(fileContent) as Count; | ||
return parsed; | ||
} catch (err) { | ||
fs.writeFileSync(countFilePath, "{}", "utf-8"); | ||
return {}; | ||
} | ||
} | ||
|
||
public writeCountFile(storage: Count): void { | ||
const stringifiedStorage = JSON.stringify(storage); | ||
fs.writeFileSync(countFilePath, stringifiedStorage, "utf-8"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import * as path from "path"; | ||
import { CountLoader, Count } from "./count-loader"; | ||
|
||
let countLoader = new CountLoader(); | ||
let countStorage = countLoader.loadCountFromCountFile(); | ||
|
||
export class CountManager { | ||
public constructor(loader?: CountLoader) { | ||
if (loader !== undefined) { | ||
countLoader = loader; | ||
countStorage = loader.loadCountFromCountFile(); | ||
} | ||
} | ||
|
||
public getCount(key: string): number { | ||
const score = countStorage[key]; | ||
if (isNaN(score)) { | ||
return 0; | ||
} else { | ||
return score; | ||
} | ||
} | ||
|
||
public addCount(key: string): void { | ||
let score = this.getCount(key); | ||
score++; | ||
if (score <= 40) { | ||
countStorage[key] = score; | ||
countLoader.writeCountFile(countStorage); | ||
} | ||
} | ||
|
||
public removeCountKey(key: string): void { | ||
delete countStorage[key]; | ||
countLoader.writeCountFile(countStorage); | ||
} | ||
|
||
public getStorage(): Count { | ||
return countStorage; | ||
} | ||
|
||
public clearStorage(): void { | ||
countStorage = {} as Count; | ||
countLoader.writeCountFile(countStorage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters