Options.esm.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { buildRankedDictionary } from './helper.esm.js';
  2. import l33tTable from './data/l33tTable.esm.js';
  3. import translationKeys from './data/translationKeys.esm.js';
  4. class Options {
  5. constructor() {
  6. this.matchers = {};
  7. this.l33tTable = l33tTable;
  8. this.dictionary = {
  9. userInputs: []
  10. };
  11. this.rankedDictionaries = {};
  12. this.rankedDictionariesMaxWordSize = {};
  13. this.translations = translationKeys;
  14. this.graphs = {};
  15. this.useLevenshteinDistance = false;
  16. this.levenshteinThreshold = 2;
  17. this.l33tMaxSubstitutions = 100;
  18. this.maxLength = 256;
  19. this.setRankedDictionaries();
  20. }
  21. // eslint-disable-next-line max-statements,complexity
  22. setOptions(options = {}) {
  23. if (options.l33tTable) {
  24. this.l33tTable = options.l33tTable;
  25. }
  26. if (options.dictionary) {
  27. this.dictionary = options.dictionary;
  28. this.setRankedDictionaries();
  29. }
  30. if (options.translations) {
  31. this.setTranslations(options.translations);
  32. }
  33. if (options.graphs) {
  34. this.graphs = options.graphs;
  35. }
  36. if (options.useLevenshteinDistance !== undefined) {
  37. this.useLevenshteinDistance = options.useLevenshteinDistance;
  38. }
  39. if (options.levenshteinThreshold !== undefined) {
  40. this.levenshteinThreshold = options.levenshteinThreshold;
  41. }
  42. if (options.l33tMaxSubstitutions !== undefined) {
  43. this.l33tMaxSubstitutions = options.l33tMaxSubstitutions;
  44. }
  45. if (options.maxLength !== undefined) {
  46. this.maxLength = options.maxLength;
  47. }
  48. }
  49. setTranslations(translations) {
  50. if (this.checkCustomTranslations(translations)) {
  51. this.translations = translations;
  52. } else {
  53. throw new Error('Invalid translations object fallback to keys');
  54. }
  55. }
  56. checkCustomTranslations(translations) {
  57. let valid = true;
  58. Object.keys(translationKeys).forEach(type => {
  59. if (type in translations) {
  60. const translationType = type;
  61. Object.keys(translationKeys[translationType]).forEach(key => {
  62. if (!(key in translations[translationType])) {
  63. valid = false;
  64. }
  65. });
  66. } else {
  67. valid = false;
  68. }
  69. });
  70. return valid;
  71. }
  72. setRankedDictionaries() {
  73. const rankedDictionaries = {};
  74. const rankedDictionariesMaxWorkSize = {};
  75. Object.keys(this.dictionary).forEach(name => {
  76. rankedDictionaries[name] = this.getRankedDictionary(name);
  77. rankedDictionariesMaxWorkSize[name] = this.getRankedDictionariesMaxWordSize(name);
  78. });
  79. this.rankedDictionaries = rankedDictionaries;
  80. this.rankedDictionariesMaxWordSize = rankedDictionariesMaxWorkSize;
  81. }
  82. getRankedDictionariesMaxWordSize(name) {
  83. const data = this.dictionary[name].map(el => {
  84. if (typeof el !== 'string') {
  85. return el.toString().length;
  86. }
  87. return el.length;
  88. });
  89. // do not use Math.max(...data) because it can result in max stack size error because every entry will be used as an argument
  90. if (data.length === 0) {
  91. return 0;
  92. }
  93. return data.reduce((a, b) => Math.max(a, b), -Infinity);
  94. }
  95. getRankedDictionary(name) {
  96. const list = this.dictionary[name];
  97. if (name === 'userInputs') {
  98. const sanitizedInputs = [];
  99. list.forEach(input => {
  100. const inputType = typeof input;
  101. if (inputType === 'string' || inputType === 'number' || inputType === 'boolean') {
  102. sanitizedInputs.push(input.toString().toLowerCase());
  103. }
  104. });
  105. return buildRankedDictionary(sanitizedInputs);
  106. }
  107. return buildRankedDictionary(list);
  108. }
  109. extendUserInputsDictionary(dictionary) {
  110. if (this.dictionary.userInputs) {
  111. this.dictionary.userInputs = [...this.dictionary.userInputs, ...dictionary];
  112. } else {
  113. this.dictionary.userInputs = dictionary;
  114. }
  115. this.rankedDictionaries.userInputs = this.getRankedDictionary('userInputs');
  116. this.rankedDictionariesMaxWordSize.userInputs = this.getRankedDictionariesMaxWordSize('userInputs');
  117. }
  118. addMatcher(name, matcher) {
  119. if (this.matchers[name]) {
  120. console.info(`Matcher ${name} already exists`);
  121. } else {
  122. this.matchers[name] = matcher;
  123. }
  124. }
  125. }
  126. const zxcvbnOptions = new Options();
  127. export { Options, zxcvbnOptions as default };
  128. //# sourceMappingURL=Options.esm.js.map