@planecloud/utils 1.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/dist/auth/index.d.ts +22 -0
- package/dist/auth/index.d.ts.map +1 -0
- package/dist/auth/index.js +111 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/crypto/index.d.ts +34 -0
- package/dist/crypto/index.d.ts.map +1 -0
- package/dist/crypto/index.js +195 -0
- package/dist/crypto/index.js.map +1 -0
- package/dist/date/index.d.ts +36 -0
- package/dist/date/index.d.ts.map +1 -0
- package/dist/date/index.js +199 -0
- package/dist/date/index.js.map +1 -0
- package/dist/format/index.d.ts +20 -0
- package/dist/format/index.d.ts.map +1 -0
- package/dist/format/index.js +223 -0
- package/dist/format/index.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +63 -0
- package/dist/index.js.map +1 -0
- package/dist/network/index.d.ts +24 -0
- package/dist/network/index.d.ts.map +1 -0
- package/dist/network/index.js +227 -0
- package/dist/network/index.js.map +1 -0
- package/dist/string/index.d.ts +19 -0
- package/dist/string/index.d.ts.map +1 -0
- package/dist/string/index.js +148 -0
- package/dist/string/index.js.map +1 -0
- package/dist/validation/index.d.ts +44 -0
- package/dist/validation/index.d.ts.map +1 -0
- package/dist/validation/index.js +225 -0
- package/dist/validation/index.js.map +1 -0
- package/package.json +39 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export declare const generateId: () => string;
|
|
2
|
+
export declare const generateShortId: (length?: number) => string;
|
|
3
|
+
export declare const hashPassword: (password: string) => Promise<string>;
|
|
4
|
+
export declare const verifyPassword: (password: string, hash: string) => Promise<boolean>;
|
|
5
|
+
export declare const generatePassword: (length?: number) => string;
|
|
6
|
+
export declare const generateToken: (length?: number) => string;
|
|
7
|
+
export declare const generateApiKey: () => string;
|
|
8
|
+
export declare const hashApiKey: (apiKey: string) => string;
|
|
9
|
+
export declare const generateSessionToken: () => string;
|
|
10
|
+
export declare const generateRefreshToken: () => string;
|
|
11
|
+
export declare const generateRateLimitKey: (identifier: string, action: string) => string;
|
|
12
|
+
export declare const generateVerificationCode: (length?: number) => string;
|
|
13
|
+
export declare const generateContentSecurityPolicy: (options?: {
|
|
14
|
+
scriptSrc?: string[];
|
|
15
|
+
styleSrc?: string[];
|
|
16
|
+
imgSrc?: string[];
|
|
17
|
+
connectSrc?: string[];
|
|
18
|
+
fontSrc?: string[];
|
|
19
|
+
}) => string;
|
|
20
|
+
export declare const simpleEncrypt: (text: string, key: string) => string;
|
|
21
|
+
export declare const simpleDecrypt: (encryptedText: string, key: string) => string;
|
|
22
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/auth/index.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,UAAU,QAAO,MAAkB,CAAC;AACjD,eAAO,MAAM,eAAe,GAAI,SAAQ,MAAU,KAAG,MACQ,CAAC;AAG9D,eAAO,MAAM,YAAY,GAAU,UAAU,MAAM,KAAG,OAAO,CAAC,MAAM,CAGnE,CAAC;AAEF,eAAO,MAAM,cAAc,GACzB,UAAU,MAAM,EAChB,MAAM,MAAM,KACX,OAAO,CAAC,OAAO,CAEjB,CAAC;AAEF,eAAO,MAAM,gBAAgB,GAAI,SAAQ,MAAW,KAAG,MAYtD,CAAC;AAGF,eAAO,MAAM,aAAa,GAAI,SAAQ,MAAW,KAAG,MACR,CAAC;AAE7C,eAAO,MAAM,cAAc,QAAO,MAIjC,CAAC;AAEF,eAAO,MAAM,UAAU,GAAI,QAAQ,MAAM,KAAG,MAE3C,CAAC;AAGF,eAAO,MAAM,oBAAoB,QAAO,MAA2B,CAAC;AACpE,eAAO,MAAM,oBAAoB,QAAO,MAA2B,CAAC;AAGpE,eAAO,MAAM,oBAAoB,GAC/B,YAAY,MAAM,EAClB,QAAQ,MAAM,KACb,MAEF,CAAC;AAGF,eAAO,MAAM,wBAAwB,GAAI,SAAQ,MAAU,KAAG,MAO7D,CAAC;AAGF,eAAO,MAAM,6BAA6B,GACxC,UAAS;IACP,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;IACrB,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;IACtB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;CACf,KACL,MAiBF,CAAC;AAGF,eAAO,MAAM,aAAa,GAAI,MAAM,MAAM,EAAE,KAAK,MAAM,KAAG,MAWzD,CAAC;AAEF,eAAO,MAAM,aAAa,GAAI,eAAe,MAAM,EAAE,KAAK,MAAM,KAAG,MAmBlE,CAAC"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.simpleDecrypt = exports.simpleEncrypt = exports.generateContentSecurityPolicy = exports.generateVerificationCode = exports.generateRateLimitKey = exports.generateRefreshToken = exports.generateSessionToken = exports.hashApiKey = exports.generateApiKey = exports.generateToken = exports.generatePassword = exports.verifyPassword = exports.hashPassword = exports.generateShortId = exports.generateId = void 0;
|
|
7
|
+
const uuid_1 = require("uuid");
|
|
8
|
+
const bcrypt_1 = __importDefault(require("bcrypt"));
|
|
9
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
10
|
+
// UUID utilities
|
|
11
|
+
const generateId = () => (0, uuid_1.v4)();
|
|
12
|
+
exports.generateId = generateId;
|
|
13
|
+
const generateShortId = (length = 8) => crypto_1.default.randomBytes(length).toString("hex").slice(0, length);
|
|
14
|
+
exports.generateShortId = generateShortId;
|
|
15
|
+
// Password utilities
|
|
16
|
+
const hashPassword = async (password) => {
|
|
17
|
+
const saltRounds = 12;
|
|
18
|
+
return bcrypt_1.default.hash(password, saltRounds);
|
|
19
|
+
};
|
|
20
|
+
exports.hashPassword = hashPassword;
|
|
21
|
+
const verifyPassword = async (password, hash) => {
|
|
22
|
+
return bcrypt_1.default.compare(password, hash);
|
|
23
|
+
};
|
|
24
|
+
exports.verifyPassword = verifyPassword;
|
|
25
|
+
const generatePassword = (length = 16) => {
|
|
26
|
+
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*";
|
|
27
|
+
let password = "";
|
|
28
|
+
const randomValues = new Uint32Array(length);
|
|
29
|
+
crypto_1.default.getRandomValues(randomValues);
|
|
30
|
+
for (let i = 0; i < length; i++) {
|
|
31
|
+
password += charset[randomValues[i] % charset.length];
|
|
32
|
+
}
|
|
33
|
+
return password;
|
|
34
|
+
};
|
|
35
|
+
exports.generatePassword = generatePassword;
|
|
36
|
+
// Token utilities
|
|
37
|
+
const generateToken = (length = 32) => crypto_1.default.randomBytes(length).toString("hex");
|
|
38
|
+
exports.generateToken = generateToken;
|
|
39
|
+
const generateApiKey = () => {
|
|
40
|
+
const prefix = "pc_";
|
|
41
|
+
const token = crypto_1.default.randomBytes(32).toString("hex");
|
|
42
|
+
return `${prefix}${token}`;
|
|
43
|
+
};
|
|
44
|
+
exports.generateApiKey = generateApiKey;
|
|
45
|
+
const hashApiKey = (apiKey) => {
|
|
46
|
+
return crypto_1.default.createHash("sha256").update(apiKey).digest("hex");
|
|
47
|
+
};
|
|
48
|
+
exports.hashApiKey = hashApiKey;
|
|
49
|
+
// Session utilities
|
|
50
|
+
const generateSessionToken = () => (0, exports.generateToken)(64);
|
|
51
|
+
exports.generateSessionToken = generateSessionToken;
|
|
52
|
+
const generateRefreshToken = () => (0, exports.generateToken)(64);
|
|
53
|
+
exports.generateRefreshToken = generateRefreshToken;
|
|
54
|
+
// Rate limiting utilities
|
|
55
|
+
const generateRateLimitKey = (identifier, action) => {
|
|
56
|
+
return `rate_limit:${identifier}:${action}`;
|
|
57
|
+
};
|
|
58
|
+
exports.generateRateLimitKey = generateRateLimitKey;
|
|
59
|
+
// Verification utilities
|
|
60
|
+
const generateVerificationCode = (length = 6) => {
|
|
61
|
+
const digits = "0123456789";
|
|
62
|
+
let code = "";
|
|
63
|
+
for (let i = 0; i < length; i++) {
|
|
64
|
+
code += digits[Math.floor(Math.random() * digits.length)];
|
|
65
|
+
}
|
|
66
|
+
return code;
|
|
67
|
+
};
|
|
68
|
+
exports.generateVerificationCode = generateVerificationCode;
|
|
69
|
+
// Security headers utilities
|
|
70
|
+
const generateContentSecurityPolicy = (options = {}) => {
|
|
71
|
+
const defaults = {
|
|
72
|
+
scriptSrc: ["'self'"],
|
|
73
|
+
styleSrc: ["'self'", "'unsafe-inline'"],
|
|
74
|
+
imgSrc: ["'self'", "data:", "https:"],
|
|
75
|
+
connectSrc: ["'self'"],
|
|
76
|
+
fontSrc: ["'self'"],
|
|
77
|
+
};
|
|
78
|
+
const merged = { ...defaults, ...options };
|
|
79
|
+
return Object.entries(merged)
|
|
80
|
+
.map(([key, values]) => `${key.replace(/([A-Z])/g, "-$1").toLowerCase()} ${values.join(" ")}`)
|
|
81
|
+
.join("; ");
|
|
82
|
+
};
|
|
83
|
+
exports.generateContentSecurityPolicy = generateContentSecurityPolicy;
|
|
84
|
+
// Simple encryption utilities (removed AES for now to avoid crypto API issues)
|
|
85
|
+
const simpleEncrypt = (text, key) => {
|
|
86
|
+
const algorithm = "aes-256-gcm";
|
|
87
|
+
const iv = crypto_1.default.randomBytes(16);
|
|
88
|
+
const cipher = crypto_1.default.createCipheriv(algorithm, key, iv);
|
|
89
|
+
let encrypted = cipher.update(text, "utf8", "hex");
|
|
90
|
+
encrypted += cipher.final("hex");
|
|
91
|
+
const authTag = cipher.getAuthTag();
|
|
92
|
+
return iv.toString("hex") + ":" + authTag.toString("hex") + ":" + encrypted;
|
|
93
|
+
};
|
|
94
|
+
exports.simpleEncrypt = simpleEncrypt;
|
|
95
|
+
const simpleDecrypt = (encryptedText, key) => {
|
|
96
|
+
const algorithm = "aes-256-gcm";
|
|
97
|
+
const parts = encryptedText.split(":");
|
|
98
|
+
if (parts.length !== 3) {
|
|
99
|
+
throw new Error("Invalid encrypted text format");
|
|
100
|
+
}
|
|
101
|
+
const iv = Buffer.from(parts[0], "hex");
|
|
102
|
+
const authTag = Buffer.from(parts[1], "hex");
|
|
103
|
+
const encrypted = parts[2];
|
|
104
|
+
const decipher = crypto_1.default.createDecipheriv(algorithm, key, iv);
|
|
105
|
+
decipher.setAuthTag(authTag);
|
|
106
|
+
let decrypted = decipher.update(encrypted, "hex", "utf8");
|
|
107
|
+
decrypted += decipher.final("utf8");
|
|
108
|
+
return decrypted;
|
|
109
|
+
};
|
|
110
|
+
exports.simpleDecrypt = simpleDecrypt;
|
|
111
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/auth/index.ts"],"names":[],"mappings":";;;;;;AAAA,+BAAoC;AACpC,oDAA4B;AAC5B,oDAA4B;AAE5B,iBAAiB;AACV,MAAM,UAAU,GAAG,GAAW,EAAE,CAAC,IAAA,SAAM,GAAE,CAAC;AAApC,QAAA,UAAU,cAA0B;AAC1C,MAAM,eAAe,GAAG,CAAC,SAAiB,CAAC,EAAU,EAAE,CAC5D,gBAAM,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;AADjD,QAAA,eAAe,mBACkC;AAE9D,qBAAqB;AACd,MAAM,YAAY,GAAG,KAAK,EAAE,QAAgB,EAAmB,EAAE;IACtE,MAAM,UAAU,GAAG,EAAE,CAAC;IACtB,OAAO,gBAAM,CAAC,IAAI,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC;AAC3C,CAAC,CAAC;AAHW,QAAA,YAAY,gBAGvB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K,GAAG,aAAa,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAEvC,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACvB,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC;IACnD,CAAC;IAED,MAAM,EAAE,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;IACxC,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;IAC7C,MAAM,SAAS,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;IAE3B,MAAM,QAAQ,GAAG,gBAAM,CAAC,gBAAgB,CAAC,SAAS,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC;IAC7D,QAAQ,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;IAE7B,IAAI,SAAS,GAAG,QAAQ,CAAC,MAAM,CAAC,SAAS,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;IAC1D,SAAS,IAAI,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAEpC,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC;AAnBW,QAAA,aAAa,iBAmBxB"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export declare const md5: (data: string) => string;
|
|
2
|
+
export declare const sha1: (data: string) => string;
|
|
3
|
+
export declare const sha256: (data: string) => string;
|
|
4
|
+
export declare const sha512: (data: string) => string;
|
|
5
|
+
export declare const hmacMd5: (data: string, key: string) => string;
|
|
6
|
+
export declare const hmacSha1: (data: string, key: string) => string;
|
|
7
|
+
export declare const hmacSha256: (data: string, key: string) => string;
|
|
8
|
+
export declare const hmacSha512: (data: string, key: string) => string;
|
|
9
|
+
export declare const encryptAES: (data: string, key: string, iv?: string) => string;
|
|
10
|
+
export declare const decryptAES: (encryptedData: string, key: string) => string;
|
|
11
|
+
export declare const generateKey: (algorithm?: string, length?: number) => string;
|
|
12
|
+
export declare const generateKeyPair: (type?: string, options?: any) => {
|
|
13
|
+
publicKey: string;
|
|
14
|
+
privateKey: string;
|
|
15
|
+
};
|
|
16
|
+
export declare const randomBytes: (size: number) => Buffer;
|
|
17
|
+
export declare const randomHex: (size: number) => string;
|
|
18
|
+
export declare const randomBase64: (size: number) => string;
|
|
19
|
+
export declare const randomUUID: () => string;
|
|
20
|
+
export declare const randomInt: (min: number, max: number) => number;
|
|
21
|
+
export declare const compareHashes: (hash1: string, hash2: string) => boolean;
|
|
22
|
+
export declare const createHash: (data: string, algorithm?: string) => string;
|
|
23
|
+
export declare const pbkdf2: (password: string, salt: string, iterations?: number, keylen?: number, digest?: string) => Promise<string>;
|
|
24
|
+
export declare const scrypt: (password: string, salt: string, keylen?: number) => Promise<string>;
|
|
25
|
+
export declare const sign: (data: string, privateKey: string) => string;
|
|
26
|
+
export declare const verify: (data: string, signature: string, publicKey: string) => boolean;
|
|
27
|
+
export declare const createJWT: (payload: Record<string, any>, secret: string, algorithm?: string) => string;
|
|
28
|
+
export declare const verifyJWT: (token: string, secret: string) => {
|
|
29
|
+
valid: boolean;
|
|
30
|
+
payload?: Record<string, any>;
|
|
31
|
+
};
|
|
32
|
+
export declare const hashPassword: (password: string, saltRounds?: number) => Promise<string>;
|
|
33
|
+
export declare const verifyPassword: (password: string, hash: string) => Promise<boolean>;
|
|
34
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/crypto/index.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,GAAG,GAAI,MAAM,MAAM,KAAG,MAElC,CAAC;AAEF,eAAO,MAAM,IAAI,GAAI,MAAM,MAAM,KAAG,MAEnC,CAAC;AAEF,eAAO,MAAM,MAAM,GAAI,MAAM,MAAM,KAAG,MAErC,CAAC;AAEF,eAAO,MAAM,MAAM,GAAI,MAAM,MAAM,KAAG,MAErC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EAChB,MAAM,MAAM,EACZ,aAAY,MAAe,EAC3B,SAAQ,MAAW,EACnB,SAAQ,MAAiB,KACxB,OAAO,CAAC,MAAM,CAchB,CAAC;AAEF,eAAO,MAAM,MAAM,GACjB,UAAU,MAAM,EAChB,MAAM,MAAM,EACZ,SAAQ,MAAW,KAClB,OAAO,CAAC,MAAM,CAOhB,CAAC;AAGF,eAAO,MAAM,IAAI,GAAI,MAAM,MAAM,EAAE,YAAY,MAAM,KAAG,MAIvD,CAAC;AAEF,eAAO,MAAM,MAAM,GACjB,MAAM,MAAM,EACZ,WAAW,MAAM,EACjB,WAAW,MAAM,KAChB,OAIF,CAAC;AAGF,eAAO,MAAM,SAAS,GACpB,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC5B,QAAQ,MAAM,EACd,YAAW,MAAgB,KAC1B,MAaF,CAAC;AAEF,eAAO,MAAM,SAAS,GACpB,OAAO,MAAM,EACb,QAAQ,MAAM,KACb;IAAE,KAAK,EAAE,OAAO,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;CAiBjD,CAAC;AAGF,eAAO,MAAM,YAAY,GACvB,UAAU,MAAM,EAChB,aAAY,MAAW,KACtB,OAAO,CAAC,MAAM,CAehB,CAAC;AAEF,eAAO,MAAM,cAAc,GACzB,UAAU,MAAM,EAChB,MAAM,MAAM,KACX,OAAO,CAAC,OAAO,CAcjB,CAAC"}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.verifyPassword = exports.hashPassword = exports.verifyJWT = exports.createJWT = exports.verify = exports.sign = exports.scrypt = exports.pbkdf2 = exports.createHash = exports.compareHashes = exports.randomInt = exports.randomUUID = exports.randomBase64 = exports.randomHex = exports.randomBytes = exports.generateKeyPair = exports.generateKey = exports.decryptAES = exports.encryptAES = exports.hmacSha512 = exports.hmacSha256 = exports.hmacSha1 = exports.hmacMd5 = exports.sha512 = exports.sha256 = exports.sha1 = exports.md5 = void 0;
|
|
7
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
8
|
+
// Hash utilities
|
|
9
|
+
const md5 = (data) => {
|
|
10
|
+
return crypto_1.default.createHash("md5").update(data).digest("hex");
|
|
11
|
+
};
|
|
12
|
+
exports.md5 = md5;
|
|
13
|
+
const sha1 = (data) => {
|
|
14
|
+
return crypto_1.default.createHash("sha1").update(data).digest("hex");
|
|
15
|
+
};
|
|
16
|
+
exports.sha1 = sha1;
|
|
17
|
+
const sha256 = (data) => {
|
|
18
|
+
return crypto_1.default.createHash("sha256").update(data).digest("hex");
|
|
19
|
+
};
|
|
20
|
+
exports.sha256 = sha256;
|
|
21
|
+
const sha512 = (data) => {
|
|
22
|
+
return crypto_1.default.createHash("sha512").update(data).digest("hex");
|
|
23
|
+
};
|
|
24
|
+
exports.sha512 = sha512;
|
|
25
|
+
// HMAC utilities
|
|
26
|
+
const hmacMd5 = (data, key) => {
|
|
27
|
+
return crypto_1.default.createHmac("md5", key).update(data).digest("hex");
|
|
28
|
+
};
|
|
29
|
+
exports.hmacMd5 = hmacMd5;
|
|
30
|
+
const hmacSha1 = (data, key) => {
|
|
31
|
+
return crypto_1.default.createHmac("sha1", key).update(data).digest("hex");
|
|
32
|
+
};
|
|
33
|
+
exports.hmacSha1 = hmacSha1;
|
|
34
|
+
const hmacSha256 = (data, key) => {
|
|
35
|
+
return crypto_1.default.createHmac("sha256", key).update(data).digest("hex");
|
|
36
|
+
};
|
|
37
|
+
exports.hmacSha256 = hmacSha256;
|
|
38
|
+
const hmacSha512 = (data, key) => {
|
|
39
|
+
return crypto_1.default.createHmac("sha512", key).update(data).digest("hex");
|
|
40
|
+
};
|
|
41
|
+
exports.hmacSha512 = hmacSha512;
|
|
42
|
+
// Encryption utilities
|
|
43
|
+
const encryptAES = (data, key, iv) => {
|
|
44
|
+
const algorithm = "aes-256-cbc";
|
|
45
|
+
const actualIv = iv || crypto_1.default.randomBytes(16);
|
|
46
|
+
const cipher = crypto_1.default.createCipheriv(algorithm, key, actualIv);
|
|
47
|
+
let encrypted = cipher.update(data, "utf8", "hex");
|
|
48
|
+
encrypted += cipher.final("hex");
|
|
49
|
+
const ivHex = iv ? iv : actualIv.toString("hex");
|
|
50
|
+
return ivHex + ":" + encrypted;
|
|
51
|
+
};
|
|
52
|
+
exports.encryptAES = encryptAES;
|
|
53
|
+
const decryptAES = (encryptedData, key) => {
|
|
54
|
+
const algorithm = "aes-256-cbc";
|
|
55
|
+
const parts = encryptedData.split(":");
|
|
56
|
+
if (parts.length !== 2) {
|
|
57
|
+
throw new Error("Invalid encrypted data format");
|
|
58
|
+
}
|
|
59
|
+
const iv = Buffer.from(parts[0], "hex");
|
|
60
|
+
const encrypted = parts[1];
|
|
61
|
+
const decipher = crypto_1.default.createDecipheriv(algorithm, key, iv);
|
|
62
|
+
let decrypted = decipher.update(encrypted, "hex", "utf8");
|
|
63
|
+
decrypted += decipher.final("utf8");
|
|
64
|
+
return decrypted;
|
|
65
|
+
};
|
|
66
|
+
exports.decryptAES = decryptAES;
|
|
67
|
+
// Key generation utilities
|
|
68
|
+
const generateKey = (algorithm = "aes256", length) => {
|
|
69
|
+
return crypto_1.default.randomBytes(length || 32).toString("hex");
|
|
70
|
+
};
|
|
71
|
+
exports.generateKey = generateKey;
|
|
72
|
+
const generateKeyPair = (type = "rsa", options) => {
|
|
73
|
+
const { publicKey, privateKey } = crypto_1.default.generateKeyPairSync(type, options ||
|
|
74
|
+
{
|
|
75
|
+
modulusLength: 2048,
|
|
76
|
+
publicKeyEncoding: { type: "spki", format: "pem" },
|
|
77
|
+
privateKeyEncoding: { type: "pkcs8", format: "pem" },
|
|
78
|
+
});
|
|
79
|
+
return { publicKey: publicKey.toString(), privateKey: privateKey.toString() };
|
|
80
|
+
};
|
|
81
|
+
exports.generateKeyPair = generateKeyPair;
|
|
82
|
+
// Random utilities
|
|
83
|
+
const randomBytes = (size) => {
|
|
84
|
+
return crypto_1.default.randomBytes(size);
|
|
85
|
+
};
|
|
86
|
+
exports.randomBytes = randomBytes;
|
|
87
|
+
const randomHex = (size) => {
|
|
88
|
+
return crypto_1.default.randomBytes(size).toString("hex");
|
|
89
|
+
};
|
|
90
|
+
exports.randomHex = randomHex;
|
|
91
|
+
const randomBase64 = (size) => {
|
|
92
|
+
return crypto_1.default.randomBytes(size).toString("base64");
|
|
93
|
+
};
|
|
94
|
+
exports.randomBase64 = randomBase64;
|
|
95
|
+
const randomUUID = () => {
|
|
96
|
+
return crypto_1.default.randomUUID();
|
|
97
|
+
};
|
|
98
|
+
exports.randomUUID = randomUUID;
|
|
99
|
+
const randomInt = (min, max) => {
|
|
100
|
+
return crypto_1.default.randomInt(min, max + 1);
|
|
101
|
+
};
|
|
102
|
+
exports.randomInt = randomInt;
|
|
103
|
+
// Utility functions
|
|
104
|
+
const compareHashes = (hash1, hash2) => {
|
|
105
|
+
return crypto_1.default.timingSafeEqual(Buffer.from(hash1), Buffer.from(hash2));
|
|
106
|
+
};
|
|
107
|
+
exports.compareHashes = compareHashes;
|
|
108
|
+
const createHash = (data, algorithm = "sha256") => {
|
|
109
|
+
return crypto_1.default.createHash(algorithm).update(data).digest("hex");
|
|
110
|
+
};
|
|
111
|
+
exports.createHash = createHash;
|
|
112
|
+
const pbkdf2 = (password, salt, iterations = 100000, keylen = 32, digest = "sha256") => {
|
|
113
|
+
return new Promise((resolve, reject) => {
|
|
114
|
+
crypto_1.default.pbkdf2(password, salt, iterations, keylen, digest, (err, derivedKey) => {
|
|
115
|
+
if (err)
|
|
116
|
+
reject(err);
|
|
117
|
+
else
|
|
118
|
+
resolve(derivedKey.toString("hex"));
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
exports.pbkdf2 = pbkdf2;
|
|
123
|
+
const scrypt = (password, salt, keylen = 32) => {
|
|
124
|
+
return new Promise((resolve, reject) => {
|
|
125
|
+
crypto_1.default.scrypt(password, salt, keylen, (err, derivedKey) => {
|
|
126
|
+
if (err)
|
|
127
|
+
reject(err);
|
|
128
|
+
else
|
|
129
|
+
resolve(derivedKey.toString("hex"));
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
};
|
|
133
|
+
exports.scrypt = scrypt;
|
|
134
|
+
// Digital signatures
|
|
135
|
+
const sign = (data, privateKey) => {
|
|
136
|
+
const sign = crypto_1.default.createSign("RSA-SHA256");
|
|
137
|
+
sign.update(data);
|
|
138
|
+
return sign.sign(privateKey, "hex");
|
|
139
|
+
};
|
|
140
|
+
exports.sign = sign;
|
|
141
|
+
const verify = (data, signature, publicKey) => {
|
|
142
|
+
const verify = crypto_1.default.createVerify("RSA-SHA256");
|
|
143
|
+
verify.update(data);
|
|
144
|
+
return verify.verify(publicKey, signature, "hex");
|
|
145
|
+
};
|
|
146
|
+
exports.verify = verify;
|
|
147
|
+
// Token utilities
|
|
148
|
+
const createJWT = (payload, secret, algorithm = "HS256") => {
|
|
149
|
+
const header = { alg: algorithm, typ: "JWT" };
|
|
150
|
+
const encodedHeader = Buffer.from(JSON.stringify(header)).toString("base64url");
|
|
151
|
+
const encodedPayload = Buffer.from(JSON.stringify(payload)).toString("base64url");
|
|
152
|
+
const signatureData = `${encodedHeader}.${encodedPayload}`;
|
|
153
|
+
const signature = (0, exports.hmacSha256)(signatureData, secret);
|
|
154
|
+
return `${signatureData}.${signature}`;
|
|
155
|
+
};
|
|
156
|
+
exports.createJWT = createJWT;
|
|
157
|
+
const verifyJWT = (token, secret) => {
|
|
158
|
+
try {
|
|
159
|
+
const [encodedHeader, encodedPayload, signature] = token.split(".");
|
|
160
|
+
const signatureData = `${encodedHeader}.${encodedPayload}`;
|
|
161
|
+
const expectedSignature = (0, exports.hmacSha256)(signatureData, secret);
|
|
162
|
+
if (!(0, exports.compareHashes)(signature, expectedSignature)) {
|
|
163
|
+
return { valid: false };
|
|
164
|
+
}
|
|
165
|
+
const payload = JSON.parse(Buffer.from(encodedPayload, "base64url").toString());
|
|
166
|
+
return { valid: true, payload };
|
|
167
|
+
}
|
|
168
|
+
catch {
|
|
169
|
+
return { valid: false };
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
exports.verifyJWT = verifyJWT;
|
|
173
|
+
// Password hashing
|
|
174
|
+
const hashPassword = (password, saltRounds = 12) => {
|
|
175
|
+
return new Promise((resolve, reject) => {
|
|
176
|
+
const salt = (0, exports.randomBytes)(16).toString("hex");
|
|
177
|
+
crypto_1.default.pbkdf2(password, salt, saltRounds, 64, "sha512", (err, derivedKey) => {
|
|
178
|
+
if (err)
|
|
179
|
+
reject(err);
|
|
180
|
+
else
|
|
181
|
+
resolve(`${saltRounds}:${salt}:${derivedKey.toString("hex")}`);
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
};
|
|
185
|
+
exports.hashPassword = hashPassword;
|
|
186
|
+
const verifyPassword = (password, hash) => {
|
|
187
|
+
return new Promise((resolve) => {
|
|
188
|
+
const [saltRounds, salt, key] = hash.split(":");
|
|
189
|
+
crypto_1.default.pbkdf2(password, salt, parseInt(saltRounds), 64, "sha512", (err, derivedKey) => {
|
|
190
|
+
resolve(!err && derivedKey.toString("hex") === key);
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
};
|
|
194
|
+
exports.verifyPassword = verifyPassword;
|
|
195
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/crypto/index.ts"],"names":[],"mappings":";;;;;;AAAA,oDAA4B;AAE5B,iBAAiB;AACV,MAAM,GAAG,GAAG,CAAC,IAAY,EAAU,EAAE;IAC1C,OAAO,gBAAM,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,KAAK,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CAAC,CAAC;QACtE,CAAC,CACF,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AAlBW,QAAA,YAAY,gBAkBvB;AAEK,MAAM,cAAc,GAAG,CAC5B,QAAgB,EAChB,IAAY,EACM,EAAE;IACpB,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC7B,MAAM,CAAC,UAAU,EAAE,IAAI,EAAE,GAAG,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAChD,gBAAM,CAAC,MAAM,CACX,QAAQ,EACR,IAAI,EACJ,QAAQ,CAAC,UAAU,CAAC,EACpB,EAAE,EACF,QAAQ,EACR,CAAC,GAAG,EAAE,UAAU,EAAE,EAAE;YAClB,OAAO,CAAC,CAAC,GAAG,IAAI,UAAU,CAAC,QAAQ,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC;QACtD,CAAC,CACF,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AAjBW,QAAA,cAAc,kBAiBzB"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export declare const formatDate: (date: Date, format?: string) => string;
|
|
2
|
+
export declare const formatDateTime: (date: Date) => string;
|
|
3
|
+
export declare const formatTime: (date: Date) => string;
|
|
4
|
+
export declare const parseDate: (dateString: string) => Date | null;
|
|
5
|
+
export declare const isValidDate: (date: Date) => boolean;
|
|
6
|
+
export declare const addDays: (date: Date, days: number) => Date;
|
|
7
|
+
export declare const addMonths: (date: Date, months: number) => Date;
|
|
8
|
+
export declare const addYears: (date: Date, years: number) => Date;
|
|
9
|
+
export declare const subDays: (date: Date, days: number) => Date;
|
|
10
|
+
export declare const subMonths: (date: Date, months: number) => Date;
|
|
11
|
+
export declare const subYears: (date: Date, years: number) => Date;
|
|
12
|
+
export declare const startOfDay: (date: Date) => Date;
|
|
13
|
+
export declare const endOfDay: (date: Date) => Date;
|
|
14
|
+
export declare const startOfWeek: (date: Date, startOfWeek?: number) => Date;
|
|
15
|
+
export declare const endOfWeek: (date: Date, startOfWeekValue?: number) => Date;
|
|
16
|
+
export declare const startOfMonth: (date: Date) => Date;
|
|
17
|
+
export declare const endOfMonth: (date: Date) => Date;
|
|
18
|
+
export declare const startOfYear: (date: Date) => Date;
|
|
19
|
+
export declare const endOfYear: (date: Date) => Date;
|
|
20
|
+
export declare const isSameDay: (date1: Date, date2: Date) => boolean;
|
|
21
|
+
export declare const isSameMonth: (date1: Date, date2: Date) => boolean;
|
|
22
|
+
export declare const isSameYear: (date1: Date, date2: Date) => boolean;
|
|
23
|
+
export declare const isToday: (date: Date) => boolean;
|
|
24
|
+
export declare const isYesterday: (date: Date) => boolean;
|
|
25
|
+
export declare const isTomorrow: (date: Date) => boolean;
|
|
26
|
+
export declare const isPast: (date: Date) => boolean;
|
|
27
|
+
export declare const isFuture: (date: Date) => boolean;
|
|
28
|
+
export declare const isBetween: (date: Date, startDate: Date, endDate: Date) => boolean;
|
|
29
|
+
export declare const differenceInDays: (date1: Date, date2: Date) => number;
|
|
30
|
+
export declare const differenceInHours: (date1: Date, date2: Date) => number;
|
|
31
|
+
export declare const differenceInMinutes: (date1: Date, date2: Date) => number;
|
|
32
|
+
export declare const differenceInSeconds: (date1: Date, date2: Date) => number;
|
|
33
|
+
export declare const getAge: (birthDate: Date) => number;
|
|
34
|
+
export declare const getDaysInMonth: (date: Date) => number;
|
|
35
|
+
export declare const getWeekOfYear: (date: Date) => number;
|
|
36
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/date/index.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,UAAU,GACrB,MAAM,IAAI,EACV,SAAQ,MAAqB,KAC5B,MAeF,CAAC;AAEF,eAAO,MAAM,cAAc,GAAI,MAAM,IAAI,KAAG,MAE3C,CAAC;AAEF,eAAO,MAAM,UAAU,GAAI,MAAM,IAAI,KAAG,MA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AAEF,eAAO,MAAM,WAAW,GAAI,OAAO,IAAI,EAAE,OAAO,IAAI,KAAG,OAKtD,CAAC;AAEF,eAAO,MAAM,UAAU,GAAI,OAAO,IAAI,EAAE,OAAO,IAAI,KAAG,OAErD,CAAC;AAEF,eAAO,MAAM,OAAO,GAAI,MAAM,IAAI,KAAG,OAEpC,CAAC;AAEF,eAAO,MAAM,WAAW,GAAI,MAAM,IAAI,KAAG,OAExC,CAAC;AAEF,eAAO,MAAM,UAAU,GAAI,MAAM,IAAI,KAAG,OAEvC,CAAC;AAEF,eAAO,MAAM,MAAM,GAAI,MAAM,IAAI,KAAG,OAEnC,CAAC;AAEF,eAAO,MAAM,QAAQ,GAAI,MAAM,IAAI,KAAG,OAErC,CAAC;AAEF,eAAO,MAAM,SAAS,GACpB,MAAM,IAAI,EACV,WAAW,IAAI,EACf,SAAS,IAAI,KACZ,OAEF,CAAC;AAEF,eAAO,MAAM,gBAAgB,GAAI,OAAO,IAAI,EAAE,OAAO,IAAI,KAAG,MAG3D,CAAC;AAEF,eAAO,MAAM,iBAAiB,GAAI,OAAO,IAAI,EAAE,OAAO,IAAI,KAAG,MAG5D,CAAC;AAEF,eAAO,MAAM,mBAAmB,GAAI,OAAO,IAAI,EAAE,OAAO,IAAI,KAAG,MAG9D,CAAC;AAEF,eAAO,MAAM,mBAAmB,GAAI,OAAO,IAAI,EAAE,OAAO,IAAI,KAAG,MAG9D,CAAC;AAEF,eAAO,MAAM,MAAM,GAAI,WAAW,IAAI,KAAG,MAaxC,CAAC;AAEF,eAAO,MAAM,cAAc,GAAI,MAAM,IAAI,KAAG,MAE3C,CAAC;AAEF,eAAO,MAAM,aAAa,GAAI,MAAM,IAAI,KAAG,MAI1C,CAAC"}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getWeekOfYear = exports.getDaysInMonth = exports.getAge = exports.differenceInSeconds = exports.differenceInMinutes = exports.differenceInHours = exports.differenceInDays = exports.isBetween = exports.isFuture = exports.isPast = exports.isTomorrow = exports.isYesterday = exports.isToday = exports.isSameYear = exports.isSameMonth = exports.isSameDay = exports.endOfYear = exports.startOfYear = exports.endOfMonth = exports.startOfMonth = exports.endOfWeek = exports.startOfWeek = exports.endOfDay = exports.startOfDay = exports.subYears = exports.subMonths = exports.subDays = exports.addYears = exports.addMonths = exports.addDays = exports.isValidDate = exports.parseDate = exports.formatTime = exports.formatDateTime = exports.formatDate = void 0;
|
|
4
|
+
// Date utilities
|
|
5
|
+
const formatDate = (date, format = "YYYY-MM-DD") => {
|
|
6
|
+
const year = date.getFullYear();
|
|
7
|
+
const month = String(date.getMonth() + 1).padStart(2, "0");
|
|
8
|
+
const day = String(date.getDate()).padStart(2, "0");
|
|
9
|
+
const hours = String(date.getHours()).padStart(2, "0");
|
|
10
|
+
const minutes = String(date.getMinutes()).padStart(2, "0");
|
|
11
|
+
const seconds = String(date.getSeconds()).padStart(2, "0");
|
|
12
|
+
return format
|
|
13
|
+
.replace("YYYY", String(year))
|
|
14
|
+
.replace("MM", month)
|
|
15
|
+
.replace("DD", day)
|
|
16
|
+
.replace("HH", hours)
|
|
17
|
+
.replace("mm", minutes)
|
|
18
|
+
.replace("ss", seconds);
|
|
19
|
+
};
|
|
20
|
+
exports.formatDate = formatDate;
|
|
21
|
+
const formatDateTime = (date) => {
|
|
22
|
+
return (0, exports.formatDate)(date, "YYYY-MM-DD HH:mm:ss");
|
|
23
|
+
};
|
|
24
|
+
exports.formatDateTime = formatDateTime;
|
|
25
|
+
const formatTime = (date) => {
|
|
26
|
+
return (0, exports.formatDate)(date, "HH:mm:ss");
|
|
27
|
+
};
|
|
28
|
+
exports.formatTime = formatTime;
|
|
29
|
+
const parseDate = (dateString) => {
|
|
30
|
+
const date = new Date(dateString);
|
|
31
|
+
return isNaN(date.getTime()) ? null : date;
|
|
32
|
+
};
|
|
33
|
+
exports.parseDate = parseDate;
|
|
34
|
+
const isValidDate = (date) => {
|
|
35
|
+
return !isNaN(date.getTime());
|
|
36
|
+
};
|
|
37
|
+
exports.isValidDate = isValidDate;
|
|
38
|
+
const addDays = (date, days) => {
|
|
39
|
+
const result = new Date(date);
|
|
40
|
+
result.setDate(result.getDate() + days);
|
|
41
|
+
return result;
|
|
42
|
+
};
|
|
43
|
+
exports.addDays = addDays;
|
|
44
|
+
const addMonths = (date, months) => {
|
|
45
|
+
const result = new Date(date);
|
|
46
|
+
result.setMonth(result.getMonth() + months);
|
|
47
|
+
return result;
|
|
48
|
+
};
|
|
49
|
+
exports.addMonths = addMonths;
|
|
50
|
+
const addYears = (date, years) => {
|
|
51
|
+
const result = new Date(date);
|
|
52
|
+
result.setFullYear(result.getFullYear() + years);
|
|
53
|
+
return result;
|
|
54
|
+
};
|
|
55
|
+
exports.addYears = addYears;
|
|
56
|
+
const subDays = (date, days) => {
|
|
57
|
+
return (0, exports.addDays)(date, -days);
|
|
58
|
+
};
|
|
59
|
+
exports.subDays = subDays;
|
|
60
|
+
const subMonths = (date, months) => {
|
|
61
|
+
return (0, exports.addMonths)(date, -months);
|
|
62
|
+
};
|
|
63
|
+
exports.subMonths = subMonths;
|
|
64
|
+
const subYears = (date, years) => {
|
|
65
|
+
return (0, exports.addYears)(date, -years);
|
|
66
|
+
};
|
|
67
|
+
exports.subYears = subYears;
|
|
68
|
+
const startOfDay = (date) => {
|
|
69
|
+
const result = new Date(date);
|
|
70
|
+
result.setHours(0, 0, 0, 0);
|
|
71
|
+
return result;
|
|
72
|
+
};
|
|
73
|
+
exports.startOfDay = startOfDay;
|
|
74
|
+
const endOfDay = (date) => {
|
|
75
|
+
const result = new Date(date);
|
|
76
|
+
result.setHours(23, 59, 59, 999);
|
|
77
|
+
return result;
|
|
78
|
+
};
|
|
79
|
+
exports.endOfDay = endOfDay;
|
|
80
|
+
const startOfWeek = (date, startOfWeek = 1) => {
|
|
81
|
+
const result = new Date(date);
|
|
82
|
+
const day = result.getDay();
|
|
83
|
+
const diff = (day < startOfWeek ? 7 : 0) + day - startOfWeek;
|
|
84
|
+
result.setDate(result.getDate() - diff);
|
|
85
|
+
return (0, exports.startOfDay)(result);
|
|
86
|
+
};
|
|
87
|
+
exports.startOfWeek = startOfWeek;
|
|
88
|
+
const endOfWeek = (date, startOfWeekValue = 1) => {
|
|
89
|
+
const result = (0, exports.startOfWeek)(date, startOfWeekValue);
|
|
90
|
+
result.setDate(result.getDate() + 6);
|
|
91
|
+
return (0, exports.endOfDay)(result);
|
|
92
|
+
};
|
|
93
|
+
exports.endOfWeek = endOfWeek;
|
|
94
|
+
const startOfMonth = (date) => {
|
|
95
|
+
const result = new Date(date);
|
|
96
|
+
result.setDate(1);
|
|
97
|
+
return (0, exports.startOfDay)(result);
|
|
98
|
+
};
|
|
99
|
+
exports.startOfMonth = startOfMonth;
|
|
100
|
+
const endOfMonth = (date) => {
|
|
101
|
+
const result = new Date(date);
|
|
102
|
+
result.setMonth(result.getMonth() + 1, 0);
|
|
103
|
+
return (0, exports.endOfDay)(result);
|
|
104
|
+
};
|
|
105
|
+
exports.endOfMonth = endOfMonth;
|
|
106
|
+
const startOfYear = (date) => {
|
|
107
|
+
const result = new Date(date);
|
|
108
|
+
result.setMonth(0, 1);
|
|
109
|
+
return (0, exports.startOfDay)(result);
|
|
110
|
+
};
|
|
111
|
+
exports.startOfYear = startOfYear;
|
|
112
|
+
const endOfYear = (date) => {
|
|
113
|
+
const result = new Date(date);
|
|
114
|
+
result.setMonth(11, 31);
|
|
115
|
+
return (0, exports.endOfDay)(result);
|
|
116
|
+
};
|
|
117
|
+
exports.endOfYear = endOfYear;
|
|
118
|
+
const isSameDay = (date1, date2) => {
|
|
119
|
+
return (date1.getFullYear() === date2.getFullYear() &&
|
|
120
|
+
date1.getMonth() === date2.getMonth() &&
|
|
121
|
+
date1.getDate() === date2.getDate());
|
|
122
|
+
};
|
|
123
|
+
exports.isSameDay = isSameDay;
|
|
124
|
+
const isSameMonth = (date1, date2) => {
|
|
125
|
+
return (date1.getFullYear() === date2.getFullYear() &&
|
|
126
|
+
date1.getMonth() === date2.getMonth());
|
|
127
|
+
};
|
|
128
|
+
exports.isSameMonth = isSameMonth;
|
|
129
|
+
const isSameYear = (date1, date2) => {
|
|
130
|
+
return date1.getFullYear() === date2.getFullYear();
|
|
131
|
+
};
|
|
132
|
+
exports.isSameYear = isSameYear;
|
|
133
|
+
const isToday = (date) => {
|
|
134
|
+
return (0, exports.isSameDay)(date, new Date());
|
|
135
|
+
};
|
|
136
|
+
exports.isToday = isToday;
|
|
137
|
+
const isYesterday = (date) => {
|
|
138
|
+
return (0, exports.isSameDay)(date, (0, exports.subDays)(new Date(), 1));
|
|
139
|
+
};
|
|
140
|
+
exports.isYesterday = isYesterday;
|
|
141
|
+
const isTomorrow = (date) => {
|
|
142
|
+
return (0, exports.isSameDay)(date, (0, exports.addDays)(new Date(), 1));
|
|
143
|
+
};
|
|
144
|
+
exports.isTomorrow = isTomorrow;
|
|
145
|
+
const isPast = (date) => {
|
|
146
|
+
return date < new Date();
|
|
147
|
+
};
|
|
148
|
+
exports.isPast = isPast;
|
|
149
|
+
const isFuture = (date) => {
|
|
150
|
+
return date > new Date();
|
|
151
|
+
};
|
|
152
|
+
exports.isFuture = isFuture;
|
|
153
|
+
const isBetween = (date, startDate, endDate) => {
|
|
154
|
+
return date >= startDate && date <= endDate;
|
|
155
|
+
};
|
|
156
|
+
exports.isBetween = isBetween;
|
|
157
|
+
const differenceInDays = (date1, date2) => {
|
|
158
|
+
const diffTime = Math.abs(date1.getTime() - date2.getTime());
|
|
159
|
+
return Math.ceil(diffTime / (1000 * 60 * 60 * 24));
|
|
160
|
+
};
|
|
161
|
+
exports.differenceInDays = differenceInDays;
|
|
162
|
+
const differenceInHours = (date1, date2) => {
|
|
163
|
+
const diffTime = Math.abs(date1.getTime() - date2.getTime());
|
|
164
|
+
return Math.floor(diffTime / (1000 * 60 * 60));
|
|
165
|
+
};
|
|
166
|
+
exports.differenceInHours = differenceInHours;
|
|
167
|
+
const differenceInMinutes = (date1, date2) => {
|
|
168
|
+
const diffTime = Math.abs(date1.getTime() - date2.getTime());
|
|
169
|
+
return Math.floor(diffTime / (1000 * 60));
|
|
170
|
+
};
|
|
171
|
+
exports.differenceInMinutes = differenceInMinutes;
|
|
172
|
+
const differenceInSeconds = (date1, date2) => {
|
|
173
|
+
const diffTime = Math.abs(date1.getTime() - date2.getTime());
|
|
174
|
+
return Math.floor(diffTime / 1000);
|
|
175
|
+
};
|
|
176
|
+
exports.differenceInSeconds = differenceInSeconds;
|
|
177
|
+
const getAge = (birthDate) => {
|
|
178
|
+
const today = new Date();
|
|
179
|
+
let age = today.getFullYear() - birthDate.getFullYear();
|
|
180
|
+
const monthDiff = today.getMonth() - birthDate.getMonth();
|
|
181
|
+
if (monthDiff < 0 ||
|
|
182
|
+
(monthDiff === 0 && today.getDate() < birthDate.getDate())) {
|
|
183
|
+
age--;
|
|
184
|
+
}
|
|
185
|
+
return age;
|
|
186
|
+
};
|
|
187
|
+
exports.getAge = getAge;
|
|
188
|
+
const getDaysInMonth = (date) => {
|
|
189
|
+
return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
|
|
190
|
+
};
|
|
191
|
+
exports.getDaysInMonth = getDaysInMonth;
|
|
192
|
+
const getWeekOfYear = (date) => {
|
|
193
|
+
const firstDayOfYear = new Date(date.getFullYear(), 0, 1);
|
|
194
|
+
const pastDaysOfYear = (date.getTime() - firstDayOfYear.getTime()) / 86400000;
|
|
195
|
+
return Math.ceil((pastDaysOfYear + firstDayOfYear.getDay() + 1) / 7);
|
|
196
|
+
};
|
|
197
|
+
exports.getWeekOfYear = getWeekOfYear;
|
|
198
|
+
// formatRelativeTime is exported from format module
|
|
199
|
+
//# sourceMappingURL=index.js.map
|