{"record":{"id":"7197fd362d241002","repo":"TheAlgorithms/JavaScript","slug":"invalid-capacity","errorCode":null,"errorMessage":"Invalid capacity","messagePattern":"Invalid capacity","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Cache/LRUCache.js","lineNumber":11,"sourceCode":"class LRUCache {\n  // LRU Cache to store a given capacity of data\n  #capacity\n\n  /**\n   * @param {number} capacity - the capacity of LRUCache\n   * @returns {LRUCache} - sealed\n   */\n  constructor(capacity) {\n    if (!Number.isInteger(capacity) || capacity < 0) {\n      throw new TypeError('Invalid capacity')\n    }\n\n    this.#capacity = ~~capacity\n    this.misses = 0\n    this.hits = 0\n    this.cache = new Map()\n\n    return Object.seal(this)\n  }\n\n  get info() {\n    return Object.freeze({\n      misses: this.misses,\n      hits: this.hits,\n      capacity: this.capacity,\n      size: this.size\n    })\n  }","sourceCodeStart":1,"sourceCodeEnd":29,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Cache/LRUCache.js#L1-L29","documentation":"LRUCache constructor requires a non-negative integer capacity. It uses Number.isInteger and a >=0 check, then coerces with ~~capacity. Negative, fractional, NaN, or non-numeric capacities are rejected because the internal Map-based eviction logic needs a whole-number size bound.","triggerScenarios":"new LRUCache(-1), new LRUCache(2.5), new LRUCache(NaN), new LRUCache(\"8\"), new LRUCache(undefined) (NaN), or new LRUCache() (undefined).","commonSituations":"Reading capacity from an env var or config string without converting, computing capacity from a division that yields a fraction, or forgetting the argument entirely.","solutions":["Pass a non-negative integer literal: new LRUCache(128).","Parse and validate config-derived values: Math.max(0, Math.trunc(Number(raw))).","Default the argument explicitly when it may be undefined."],"exampleFix":"// before\nnew LRUCache(process.env.CACHE_SIZE) // string\n// after\nnew LRUCache(Math.max(0, Math.trunc(Number(process.env.CACHE_SIZE) || 0)))","handlingStrategy":"validation","validationCode":"function makeLRU(raw) {\n  const cap = Math.max(0, Math.trunc(Number(raw) || 0));\n  return new LRUCache(cap);\n}","typeGuard":"/** @param {unknown} c @returns {c is number} */\nconst isNonNegInt = c => Number.isInteger(c) && c >= 0;","tryCatchPattern":"try { return new LRUCache(rawCap); }\ncatch (e) {\n  if (e instanceof TypeError && e.message === 'Invalid capacity') {\n    return new LRUCache(Math.max(0, Math.trunc(Number(rawCap) || 0)));\n  }\n  throw e;\n}","preventionTips":["Always convert config/env strings to numbers before constructing.","Default capacity explicitly so the argument is never undefined.","Validate integer + non-negative at the boundary."],"tags":["cache","lru","capacity","constructor","type-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}