Last active
January 12, 2023 05:43
-
-
Save tannerdolby/ffb9c8fc7992a8b2cff1d2a6dca524c3 to your computer and use it in GitHub Desktop.
Write caesar cipher output from 'rotation-cipher' package to file. My decision to remove `writeCiphers` from the `rotation-cipher` package was because I wanted users to use the cipher utility on their own terms rather than forcing the package only to be used in a non-browser environment.
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
async function writeCiphers(cipherObj) { | |
if (!cipherObj) return | |
const allCiphers = [] | |
const {input, folder, filename, customRotations, useAscii, randomRotations} = cipherObj; | |
let uniform = makeSectionHeader(input, 'Uniform Rotations') | |
let custom = makeSectionHeader(input, 'Custom Rotations', true) | |
let unique = makeSectionHeader(input, 'Random Rotations', true) | |
const uniformCiphers = getUniformCiphers(input, useAscii) | |
uniform += uniformCiphers.join('\n') | |
const customCiphers = getCustomCiphers(input, useAscii, customRotations) | |
custom += customCiphers.join('\n') | |
const randomCiphers = getRandomCiphers(input, useAscii, randomRotations) | |
unique += randomCiphers.join('\n') | |
const fileContent = `${uniform}\n${custom}\n${unique}` | |
const dateString = cipherObj.date || new Date().toISOString() | |
const fileExt = filename.match(/\.\w+$/gm) | |
const name = filename.slice(0, -1 * fileExt[0].length) | |
const pathToWrite = `${folder}/${name}-${dateString}${fileExt[0]}` | |
allCiphers.push(...uniformCiphers) | |
allCiphers.push(...customCiphers) | |
allCiphers.push(...randomCiphers) | |
fs.mkdir(folder, {recursive: true}, (err) => { | |
if (err) { | |
console.error(`Unable to create directory: ${err}`) | |
} | |
console.log(`Successfully created directory: '${folder}'`) | |
}) | |
fs.writeFile(pathToWrite, fileContent, (err) => { | |
if (err) { | |
console.error(`Unable to write file: ${err}`) | |
} | |
console.log(`Successfully wrote file: '${pathToWrite}'`) | |
}) | |
return { | |
ciphers: allCiphers, | |
fileContent: fileContent, | |
createdAt: dateString, | |
filePath: pathToWrite, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And the corresponding test file for Jest. (This gist was originally apart of the package, but since it requires 'fs', places like React can't even use 'rotation-cipher')