@jrrembert/luhnjs 1.0.1-rc.2 → 1.1.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.
Files changed (2) hide show
  1. package/dist/src/luhn.js +30 -16
  2. package/package.json +1 -1
package/dist/src/luhn.js CHANGED
@@ -33,6 +33,30 @@ function handleErrors(value) {
33
33
  throw new Error('string must be convertible to a number');
34
34
  }
35
35
  }
36
+ /**
37
+ * Validates that input is a non-empty string with valid characters for the given modulus
38
+ *
39
+ * @param value - String to validate
40
+ * @param n - Modulus (determines valid character set)
41
+ * @throws {Error} If value is not a string, is empty, contains spaces, or has invalid characters
42
+ */
43
+ function handleModNErrors(value, n) {
44
+ if (typeof value !== 'string') {
45
+ throw new Error(`value must be a string - received ${value}`);
46
+ }
47
+ if (!value.length) {
48
+ throw new Error('string cannot be empty');
49
+ }
50
+ if (value.includes(' ')) {
51
+ throw new Error('string cannot contain spaces');
52
+ }
53
+ const validChars = CODE_POINTS.slice(0, n);
54
+ for (const char of value) {
55
+ if (!validChars.includes(char.toUpperCase())) {
56
+ throw new Error(`invalid character: <${char}>`);
57
+ }
58
+ }
59
+ }
36
60
  /**
37
61
  * Generates a Luhn algorithm checksum digit for a string of numbers
38
62
  *
@@ -121,10 +145,10 @@ function random(length) {
121
145
  * @returns String containing either the checksum character alone or input with checksum appended
122
146
  */
123
147
  function generateModN(value, n, options) {
124
- handleErrors(value);
125
148
  if (n < 1 || n > 36) {
126
149
  throw new Error('n must be between 1 and 36');
127
150
  }
151
+ handleModNErrors(value, n);
128
152
  const checkSum = checksumModN(value, n);
129
153
  const checkChar = CODE_POINTS[checkSum];
130
154
  return (options === null || options === void 0 ? void 0 : options.checkSumOnly) ? checkChar : value.concat(checkChar);
@@ -137,18 +161,13 @@ function generateModN(value, n, options) {
137
161
  * @returns boolean indicating whether the checksum is valid
138
162
  */
139
163
  function validateModN(value, n) {
140
- if (typeof value !== 'string') {
141
- throw new Error(`value must be a string - received ${value}`);
142
- }
143
- if (!value.length) {
144
- throw new Error('string cannot be empty');
164
+ if (n < 1 || n > 36) {
165
+ throw new Error('n must be between 1 and 36');
145
166
  }
167
+ handleModNErrors(value, n);
146
168
  if (value.length === 1) {
147
169
  throw new Error('string must be longer than 1 character');
148
170
  }
149
- if (n < 1 || n > 36) {
150
- throw new Error('n must be between 1 and 36');
151
- }
152
171
  const valueWithoutCheckSum = value.substring(0, value.length - 1);
153
172
  return value === generateModN(valueWithoutCheckSum, n);
154
173
  }
@@ -158,7 +177,7 @@ function validateModN(value, n) {
158
177
  function charToInt(char) {
159
178
  const index = CODE_POINTS.indexOf(char.toUpperCase());
160
179
  if (index === -1) {
161
- throw new Error(`Invalid character: ${char}`);
180
+ throw new Error(`invalid character: <${char}>`);
162
181
  }
163
182
  return index;
164
183
  }
@@ -170,15 +189,10 @@ function charToInt(char) {
170
189
  * @returns Check digit as a number
171
190
  */
172
191
  function checksumModN(value, n) {
173
- if (typeof value !== 'string') {
174
- throw new Error(`value must be a string - received ${value}`);
175
- }
176
- if (!value.length) {
177
- throw new Error('string cannot be empty');
178
- }
179
192
  if (n < 1 || n > 36) {
180
193
  throw new Error('n must be between 1 and 36');
181
194
  }
195
+ handleModNErrors(value, n);
182
196
  const chars = Array.from(value);
183
197
  let factor = 2;
184
198
  let sum = 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jrrembert/luhnjs",
3
- "version": "1.0.1-rc.2",
3
+ "version": "1.1.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [