Skip to content

Instantly share code, notes, and snippets.

@tannerdolby
Last active January 12, 2023 05:43
Show Gist options
  • Save tannerdolby/ffb9c8fc7992a8b2cff1d2a6dca524c3 to your computer and use it in GitHub Desktop.
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.
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,
}
}
@tannerdolby
Copy link
Author

tannerdolby commented Jan 3, 2023

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')

const { writeCiphers } = require('../index')
const fs = require('fs/promises')

describe('tests for writing ciphers to output file', () => {
    beforeEach(() => {
        this.testFolder = './test-ciphers'
        this.testFile = 'test.txt'
    })

    test('write cipher to file', async () => {
        const output = await writeCiphers({
            input: 'tanner',
            folder: this.testFolder,
            filename: this.testFile,
            customRotations: [
                [15, 2, 8, 19, 12, 21],
                [3, 13, 11, 17, 10, 25],
            ],
            useAscii: false,
            randomRotations: 250,
        })

        const path = `${this.testFolder}/${this.testFile.slice(0, -4)}-${output.createdAt}.txt`
        const fileContent = await fs.readFile(path)
        
        expect(output.fileContent).toBe(fileContent.toString())
        expect(output.filePath).toBe(path)

        // delete the test folder
        await fs.rm(this.testFolder, { recursive: true })
    })
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment