{"record":{"id":"5fbc59afaddcc13d","repo":"TheAlgorithms/JavaScript","slug":"the-arg-must-be-a-valid-non-empty-string","errorCode":null,"errorMessage":"The arg must be a valid, non empty string","messagePattern":"The arg must be a valid, non empty string","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"String/PermutateString.js","lineNumber":5,"sourceCode":"'use strict'\n\nconst permutate = (aString) => {\n  if (typeof aString !== 'string' || !aString) {\n    throw new Error('The arg must be a valid, non empty string')\n  }\n  const characters = aString.split('')\n  let permutations = [[characters.shift()]]\n  while (characters.length) {\n    const currentCharacter = characters.shift()\n    permutations = calculateCurrentCharacterPermutation(\n      permutations,\n      currentCharacter\n    )\n  }\n  return permutations\n    .map((character) => character.join(''))\n    .filter((item, index, self) => self.indexOf(item) === index)\n    .sort()\n}\n\nconst calculateCurrentCharacterPermutation = (\n  allPermutations,","sourceCodeStart":1,"sourceCodeEnd":23,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/String/PermutateString.js#L1-L23","documentation":"Thrown by permutate() in String/PermutateString.js when its single argument is not a string, or is the empty string ''. The guard combines a typeof check with a truthiness check, so both non-string inputs (numbers, objects, undefined) and the empty string are rejected before the .split('') permutation algorithm runs. The library refuses to compute permutations of nothing.","triggerScenarios":"Calling permutate(123), permutate(undefined), permutate(null), permutate(['a','b']), permutate({}), or permutate(''). Any value where typeof aString !== 'string' OR the string coerces to falsy (only '') triggers the throw at line 5.","commonSituations":"Reading input from process.argv or a form field that arrives as undefined when omitted; passing a parsed JSON value that was a number; forgetting to default a text input and forwarding an empty string; unit-testing edge cases and supplying '' to mean 'no input'.","solutions":["Pass a non-empty string literal, e.g. permutate('abc').","Coerce or validate upstream: if (typeof input === 'string' && input.length) { permutate(input) }.","Default the argument with a guard: permutate(input ?? '') will still throw on '', so supply a real fallback string instead.","If the caller genuinely may have non-string data, normalize first: permutate(String(input)) only when input is non-null/defined and not empty."],"exampleFix":"// before\nconst result = permutate(userInput) // userInput may be undefined or ''\n\n// after\nif (typeof userInput === 'string' && userInput.length > 0) {\n  const result = permutate(userInput)\n} else {\n  throw new Error('userInput missing')\n}","handlingStrategy":"validation","validationCode":"function permutateSafe(input) {\n  if (typeof input !== 'string' || input.length === 0) {\n    throw new TypeError('permutate requires a non-empty string')\n  }\n  return permutate(input)\n}","typeGuard":"function isNonEmptyString(v) {\n  return typeof v === 'string' && v.length > 0\n}","tryCatchPattern":"try {\n  const perms = permutate(maybeInput)\n} catch (err) {\n  if (/non empty string/.test(err.message)) {\n    // handle bad input\n  } else throw err\n}","preventionTips":["Always typeof-check string inputs at the boundary before forwarding.","Treat empty string as 'missing' explicitly rather than relying on the library to reject it.","Add a thin wrapper in your codebase that type-guards once and re-exports the function."],"tags":["string","validation","type-check","input-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}