{"version":3,"file":"pseudo-shuffle.cjs","sources":["../src/pseudo-shuffle.ts"],"sourcesContent":["import fe1 from \"node-fe1-fpe\";\n\nexport interface IPsuedoShuffleOption {\n  /**\n   * The starting value of the shuffleable range.\n   */\n  min: number;\n  /**\n   * The ending value of the shuffleable range.\n   *\n   * Algorithm can be applied only when the difference\n   * between the min and max values is at least 4.\n   */\n  max: number;\n  /**\n   * The index value to be shuffled.\n   */\n  index: number;\n  /**\n   * The private key used to encrypt the index value.\n   *\n   * If not specified, the default value is used.\n   * (**Default value:** `psuedo-shuffle`)\n   */\n  privateKey?: string;\n  /**\n   * The public key used to encrypt the index value.\n   *\n   * If not specified, the default value is used.\n   * (**Default value:** `psuedo-shuffle`)\n   */\n  publicKey?: string;\n}\n\nconst defaultKey = \"psuedo-shuffle\";\n\nexport const encode = ({\n  index,\n  min,\n  max,\n  privateKey = defaultKey,\n  publicKey = defaultKey,\n}: IPsuedoShuffleOption): number => {\n  // Algorithm can be applied only when the difference\n  // between the min and max values is at least 4.\n  if (max - min < 3) return index;\n\n  // Algorithms can only be applied when the\n  // range difference between min and max is prime number.\n  // Therefore, when the range difference is not prime number,\n  // the algorithm is applied while leaving the last index intact.\n  // if ((max - min) % 2 === 0) {\n  //   if (index === max) return index\n  //   --max\n  // }\n  if ((max - min) % 2 === 0) {\n    const middle = Math.ceil(min + (max - min) / 2);\n    if (index === middle) return max;\n    if (index === max) index = middle;\n    --max;\n  }\n\n  // Algorithm does not apply to index\n  // values that are not in the range.\n  if (index < min || index > max) return index;\n\n  return (\n    fe1.encrypt(\n      max - min + 1,\n      index - min,\n      privateKey,\n      publicKey\n    ) + min\n  );\n};\n\nexport const decode = ({\n  index,\n  min,\n  max,\n  privateKey = defaultKey,\n  publicKey = defaultKey,\n}: IPsuedoShuffleOption): number => {\n  // Algorithm can be applied only when the difference\n  // between the min and max values is at least 4.\n  if (max - min < 3) return index;\n\n  // Algorithms can only be applied when the\n  // range difference between min and max is prime number.\n  // Therefore, when the range difference is not prime number,\n  // the algorithm is applied while leaving the last index intact.\n\n  const isNonPrime = (max - min) % 2 === 0;\n  if (isNonPrime) {\n    if (index > max - 1) {\n      return Math.ceil(min + (max - min) / 2);\n    }\n    --max;\n  }\n\n  // Algorithm does not apply to index\n  // values that are not in the range.\n  if (index < min || index > max) return index;\n\n  if (isNonPrime) {\n    if (\n      index ===\n      encode({\n        index: Math.ceil(min + (max - min) / 2),\n        max,\n        min,\n        privateKey,\n        publicKey,\n      })\n    )\n      return max + 1;\n  }\n  return (\n    fe1.decrypt(\n      max - min + 1,\n      index - min,\n      privateKey,\n      publicKey\n    ) + min\n  );\n};\n"],"names":["defaultKey","encode","_ref","index","min","max","_ref$privateKey","privateKey","_ref$publicKey","publicKey","middle","Math","ceil","fe1","encrypt","_ref2","_ref2$privateKey","_ref2$publicKey","isNonPrime","decrypt"],"mappings":"sHAkCMA,EAAa,iBAENC,EAAS,SAAHC,GAMgB,IALjCC,EAAKD,EAALC,MACAC,EAAGF,EAAHE,IACAC,EAAGH,EAAHG,IAAGC,EAAAJ,EACHK,WAAAA,OAAU,IAAAD,EAAGN,EAAUM,EAAAE,EAAAN,EACvBO,UAAAA,OAAYT,IAAHQ,EAAGR,EAAUQ,EAItB,GAAIH,EAAMD,EAAM,EAAG,OAAOD,EAU1B,IAAKE,EAAMD,GAAO,GAAM,EAAG,CACzB,IAAMM,EAASC,KAAKC,KAAKR,GAAOC,EAAMD,GAAO,GAC7C,GAAID,IAAUO,EAAQ,OAAOL,EACzBF,IAAUE,IAAKF,EAAQO,KACzBL,CACJ,CAIA,OAAIF,EAAQC,GAAOD,EAAQE,EAAYF,EAGrCU,EAAG,QAACC,QACFT,EAAMD,EAAM,EACZD,EAAQC,EACRG,EACAE,GACEL,CAER,iBAEsB,SAAHW,GAMgB,IALjCZ,EAAKY,EAALZ,MACAC,EAAGW,EAAHX,IACAC,EAAGU,EAAHV,IAAGW,EAAAD,EACHR,WAAAA,WAAUS,EAAGhB,EAAUgB,EAAAC,EAAA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