{"record":{"id":"30705a464912d506","repo":"TheAlgorithms/JavaScript","slug":"argument-should-be-string","errorCode":null,"errorMessage":"Argument should be string","messagePattern":"Argument should be string","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Ciphers/Atbash.js","lineNumber":10,"sourceCode":"/**\n * @function Atbash - Decrypt a Atbash cipher\n * @description - The Atbash cipher is a particular type of monoalphabetic cipher formed by taking the alphabet and mapping it to its reverse, so that the first letter becomes the last letter, the second letter becomes the second to last letter, and so on.\n * @param {string} str - string to be decrypted/encrypt\n * @return {string} decrypted/encrypted string\n * @see - [wiki](https://en.wikipedia.org/wiki/Atbash)\n */\nconst Atbash = (str) => {\n  if (typeof str !== 'string') {\n    throw new TypeError('Argument should be string')\n  }\n\n  return str.replace(/[a-z]/gi, (char) => {\n    const charCode = char.charCodeAt()\n\n    if (/[A-Z]/.test(char)) {\n      return String.fromCharCode(90 + 65 - charCode)\n    }\n\n    return String.fromCharCode(122 + 97 - charCode)\n  })\n}\n\nexport default Atbash\n","sourceCodeStart":1,"sourceCodeEnd":25,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Ciphers/Atbash.js#L1-L25","documentation":"Atbash rejects any non-string input because it calls str.replace(/[a-z]/gi, ...). Passing a non-string would either coerce oddly or throw a less informative error from String.prototype.replace, so the guard validates up front.","triggerScenarios":"Passing a number, null, undefined, object, array, or Buffer instead of the text string.","commonSituations":"Encrypting a numeric ID without converting to string, or passing a Buffer from file/network input.","solutions":["Pass a string: Atbash('secret').","Convert with String(value) or .toString() for non-string inputs.","Decode Buffers to utf8 before calling."],"exampleFix":"// before\nAtbash(12345)\n// after\nAtbash(String(12345))","handlingStrategy":"type-guard","validationCode":"function atbashSafe(v) {\n  return Atbash(typeof v === 'string' ? v : String(v));\n}","typeGuard":"/** @param {unknown} s @returns {s is string} */\nconst isString = s => typeof s === 'string';","tryCatchPattern":"try { return Atbash(text); }\ncatch (e) {\n  if (e instanceof TypeError && /should be string/.test(e.message)) {\n    return Atbash(String(text));\n  }\n  throw e;\n}","preventionTips":["Wrap potentially non-string values with String().","Decode Buffers to utf8 before ciphering.","Validate typeof at the input boundary."],"tags":["cipher","atbash","type-validation","string"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}