redux-freeform 6.2.0-beta.1 → 6.2.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.
@@ -155,38 +155,38 @@ function luhnValid(string) {
155
155
  return false;
156
156
  }
157
157
 
158
+ // Extract all digit characters from the string.
158
159
  const digits = string.match(/\d/g);
159
160
  if (!digits) {
160
161
  return false;
161
162
  }
162
163
 
163
- const digitArray = digits.map(Number);
164
164
  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;
165
+ let shouldDouble = false;
166
+
167
+ // Process digits from right to left
168
+ for (let i = digits.length - 1; i >= 0; i--) {
169
+ // Convert the current character to a number.
170
+ let digit = parseInt(digits[i], 10);
171
+
172
+ // Double the digit and adjust if it exceeds 9.
173
+ if (shouldDouble) {
174
+ digit *= 2;
175
+ if (digit > 9) {
176
+ // If the result of any doubling operation is greater than nine,
177
+ // then add the digits of the result together to obtain a single-digit number.
178
+ // This is equivalent to adding the digits together (e.g. 16: 1 + 6 = 7 = 16 - 9).
179
+ digit -= 9;
179
180
  }
180
-
181
- totalSum += secondToLastDigitDoubled;
182
181
  }
183
182
 
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;
183
+ // Add the processed digit to the total sum.
184
+ totalSum += digit;
185
+ // After doubling the value of the second-to-last digit, continue doing the same for every other digit.
186
+ shouldDouble = !shouldDouble;
186
187
  }
187
188
 
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.
189
+ // The number is valid if totalSum is divisible by 10.
190
190
  return totalSum % 10 === 0;
191
191
  }
192
192
 
@@ -151,38 +151,38 @@ function luhnValid(string) {
151
151
  return false;
152
152
  }
153
153
 
154
+ // Extract all digit characters from the string.
154
155
  const digits = string.match(/\d/g);
155
156
  if (!digits) {
156
157
  return false;
157
158
  }
158
159
 
159
- const digitArray = digits.map(Number);
160
160
  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;
161
+ let shouldDouble = false;
162
+
163
+ // Process digits from right to left
164
+ for (let i = digits.length - 1; i >= 0; i--) {
165
+ // Convert the current character to a number.
166
+ let digit = parseInt(digits[i], 10);
167
+
168
+ // Double the digit and adjust if it exceeds 9.
169
+ if (shouldDouble) {
170
+ digit *= 2;
171
+ if (digit > 9) {
172
+ // If the result of any doubling operation is greater than nine,
173
+ // then add the digits of the result together to obtain a single-digit number.
174
+ // This is equivalent to adding the digits together (e.g. 16: 1 + 6 = 7 = 16 - 9).
175
+ digit -= 9;
175
176
  }
176
-
177
- totalSum += secondToLastDigitDoubled;
178
177
  }
179
178
 
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;
179
+ // Add the processed digit to the total sum.
180
+ totalSum += digit;
181
+ // After doubling the value of the second-to-last digit, continue doing the same for every other digit.
182
+ shouldDouble = !shouldDouble;
182
183
  }
183
184
 
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.
185
+ // The number is valid if totalSum is divisible by 10.
186
186
  return totalSum % 10 === 0;
187
187
  }
188
188
 
@@ -157,38 +157,38 @@
157
157
  return false;
158
158
  }
159
159
 
160
+ // Extract all digit characters from the string.
160
161
  const digits = string.match(/\d/g);
161
162
  if (!digits) {
162
163
  return false;
163
164
  }
164
165
 
165
- const digitArray = digits.map(Number);
166
166
  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;
167
+ let shouldDouble = false;
168
+
169
+ // Process digits from right to left
170
+ for (let i = digits.length - 1; i >= 0; i--) {
171
+ // Convert the current character to a number.
172
+ let digit = parseInt(digits[i], 10);
173
+
174
+ // Double the digit and adjust if it exceeds 9.
175
+ if (shouldDouble) {
176
+ digit *= 2;
177
+ if (digit > 9) {
178
+ // If the result of any doubling operation is greater than nine,
179
+ // then add the digits of the result together to obtain a single-digit number.
180
+ // This is equivalent to adding the digits together (e.g. 16: 1 + 6 = 7 = 16 - 9).
181
+ digit -= 9;
181
182
  }
182
-
183
- totalSum += secondToLastDigitDoubled;
184
183
  }
185
184
 
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;
185
+ // Add the processed digit to the total sum.
186
+ totalSum += digit;
187
+ // After doubling the value of the second-to-last digit, continue doing the same for every other digit.
188
+ shouldDouble = !shouldDouble;
188
189
  }
189
190
 
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.
191
+ // The number is valid if totalSum is divisible by 10.
192
192
  return totalSum % 10 === 0;
193
193
  }
194
194
 
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.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",