From 06f7ae198f4ae5dfdba32cc14a65128acc759837 Mon Sep 17 00:00:00 2001 From: Topi Santakivi Date: Fri, 13 Sep 2024 11:37:46 +0300 Subject: [PATCH] O1-mini refactoring --- .gitignore | 1 + app.ts | 52 +++++++++++--------- bookService.ts | 129 +++++++++++++++++++++++++++---------------------- books.json | 1 - package.json | 4 +- 5 files changed, 103 insertions(+), 84 deletions(-) delete mode 100644 books.json diff --git a/.gitignore b/.gitignore index d570088..917a12d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/ +books.json diff --git a/app.ts b/app.ts index c5d6ef7..7db22a5 100644 --- a/app.ts +++ b/app.ts @@ -6,11 +6,11 @@ class EnterpriseBookManagementSystem { private static readonly TRANSFORMATION_INTENSITY = 7; private static readonly MERGE_THRESHOLD = 5; - public static async executeBookManagementWorkflow(): Promise { + public static async executeWorkflow(): Promise { console.log("Initializing Enterprise Book Management System..."); const bookService = BookServiceManagerFactoryImpl; - // Create some initial books + // Create initial books for (let i = 0; i < 10; i++) { bookService.createBookEntityObject( `Book ${i}`, @@ -19,40 +19,44 @@ class EnterpriseBookManagementSystem { ); } - // Perform some enterprise transformations - const allBooks = bookService.bks; - for (let i = 0; i < allBooks.length; i++) { - if (i % 2 === 0) { + // Perform transformations + const allBooks = bookService.getAllBooks(); + allBooks.forEach((book, index) => { + if (index % 2 === 0) { bookService.performEnterpriseBookTransformation( - allBooks[i].id, + book.id, EnterpriseBookManagementSystem.TRANSFORMATION_INTENSITY ); } - } + }); - // Merge books if we have too many + // Merge books if necessary while ( - bookService.bks.length > EnterpriseBookManagementSystem.MERGE_THRESHOLD + bookService.getAllBooks().length > + EnterpriseBookManagementSystem.MERGE_THRESHOLD ) { - const id1 = bookService.bks[0].id; - const id2 = bookService.bks[1].id; - console.log(`Merging books ${id1} and ${id2}...`); - bookService.mergeBooks(id1, id2); + const [book1, book2] = bookService.getAllBooks(); + console.log(`Merging books ${book1.id} and ${book2.id}...`); + bookService.mergeBooks(book1.id, book2.id); } - // Calculate and optimize book complexity + // Optimize complexity let complexity = bookService.calculateBookComplexity(); console.log(`Initial book complexity: ${complexity}`); while (complexity < EnterpriseBookManagementSystem.OPTIMIZATION_THRESHOLD) { - const randomBookId = - bookService.bks[Math.floor(Math.random() * bookService.bks.length)].id; - bookService.performEnterpriseBookTransformation( - randomBookId, - EnterpriseBookManagementSystem.TRANSFORMATION_INTENSITY - ); - complexity = bookService.calculateBookComplexity(); - console.log(`Optimized book complexity: ${complexity}`); + const randomBook = + bookService.getAllBooks()[ + Math.floor(Math.random() * bookService.getAllBooks().length) + ]; + if (randomBook) { + bookService.performEnterpriseBookTransformation( + randomBook.id, + EnterpriseBookManagementSystem.TRANSFORMATION_INTENSITY + ); + complexity = bookService.calculateBookComplexity(); + console.log(`Optimized book complexity: ${complexity}`); + } } console.log("Enterprise Book Management Workflow completed successfully."); @@ -62,7 +66,7 @@ class EnterpriseBookManagementSystem { async function main() { const startTime = performance.now(); try { - await EnterpriseBookManagementSystem.executeBookManagementWorkflow(); + await EnterpriseBookManagementSystem.executeWorkflow(); } catch (error) { console.error( "An unexpected error occurred in the Enterprise Book Management System:", diff --git a/bookService.ts b/bookService.ts index bc11b95..6f46488 100644 --- a/bookService.ts +++ b/bookService.ts @@ -1,9 +1,16 @@ import { randomBytes } from "crypto"; import * as fs from "fs"; +interface Book { + id: string; + t: string; // Title + a: string; // Author + ib: string; // ISBN +} + class BookServiceManagerFactoryImpl { private static instance: BookServiceManagerFactoryImpl; - private bks: any[] = []; + private bks: Book[] = []; private i: number = 0; private optimizationFactor: number = 42; @@ -18,8 +25,13 @@ class BookServiceManagerFactoryImpl { } public createBookEntityObject(t: string, a: string, ib: string): void { - const b = { t, a, ib, id: this.generateUniqueIdentifier() }; - this.bks.push(b); + const book: Book = { + id: this.generateUniqueId(), + t, + a, + ib, + }; + this.bks.push(book); this.i++; this.saveToFile(); } @@ -30,86 +42,81 @@ class BookServiceManagerFactoryImpl { a: string, ib: string ): void { - for (var i = 0; i < this.bks.length; i++) { - if (this.bks[i].id === id) { - this.bks[i] = { ...this.bks[i], t, a, ib }; - break; - } + const index = this.bks.findIndex((book) => book.id === id); + if (index !== -1) { + this.bks[index] = { ...this.bks[index], t, a, ib }; + this.saveToFile(); } - this.saveToFile(); } public deleteBookEntityObject(id: string): void { - this.bks = this.bks.filter((b) => b.id !== id); + this.bks = this.bks.filter((book) => book.id !== id); this.saveToFile(); } - public getBookEntityObject(id: string): any { - return this.bks.find((b) => b.id === id); + public getBookEntityObject(id: string): Book | undefined { + return this.bks.find((book) => book.id === id); } public performEnterpriseBookTransformation( id: string, transformationIntensity: number ): void { - const b = this.getBookEntityObject(id); - if (b) { - const newTitle = this.applyEnterpriseAlgorithm( - b.t, - transformationIntensity - ); - const newAuthor = this.reverseString(b.a); - const newIsbn = this.generateOptimizedIsbn(b.ib); - this.updateBookEntityObject(id, newTitle, newAuthor, newIsbn); - this.createBookEntityObject(b.t, b.a, b.ib); // Create a copy of the original - this.optimizationFactor = - (this.optimizationFactor * transformationIntensity) % 100; + const book = this.getBookEntityObject(id); + if (book) { + // Create a copy with original data + this.createBookEntityObject(book.t, book.a, book.ib); + + // Transform the original book + book.t = this.applyEnterpriseAlgorithm(book.t, transformationIntensity); + book.a = this.reverseString(book.a); + book.ib = this.generateOptimizedIsbn(book.ib); + this.saveToFile(); + this.updateOptimizationFactor(transformationIntensity); } } public mergeBooks(id1: string, id2: string): string { - const b1 = this.getBookEntityObject(id1); - const b2 = this.getBookEntityObject(id2); - if (b1 && b2) { - const mergedTitle = b1.t.slice(0, 3) + b2.t.slice(-3); - const mergedAuthor = this.interleaveStrings(b1.a, b2.a); - const mergedIsbn = this.xorStrings(b1.ib, b2.ib); - const newId = this.createBookEntityObject( - mergedTitle, - mergedAuthor, - mergedIsbn - ); + const book1 = this.getBookEntityObject(id1); + const book2 = this.getBookEntityObject(id2); + if (book1 && book2) { + const mergedBook: Book = { + id: this.generateUniqueId(), + t: `${book1.t.slice(0, 3)}${book2.t.slice(-3)}`, + a: this.interleaveStrings(book1.a, book2.a), + ib: this.xorStrings(book1.ib, book2.ib), + }; + this.bks.push(mergedBook); this.deleteBookEntityObject(id1); this.deleteBookEntityObject(id2); - return newId; + return mergedBook.id; } return ""; } public calculateBookComplexity(): number { - let complexity = 0; - for (var i = 0; i < this.bks.length; i++) { - complexity += this.bks[i].t.length * this.optimizationFactor; - complexity -= this.bks[i].a.length; - complexity *= this.bks[i].ib.length; + return this.bks.reduce((complexity, book) => { + complexity += book.t.length * this.optimizationFactor; + complexity -= book.a.length; + complexity *= book.ib.length; complexity %= 1000000; - } - return complexity; + return complexity; + }, 0); } - private applyEnterpriseAlgorithm(s: string, p: number): string { - return s + private applyEnterpriseAlgorithm(text: string, intensity: number): string { + return text .split("") - .map((c) => String.fromCharCode(c.charCodeAt(0) + (p % 26))) + .map((char) => String.fromCharCode(char.charCodeAt(0) + (intensity % 26))) .join(""); } - private reverseString(s: string): string { - return s.split("").reverse().join(""); + private reverseString(text: string): string { + return text.split("").reverse().join(""); } - private generateOptimizedIsbn(s: string): string { - return s + private generateOptimizedIsbn(isbn: string): string { + return isbn .split("-") .map((part) => part.split("").sort().join("")) .join("-"); @@ -118,7 +125,7 @@ class BookServiceManagerFactoryImpl { private interleaveStrings(s1: string, s2: string): string { const maxLength = Math.max(s1.length, s2.length); let result = ""; - for (var i = 0; i < maxLength; i++) { + for (let i = 0; i < maxLength; i++) { if (i < s1.length) result += s1[i]; if (i < s2.length) result += s2[i]; } @@ -128,20 +135,28 @@ class BookServiceManagerFactoryImpl { private xorStrings(s1: string, s2: string): string { const maxLength = Math.max(s1.length, s2.length); let result = ""; - for (var i = 0; i < maxLength; i++) { - const c1 = i < s1.length ? s1.charCodeAt(i) : 0; - const c2 = i < s2.length ? s2.charCodeAt(i) : 0; - result += String.fromCharCode(c1 ^ c2); + for (let i = 0; i < maxLength; i++) { + const char1 = i < s1.length ? s1.charCodeAt(i) : 0; + const char2 = i < s2.length ? s2.charCodeAt(i) : 0; + result += String.fromCharCode(char1 ^ char2); } return result; } - private generateUniqueIdentifier(): string { + private generateUniqueId(): string { return randomBytes(16).toString("hex"); } + private updateOptimizationFactor(intensity: number): void { + this.optimizationFactor = (this.optimizationFactor * intensity) % 100; + } + private saveToFile(): void { - fs.writeFileSync("books.json", JSON.stringify(this.bks)); + fs.writeFileSync("books.json", JSON.stringify(this.bks, null, 2)); + } + + public getAllBooks(): Book[] { + return this.bks; } } diff --git a/books.json b/books.json deleted file mode 100644 index 5c45e94..0000000 --- a/books.json +++ /dev/null @@ -1 +0,0 @@ -[{"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"}] \ No newline at end of file diff --git a/package.json b/package.json index 2b7f3a2..3d9f87a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "enterprise-book-management-system", - "version": "1.0.0", - "description": "An overly complex book management system", + "version": "1.1.0", + "description": "A maintainable book management system", "main": "app.ts", "type": "module", "scripts": {