Created
April 1, 2023 18:38
-
-
Save vpnry/8894e7047acd852530572b4ae521a005 to your computer and use it in GitHub Desktop.
nodejs split dictionary
This file contains hidden or 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
| const fs = require("fs"); | |
| const PARTS = 10; | |
| // Load the original dictionary from a file | |
| const DICTOBJ = require("./DICTOBJ.js"); | |
| // Split the keys into 4 roughly equal PARTS | |
| const keys = Object.keys(DICTOBJ); | |
| const chunkSize = Math.ceil(keys.length / PARTS); | |
| const chunks = []; | |
| for (let i = 0; i < keys.length; i += chunkSize) { | |
| chunks.push(keys.slice(i, i + chunkSize)); | |
| } | |
| // Create n PARTS dictionaries and write them to files | |
| for (let i = 0; i < PARTS; i++) { | |
| const baseName = `iosPart${(i + 1).toString().padStart(3, "0")}`; | |
| const fileName = `${baseName}.js`; | |
| const chunk = chunks[i]; | |
| const dict = {}; | |
| for (const key of chunk) { | |
| dict[key] = DICTOBJ[key]; | |
| } | |
| const content = `if (!DICTOBJ) { var DICTOBJ = {};}\n\nlet ${baseName} = ${JSON.stringify( | |
| dict, | |
| null, | |
| 2 | |
| )};\n`; | |
| let indentedContent = content.replace(/^/gm, " "); | |
| indentedContent += ` | |
| for (const key of ${baseName}) { | |
| DICTOBJ[key] = ${baseName}[key]; | |
| } | |
| ${baseName} = ''; | |
| delete ${baseName}; | |
| `; | |
| fs.writeFileSync(fileName, indentedContent); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment