redux-freeform 6.2.0-beta.1 → 6.2.1-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -150,43 +150,42 @@ validatorFns[NO_MORE_THAN_SIX_NUMBERS] = (value) => {
150
150
  * https://stripe.com/en-ca/resources/more/how-to-use-the-luhn-algorithm-a-guide-in-applications-for-businesses#luhn-algorithm-formula
151
151
  */
152
152
 
153
- function luhnValid(string) {
154
- if (!string) {
153
+ function luhnValid(string) { if (!string) {
155
154
  return false;
156
155
  }
157
156
 
157
+ // Extract all digit characters from the string.
158
158
  const digits = string.match(/\d/g);
159
159
  if (!digits) {
160
160
  return false;
161
161
  }
162
162
 
163
- const digitArray = digits.map(Number);
164
163
  let totalSum = 0;
165
-
166
- // Start from the right.
167
- for (let i = digitArray.length - 1; i >= 0; i -= 2) {
168
- const lastDigit = digitArray[i];
169
- const secondToLastDigit = digitArray[i - 1];
170
-
171
- if (secondToLastDigit !== undefined) {
172
- // Double the value of the second-to-last digit and continue doing the same for every second digit.
173
- let secondToLastDigitDoubled = secondToLastDigit * 2;
174
-
175
- // If the result of any doubling operation is greater than nine,
176
- // then add the digits of the result together to obtain a single-digit number.
177
- if (secondToLastDigitDoubled > 9) {
178
- secondToLastDigitDoubled -= 9;
164
+ let shouldDouble = false;
165
+
166
+ // Process digits from right to left
167
+ for (let i = digits.length - 1; i >= 0; i--) {
168
+ // Convert the current character to a number.
169
+ let digit = parseInt(digits[i], 10);
170
+
171
+ // Double the digit and adjust if it exceeds 9.
172
+ if (shouldDouble) {
173
+ digit *= 2;
174
+ if (digit > 9) {
175
+ // If the result of any doubling operation is greater than nine,
176
+ // then add the digits of the result together to obtain a single-digit number.
177
+ // This is equivalent to adding the digits together (e.g. 16: 1 + 6 = 7 = 16 - 9).
178
+ digit -= 9;
179
179
  }
180
-
181
- totalSum += secondToLastDigitDoubled;
182
180
  }
183
181
 
184
- // Find the sum of all the digits that you didn't double and the new values that you got from doubling.
185
- totalSum += lastDigit;
182
+ // Add the processed digit to the total sum.
183
+ totalSum += digit;
184
+ // After doubling the value of the second-to-last digit, continue doing the same for every other digit.
185
+ shouldDouble = !shouldDouble;
186
186
  }
187
187
 
188
- // Determine if the total sum is a multiple of ten.
189
- // According to the Luhn algorithm, the number is considered to be valid if the total ends in zero.
188
+ // The number is valid if totalSum is divisible by 10.
190
189
  return totalSum % 10 === 0;
191
190
  }
192
191
 
@@ -203,7 +202,7 @@ validatorFns[INCLUDES_POTENTIAL_CARD_NUMBER] = (value) => {
203
202
  // A potential credit card number is a sequence of 8 to 20 digits, with optional spaces or hyphens between them
204
203
  const regexString = '(?:\\d[ -]*){8,20}';
205
204
  const regex = new RegExp(regexString);
206
- return regex.test(value) && luhnValid(value);
205
+ return !(luhnValid(value) && regex.test(value));
207
206
  };
208
207
 
209
208
  const NUMBER_LESS_THAN = 'validator/NUMBER_LESS_THAN';
@@ -146,43 +146,42 @@ validatorFns[NO_MORE_THAN_SIX_NUMBERS] = (value) => {
146
146
  * https://stripe.com/en-ca/resources/more/how-to-use-the-luhn-algorithm-a-guide-in-applications-for-businesses#luhn-algorithm-formula
147
147
  */
148
148
 
149
- function luhnValid(string) {
150
- if (!string) {
149
+ function luhnValid(string) { if (!string) {
151
150
  return false;
152
151
  }
153
152
 
153
+ // Extract all digit characters from the string.
154
154
  const digits = string.match(/\d/g);
155
155
  if (!digits) {
156
156
  return false;
157
157
  }
158
158
 
159
- const digitArray = digits.map(Number);
160
159
  let totalSum = 0;
161
-
162
- // Start from the right.
163
- for (let i = digitArray.length - 1; i >= 0; i -= 2) {
164
- const lastDigit = digitArray[i];
165
- const secondToLastDigit = digitArray[i - 1];
166
-
167
- if (secondToLastDigit !== undefined) {
168
- // Double the value of the second-to-last digit and continue doing the same for every second digit.
169
- let secondToLastDigitDoubled = secondToLastDigit * 2;
170
-
171
- // If the result of any doubling operation is greater than nine,
172
- // then add the digits of the result together to obtain a single-digit number.
173
- if (secondToLastDigitDoubled > 9) {
174
- secondToLastDigitDoubled -= 9;
160
+ let shouldDouble = false;
161
+
162
+ // Process digits from right to left
163
+ for (let i = digits.length - 1; i >= 0; i--) {
164
+ // Convert the current character to a number.
165
+ let digit = parseInt(digits[i], 10);
166
+
167
+ // Double the digit and adjust if it exceeds 9.
168
+ if (shouldDouble) {
169
+ digit *= 2;
170
+ if (digit > 9) {
171
+ // If the result of any doubling operation is greater than nine,
172
+ // then add the digits of the result together to obtain a single-digit number.
173
+ // This is equivalent to adding the digits together (e.g. 16: 1 + 6 = 7 = 16 - 9).
174
+ digit -= 9;
175
175
  }
176
-
177
- totalSum += secondToLastDigitDoubled;
178
176
  }
179
177
 
180
- // Find the sum of all the digits that you didn't double and the new values that you got from doubling.
181
- totalSum += lastDigit;
178
+ // Add the processed digit to the total sum.
179
+ totalSum += digit;
180
+ // After doubling the value of the second-to-last digit, continue doing the same for every other digit.
181
+ shouldDouble = !shouldDouble;
182
182
  }
183
183
 
184
- // Determine if the total sum is a multiple of ten.
185
- // According to the Luhn algorithm, the number is considered to be valid if the total ends in zero.
184
+ // The number is valid if totalSum is divisible by 10.
186
185
  return totalSum % 10 === 0;
187
186
  }
188
187
 
@@ -199,7 +198,7 @@ validatorFns[INCLUDES_POTENTIAL_CARD_NUMBER] = (value) => {
199
198
  // A potential credit card number is a sequence of 8 to 20 digits, with optional spaces or hyphens between them
200
199
  const regexString = '(?:\\d[ -]*){8,20}';
201
200
  const regex = new RegExp(regexString);
202
- return regex.test(value) && luhnValid(value);
201
+ return !(luhnValid(value) && regex.test(value));
203
202
  };
204
203
 
205
204
  const NUMBER_LESS_THAN = 'validator/NUMBER_LESS_THAN';
@@ -152,43 +152,42 @@
152
152
  * https://stripe.com/en-ca/resources/more/how-to-use-the-luhn-algorithm-a-guide-in-applications-for-businesses#luhn-algorithm-formula
153
153
  */
154
154
 
155
- function luhnValid(string) {
156
- if (!string) {
155
+ function luhnValid(string) { if (!string) {
157
156
  return false;
158
157
  }
159
158
 
159
+ // Extract all digit characters from the string.
160
160
  const digits = string.match(/\d/g);
161
161
  if (!digits) {
162
162
  return false;
163
163
  }
164
164
 
165
- const digitArray = digits.map(Number);
166
165
  let totalSum = 0;
167
-
168
- // Start from the right.
169
- for (let i = digitArray.length - 1; i >= 0; i -= 2) {
170
- const lastDigit = digitArray[i];
171
- const secondToLastDigit = digitArray[i - 1];
172
-
173
- if (secondToLastDigit !== undefined) {
174
- // Double the value of the second-to-last digit and continue doing the same for every second digit.
175
- let secondToLastDigitDoubled = secondToLastDigit * 2;
176
-
177
- // If the result of any doubling operation is greater than nine,
178
- // then add the digits of the result together to obtain a single-digit number.
179
- if (secondToLastDigitDoubled > 9) {
180
- secondToLastDigitDoubled -= 9;
166
+ let shouldDouble = false;
167
+
168
+ // Process digits from right to left
169
+ for (let i = digits.length - 1; i >= 0; i--) {
170
+ // Convert the current character to a number.
171
+ let digit = parseInt(digits[i], 10);
172
+
173
+ // Double the digit and adjust if it exceeds 9.
174
+ if (shouldDouble) {
175
+ digit *= 2;
176
+ if (digit > 9) {
177
+ // If the result of any doubling operation is greater than nine,
178
+ // then add the digits of the result together to obtain a single-digit number.
179
+ // This is equivalent to adding the digits together (e.g. 16: 1 + 6 = 7 = 16 - 9).
180
+ digit -= 9;
181
181
  }
182
-
183
- totalSum += secondToLastDigitDoubled;
184
182
  }
185
183
 
186
- // Find the sum of all the digits that you didn't double and the new values that you got from doubling.
187
- totalSum += lastDigit;
184
+ // Add the processed digit to the total sum.
185
+ totalSum += digit;
186
+ // After doubling the value of the second-to-last digit, continue doing the same for every other digit.
187
+ shouldDouble = !shouldDouble;
188
188
  }
189
189
 
190
- // Determine if the total sum is a multiple of ten.
191
- // According to the Luhn algorithm, the number is considered to be valid if the total ends in zero.
190
+ // The number is valid if totalSum is divisible by 10.
192
191
  return totalSum % 10 === 0;
193
192
  }
194
193
 
@@ -205,7 +204,7 @@
205
204
  // A potential credit card number is a sequence of 8 to 20 digits, with optional spaces or hyphens between them
206
205
  const regexString = '(?:\\d[ -]*){8,20}';
207
206
  const regex = new RegExp(regexString);
208
- return regex.test(value) && luhnValid(value);
207
+ return !(luhnValid(value) && regex.test(value));
209
208
  };
210
209
 
211
210
  const NUMBER_LESS_THAN = 'validator/NUMBER_LESS_THAN';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "redux-freeform",
3
- "version": "6.2.0-beta.1",
3
+ "version": "6.2.1-beta.0",
4
4
  "description": "A small Redux form library that supports purely functional apps, without magic",
5
5
  "main": "dist/redux-freeform.cjs.js",
6
6
  "module": "dist/redux-freeform.esm.js",