card-validator 9.1.0 → 10.0.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.
- package/README.md +2 -2
- package/dist/postal-code.js +4 -0
- package/package.json +1 -1
- package/src/__tests__/postal-code.ts +14 -0
- package/src/postal-code.ts +3 -0
package/README.md
CHANGED
|
@@ -345,7 +345,7 @@ The `cvv` validation by default tests for a numeric string of 3 characters in le
|
|
|
345
345
|
|
|
346
346
|
#### `valid.postalCode(value: string, [options: object]): object`
|
|
347
347
|
|
|
348
|
-
The `postalCode` validation essentially tests for a valid string greater than 3 characters in length.
|
|
348
|
+
The `postalCode` validation essentially ignores leading/trailing whitespace and tests for a valid string greater than 3 characters in length. It also verifies that the first 3 letters are alphanumeric.
|
|
349
349
|
|
|
350
350
|
```javascript
|
|
351
351
|
{
|
|
@@ -354,7 +354,7 @@ The `postalCode` validation essentially tests for a valid string greater than 3
|
|
|
354
354
|
}
|
|
355
355
|
```
|
|
356
356
|
|
|
357
|
-
You can optionally pass `minLength` as a property of an object as a second argument. This will override the default min length of 3.
|
|
357
|
+
You can optionally pass `minLength` as a property of an object as a second argument. This will override the default min length of 3 and verify that the characters for the specified length are all alphanumeric.
|
|
358
358
|
|
|
359
359
|
```javascript
|
|
360
360
|
valid.postalCode('123', {minLength: 5});
|
package/dist/postal-code.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.postalCode = void 0;
|
|
4
4
|
var DEFAULT_MIN_POSTAL_CODE_LENGTH = 3;
|
|
5
|
+
var ALPHANUM = new RegExp(/^[a-z0-9]+$/i);
|
|
5
6
|
function verification(isValid, isPotentiallyValid) {
|
|
6
7
|
return { isValid: isValid, isPotentiallyValid: isPotentiallyValid };
|
|
7
8
|
}
|
|
@@ -14,6 +15,9 @@ function postalCode(value, options) {
|
|
|
14
15
|
else if (value.length < minLength) {
|
|
15
16
|
return verification(false, true);
|
|
16
17
|
}
|
|
18
|
+
else if (!ALPHANUM.test(value.trim().slice(0, minLength))) {
|
|
19
|
+
return verification(false, true);
|
|
20
|
+
}
|
|
17
21
|
return verification(true, true);
|
|
18
22
|
}
|
|
19
23
|
exports.postalCode = postalCode;
|
package/package.json
CHANGED
|
@@ -41,6 +41,16 @@ describe("postalCode", () => {
|
|
|
41
41
|
[["hello world", { isValid: true, isPotentiallyValid: true }]],
|
|
42
42
|
],
|
|
43
43
|
|
|
44
|
+
[
|
|
45
|
+
"returns isPotentiallyValid for strings without alphanumeric first three characters",
|
|
46
|
+
[
|
|
47
|
+
["123", { isValid: true, isPotentiallyValid: true }],
|
|
48
|
+
[" 123", { isValid: true, isPotentiallyValid: true }],
|
|
49
|
+
["---", { isValid: false, isPotentiallyValid: true }],
|
|
50
|
+
[" ---", { isValid: false, isPotentiallyValid: true }],
|
|
51
|
+
],
|
|
52
|
+
],
|
|
53
|
+
|
|
44
54
|
[
|
|
45
55
|
"returns isPotentiallyValid for shorter-than-3 strings",
|
|
46
56
|
[
|
|
@@ -87,6 +97,10 @@ describe("postalCode", () => {
|
|
|
87
97
|
isValid: true,
|
|
88
98
|
isPotentiallyValid: true,
|
|
89
99
|
});
|
|
100
|
+
expect(postalCode(" -$%", { minLength: 0 })).toEqual({
|
|
101
|
+
isValid: false,
|
|
102
|
+
isPotentiallyValid: true,
|
|
103
|
+
});
|
|
90
104
|
});
|
|
91
105
|
});
|
|
92
106
|
});
|
package/src/postal-code.ts
CHANGED
|
@@ -5,6 +5,7 @@ type PostalCodeOptions = {
|
|
|
5
5
|
};
|
|
6
6
|
|
|
7
7
|
const DEFAULT_MIN_POSTAL_CODE_LENGTH = 3;
|
|
8
|
+
const ALPHANUM = new RegExp(/^[a-z0-9]+$/i);
|
|
8
9
|
|
|
9
10
|
function verification(
|
|
10
11
|
isValid: boolean,
|
|
@@ -23,6 +24,8 @@ export function postalCode(
|
|
|
23
24
|
return verification(false, false);
|
|
24
25
|
} else if (value.length < minLength) {
|
|
25
26
|
return verification(false, true);
|
|
27
|
+
} else if (!ALPHANUM.test(value.trim().slice(0, minLength))) {
|
|
28
|
+
return verification(false, true);
|
|
26
29
|
}
|
|
27
30
|
|
|
28
31
|
return verification(true, true);
|