Skip to content

Commit ce7d35c

Browse files
author
Topi Santakivi
committed
o1-preview's refactoring
1 parent 4438413 commit ce7d35c

File tree

4 files changed

+101
-87
lines changed

4 files changed

+101
-87
lines changed

app.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import BookServiceManagerFactoryImpl from "./bookService";
1+
import BookService from "./bookService";
22
import { performance } from "perf_hooks";
33

44
class EnterpriseBookManagementSystem {
@@ -8,7 +8,7 @@ class EnterpriseBookManagementSystem {
88

99
public static async executeBookManagementWorkflow(): Promise<void> {
1010
console.log("Initializing Enterprise Book Management System...");
11-
const bookService = BookServiceManagerFactoryImpl;
11+
const bookService = BookService.getInstance();
1212

1313
// Create some initial books
1414
for (let i = 0; i < 10; i++) {
@@ -20,7 +20,7 @@ class EnterpriseBookManagementSystem {
2020
}
2121

2222
// Perform some enterprise transformations
23-
const allBooks = bookService.bks;
23+
const allBooks = bookService.getAllBooks();
2424
for (let i = 0; i < allBooks.length; i++) {
2525
if (i % 2 === 0) {
2626
bookService.performEnterpriseBookTransformation(
@@ -32,10 +32,12 @@ class EnterpriseBookManagementSystem {
3232

3333
// Merge books if we have too many
3434
while (
35-
bookService.bks.length > EnterpriseBookManagementSystem.MERGE_THRESHOLD
35+
bookService.getAllBooks().length >
36+
EnterpriseBookManagementSystem.MERGE_THRESHOLD
3637
) {
37-
const id1 = bookService.bks[0].id;
38-
const id2 = bookService.bks[1].id;
38+
const books = bookService.getAllBooks();
39+
const id1 = books[0].id;
40+
const id2 = books[1].id;
3941
console.log(`Merging books ${id1} and ${id2}...`);
4042
bookService.mergeBooks(id1, id2);
4143
}
@@ -46,7 +48,9 @@ class EnterpriseBookManagementSystem {
4648

4749
while (complexity < EnterpriseBookManagementSystem.OPTIMIZATION_THRESHOLD) {
4850
const randomBookId =
49-
bookService.bks[Math.floor(Math.random() * bookService.bks.length)].id;
51+
bookService["books"][
52+
Math.floor(Math.random() * bookService["books"].length)
53+
].id;
5054
bookService.performEnterpriseBookTransformation(
5155
randomBookId,
5256
EnterpriseBookManagementSystem.TRANSFORMATION_INTENSITY

bookService.test.ts

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect, beforeEach, vi } from "vitest";
2-
import BookServiceManagerFactoryImpl from "./bookService";
2+
import BookService from "./bookService";
33
import * as fs from "fs";
44

55
vi.mock("fs");
@@ -14,9 +14,8 @@ describe("BookServiceManagerFactoryImpl", () => {
1414

1515
beforeEach(() => {
1616
vi.clearAllMocks();
17-
bookService = BookServiceManagerFactoryImpl;
18-
bookService["bks"] = [];
19-
bookService["i"] = 0;
17+
bookService = BookService.getInstance();
18+
bookService["books"] = [];
2019
bookService["optimizationFactor"] = 42;
2120
});
2221

@@ -27,11 +26,11 @@ describe("BookServiceManagerFactoryImpl", () => {
2726
"Test Author",
2827
"Test-ISBN"
2928
);
30-
expect(bookService["bks"]).toHaveLength(1);
31-
expect(bookService["bks"][0]).toEqual({
32-
t: "Test Title",
33-
a: "Test Author",
34-
ib: "Test-ISBN",
29+
expect(bookService["books"]).toHaveLength(1);
30+
expect(bookService["books"][0]).toEqual({
31+
title: "Test Title",
32+
author: "Test Author",
33+
isbn: "Test-ISBN",
3534
id: "mocked-id",
3635
});
3736
expect(fs.writeFileSync).toHaveBeenCalled();
@@ -40,60 +39,65 @@ describe("BookServiceManagerFactoryImpl", () => {
4039

4140
describe("updateBookEntityObject", () => {
4241
it("should update an existing book", () => {
43-
bookService["bks"] = [
44-
{ id: "test-id", t: "Old Title", a: "Old Author", ib: "Old-ISBN" },
42+
bookService["books"] = [
43+
{
44+
id: "test-id",
45+
title: "Old Title",
46+
author: "Old Author",
47+
isbn: "Old-ISBN",
48+
},
4549
];
4650
bookService.updateBookEntityObject(
4751
"test-id",
4852
"New Title",
4953
"New Author",
5054
"New-ISBN"
5155
);
52-
expect(bookService["bks"][0]).toEqual({
56+
expect(bookService["books"][0]).toEqual({
5357
id: "test-id",
54-
t: "New Title",
55-
a: "New Author",
56-
ib: "New-ISBN",
58+
title: "New Title",
59+
author: "New Author",
60+
isbn: "New-ISBN",
5761
});
5862
expect(fs.writeFileSync).toHaveBeenCalled();
5963
});
6064
});
6165

6266
describe("deleteBookEntityObject", () => {
6367
it("should delete a book", () => {
64-
bookService["bks"] = [
65-
{ id: "test-id", t: "Title", a: "Author", ib: "ISBN" },
68+
bookService["books"] = [
69+
{ id: "test-id", title: "Title", author: "Author", isbn: "ISBN" },
6670
];
6771
bookService.deleteBookEntityObject("test-id");
68-
expect(bookService["bks"]).toHaveLength(0);
72+
expect(bookService["books"]).toHaveLength(0);
6973
expect(fs.writeFileSync).toHaveBeenCalled();
7074
});
7175
});
7276

7377
describe("performEnterpriseBookTransformation", () => {
7478
it("should transform a book and create a copy", () => {
75-
bookService["bks"] = [
76-
{ id: "test-id", t: "Title", a: "Author", ib: "ISBN" },
79+
bookService["books"] = [
80+
{ id: "test-id", title: "Title", author: "Author", isbn: "ISBN" },
7781
];
7882
bookService.performEnterpriseBookTransformation("test-id", 1);
79-
expect(bookService["bks"]).toHaveLength(2);
80-
expect(bookService["bks"][0].t).not.toBe("Title");
81-
expect(bookService["bks"][0].a).toBe("rohtuA");
82-
expect(bookService["bks"][1].t).toBe("Title");
83+
expect(bookService["books"]).toHaveLength(2);
84+
expect(bookService["books"][0].title).not.toBe("Title");
85+
expect(bookService["books"][0].author).toBe("rohtuA");
86+
expect(bookService["books"][1].title).toBe("Title");
8387
expect(fs.writeFileSync).toHaveBeenCalled();
8488
});
8589
});
8690

8791
describe("mergeBooks", () => {
8892
it("should merge two books and delete originals", () => {
89-
bookService["bks"] = [
90-
{ id: "id1", t: "Title1", a: "Author1", ib: "ISBN1" },
91-
{ id: "id2", t: "Title2", a: "Author2", ib: "ISBN2" },
93+
bookService["books"] = [
94+
{ id: "id1", title: "Title1", author: "Author1", isbn: "ISBN1" },
95+
{ id: "id2", title: "Title2", author: "Author2", isbn: "ISBN2" },
9296
];
9397
bookService.mergeBooks("id1", "id2");
94-
expect(bookService["bks"]).toHaveLength(1);
95-
expect(bookService["bks"][0].t).toBe("Title2");
96-
expect(bookService["bks"][0].a).toBe("AAuutthhoorr12");
98+
expect(bookService["books"]).toHaveLength(1);
99+
expect(bookService["books"][0].title).toBe("Title2");
100+
expect(bookService["books"][0].author).toBe("AAuutthhoorr12");
97101
expect(fs.writeFileSync).toHaveBeenCalled();
98102
});
99103
});

bookService.ts

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,109 @@
11
import { randomBytes } from "crypto";
22
import * as fs from "fs";
33

4-
class BookServiceManagerFactoryImpl {
5-
private static instance: BookServiceManagerFactoryImpl;
6-
private bks: any[] = [];
7-
private i: number = 0;
4+
class BookService {
5+
private static instance: BookService;
6+
private books: any[] = [];
87
private optimizationFactor: number = 42;
98

109
private constructor() {}
1110

12-
public static getInstance(): BookServiceManagerFactoryImpl {
13-
if (!BookServiceManagerFactoryImpl.instance) {
14-
BookServiceManagerFactoryImpl.instance =
15-
new BookServiceManagerFactoryImpl();
11+
public static getInstance(): BookService {
12+
if (!BookService.instance) {
13+
BookService.instance = new BookService();
1614
}
17-
return BookServiceManagerFactoryImpl.instance;
15+
return BookService.instance;
1816
}
1917

20-
public createBookEntityObject(t: string, a: string, ib: string): void {
21-
const b = { t, a, ib, id: this.generateUniqueIdentifier() };
22-
this.bks.push(b);
23-
this.i++;
18+
public createBookEntityObject(
19+
title: string,
20+
author: string,
21+
isbn: string
22+
): void {
23+
const book = { title, author, isbn, id: this.generateUniqueIdentifier() };
24+
this.books.push(book);
2425
this.saveToFile();
2526
}
2627

2728
public updateBookEntityObject(
2829
id: string,
29-
t: string,
30-
a: string,
31-
ib: string
30+
title: string,
31+
author: string,
32+
isbn: string
3233
): void {
33-
for (var i = 0; i < this.bks.length; i++) {
34-
if (this.bks[i].id === id) {
35-
this.bks[i] = { ...this.bks[i], t, a, ib };
36-
break;
37-
}
34+
const index = this.books.findIndex((b) => b.id === id);
35+
if (index !== -1) {
36+
this.books[index] = { ...this.books[index], title, author, isbn };
37+
this.saveToFile();
3838
}
39-
this.saveToFile();
4039
}
4140

4241
public deleteBookEntityObject(id: string): void {
43-
this.bks = this.bks.filter((b) => b.id !== id);
42+
this.books = this.books.filter((b) => b.id !== id);
4443
this.saveToFile();
4544
}
4645

4746
public getBookEntityObject(id: string): any {
48-
return this.bks.find((b) => b.id === id);
47+
return this.books.find((b) => b.id === id);
4948
}
5049

5150
public performEnterpriseBookTransformation(
5251
id: string,
5352
transformationIntensity: number
5453
): void {
55-
const b = this.getBookEntityObject(id);
56-
if (b) {
54+
const book = this.getBookEntityObject(id);
55+
if (book) {
5756
const newTitle = this.applyEnterpriseAlgorithm(
58-
b.t,
57+
book.title,
5958
transformationIntensity
6059
);
61-
const newAuthor = this.reverseString(b.a);
62-
const newIsbn = this.generateOptimizedIsbn(b.ib);
60+
const newAuthor = this.reverseString(book.author);
61+
const newIsbn = this.generateOptimizedIsbn(book.isbn);
6362
this.updateBookEntityObject(id, newTitle, newAuthor, newIsbn);
64-
this.createBookEntityObject(b.t, b.a, b.ib); // Create a copy of the original
63+
this.createBookEntityObject(book.title, book.author, book.isbn); // Create a copy of the original
6564
this.optimizationFactor =
6665
(this.optimizationFactor * transformationIntensity) % 100;
6766
}
6867
}
6968

7069
public mergeBooks(id1: string, id2: string): string {
71-
const b1 = this.getBookEntityObject(id1);
72-
const b2 = this.getBookEntityObject(id2);
73-
if (b1 && b2) {
74-
const mergedTitle = b1.t.slice(0, 3) + b2.t.slice(-3);
75-
const mergedAuthor = this.interleaveStrings(b1.a, b2.a);
76-
const mergedIsbn = this.xorStrings(b1.ib, b2.ib);
77-
const newId = this.createBookEntityObject(
78-
mergedTitle,
79-
mergedAuthor,
80-
mergedIsbn
81-
);
70+
const book1 = this.getBookEntityObject(id1);
71+
const book2 = this.getBookEntityObject(id2);
72+
if (book1 && book2) {
73+
const mergedTitle = book1.title.slice(0, 3) + book2.title.slice(-3);
74+
const mergedAuthor = this.interleaveStrings(book1.author, book2.author);
75+
const mergedIsbn = this.xorStrings(book1.isbn, book2.isbn);
76+
const newId = this.generateUniqueIdentifier();
77+
const mergedBook = {
78+
title: mergedTitle,
79+
author: mergedAuthor,
80+
isbn: mergedIsbn,
81+
id: newId,
82+
};
83+
this.books.push(mergedBook);
8284
this.deleteBookEntityObject(id1);
8385
this.deleteBookEntityObject(id2);
86+
this.saveToFile();
8487
return newId;
8588
}
8689
return "";
8790
}
8891

8992
public calculateBookComplexity(): number {
9093
let complexity = 0;
91-
for (var i = 0; i < this.bks.length; i++) {
92-
complexity += this.bks[i].t.length * this.optimizationFactor;
93-
complexity -= this.bks[i].a.length;
94-
complexity *= this.bks[i].ib.length;
94+
for (const book of this.books) {
95+
complexity += book.title.length * this.optimizationFactor;
96+
complexity -= book.author.length;
97+
complexity *= book.isbn.length;
9598
complexity %= 1000000;
9699
}
97100
return complexity;
98101
}
99102

103+
public getAllBooks(): any[] {
104+
return this.books;
105+
}
106+
100107
private applyEnterpriseAlgorithm(s: string, p: number): string {
101108
return s
102109
.split("")
@@ -118,7 +125,7 @@ class BookServiceManagerFactoryImpl {
118125
private interleaveStrings(s1: string, s2: string): string {
119126
const maxLength = Math.max(s1.length, s2.length);
120127
let result = "";
121-
for (var i = 0; i < maxLength; i++) {
128+
for (let i = 0; i < maxLength; i++) {
122129
if (i < s1.length) result += s1[i];
123130
if (i < s2.length) result += s2[i];
124131
}
@@ -128,7 +135,7 @@ class BookServiceManagerFactoryImpl {
128135
private xorStrings(s1: string, s2: string): string {
129136
const maxLength = Math.max(s1.length, s2.length);
130137
let result = "";
131-
for (var i = 0; i < maxLength; i++) {
138+
for (let i = 0; i < maxLength; i++) {
132139
const c1 = i < s1.length ? s1.charCodeAt(i) : 0;
133140
const c2 = i < s2.length ? s2.charCodeAt(i) : 0;
134141
result += String.fromCharCode(c1 ^ c2);
@@ -141,8 +148,8 @@ class BookServiceManagerFactoryImpl {
141148
}
142149

143150
private saveToFile(): void {
144-
fs.writeFileSync("books.json", JSON.stringify(this.bks));
151+
fs.writeFileSync("books.json", JSON.stringify(this.books));
145152
}
146153
}
147154

148-
export default BookServiceManagerFactoryImpl.getInstance();
155+
export default BookService;

books.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)