UNPKG

@easy-cipher/caesar

Version:

[![npm](https://img.shields.io/npm/v/@easy-cipher/caesar.svg)](https://www.npmjs.com/package/@easy-cipher/caesar) [![npm bundle size (minified + gzip)](https://badgen.net/bundlephobia/minzip/@easy-cipher/caesar)](https://bundlephobia.com/result?p=@easy-ci

123 lines (116 loc) 3.42 kB
/** * @easy-cipher/caesar@1.0.1 * Copyright (c) 2021 Bartłomiej Wiśniewski * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * @easy-cipher/core@1.0.1 * Copyright (c) 2021 Bartłomiej Wiśniewski * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ const empty = ""; const space = " "; const core = ({ mappings, ciphertextWordsSeparator = space, ciphertextCharactersSeparator = empty, plaintextWordsSeparator = space, plaintextCharactersSeparator = empty, }) => { const reversedMappings = Object.entries(mappings).reduce( (acc, [x, y]) => ({ ...acc, [y]: x }), {} ); const encode = ( plaintext, { throwOnUnknownCharacters } = { throwOnUnknownCharacters: true } ) => { return plaintext .split(plaintextWordsSeparator) .map((plaintextWord) => plaintextWord .split(plaintextCharactersSeparator) .map((plaintextCharacter) => { const mapping = mappings[plaintextCharacter]; if (mapping) { return mapping; } if (throwOnUnknownCharacters) { throw new Error( `Unsupported character ${JSON.stringify( plaintextCharacter )}, not found in ${JSON.stringify(mappings)}` ); } return empty; }) .join(ciphertextCharactersSeparator) ) .join(ciphertextWordsSeparator); }; const decode = ( ciphertext, { throwOnUnknownCharacters } = { throwOnUnknownCharacters: true } ) => { return ciphertext .split(ciphertextWordsSeparator) .map((ciphertextWord) => ciphertextWord .split(ciphertextCharactersSeparator) .map((ciphertextCharacter) => { const character = reversedMappings[ciphertextCharacter]; if (character) { return character; } if (throwOnUnknownCharacters) { throw new Error( `Unsupported ciphertext ${JSON.stringify( ciphertextCharacter )}, not found in ${JSON.stringify(reversedMappings)}` ); } return empty; }) .join(plaintextCharactersSeparator) ) .join(plaintextWordsSeparator); }; return { encode, decode, }; }; const defaultAlphabet = "abcdefghijklmnopqrstuvwxyz".split(""); const caesar = ({ encryptionOffset, alphabet = defaultAlphabet, ciphertextWordsSeparator, ciphertextCharactersSeparator, plaintextWordsSeparator, plaintextCharactersSeparator, }) => { const offset = encryptionOffset > 0 ? encryptionOffset % alphabet.length : alphabet.length + (encryptionOffset % alphabet.length); const mappings = alphabet.reduce( (acc, character, index, alphabet) => ({ ...acc, [character]: alphabet[(index + offset) % alphabet.length], }), {} ); return core({ mappings, ciphertextWordsSeparator, ciphertextCharactersSeparator, plaintextWordsSeparator, plaintextCharactersSeparator, }); }; exports.caesar = caesar; //# sourceMappingURL=index.common.js.map