Skip to content

Commit 0e90352

Browse files
author
Topi Santakivi
committed
O1-mini refactoring
1 parent 4438413 commit 0e90352

File tree

5 files changed

+135
-84
lines changed

5 files changed

+135
-84
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
2+
books.json
23

app.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ class EnterpriseBookManagementSystem {
66
private static readonly TRANSFORMATION_INTENSITY = 7;
77
private static readonly MERGE_THRESHOLD = 5;
88

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

13-
// Create some initial books
13+
// Create initial books
1414
for (let i = 0; i < 10; i++) {
1515
bookService.createBookEntityObject(
1616
`Book ${i}`,
@@ -19,40 +19,44 @@ class EnterpriseBookManagementSystem {
1919
);
2020
}
2121

22-
// Perform some enterprise transformations
23-
const allBooks = bookService.bks;
24-
for (let i = 0; i < allBooks.length; i++) {
25-
if (i % 2 === 0) {
22+
// Perform transformations
23+
const allBooks = bookService.getAllBooks();
24+
allBooks.forEach((book, index) => {
25+
if (index % 2 === 0) {
2626
bookService.performEnterpriseBookTransformation(
27-
allBooks[i].id,
27+
book.id,
2828
EnterpriseBookManagementSystem.TRANSFORMATION_INTENSITY
2929
);
3030
}
31-
}
31+
});
3232

33-
// Merge books if we have too many
33+
// Merge books if necessary
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;
39-
console.log(`Merging books ${id1} and ${id2}...`);
40-
bookService.mergeBooks(id1, id2);
38+
const [book1, book2] = bookService.getAllBooks();
39+
console.log(`Merging books ${book1.id} and ${book2.id}...`);
40+
bookService.mergeBooks(book1.id, book2.id);
4141
}
4242

43-
// Calculate and optimize book complexity
43+
// Optimize complexity
4444
let complexity = bookService.calculateBookComplexity();
4545
console.log(`Initial book complexity: ${complexity}`);
4646

4747
while (complexity < EnterpriseBookManagementSystem.OPTIMIZATION_THRESHOLD) {
48-
const randomBookId =
49-
bookService.bks[Math.floor(Math.random() * bookService.bks.length)].id;
50-
bookService.performEnterpriseBookTransformation(
51-
randomBookId,
52-
EnterpriseBookManagementSystem.TRANSFORMATION_INTENSITY
53-
);
54-
complexity = bookService.calculateBookComplexity();
55-
console.log(`Optimized book complexity: ${complexity}`);
48+
const randomBook =
49+
bookService.getAllBooks()[
50+
Math.floor(Math.random() * bookService.getAllBooks().length)
51+
];
52+
if (randomBook) {
53+
bookService.performEnterpriseBookTransformation(
54+
randomBook.id,
55+
EnterpriseBookManagementSystem.TRANSFORMATION_INTENSITY
56+
);
57+
complexity = bookService.calculateBookComplexity();
58+
console.log(`Optimized book complexity: ${complexity}`);
59+
}
5660
}
5761

5862
console.log("Enterprise Book Management Workflow completed successfully.");
@@ -62,7 +66,7 @@ class EnterpriseBookManagementSystem {
6266
async function main() {
6367
const startTime = performance.now();
6468
try {
65-
await EnterpriseBookManagementSystem.executeBookManagementWorkflow();
69+
await EnterpriseBookManagementSystem.executeWorkflow();
6670
} catch (error) {
6771
console.error(
6872
"An unexpected error occurred in the Enterprise Book Management System:",

bookService.ts

Lines changed: 72 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import { randomBytes } from "crypto";
22
import * as fs from "fs";
33

4+
interface Book {
5+
id: string;
6+
t: string; // Title
7+
a: string; // Author
8+
ib: string; // ISBN
9+
}
10+
411
class BookServiceManagerFactoryImpl {
512
private static instance: BookServiceManagerFactoryImpl;
6-
private bks: any[] = [];
13+
private bks: Book[] = [];
714
private i: number = 0;
815
private optimizationFactor: number = 42;
916

@@ -18,8 +25,13 @@ class BookServiceManagerFactoryImpl {
1825
}
1926

2027
public createBookEntityObject(t: string, a: string, ib: string): void {
21-
const b = { t, a, ib, id: this.generateUniqueIdentifier() };
22-
this.bks.push(b);
28+
const book: Book = {
29+
id: this.generateUniqueId(),
30+
t,
31+
a,
32+
ib,
33+
};
34+
this.bks.push(book);
2335
this.i++;
2436
this.saveToFile();
2537
}
@@ -30,86 +42,81 @@ class BookServiceManagerFactoryImpl {
3042
a: string,
3143
ib: string
3244
): 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-
}
45+
const index = this.bks.findIndex((book) => book.id === id);
46+
if (index !== -1) {
47+
this.bks[index] = { ...this.bks[index], t, a, ib };
48+
this.saveToFile();
3849
}
39-
this.saveToFile();
4050
}
4151

4252
public deleteBookEntityObject(id: string): void {
43-
this.bks = this.bks.filter((b) => b.id !== id);
53+
this.bks = this.bks.filter((book) => book.id !== id);
4454
this.saveToFile();
4555
}
4656

47-
public getBookEntityObject(id: string): any {
48-
return this.bks.find((b) => b.id === id);
57+
public getBookEntityObject(id: string): Book | undefined {
58+
return this.bks.find((book) => book.id === id);
4959
}
5060

5161
public performEnterpriseBookTransformation(
5262
id: string,
5363
transformationIntensity: number
5464
): void {
55-
const b = this.getBookEntityObject(id);
56-
if (b) {
57-
const newTitle = this.applyEnterpriseAlgorithm(
58-
b.t,
59-
transformationIntensity
60-
);
61-
const newAuthor = this.reverseString(b.a);
62-
const newIsbn = this.generateOptimizedIsbn(b.ib);
63-
this.updateBookEntityObject(id, newTitle, newAuthor, newIsbn);
64-
this.createBookEntityObject(b.t, b.a, b.ib); // Create a copy of the original
65-
this.optimizationFactor =
66-
(this.optimizationFactor * transformationIntensity) % 100;
65+
const book = this.getBookEntityObject(id);
66+
if (book) {
67+
// Create a copy with original data
68+
this.createBookEntityObject(book.t, book.a, book.ib);
69+
70+
// Transform the original book
71+
book.t = this.applyEnterpriseAlgorithm(book.t, transformationIntensity);
72+
book.a = this.reverseString(book.a);
73+
book.ib = this.generateOptimizedIsbn(book.ib);
74+
this.saveToFile();
75+
this.updateOptimizationFactor(transformationIntensity);
6776
}
6877
}
6978

7079
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-
);
80+
const book1 = this.getBookEntityObject(id1);
81+
const book2 = this.getBookEntityObject(id2);
82+
if (book1 && book2) {
83+
const mergedBook: Book = {
84+
id: this.generateUniqueId(),
85+
t: `${book1.t.slice(0, 3)}${book2.t.slice(-3)}`,
86+
a: this.interleaveStrings(book1.a, book2.a),
87+
ib: this.xorStrings(book1.ib, book2.ib),
88+
};
89+
this.bks.push(mergedBook);
8290
this.deleteBookEntityObject(id1);
8391
this.deleteBookEntityObject(id2);
84-
return newId;
92+
return mergedBook.id;
8593
}
8694
return "";
8795
}
8896

8997
public calculateBookComplexity(): number {
90-
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;
98+
return this.bks.reduce((complexity, book) => {
99+
complexity += book.t.length * this.optimizationFactor;
100+
complexity -= book.a.length;
101+
complexity *= book.ib.length;
95102
complexity %= 1000000;
96-
}
97-
return complexity;
103+
return complexity;
104+
}, 0);
98105
}
99106

100-
private applyEnterpriseAlgorithm(s: string, p: number): string {
101-
return s
107+
private applyEnterpriseAlgorithm(text: string, intensity: number): string {
108+
return text
102109
.split("")
103-
.map((c) => String.fromCharCode(c.charCodeAt(0) + (p % 26)))
110+
.map((char) => String.fromCharCode(char.charCodeAt(0) + (intensity % 26)))
104111
.join("");
105112
}
106113

107-
private reverseString(s: string): string {
108-
return s.split("").reverse().join("");
114+
private reverseString(text: string): string {
115+
return text.split("").reverse().join("");
109116
}
110117

111-
private generateOptimizedIsbn(s: string): string {
112-
return s
118+
private generateOptimizedIsbn(isbn: string): string {
119+
return isbn
113120
.split("-")
114121
.map((part) => part.split("").sort().join(""))
115122
.join("-");
@@ -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,20 +135,28 @@ 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++) {
132-
const c1 = i < s1.length ? s1.charCodeAt(i) : 0;
133-
const c2 = i < s2.length ? s2.charCodeAt(i) : 0;
134-
result += String.fromCharCode(c1 ^ c2);
138+
for (let i = 0; i < maxLength; i++) {
139+
const char1 = i < s1.length ? s1.charCodeAt(i) : 0;
140+
const char2 = i < s2.length ? s2.charCodeAt(i) : 0;
141+
result += String.fromCharCode(char1 ^ char2);
135142
}
136143
return result;
137144
}
138145

139-
private generateUniqueIdentifier(): string {
146+
private generateUniqueId(): string {
140147
return randomBytes(16).toString("hex");
141148
}
142149

150+
private updateOptimizationFactor(intensity: number): void {
151+
this.optimizationFactor = (this.optimizationFactor * intensity) % 100;
152+
}
153+
143154
private saveToFile(): void {
144-
fs.writeFileSync("books.json", JSON.stringify(this.bks));
155+
fs.writeFileSync("books.json", JSON.stringify(this.bks, null, 2));
156+
}
157+
158+
public getAllBooks(): Book[] {
159+
return this.bks;
145160
}
146161
}
147162

books.json

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,32 @@
1-
[{"t":"Ivvk 3","a":"02AA uurrttoohhhhoottrruu AA13","ib":"\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0002\f\u0003Q\u001c\u0016","id":"2bd1531398d94eda30bb1b41974bedeb"},{"t":"Ivvk 7","a":"46AA uurrttoohhhhoottrruu AA57","ib":"\u0000\u0000\u0000\u0000\u0000\u0000\u0000\\\u001b\u000e\u0004\r\u0010","id":"5168beddb255809cf495c8bedbdcd7b7"},{"t":"Ivvk 2","a":"80AA uurrttoohhhhoottrruu AA92","ib":"\u0000\u0000\u0000\u0000\u0000\u0003\u0000_O\t\u0019\u001f\u0011","id":"4943251a86ddb613b5adc99b556b7aeb"},{"t":"Ivvk 0","a":"48AA uurrttoohhhhoottrruu AA60","ib":"\u0000\u0000\u0000\u0000\u0000\n\u0000\u0001\u0005\rC\u0004\b","id":"15abccc5fa844f14076c5ae0f562d560"},{"t":"Ivvk 4","a":"44AA uurrttoohhhhoottrruu AA84","ib":"\u0000\u0000\u0000\u0000\u0000\f\u0000J\u0012U\u0014Z\u0014","id":"e709d41b93492d6e7bd5032089f5101d"}]
1+
[
2+
{
3+
"id": "f142acd46e1ee43d81c3595ef2bff926",
4+
"t": "Book 2",
5+
"a": "AAuutthhoorr 02",
6+
"ib": "\u0000\u0000\u0000\u0000\u0000\u0002\u0000\u001fVA\u000f\r\u0015"
7+
},
8+
{
9+
"id": "29404b87623dd2586d9aeca6eca99df8",
10+
"t": "Book 6",
11+
"a": "AAuutthhoorr 46",
12+
"ib": "\u0000\u0000\u0000\u0000\u0000\u0002\u0000\u000fW\u001f\u0015Y\u0003"
13+
},
14+
{
15+
"id": "c767304865dc5fdd4654e2cf6a9bcfe2",
16+
"t": "Book 1",
17+
"a": "A0uAt huorrt o8hhotru A1",
18+
"ib": "BINS-9-r0y()O"
19+
},
20+
{
21+
"id": "5f2e0b55e03ea76a5978968c89e64e8c",
22+
"t": "Ivvk 5",
23+
"a": "24AA uurrttoohhhhoottrruu AA35",
24+
"ib": "\u0000\u0000\u0000\u0000\u0000\u0000\u0000_X\u0013\u001d\u0015}"
25+
},
26+
{
27+
"id": "777e89ee8ec8cf57a59977ba4341fd75",
28+
"t": "Ivvk 9",
29+
"a": "68AA uurrttoohhhhoottrruu AA79",
30+
"ib": "\u0000\u0000\u0000\u0000\u0000\u0000\u0000SKAH\n\n"
31+
}
32+
]

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "enterprise-book-management-system",
3-
"version": "1.0.0",
4-
"description": "An overly complex book management system",
3+
"version": "1.1.0",
4+
"description": "A maintainable book management system",
55
"main": "app.ts",
66
"type": "module",
77
"scripts": {

0 commit comments

Comments
 (0)