@pallasoft/ps-utilities 2.7.0 → 2.8.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/dist/common/amount-service.d.ts +5 -0
- package/dist/common/amount-service.js +35 -0
- package/dist/common/constants/amount-config.d.ts +7 -0
- package/dist/common/constants/amount-config.js +10 -0
- package/dist/common/constants/index.d.ts +4 -0
- package/dist/common/constants/index.js +4 -0
- package/dist/common/constants/phone-config.d.ts +6 -0
- package/dist/common/constants/phone-config.js +9 -0
- package/dist/common/constants/postal-code-formats.d.ts +5 -0
- package/dist/common/constants/postal-code-formats.js +13 -0
- package/dist/common/constants/validation-config.d.ts +8 -0
- package/dist/common/constants/validation-config.js +11 -0
- package/dist/common/index.d.ts +6 -2
- package/dist/common/index.js +6 -2
- package/dist/common/phone-service.d.ts +14 -0
- package/dist/common/phone-service.js +78 -0
- package/dist/common/postal-code-service.d.ts +5 -0
- package/dist/common/postal-code-service.js +42 -0
- package/dist/common/validation-service.d.ts +7 -0
- package/dist/common/validation-service.js +36 -0
- package/dist/index.d.ts +3 -0
- package/dist/models/phone-country-data.d.ts +5 -0
- package/dist/models/phone-country-data.js +2 -0
- package/dist/models/phone-display-data.d.ts +4 -0
- package/dist/models/phone-display-data.js +2 -0
- package/dist/models/postal-code-format-data.d.ts +5 -0
- package/dist/models/postal-code-format-data.js +2 -0
- package/package.json +3 -2
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AmountService = void 0;
|
|
4
|
+
var amount_config_1 = require("./constants/amount-config");
|
|
5
|
+
var AmountService = /** @class */ (function () {
|
|
6
|
+
function AmountService() {
|
|
7
|
+
}
|
|
8
|
+
AmountService.normalise = function (raw) {
|
|
9
|
+
var trimmed = raw.trim();
|
|
10
|
+
var isBracketed = trimmed.startsWith(amount_config_1.AmountConfig.openBracket) && trimmed.endsWith(amount_config_1.AmountConfig.closeBracket);
|
|
11
|
+
var isNegative = isBracketed || trimmed.startsWith(amount_config_1.AmountConfig.minus);
|
|
12
|
+
var kept = trimmed.replace(amount_config_1.AmountConfig.disallowed, '');
|
|
13
|
+
var pointIndex = kept.indexOf(amount_config_1.AmountConfig.point);
|
|
14
|
+
var digits = pointIndex === -1
|
|
15
|
+
? kept
|
|
16
|
+
: "".concat(kept.slice(0, pointIndex + 1)).concat(kept
|
|
17
|
+
.slice(pointIndex + 1)
|
|
18
|
+
.split(amount_config_1.AmountConfig.point)
|
|
19
|
+
.join(''));
|
|
20
|
+
if (digits === '')
|
|
21
|
+
return isNegative && !isBracketed ? amount_config_1.AmountConfig.minus : '';
|
|
22
|
+
return isNegative ? "".concat(amount_config_1.AmountConfig.minus).concat(digits) : digits;
|
|
23
|
+
};
|
|
24
|
+
AmountService.toNumber = function (raw) {
|
|
25
|
+
var normalised = AmountService.normalise(raw);
|
|
26
|
+
var parsed = Number(normalised);
|
|
27
|
+
return normalised === '' || !Number.isFinite(parsed) ? 0 : parsed;
|
|
28
|
+
};
|
|
29
|
+
AmountService.isAmount = function (raw) {
|
|
30
|
+
var normalised = AmountService.normalise(raw);
|
|
31
|
+
return (normalised !== '' && normalised !== amount_config_1.AmountConfig.minus && Number.isFinite(Number(normalised)));
|
|
32
|
+
};
|
|
33
|
+
return AmountService;
|
|
34
|
+
}());
|
|
35
|
+
exports.AmountService = AmountService;
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
export * from './amount-config';
|
|
1
2
|
export * from './email-preview-config';
|
|
3
|
+
export * from './phone-config';
|
|
4
|
+
export * from './postal-code-formats';
|
|
2
5
|
export * from './signed-token-config';
|
|
3
6
|
export * from './user-token-config';
|
|
7
|
+
export * from './validation-config';
|
|
@@ -14,6 +14,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./amount-config"), exports);
|
|
17
18
|
__exportStar(require("./email-preview-config"), exports);
|
|
19
|
+
__exportStar(require("./phone-config"), exports);
|
|
20
|
+
__exportStar(require("./postal-code-formats"), exports);
|
|
18
21
|
__exportStar(require("./signed-token-config"), exports);
|
|
19
22
|
__exportStar(require("./user-token-config"), exports);
|
|
23
|
+
__exportStar(require("./validation-config"), exports);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PostalCodeConfig = exports.PostalCodeFormats = void 0;
|
|
4
|
+
exports.PostalCodeFormats = {
|
|
5
|
+
CA: { allowed: /[^A-Z0-9]/g, groups: [3, 3], separator: ' ' },
|
|
6
|
+
GB: { allowed: /[^A-Z0-9]/g, groups: [], separator: ' ' },
|
|
7
|
+
IN: { allowed: /[^0-9]/g, groups: [6], separator: '' },
|
|
8
|
+
NL: { allowed: /[^A-Z0-9]/g, groups: [4, 2], separator: ' ' },
|
|
9
|
+
US: { allowed: /[^0-9]/g, groups: [5, 4], separator: '-' },
|
|
10
|
+
};
|
|
11
|
+
exports.PostalCodeConfig = {
|
|
12
|
+
fallbackAllowed: /[^A-Z0-9 -]/g,
|
|
13
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ValidationConfig = void 0;
|
|
4
|
+
exports.ValidationConfig = {
|
|
5
|
+
email: /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
|
|
6
|
+
webProtocols: ['http:', 'https:'],
|
|
7
|
+
hostSeparator: '.',
|
|
8
|
+
urlProtocol: 'https://',
|
|
9
|
+
urlProtocolPattern: /^[a-z][a-z0-9+.-]*:\/\//i,
|
|
10
|
+
uuid: /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
|
|
11
|
+
};
|
package/dist/common/index.d.ts
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
|
+
export * from './amount-service';
|
|
1
2
|
export * from './color-service';
|
|
2
3
|
export * from './constants';
|
|
3
4
|
export * from './crypto-service';
|
|
5
|
+
export * from './date-service';
|
|
4
6
|
export * from './email-preview-service';
|
|
5
7
|
export * from './encrypted-fields-service';
|
|
6
8
|
export * from './envelope-crypto-service';
|
|
7
|
-
export * from './date-service';
|
|
8
9
|
export * from './number-format-service';
|
|
10
|
+
export * from './phone-service';
|
|
11
|
+
export * from './postal-code-service';
|
|
9
12
|
export * from './recaptcha-service';
|
|
10
13
|
export * from './signed-token-service';
|
|
11
|
-
export * from './url-service';
|
|
12
14
|
export * from './string-service';
|
|
13
15
|
export * from './transform-service';
|
|
16
|
+
export * from './url-service';
|
|
14
17
|
export * from './user-token-service';
|
|
18
|
+
export * from './validation-service';
|
package/dist/common/index.js
CHANGED
|
@@ -14,17 +14,21 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./amount-service"), exports);
|
|
17
18
|
__exportStar(require("./color-service"), exports);
|
|
18
19
|
__exportStar(require("./constants"), exports);
|
|
19
20
|
__exportStar(require("./crypto-service"), exports);
|
|
21
|
+
__exportStar(require("./date-service"), exports);
|
|
20
22
|
__exportStar(require("./email-preview-service"), exports);
|
|
21
23
|
__exportStar(require("./encrypted-fields-service"), exports);
|
|
22
24
|
__exportStar(require("./envelope-crypto-service"), exports);
|
|
23
|
-
__exportStar(require("./date-service"), exports);
|
|
24
25
|
__exportStar(require("./number-format-service"), exports);
|
|
26
|
+
__exportStar(require("./phone-service"), exports);
|
|
27
|
+
__exportStar(require("./postal-code-service"), exports);
|
|
25
28
|
__exportStar(require("./recaptcha-service"), exports);
|
|
26
29
|
__exportStar(require("./signed-token-service"), exports);
|
|
27
|
-
__exportStar(require("./url-service"), exports);
|
|
28
30
|
__exportStar(require("./string-service"), exports);
|
|
29
31
|
__exportStar(require("./transform-service"), exports);
|
|
32
|
+
__exportStar(require("./url-service"), exports);
|
|
30
33
|
__exportStar(require("./user-token-service"), exports);
|
|
34
|
+
__exportStar(require("./validation-service"), exports);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type CountryCode } from 'libphonenumber-js';
|
|
2
|
+
import type { PhoneCountryData } from '../models/phone-country-data';
|
|
3
|
+
import type { PhoneDisplayData } from '../models/phone-display-data';
|
|
4
|
+
export declare class PhoneService {
|
|
5
|
+
static isCountry(code: string): code is CountryCode;
|
|
6
|
+
static countries(preferred: string, locale?: string): PhoneCountryData[];
|
|
7
|
+
static display(value: string, fallbackCountry: string): PhoneDisplayData;
|
|
8
|
+
static format(national: string, country: string): string;
|
|
9
|
+
static formatForDisplay(value: string, country: string): string;
|
|
10
|
+
static toStored(national: string, country: string): string;
|
|
11
|
+
static isValid(national: string, country: string): boolean;
|
|
12
|
+
static callingCode(country: string): string;
|
|
13
|
+
private static parse;
|
|
14
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PhoneService = void 0;
|
|
4
|
+
var libphonenumber_js_1 = require("libphonenumber-js");
|
|
5
|
+
var phone_config_1 = require("./constants/phone-config");
|
|
6
|
+
var PhoneService = /** @class */ (function () {
|
|
7
|
+
function PhoneService() {
|
|
8
|
+
}
|
|
9
|
+
PhoneService.isCountry = function (code) {
|
|
10
|
+
return (0, libphonenumber_js_1.getCountries)().includes(code);
|
|
11
|
+
};
|
|
12
|
+
PhoneService.countries = function (preferred, locale) {
|
|
13
|
+
if (locale === void 0) { locale = phone_config_1.PhoneConfig.locale; }
|
|
14
|
+
var names = new Intl.DisplayNames([locale], { type: phone_config_1.PhoneConfig.displayNamesType });
|
|
15
|
+
var options = (0, libphonenumber_js_1.getCountries)().map(function (code) {
|
|
16
|
+
var name = names.of(code);
|
|
17
|
+
return {
|
|
18
|
+
callingCode: (0, libphonenumber_js_1.getCountryCallingCode)(code),
|
|
19
|
+
code: code,
|
|
20
|
+
label: name !== undefined ? name : code,
|
|
21
|
+
};
|
|
22
|
+
});
|
|
23
|
+
return options.sort(function (left, right) {
|
|
24
|
+
if (left.code === preferred)
|
|
25
|
+
return -1;
|
|
26
|
+
if (right.code === preferred)
|
|
27
|
+
return 1;
|
|
28
|
+
return left.label.localeCompare(right.label);
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
PhoneService.display = function (value, fallbackCountry) {
|
|
32
|
+
var parsed = value.startsWith(phone_config_1.PhoneConfig.plus)
|
|
33
|
+
? (0, libphonenumber_js_1.parsePhoneNumberFromString)(value)
|
|
34
|
+
: undefined;
|
|
35
|
+
if (parsed !== undefined && parsed.country !== undefined)
|
|
36
|
+
return { country: parsed.country, national: parsed.formatNational() };
|
|
37
|
+
return { country: fallbackCountry, national: value };
|
|
38
|
+
};
|
|
39
|
+
PhoneService.format = function (national, country) {
|
|
40
|
+
if (!PhoneService.isCountry(country))
|
|
41
|
+
return national;
|
|
42
|
+
return new libphonenumber_js_1.AsYouType(country).input(national);
|
|
43
|
+
};
|
|
44
|
+
PhoneService.formatForDisplay = function (value, country) {
|
|
45
|
+
var parsed = PhoneService.parse(value, country);
|
|
46
|
+
if (parsed === undefined || !parsed.isValid())
|
|
47
|
+
return value.trim();
|
|
48
|
+
return parsed.country === country ? parsed.formatNational() : parsed.formatInternational();
|
|
49
|
+
};
|
|
50
|
+
PhoneService.toStored = function (national, country) {
|
|
51
|
+
var trimmed = national.trim();
|
|
52
|
+
if (trimmed === '' || !PhoneService.isCountry(country))
|
|
53
|
+
return trimmed;
|
|
54
|
+
var parsed = (0, libphonenumber_js_1.parsePhoneNumberFromString)(trimmed, country);
|
|
55
|
+
return parsed !== undefined ? parsed.number : trimmed;
|
|
56
|
+
};
|
|
57
|
+
PhoneService.isValid = function (national, country) {
|
|
58
|
+
if (national.trim() === '')
|
|
59
|
+
return true;
|
|
60
|
+
var parsed = PhoneService.parse(national, country);
|
|
61
|
+
return parsed !== undefined && parsed.isValid();
|
|
62
|
+
};
|
|
63
|
+
PhoneService.callingCode = function (country) {
|
|
64
|
+
return PhoneService.isCountry(country) ? (0, libphonenumber_js_1.getCountryCallingCode)(country) : '';
|
|
65
|
+
};
|
|
66
|
+
PhoneService.parse = function (value, country) {
|
|
67
|
+
var trimmed = value.trim();
|
|
68
|
+
if (trimmed === '')
|
|
69
|
+
return undefined;
|
|
70
|
+
if (trimmed.startsWith(phone_config_1.PhoneConfig.plus))
|
|
71
|
+
return (0, libphonenumber_js_1.parsePhoneNumberFromString)(trimmed);
|
|
72
|
+
return PhoneService.isCountry(country)
|
|
73
|
+
? (0, libphonenumber_js_1.parsePhoneNumberFromString)(trimmed, country)
|
|
74
|
+
: undefined;
|
|
75
|
+
};
|
|
76
|
+
return PhoneService;
|
|
77
|
+
}());
|
|
78
|
+
exports.PhoneService = PhoneService;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PostalCodeService = void 0;
|
|
4
|
+
var postal_code_formats_1 = require("./constants/postal-code-formats");
|
|
5
|
+
var PostalCodeService = /** @class */ (function () {
|
|
6
|
+
function PostalCodeService() {
|
|
7
|
+
}
|
|
8
|
+
PostalCodeService.format = function (raw, country) {
|
|
9
|
+
var upper = raw.toUpperCase();
|
|
10
|
+
var format = postal_code_formats_1.PostalCodeFormats[country];
|
|
11
|
+
if (format === undefined)
|
|
12
|
+
return upper.replace(postal_code_formats_1.PostalCodeConfig.fallbackAllowed, '');
|
|
13
|
+
var compact = upper.replace(format.allowed, '');
|
|
14
|
+
if (format.groups.length === 0)
|
|
15
|
+
return upper.replace(postal_code_formats_1.PostalCodeConfig.fallbackAllowed, '');
|
|
16
|
+
var parts = [];
|
|
17
|
+
var cursor = 0;
|
|
18
|
+
for (var _i = 0, _a = format.groups; _i < _a.length; _i++) {
|
|
19
|
+
var size = _a[_i];
|
|
20
|
+
if (cursor >= compact.length)
|
|
21
|
+
break;
|
|
22
|
+
parts.push(compact.slice(cursor, cursor + size));
|
|
23
|
+
cursor += size;
|
|
24
|
+
}
|
|
25
|
+
return parts.join(format.separator);
|
|
26
|
+
};
|
|
27
|
+
PostalCodeService.maxLength = function (country) {
|
|
28
|
+
var format = postal_code_formats_1.PostalCodeFormats[country];
|
|
29
|
+
if (format === undefined || format.groups.length === 0)
|
|
30
|
+
return undefined;
|
|
31
|
+
return (format.groups.reduce(function (sum, size) { return sum + size; }, 0) +
|
|
32
|
+
format.separator.length * (format.groups.length - 1));
|
|
33
|
+
};
|
|
34
|
+
PostalCodeService.isValid = function (raw, country) {
|
|
35
|
+
var format = postal_code_formats_1.PostalCodeFormats[country];
|
|
36
|
+
if (raw.trim() === '' || format === undefined || format.groups.length === 0)
|
|
37
|
+
return true;
|
|
38
|
+
return PostalCodeService.format(raw, country).length === PostalCodeService.maxLength(country);
|
|
39
|
+
};
|
|
40
|
+
return PostalCodeService;
|
|
41
|
+
}());
|
|
42
|
+
exports.PostalCodeService = PostalCodeService;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare class ValidationService {
|
|
2
|
+
static isEmail(value: string): boolean;
|
|
3
|
+
static withProtocol(value: string): string;
|
|
4
|
+
static isWebUrl(value: string): boolean;
|
|
5
|
+
static isPhone(value: string, country: string): boolean;
|
|
6
|
+
static isUuid(value: string): boolean;
|
|
7
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ValidationService = void 0;
|
|
4
|
+
var validation_config_1 = require("./constants/validation-config");
|
|
5
|
+
var phone_service_1 = require("./phone-service");
|
|
6
|
+
var ValidationService = /** @class */ (function () {
|
|
7
|
+
function ValidationService() {
|
|
8
|
+
}
|
|
9
|
+
ValidationService.isEmail = function (value) {
|
|
10
|
+
return validation_config_1.ValidationConfig.email.test(value.trim());
|
|
11
|
+
};
|
|
12
|
+
ValidationService.withProtocol = function (value) {
|
|
13
|
+
var trimmed = value.trim();
|
|
14
|
+
if (trimmed === '' || validation_config_1.ValidationConfig.urlProtocolPattern.test(trimmed))
|
|
15
|
+
return trimmed;
|
|
16
|
+
return "".concat(validation_config_1.ValidationConfig.urlProtocol).concat(trimmed);
|
|
17
|
+
};
|
|
18
|
+
ValidationService.isWebUrl = function (value) {
|
|
19
|
+
try {
|
|
20
|
+
var url_1 = new URL(ValidationService.withProtocol(value));
|
|
21
|
+
return (validation_config_1.ValidationConfig.webProtocols.some(function (protocol) { return protocol === url_1.protocol; }) &&
|
|
22
|
+
url_1.hostname.includes(validation_config_1.ValidationConfig.hostSeparator));
|
|
23
|
+
}
|
|
24
|
+
catch (_a) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
ValidationService.isPhone = function (value, country) {
|
|
29
|
+
return value.trim() !== '' && phone_service_1.PhoneService.isValid(value, country);
|
|
30
|
+
};
|
|
31
|
+
ValidationService.isUuid = function (value) {
|
|
32
|
+
return validation_config_1.ValidationConfig.uuid.test(value.trim());
|
|
33
|
+
};
|
|
34
|
+
return ValidationService;
|
|
35
|
+
}());
|
|
36
|
+
exports.ValidationService = ValidationService;
|
package/dist/index.d.ts
CHANGED
|
@@ -12,3 +12,6 @@ export type { UserTokenSubject } from './models/user-token-subject';
|
|
|
12
12
|
export type { UserTokenVerifyOptions } from './models/user-token-verify-options';
|
|
13
13
|
export type { EncryptedFieldsSpec } from './common/encrypted-fields-service';
|
|
14
14
|
export type { DataKeyPair } from './common/envelope-crypto-service';
|
|
15
|
+
export type { PhoneCountryData } from './models/phone-country-data';
|
|
16
|
+
export type { PhoneDisplayData } from './models/phone-display-data';
|
|
17
|
+
export type { PostalCodeFormatData } from './models/postal-code-format-data';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pallasoft/ps-utilities",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.8.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -69,7 +69,8 @@
|
|
|
69
69
|
"dependencies": {
|
|
70
70
|
"@esbuild-plugins/tsconfig-paths": "^0.1.2",
|
|
71
71
|
"dayjs": "^1.11.19",
|
|
72
|
-
"esbuild": "^0.27.0"
|
|
72
|
+
"esbuild": "^0.27.0",
|
|
73
|
+
"libphonenumber-js": "^1.13.14"
|
|
73
74
|
},
|
|
74
75
|
"publishConfig": {
|
|
75
76
|
"access": "public",
|