@molopos/shared 2.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/data/countries.d.ts +9 -0
- package/data/countries.js +1592 -0
- package/enum/index.d.ts +124 -0
- package/enum/index.js +139 -0
- package/index.d.ts +1 -0
- package/index.js +17 -0
- package/lib/codice-fiscale/index.d.ts +15 -0
- package/lib/codice-fiscale/index.js +58 -0
- package/lib/codice-fiscale/test.d.ts +1 -0
- package/lib/codice-fiscale/test.js +25 -0
- package/lib/filter/index.d.ts +59 -0
- package/lib/filter/index.js +122 -0
- package/lib/partita-iva/index.d.ts +9 -0
- package/lib/partita-iva/index.js +40 -0
- package/package.json +1 -0
package/enum/index.d.ts
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Customer type enum
|
|
3
|
+
*/
|
|
4
|
+
export declare enum CustomerTypeEnum {
|
|
5
|
+
Private = "PRIVATE",
|
|
6
|
+
Subscription = "SUBSCRIPTION"
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Discount type enum
|
|
10
|
+
*/
|
|
11
|
+
export declare enum DiscountTypeEnum {
|
|
12
|
+
Percentage = "PERCENTAGE",
|
|
13
|
+
Amount = "AMOUNT"
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Payment method enum
|
|
17
|
+
*/
|
|
18
|
+
export declare enum PaymentMethodEnum {
|
|
19
|
+
Paypal = "PAYPAL",
|
|
20
|
+
Stripe = "STRIPE",
|
|
21
|
+
Free = "FREE",
|
|
22
|
+
Card = "CARD",
|
|
23
|
+
Office = "OFFICE",
|
|
24
|
+
Phone = "PHONE",
|
|
25
|
+
Cash = "CASH",
|
|
26
|
+
Iban = "IBAN"
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Product type enum
|
|
30
|
+
*/
|
|
31
|
+
export declare enum ProductTypeEnum {
|
|
32
|
+
Physical = "PHYSICAL",
|
|
33
|
+
Digital = "DIGITAL"
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Query type enum
|
|
37
|
+
*/
|
|
38
|
+
export declare enum QueryTypeEnum {
|
|
39
|
+
Unit = "UNIT",
|
|
40
|
+
Post = "POST",
|
|
41
|
+
Shop = "SHOP",
|
|
42
|
+
Brand = "BRAND",
|
|
43
|
+
Order = "ORDER",
|
|
44
|
+
Quote = "QUOTE",
|
|
45
|
+
Message = "MESSAGE",
|
|
46
|
+
Catalog = "CATALOG",
|
|
47
|
+
Product = "PRODUCT",
|
|
48
|
+
Contact = "CONTACT",
|
|
49
|
+
Invoice = "INVOICE",
|
|
50
|
+
Expense = "EXPENSE",
|
|
51
|
+
Profile = "PROFILE",
|
|
52
|
+
Variant = "VARIANT",
|
|
53
|
+
Checkout = "CHECKOUT",
|
|
54
|
+
Category = "CATEGORY",
|
|
55
|
+
Warehouse = "WAREHOUSE",
|
|
56
|
+
OrderItem = "ORDERITEM",
|
|
57
|
+
QuoteItem = "QUOTEITEM",
|
|
58
|
+
QuoteOption = "QUOTEOPTION",
|
|
59
|
+
Section = "SECTION",
|
|
60
|
+
Discount = "DISCOUNT",
|
|
61
|
+
UserAddress = "USERADDRESS",
|
|
62
|
+
Subcategory = "SUBCATEGORY",
|
|
63
|
+
ExpenseItem = "EXPENSEITEM",
|
|
64
|
+
Organization = "ORGANIZATION",
|
|
65
|
+
ApplicationSection = "APPLICATIONSECTION"
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Transaction direction enum
|
|
69
|
+
*/
|
|
70
|
+
export declare enum TransactionDirectionEnum {
|
|
71
|
+
In = "IN",
|
|
72
|
+
Out = "OUT",
|
|
73
|
+
InProgress = "INPROGRESS"
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Transaction model enum
|
|
77
|
+
*/
|
|
78
|
+
export declare enum TransactionModelEnum {
|
|
79
|
+
Product = "PRODUCT",
|
|
80
|
+
StoreShop = "STORESHOP",
|
|
81
|
+
Subscription = "SUBSCRIPTION",
|
|
82
|
+
Withdraw = "WITHDRAW"
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Boolean string enum
|
|
86
|
+
*/
|
|
87
|
+
export declare enum BooleanStringEnum {
|
|
88
|
+
True = "true",
|
|
89
|
+
False = "false"
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Sort type enum
|
|
93
|
+
*/
|
|
94
|
+
export declare enum SortOrderEnum {
|
|
95
|
+
Asc = "ASC",
|
|
96
|
+
Desc = "DESC"
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Export file extensions
|
|
100
|
+
*/
|
|
101
|
+
export declare enum ExportExtEnum {
|
|
102
|
+
Csv = "csv",
|
|
103
|
+
Xlsx = "xlsx"
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Currency codes used in the system
|
|
107
|
+
* @see https://en.wikipedia.org/wiki/ISO_4217
|
|
108
|
+
*/
|
|
109
|
+
export declare enum CurrencyEnum {
|
|
110
|
+
USD = "USD",
|
|
111
|
+
GBP = "GBP",
|
|
112
|
+
XAF = "XAF",
|
|
113
|
+
EUR = "EUR",
|
|
114
|
+
XOF = "XOF",
|
|
115
|
+
CHF = "CHF",
|
|
116
|
+
CAD = "CAD"
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Quote signed view enum
|
|
120
|
+
*/
|
|
121
|
+
export declare enum QuoteSignedViewEnum {
|
|
122
|
+
Signed = "signed",
|
|
123
|
+
View = "view"
|
|
124
|
+
}
|
package/enum/index.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.QuoteSignedViewEnum = exports.CurrencyEnum = exports.ExportExtEnum = exports.SortOrderEnum = exports.BooleanStringEnum = exports.TransactionModelEnum = exports.TransactionDirectionEnum = exports.QueryTypeEnum = exports.ProductTypeEnum = exports.PaymentMethodEnum = exports.DiscountTypeEnum = exports.CustomerTypeEnum = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Customer type enum
|
|
6
|
+
*/
|
|
7
|
+
var CustomerTypeEnum;
|
|
8
|
+
(function (CustomerTypeEnum) {
|
|
9
|
+
CustomerTypeEnum["Private"] = "PRIVATE";
|
|
10
|
+
CustomerTypeEnum["Subscription"] = "SUBSCRIPTION";
|
|
11
|
+
})(CustomerTypeEnum || (exports.CustomerTypeEnum = CustomerTypeEnum = {}));
|
|
12
|
+
/**
|
|
13
|
+
* Discount type enum
|
|
14
|
+
*/
|
|
15
|
+
var DiscountTypeEnum;
|
|
16
|
+
(function (DiscountTypeEnum) {
|
|
17
|
+
DiscountTypeEnum["Percentage"] = "PERCENTAGE";
|
|
18
|
+
DiscountTypeEnum["Amount"] = "AMOUNT";
|
|
19
|
+
})(DiscountTypeEnum || (exports.DiscountTypeEnum = DiscountTypeEnum = {}));
|
|
20
|
+
/**
|
|
21
|
+
* Payment method enum
|
|
22
|
+
*/
|
|
23
|
+
var PaymentMethodEnum;
|
|
24
|
+
(function (PaymentMethodEnum) {
|
|
25
|
+
PaymentMethodEnum["Paypal"] = "PAYPAL";
|
|
26
|
+
PaymentMethodEnum["Stripe"] = "STRIPE";
|
|
27
|
+
PaymentMethodEnum["Free"] = "FREE";
|
|
28
|
+
PaymentMethodEnum["Card"] = "CARD";
|
|
29
|
+
PaymentMethodEnum["Office"] = "OFFICE";
|
|
30
|
+
PaymentMethodEnum["Phone"] = "PHONE";
|
|
31
|
+
PaymentMethodEnum["Cash"] = "CASH";
|
|
32
|
+
PaymentMethodEnum["Iban"] = "IBAN";
|
|
33
|
+
})(PaymentMethodEnum || (exports.PaymentMethodEnum = PaymentMethodEnum = {}));
|
|
34
|
+
/**
|
|
35
|
+
* Product type enum
|
|
36
|
+
*/
|
|
37
|
+
var ProductTypeEnum;
|
|
38
|
+
(function (ProductTypeEnum) {
|
|
39
|
+
ProductTypeEnum["Physical"] = "PHYSICAL";
|
|
40
|
+
ProductTypeEnum["Digital"] = "DIGITAL";
|
|
41
|
+
})(ProductTypeEnum || (exports.ProductTypeEnum = ProductTypeEnum = {}));
|
|
42
|
+
/**
|
|
43
|
+
* Query type enum
|
|
44
|
+
*/
|
|
45
|
+
var QueryTypeEnum;
|
|
46
|
+
(function (QueryTypeEnum) {
|
|
47
|
+
QueryTypeEnum["Unit"] = "UNIT";
|
|
48
|
+
QueryTypeEnum["Post"] = "POST";
|
|
49
|
+
QueryTypeEnum["Shop"] = "SHOP";
|
|
50
|
+
QueryTypeEnum["Brand"] = "BRAND";
|
|
51
|
+
QueryTypeEnum["Order"] = "ORDER";
|
|
52
|
+
QueryTypeEnum["Quote"] = "QUOTE";
|
|
53
|
+
QueryTypeEnum["Message"] = "MESSAGE";
|
|
54
|
+
QueryTypeEnum["Catalog"] = "CATALOG";
|
|
55
|
+
QueryTypeEnum["Product"] = "PRODUCT";
|
|
56
|
+
QueryTypeEnum["Contact"] = "CONTACT";
|
|
57
|
+
QueryTypeEnum["Invoice"] = "INVOICE";
|
|
58
|
+
QueryTypeEnum["Expense"] = "EXPENSE";
|
|
59
|
+
QueryTypeEnum["Profile"] = "PROFILE";
|
|
60
|
+
QueryTypeEnum["Variant"] = "VARIANT";
|
|
61
|
+
QueryTypeEnum["Checkout"] = "CHECKOUT";
|
|
62
|
+
QueryTypeEnum["Category"] = "CATEGORY";
|
|
63
|
+
QueryTypeEnum["Warehouse"] = "WAREHOUSE";
|
|
64
|
+
QueryTypeEnum["OrderItem"] = "ORDERITEM";
|
|
65
|
+
QueryTypeEnum["QuoteItem"] = "QUOTEITEM";
|
|
66
|
+
QueryTypeEnum["QuoteOption"] = "QUOTEOPTION";
|
|
67
|
+
QueryTypeEnum["Section"] = "SECTION";
|
|
68
|
+
QueryTypeEnum["Discount"] = "DISCOUNT";
|
|
69
|
+
QueryTypeEnum["UserAddress"] = "USERADDRESS";
|
|
70
|
+
QueryTypeEnum["Subcategory"] = "SUBCATEGORY";
|
|
71
|
+
QueryTypeEnum["ExpenseItem"] = "EXPENSEITEM";
|
|
72
|
+
QueryTypeEnum["Organization"] = "ORGANIZATION";
|
|
73
|
+
QueryTypeEnum["ApplicationSection"] = "APPLICATIONSECTION";
|
|
74
|
+
})(QueryTypeEnum || (exports.QueryTypeEnum = QueryTypeEnum = {}));
|
|
75
|
+
/**
|
|
76
|
+
* Transaction direction enum
|
|
77
|
+
*/
|
|
78
|
+
var TransactionDirectionEnum;
|
|
79
|
+
(function (TransactionDirectionEnum) {
|
|
80
|
+
TransactionDirectionEnum["In"] = "IN";
|
|
81
|
+
TransactionDirectionEnum["Out"] = "OUT";
|
|
82
|
+
TransactionDirectionEnum["InProgress"] = "INPROGRESS";
|
|
83
|
+
})(TransactionDirectionEnum || (exports.TransactionDirectionEnum = TransactionDirectionEnum = {}));
|
|
84
|
+
/**
|
|
85
|
+
* Transaction model enum
|
|
86
|
+
*/
|
|
87
|
+
var TransactionModelEnum;
|
|
88
|
+
(function (TransactionModelEnum) {
|
|
89
|
+
TransactionModelEnum["Product"] = "PRODUCT";
|
|
90
|
+
TransactionModelEnum["StoreShop"] = "STORESHOP";
|
|
91
|
+
TransactionModelEnum["Subscription"] = "SUBSCRIPTION";
|
|
92
|
+
TransactionModelEnum["Withdraw"] = "WITHDRAW";
|
|
93
|
+
})(TransactionModelEnum || (exports.TransactionModelEnum = TransactionModelEnum = {}));
|
|
94
|
+
/**
|
|
95
|
+
* Boolean string enum
|
|
96
|
+
*/
|
|
97
|
+
var BooleanStringEnum;
|
|
98
|
+
(function (BooleanStringEnum) {
|
|
99
|
+
BooleanStringEnum["True"] = "true";
|
|
100
|
+
BooleanStringEnum["False"] = "false";
|
|
101
|
+
})(BooleanStringEnum || (exports.BooleanStringEnum = BooleanStringEnum = {}));
|
|
102
|
+
/**
|
|
103
|
+
* Sort type enum
|
|
104
|
+
*/
|
|
105
|
+
var SortOrderEnum;
|
|
106
|
+
(function (SortOrderEnum) {
|
|
107
|
+
SortOrderEnum["Asc"] = "ASC";
|
|
108
|
+
SortOrderEnum["Desc"] = "DESC";
|
|
109
|
+
})(SortOrderEnum || (exports.SortOrderEnum = SortOrderEnum = {}));
|
|
110
|
+
/**
|
|
111
|
+
* Export file extensions
|
|
112
|
+
*/
|
|
113
|
+
var ExportExtEnum;
|
|
114
|
+
(function (ExportExtEnum) {
|
|
115
|
+
ExportExtEnum["Csv"] = "csv";
|
|
116
|
+
ExportExtEnum["Xlsx"] = "xlsx";
|
|
117
|
+
})(ExportExtEnum || (exports.ExportExtEnum = ExportExtEnum = {}));
|
|
118
|
+
/**
|
|
119
|
+
* Currency codes used in the system
|
|
120
|
+
* @see https://en.wikipedia.org/wiki/ISO_4217
|
|
121
|
+
*/
|
|
122
|
+
var CurrencyEnum;
|
|
123
|
+
(function (CurrencyEnum) {
|
|
124
|
+
CurrencyEnum["USD"] = "USD";
|
|
125
|
+
CurrencyEnum["GBP"] = "GBP";
|
|
126
|
+
CurrencyEnum["XAF"] = "XAF";
|
|
127
|
+
CurrencyEnum["EUR"] = "EUR";
|
|
128
|
+
CurrencyEnum["XOF"] = "XOF";
|
|
129
|
+
CurrencyEnum["CHF"] = "CHF";
|
|
130
|
+
CurrencyEnum["CAD"] = "CAD";
|
|
131
|
+
})(CurrencyEnum || (exports.CurrencyEnum = CurrencyEnum = {}));
|
|
132
|
+
/**
|
|
133
|
+
* Quote signed view enum
|
|
134
|
+
*/
|
|
135
|
+
var QuoteSignedViewEnum;
|
|
136
|
+
(function (QuoteSignedViewEnum) {
|
|
137
|
+
QuoteSignedViewEnum["Signed"] = "signed";
|
|
138
|
+
QuoteSignedViewEnum["View"] = "view";
|
|
139
|
+
})(QuoteSignedViewEnum || (exports.QuoteSignedViewEnum = QuoteSignedViewEnum = {}));
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./enum";
|
package/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./enum"), exports);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare function checkCodiceFiscale(cf: string): boolean;
|
|
2
|
+
export type CodiceFiscaleData = {
|
|
3
|
+
firstName: string;
|
|
4
|
+
lastName: string;
|
|
5
|
+
gender: "M" | "F";
|
|
6
|
+
birth: Date;
|
|
7
|
+
/** the birth city identification code */
|
|
8
|
+
belfiore: string;
|
|
9
|
+
isOmocodia: boolean;
|
|
10
|
+
isForeign: boolean;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* @throws if the *codice fiscale* is invalid
|
|
14
|
+
*/
|
|
15
|
+
export declare function parseCodiceFiscale(cf: string): CodiceFiscaleData;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.checkCodiceFiscale = checkCodiceFiscale;
|
|
4
|
+
exports.parseCodiceFiscale = parseCodiceFiscale;
|
|
5
|
+
function checkCodiceFiscale(cf) {
|
|
6
|
+
cf = cf.toUpperCase();
|
|
7
|
+
if (!/^[A-Z]{6}[0-9LMNPQRSTUV]{2}[ABCDEHLMPRST]{1}[0-9LMNPQRSTUV]{2}[A-Z]{1}[0-9LMNPQRSTUV]{3}[A-Z]{1}$/.test(cf)) {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
let v = 0;
|
|
11
|
+
for (let i = 0; i < 15; i++) {
|
|
12
|
+
const d = cf.charAt(i);
|
|
13
|
+
const c = /\d/.test(d) ? String.fromCharCode(65 + Number(d)) : d;
|
|
14
|
+
v += i % 2 === 0
|
|
15
|
+
? "BAKPLCQDREVOSFTGUHMINJWZYX".indexOf(c)
|
|
16
|
+
: "ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(c);
|
|
17
|
+
}
|
|
18
|
+
return 65 + (v % 26) === cf.charCodeAt(15);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* @throws if the *codice fiscale* is invalid
|
|
22
|
+
*/
|
|
23
|
+
function parseCodiceFiscale(cf) {
|
|
24
|
+
cf = cf.toUpperCase();
|
|
25
|
+
if (!checkCodiceFiscale(cf)) {
|
|
26
|
+
throw new Error("Invalid Codice Fiscale");
|
|
27
|
+
}
|
|
28
|
+
let isOmocodia = false;
|
|
29
|
+
for (const i of [6, 7, 9, 10, 12, 13, 14]) {
|
|
30
|
+
const d = cf.charAt(i);
|
|
31
|
+
if (!/\d/.test(d)) {
|
|
32
|
+
isOmocodia = true;
|
|
33
|
+
cf = cf.substring(0, i)
|
|
34
|
+
+ "LMNPQRSTUV".indexOf(d)
|
|
35
|
+
+ cf.substring(i + 1);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const lastName = cf.substring(0, 3);
|
|
39
|
+
const firstName = cf.substring(3, 6);
|
|
40
|
+
let year = Number(cf.substring(6, 8));
|
|
41
|
+
year += (year > new Date().getFullYear() - 2000 ? 1900 : 2000);
|
|
42
|
+
const month = "ABCDEHLMPRST".indexOf(cf.substring(8, 9));
|
|
43
|
+
let day = Number(cf.substring(9, 11));
|
|
44
|
+
const gender = day > 31 ? "F" : "M";
|
|
45
|
+
day = day > 31 ? day - 40 : day;
|
|
46
|
+
const birth = new Date(Date.UTC(year, month, day, 0, 0, 0, 0));
|
|
47
|
+
const belfiore = cf.substring(11, 15);
|
|
48
|
+
const isForeign = belfiore.startsWith("Z");
|
|
49
|
+
return {
|
|
50
|
+
firstName,
|
|
51
|
+
lastName,
|
|
52
|
+
gender,
|
|
53
|
+
birth,
|
|
54
|
+
belfiore,
|
|
55
|
+
isOmocodia,
|
|
56
|
+
isForeign,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const _1 = require(".");
|
|
4
|
+
const data = (0, _1.parseCodiceFiscale)("BGLMRC99R30F205T");
|
|
5
|
+
if (JSON.stringify(data, null, 4) !== `{
|
|
6
|
+
"firstName": "MRC",
|
|
7
|
+
"lastName": "BGL",
|
|
8
|
+
"gender": "M",
|
|
9
|
+
"birth": "1999-10-30T00:00:00.000Z",
|
|
10
|
+
"belfiore": "F205",
|
|
11
|
+
"isOmocodia": false,
|
|
12
|
+
"isForeign": false
|
|
13
|
+
}`)
|
|
14
|
+
throw 1;
|
|
15
|
+
const dataOmocodia = (0, _1.parseCodiceFiscale)("BGLMRCVVRPLFNLRE");
|
|
16
|
+
if (JSON.stringify(dataOmocodia, null, 4) !== `{
|
|
17
|
+
"firstName": "MRC",
|
|
18
|
+
"lastName": "BGL",
|
|
19
|
+
"gender": "M",
|
|
20
|
+
"birth": "1999-10-30T00:00:00.000Z",
|
|
21
|
+
"belfiore": "F205",
|
|
22
|
+
"isOmocodia": true,
|
|
23
|
+
"isForeign": false
|
|
24
|
+
}`)
|
|
25
|
+
throw 1;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export declare enum Action {
|
|
2
|
+
Number = "NUM",
|
|
3
|
+
String = "STR",
|
|
4
|
+
Date = "DATE"
|
|
5
|
+
}
|
|
6
|
+
export declare enum Operation {
|
|
7
|
+
Eq = "=",
|
|
8
|
+
Neq = "!=",
|
|
9
|
+
Gt = ">",
|
|
10
|
+
Gte = ">=",
|
|
11
|
+
Lt = "<",
|
|
12
|
+
Lte = "<=",
|
|
13
|
+
Range = "<>",
|
|
14
|
+
Contains = "~",
|
|
15
|
+
Regex = "$",
|
|
16
|
+
In = "IN"
|
|
17
|
+
}
|
|
18
|
+
export type Payload<A extends Action = Action> = A extends Action.Number ? number : A extends Action.String ? string : A extends Action.Date ? Date : never;
|
|
19
|
+
/**
|
|
20
|
+
* ### Serialize
|
|
21
|
+
*
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const filter = new Filter(Action.Number, Operation.Gt, 10);
|
|
24
|
+
*
|
|
25
|
+
* filter.toString(); // 'NUM|>|10'
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* ### Deserialize
|
|
29
|
+
*
|
|
30
|
+
* ```typescript
|
|
31
|
+
* const filter = Filter.parse('NUM|>|10', Action.Number);
|
|
32
|
+
*
|
|
33
|
+
* filter.action; // Action.Number
|
|
34
|
+
* filter.operation; // Operation.Gt
|
|
35
|
+
* filter.payload; // [10]
|
|
36
|
+
*
|
|
37
|
+
* Filter.parse('NUM|>|10', Action.Date);
|
|
38
|
+
* // Error: Expected action DATE but got NUM
|
|
39
|
+
*
|
|
40
|
+
* Filter.parse('NUM|>|Hello World', Action.Number);
|
|
41
|
+
* // Error: Invalid number payload: Hello World
|
|
42
|
+
*
|
|
43
|
+
* Filter.parse('NUM|>|10', Action.Number, [1, 2, 3]);
|
|
44
|
+
* // Error: Invalid option: 10
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export declare class Filter<A extends Action = Action> {
|
|
48
|
+
action: A;
|
|
49
|
+
operation: Operation;
|
|
50
|
+
payload: Payload<A>[];
|
|
51
|
+
constructor(action: A, operation: Operation, ...payload: Payload<A>[]);
|
|
52
|
+
get ok(): boolean;
|
|
53
|
+
as(f: (payload: Payload<A>) => Payload<A>): Filter<A>;
|
|
54
|
+
toString(): string;
|
|
55
|
+
/**
|
|
56
|
+
* @throws {Error}
|
|
57
|
+
*/
|
|
58
|
+
static parse<A extends Action = Action>(str: string, action?: A, allowed?: Payload<A>[]): Filter<A>;
|
|
59
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Filter = exports.Operation = exports.Action = void 0;
|
|
4
|
+
var Action;
|
|
5
|
+
(function (Action) {
|
|
6
|
+
Action["Number"] = "NUM";
|
|
7
|
+
Action["String"] = "STR";
|
|
8
|
+
Action["Date"] = "DATE";
|
|
9
|
+
})(Action || (exports.Action = Action = {}));
|
|
10
|
+
;
|
|
11
|
+
var Operation;
|
|
12
|
+
(function (Operation) {
|
|
13
|
+
Operation["Eq"] = "=";
|
|
14
|
+
Operation["Neq"] = "!=";
|
|
15
|
+
Operation["Gt"] = ">";
|
|
16
|
+
Operation["Gte"] = ">=";
|
|
17
|
+
Operation["Lt"] = "<";
|
|
18
|
+
Operation["Lte"] = "<=";
|
|
19
|
+
Operation["Range"] = "<>";
|
|
20
|
+
Operation["Contains"] = "~";
|
|
21
|
+
Operation["Regex"] = "$";
|
|
22
|
+
Operation["In"] = "IN";
|
|
23
|
+
})(Operation || (exports.Operation = Operation = {}));
|
|
24
|
+
;
|
|
25
|
+
/**
|
|
26
|
+
* ### Serialize
|
|
27
|
+
*
|
|
28
|
+
* ```typescript
|
|
29
|
+
* const filter = new Filter(Action.Number, Operation.Gt, 10);
|
|
30
|
+
*
|
|
31
|
+
* filter.toString(); // 'NUM|>|10'
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* ### Deserialize
|
|
35
|
+
*
|
|
36
|
+
* ```typescript
|
|
37
|
+
* const filter = Filter.parse('NUM|>|10', Action.Number);
|
|
38
|
+
*
|
|
39
|
+
* filter.action; // Action.Number
|
|
40
|
+
* filter.operation; // Operation.Gt
|
|
41
|
+
* filter.payload; // [10]
|
|
42
|
+
*
|
|
43
|
+
* Filter.parse('NUM|>|10', Action.Date);
|
|
44
|
+
* // Error: Expected action DATE but got NUM
|
|
45
|
+
*
|
|
46
|
+
* Filter.parse('NUM|>|Hello World', Action.Number);
|
|
47
|
+
* // Error: Invalid number payload: Hello World
|
|
48
|
+
*
|
|
49
|
+
* Filter.parse('NUM|>|10', Action.Number, [1, 2, 3]);
|
|
50
|
+
* // Error: Invalid option: 10
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
class Filter {
|
|
54
|
+
constructor(action, operation, ...payload) {
|
|
55
|
+
this.action = action;
|
|
56
|
+
this.operation = operation;
|
|
57
|
+
this.payload = payload;
|
|
58
|
+
}
|
|
59
|
+
get ok() {
|
|
60
|
+
return this.operation === Operation.Range
|
|
61
|
+
? this.payload.length > 1
|
|
62
|
+
: this.payload.length > 0;
|
|
63
|
+
}
|
|
64
|
+
as(f) {
|
|
65
|
+
return new Filter(this.action, this.operation, ...this.payload.map(f));
|
|
66
|
+
}
|
|
67
|
+
toString() {
|
|
68
|
+
return this.action
|
|
69
|
+
+ "|" + this.operation
|
|
70
|
+
+ "|" + this.payload.map((p) => p instanceof Date ? p.toISOString() : p.toString()).join('|');
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* @throws {Error}
|
|
74
|
+
*/
|
|
75
|
+
static parse(str, action, allowed) {
|
|
76
|
+
const [_action, operation, ...raw] = str.split('|');
|
|
77
|
+
const payload = raw;
|
|
78
|
+
if (!_action || !operation || !payload.length) {
|
|
79
|
+
throw new Error('Invalid filter string');
|
|
80
|
+
}
|
|
81
|
+
if (action && _action !== action) {
|
|
82
|
+
throw new Error('Expected action ' + action + ' but got ' + _action);
|
|
83
|
+
}
|
|
84
|
+
switch (_action) {
|
|
85
|
+
case Action.Number:
|
|
86
|
+
raw.forEach((p, i) => {
|
|
87
|
+
if (!/^-?(\d_?)+$|^-?(\d_?)*\.(\d_?)+$/.test(p)) {
|
|
88
|
+
throw new Error('Invalid number payload: ' + p);
|
|
89
|
+
}
|
|
90
|
+
payload[i] = Number(p);
|
|
91
|
+
if (allowed && !allowed.some((p) => p === payload[i])) {
|
|
92
|
+
throw new Error('Invalid option: ' + p);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
break;
|
|
96
|
+
case Action.Date:
|
|
97
|
+
raw.forEach((p, i) => {
|
|
98
|
+
if (!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/.test(p)) {
|
|
99
|
+
throw new Error('Invalid date payload: ' + p);
|
|
100
|
+
}
|
|
101
|
+
payload[i] = new Date(p);
|
|
102
|
+
if (allowed && !allowed.some((p) => p === payload[i])) {
|
|
103
|
+
throw new Error('Invalid option: ' + p);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
break;
|
|
107
|
+
case Action.String:
|
|
108
|
+
if (allowed) {
|
|
109
|
+
raw.forEach((p, i) => {
|
|
110
|
+
if (!allowed.some((p) => p === payload[i])) {
|
|
111
|
+
throw new Error('Invalid option: ' + p);
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
break;
|
|
116
|
+
default:
|
|
117
|
+
throw new Error('Unknown action: ' + _action);
|
|
118
|
+
}
|
|
119
|
+
return new Filter(_action, operation, ...payload);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
exports.Filter = Filter;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare function checkPartitaIVA(piva: string): boolean;
|
|
2
|
+
export type PartitaIVAData = {
|
|
3
|
+
numero: string;
|
|
4
|
+
isForeign: boolean;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* @throws if the partita IVA is invalid
|
|
8
|
+
*/
|
|
9
|
+
export declare function parsePartitaIVA(piva: string): PartitaIVAData;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.checkPartitaIVA = checkPartitaIVA;
|
|
4
|
+
exports.parsePartitaIVA = parsePartitaIVA;
|
|
5
|
+
function checkPartitaIVA(piva) {
|
|
6
|
+
if (!/^\d{11}$/.test(piva))
|
|
7
|
+
return false;
|
|
8
|
+
let sum = 0;
|
|
9
|
+
for (let i = 0; i < 10; i++) {
|
|
10
|
+
let digit = parseInt(piva.charAt(i), 10);
|
|
11
|
+
if (i % 2 === 0) {
|
|
12
|
+
sum += digit;
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
digit *= 2;
|
|
16
|
+
if (digit > 9)
|
|
17
|
+
digit -= 9;
|
|
18
|
+
sum += digit;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const expectedCheckDigit = (10 - (sum % 10)) % 10;
|
|
22
|
+
const actualCheckDigit = parseInt(piva.charAt(10), 10);
|
|
23
|
+
return expectedCheckDigit === actualCheckDigit;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* @throws if the partita IVA is invalid
|
|
27
|
+
*/
|
|
28
|
+
function parsePartitaIVA(piva) {
|
|
29
|
+
piva = piva.trim();
|
|
30
|
+
if (!checkPartitaIVA(piva)) {
|
|
31
|
+
throw new Error("Invalid Partita IVA");
|
|
32
|
+
}
|
|
33
|
+
// In some conventions, the numbers from 0000001 to 1000000 are Italian.
|
|
34
|
+
// Those from 1000001 onwards may be assigned to foreign entities.
|
|
35
|
+
const isForeign = parseInt(piva.substring(0, 7), 10) >= 1000000;
|
|
36
|
+
return {
|
|
37
|
+
numero: piva,
|
|
38
|
+
isForeign,
|
|
39
|
+
};
|
|
40
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name":"@molopos/shared","publishConfig":{"access":"public"},"version":"2.0.0","description":"Shared between backend and frontend repos","license":"ISC","repository":{"type":"git","url":"https://github.com/unicubate/molopos-shared.git"}}
|