{"record":{"id":"ced9f321b8ae9763","repo":"TheAlgorithms/JavaScript","slug":"argument-should-be-string-ced9f3","errorCode":null,"errorMessage":"Argument should be string","messagePattern":"Argument should be string","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Ciphers/ROT13.js","lineNumber":10,"sourceCode":"/**\n * @function ROT13\n * @description - ROT13 (\"rotate by 13 places\", sometimes hyphenated ROT-13) is a simple letter substitution cipher that replaces a letter with the 13th letter after it in the alphabet. ROT13 is a special case of the Caesar cipher which was developed in ancient Rome. Because there are 26 letters (2×13) in the basic Latin alphabet, ROT13 is its own inverse; that is, to undo ROT13, the same algorithm is applied, so the same action can be used for encoding and decoding. The algorithm provides virtually no cryptographic security, and is often cited as a canonical example of weak encryption.\n * @see - [wiki](https://en.wikipedia.org/wiki/ROT13)\n * @param {String} str - string to be decrypted\n * @return {String} decrypted string\n */\nfunction ROT13(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 (/[n-z]/i.test(char)) {\n      return String.fromCharCode(charCode - 13)\n    }\n\n    return String.fromCharCode(charCode + 13)\n  })\n}\n\nexport default ROT13\n","sourceCodeStart":1,"sourceCodeEnd":25,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Ciphers/ROT13.js#L1-L25","documentation":"ROT13 rejects non-string input because it calls str.replace(/[a-z]/gi, ...) and relies on charCodeAt. A non-string would coerce badly or throw a vaguer internal error, so the function validates the type up front.","triggerScenarios":"Passing a number, object, null, undefined, array, or Buffer instead of the text string.","commonSituations":"Passing a decoded-but-still-typed value, a Buffer from crypto/file input, or a value read from a typed array.","solutions":["Pass a string: ROT13('hello').","Wrap with String(value) when the source might be non-string.","Decode Buffers with .toString('utf8')."],"exampleFix":"// before\nROT13(userId) // number\n// after\nROT13(String(userId))","handlingStrategy":"type-guard","validationCode":"function rot13Safe(v) {\n  return ROT13(typeof v === 'string' ? v : String(v));\n}","typeGuard":"/** @param {unknown} s @returns {s is string} */\nconst isString = s => typeof s === 'string';","tryCatchPattern":"try { return ROT13(text); }\ncatch (e) {\n  if (e instanceof TypeError && /should be string/.test(e.message)) {\n    return ROT13(String(text));\n  }\n  throw e;\n}","preventionTips":["Wrap unknown values with String() before ciphering.","Decode Buffers to utf8 first.","Validate typeof at the input boundary."],"tags":["cipher","rot13","type-validation","string"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}