t1s-utils-lite 0.0.1
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/array.d.ts +26 -0
- package/dist/array.js +90 -0
- package/dist/cookie.d.ts +8 -0
- package/dist/cookie.js +44 -0
- package/dist/date.d.ts +15 -0
- package/dist/date.js +90 -0
- package/dist/id.d.ts +10 -0
- package/dist/id.js +54 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +52 -0
- package/dist/number.d.ts +26 -0
- package/dist/number.js +55 -0
- package/dist/object.d.ts +18 -0
- package/dist/object.js +104 -0
- package/dist/string.d.ts +23 -0
- package/dist/string.js +112 -0
- package/dist/validation.d.ts +21 -0
- package/dist/validation.js +88 -0
- package/package.json +21 -0
package/dist/array.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export declare class ArrayUtils {
|
|
2
|
+
unique<T>(arr: T[]): T[];
|
|
3
|
+
chunk<T>(arr: T[], size: number): T[][];
|
|
4
|
+
groupBy<T>(arr: T[], key: keyof T | ((item: T) => string)): Record<string, T[]>;
|
|
5
|
+
sortBy<T>(arr: T[], key: keyof T, order?: 'asc' | 'desc'): T[];
|
|
6
|
+
shuffle<T>(arr: T[]): T[];
|
|
7
|
+
sample<T>(arr: T[]): T | undefined;
|
|
8
|
+
intersection<T>(a: T[], b: T[]): T[];
|
|
9
|
+
difference<T>(a: T[], b: T[]): T[];
|
|
10
|
+
compact<T>(arr: (T | null | undefined | false | 0 | '')[]): T[];
|
|
11
|
+
flatten<T>(arr: (T | T[])[]): T[];
|
|
12
|
+
flattenDeep<T>(arr: unknown[]): T[];
|
|
13
|
+
partition<T>(arr: T[], predicate: (item: T) => boolean): [T[], T[]];
|
|
14
|
+
pluck<T, K extends keyof T>(arr: T[], key: K): T[K][];
|
|
15
|
+
first<T>(arr: T[]): T | undefined;
|
|
16
|
+
last<T>(arr: T[]): T | undefined;
|
|
17
|
+
drop<T>(arr: T[], n?: number): T[];
|
|
18
|
+
take<T>(arr: T[], n?: number): T[];
|
|
19
|
+
sum(arr: number[]): number;
|
|
20
|
+
average(arr: number[]): number;
|
|
21
|
+
min(arr: number[]): number | undefined;
|
|
22
|
+
max(arr: number[]): number | undefined;
|
|
23
|
+
range(start: number, end: number, step?: number): number[];
|
|
24
|
+
zip<T>(...arrays: T[][]): T[][];
|
|
25
|
+
uniqBy<T>(arr: T[], key: keyof T): T[];
|
|
26
|
+
}
|
package/dist/array.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ArrayUtils = void 0;
|
|
4
|
+
class ArrayUtils {
|
|
5
|
+
unique(arr) {
|
|
6
|
+
return [...new Set(arr)];
|
|
7
|
+
}
|
|
8
|
+
chunk(arr, size) {
|
|
9
|
+
const chunks = [];
|
|
10
|
+
for (let i = 0; i < arr.length; i += size) {
|
|
11
|
+
chunks.push(arr.slice(i, i + size));
|
|
12
|
+
}
|
|
13
|
+
return chunks;
|
|
14
|
+
}
|
|
15
|
+
groupBy(arr, key) {
|
|
16
|
+
return arr.reduce((groups, item) => {
|
|
17
|
+
const groupKey = typeof key === 'function' ? key(item) : String(item[key]);
|
|
18
|
+
(groups[groupKey] = groups[groupKey] || []).push(item);
|
|
19
|
+
return groups;
|
|
20
|
+
}, {});
|
|
21
|
+
}
|
|
22
|
+
sortBy(arr, key, order = 'asc') {
|
|
23
|
+
return [...arr].sort((a, b) => {
|
|
24
|
+
const aVal = a[key], bVal = b[key];
|
|
25
|
+
if (aVal < bVal)
|
|
26
|
+
return order === 'asc' ? -1 : 1;
|
|
27
|
+
if (aVal > bVal)
|
|
28
|
+
return order === 'asc' ? 1 : -1;
|
|
29
|
+
return 0;
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
shuffle(arr) {
|
|
33
|
+
const result = [...arr];
|
|
34
|
+
for (let i = result.length - 1; i > 0; i--) {
|
|
35
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
36
|
+
[result[i], result[j]] = [result[j], result[i]];
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
sample(arr) {
|
|
41
|
+
return arr[Math.floor(Math.random() * arr.length)];
|
|
42
|
+
}
|
|
43
|
+
intersection(a, b) {
|
|
44
|
+
return a.filter(x => b.includes(x));
|
|
45
|
+
}
|
|
46
|
+
difference(a, b) {
|
|
47
|
+
return a.filter(x => !b.includes(x));
|
|
48
|
+
}
|
|
49
|
+
compact(arr) {
|
|
50
|
+
return arr.filter(Boolean);
|
|
51
|
+
}
|
|
52
|
+
flatten(arr) {
|
|
53
|
+
return arr.reduce((flat, item) => flat.concat(Array.isArray(item) ? item : item), []);
|
|
54
|
+
}
|
|
55
|
+
flattenDeep(arr) {
|
|
56
|
+
return arr.reduce((flat, item) => flat.concat(Array.isArray(item) ? this.flattenDeep(item) : item), []);
|
|
57
|
+
}
|
|
58
|
+
partition(arr, predicate) {
|
|
59
|
+
const pass = [], fail = [];
|
|
60
|
+
arr.forEach(item => (predicate(item) ? pass : fail).push(item));
|
|
61
|
+
return [pass, fail];
|
|
62
|
+
}
|
|
63
|
+
pluck(arr, key) {
|
|
64
|
+
return arr.map(item => item[key]);
|
|
65
|
+
}
|
|
66
|
+
first(arr) { return arr[0]; }
|
|
67
|
+
last(arr) { return arr[arr.length - 1]; }
|
|
68
|
+
drop(arr, n = 1) { return arr.slice(n); }
|
|
69
|
+
take(arr, n = 1) { return arr.slice(0, n); }
|
|
70
|
+
sum(arr) { return arr.reduce((a, b) => a + b, 0); }
|
|
71
|
+
average(arr) { return arr.length ? this.sum(arr) / arr.length : 0; }
|
|
72
|
+
min(arr) { return arr.length ? Math.min(...arr) : undefined; }
|
|
73
|
+
max(arr) { return arr.length ? Math.max(...arr) : undefined; }
|
|
74
|
+
range(start, end, step = 1) {
|
|
75
|
+
const result = [];
|
|
76
|
+
for (let i = start; i < end; i += step)
|
|
77
|
+
result.push(i);
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
zip(...arrays) {
|
|
81
|
+
const maxLen = Math.max(...arrays.map(a => a.length));
|
|
82
|
+
return Array(maxLen).fill(0).map((_, i) => arrays.map(a => a[i]));
|
|
83
|
+
}
|
|
84
|
+
uniqBy(arr, key) {
|
|
85
|
+
const seen = new Set();
|
|
86
|
+
return arr.filter(item => { const v = item[key]; if (seen.has(v))
|
|
87
|
+
return false; seen.add(v); return true; });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
exports.ArrayUtils = ArrayUtils;
|
package/dist/cookie.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare class CookieUtils {
|
|
2
|
+
set(key: string, value: string, days?: number, path?: string): void;
|
|
3
|
+
get(key: string): string | null;
|
|
4
|
+
has(key: string): boolean;
|
|
5
|
+
delete(key: string, path?: string): void;
|
|
6
|
+
clear(): void;
|
|
7
|
+
all(): Record<string, string>;
|
|
8
|
+
}
|
package/dist/cookie.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CookieUtils = void 0;
|
|
4
|
+
class CookieUtils {
|
|
5
|
+
set(key, value, days = 7, path = '/') {
|
|
6
|
+
const expires = new Date(Date.now() + days * 864e5).toUTCString();
|
|
7
|
+
document.cookie = `${encodeURIComponent(key)}=${encodeURIComponent(value)};expires=${expires};path=${path}`;
|
|
8
|
+
}
|
|
9
|
+
get(key) {
|
|
10
|
+
const cookies = document.cookie.split(';');
|
|
11
|
+
for (const cookie of cookies) {
|
|
12
|
+
const [cookieKey, ...valParts] = cookie.trim().split('=');
|
|
13
|
+
if (decodeURIComponent(cookieKey) === key) {
|
|
14
|
+
return decodeURIComponent(valParts.join('='));
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
has(key) {
|
|
20
|
+
return this.get(key) !== null;
|
|
21
|
+
}
|
|
22
|
+
delete(key, path = '/') {
|
|
23
|
+
document.cookie = `${encodeURIComponent(key)}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=${path}`;
|
|
24
|
+
}
|
|
25
|
+
clear() {
|
|
26
|
+
const cookies = document.cookie.split(';');
|
|
27
|
+
for (const cookie of cookies) {
|
|
28
|
+
const key = cookie.trim().split('=')[0];
|
|
29
|
+
this.delete(decodeURIComponent(key));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
all() {
|
|
33
|
+
const result = {};
|
|
34
|
+
const cookies = document.cookie.split(';');
|
|
35
|
+
for (const cookie of cookies) {
|
|
36
|
+
const [cookieKey, ...valParts] = cookie.trim().split('=');
|
|
37
|
+
const key = decodeURIComponent(cookieKey);
|
|
38
|
+
const value = decodeURIComponent(valParts.join('='));
|
|
39
|
+
result[key] = value;
|
|
40
|
+
}
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
exports.CookieUtils = CookieUtils;
|
package/dist/date.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare class DateUtils {
|
|
2
|
+
format(date: Date, formatStr: string): string;
|
|
3
|
+
relative(date: Date): string;
|
|
4
|
+
isToday(date: Date): boolean;
|
|
5
|
+
add(date: Date, amount: number, unit: 'day' | 'month' | 'year'): Date;
|
|
6
|
+
diff(d1: Date, d2: Date): number;
|
|
7
|
+
startOf(date: Date, unit: 'day' | 'month' | 'year'): Date;
|
|
8
|
+
endOf(date: Date, unit: 'day' | 'month' | 'year'): Date;
|
|
9
|
+
isBefore(d1: Date, d2: Date): boolean;
|
|
10
|
+
isAfter(d1: Date, d2: Date): boolean;
|
|
11
|
+
isSame(d1: Date, d2: Date): boolean;
|
|
12
|
+
parse(dateStr: string): Date | null;
|
|
13
|
+
now(): number;
|
|
14
|
+
timestamp(): number;
|
|
15
|
+
}
|
package/dist/date.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DateUtils = void 0;
|
|
4
|
+
class DateUtils {
|
|
5
|
+
format(date, formatStr) {
|
|
6
|
+
const d = new Date(date);
|
|
7
|
+
const pad = (n) => n.toString().padStart(2, '0');
|
|
8
|
+
const year = d.getFullYear();
|
|
9
|
+
const month = pad(d.getMonth() + 1);
|
|
10
|
+
const day = pad(d.getDate());
|
|
11
|
+
const hours = pad(d.getHours());
|
|
12
|
+
const minutes = pad(d.getMinutes());
|
|
13
|
+
const seconds = pad(d.getSeconds());
|
|
14
|
+
return formatStr
|
|
15
|
+
.replace('YYYY', String(year))
|
|
16
|
+
.replace('MM', month)
|
|
17
|
+
.replace('DD', day)
|
|
18
|
+
.replace('HH', hours)
|
|
19
|
+
.replace('mm', minutes)
|
|
20
|
+
.replace('ss', seconds);
|
|
21
|
+
}
|
|
22
|
+
relative(date) {
|
|
23
|
+
const now = Date.now();
|
|
24
|
+
const diff = now - new Date(date).getTime();
|
|
25
|
+
const seconds = Math.floor(diff / 1000);
|
|
26
|
+
if (seconds < 60)
|
|
27
|
+
return `${seconds} seconds ago`;
|
|
28
|
+
const minutes = Math.floor(seconds / 60);
|
|
29
|
+
if (minutes < 60)
|
|
30
|
+
return `${minutes} minutes ago`;
|
|
31
|
+
const hours = Math.floor(minutes / 60);
|
|
32
|
+
if (hours < 24)
|
|
33
|
+
return `${hours} hours ago`;
|
|
34
|
+
const days = Math.floor(hours / 24);
|
|
35
|
+
if (days < 30)
|
|
36
|
+
return `${days} days ago`;
|
|
37
|
+
const months = Math.floor(days / 30);
|
|
38
|
+
if (months < 12)
|
|
39
|
+
return `${months} months ago`;
|
|
40
|
+
return `${Math.floor(days / 365)} years ago`;
|
|
41
|
+
}
|
|
42
|
+
isToday(date) {
|
|
43
|
+
const d = new Date(date);
|
|
44
|
+
const today = new Date();
|
|
45
|
+
return d.getDate() === today.getDate() && d.getMonth() === today.getMonth() && d.getFullYear() === today.getFullYear();
|
|
46
|
+
}
|
|
47
|
+
add(date, amount, unit) {
|
|
48
|
+
const d = new Date(date);
|
|
49
|
+
if (unit === 'day')
|
|
50
|
+
d.setDate(d.getDate() + amount);
|
|
51
|
+
else if (unit === 'month')
|
|
52
|
+
d.setMonth(d.getMonth() + amount);
|
|
53
|
+
else
|
|
54
|
+
d.setFullYear(d.getFullYear() + amount);
|
|
55
|
+
return d;
|
|
56
|
+
}
|
|
57
|
+
diff(d1, d2) {
|
|
58
|
+
return Math.floor((new Date(d1).getTime() - new Date(d2).getTime()) / (1000 * 60 * 60 * 24));
|
|
59
|
+
}
|
|
60
|
+
startOf(date, unit) {
|
|
61
|
+
const d = new Date(date);
|
|
62
|
+
if (unit === 'day')
|
|
63
|
+
d.setHours(0, 0, 0, 0);
|
|
64
|
+
else if (unit === 'month')
|
|
65
|
+
d.setDate(1);
|
|
66
|
+
else
|
|
67
|
+
d.setMonth(0, 1);
|
|
68
|
+
return d;
|
|
69
|
+
}
|
|
70
|
+
endOf(date, unit) {
|
|
71
|
+
const d = this.startOf(date, unit);
|
|
72
|
+
if (unit === 'day')
|
|
73
|
+
d.setHours(23, 59, 59, 999);
|
|
74
|
+
else if (unit === 'month')
|
|
75
|
+
d.setMonth(d.getMonth() + 1, 0);
|
|
76
|
+
else
|
|
77
|
+
d.setFullYear(d.getFullYear() + 1, 0, 0);
|
|
78
|
+
return d;
|
|
79
|
+
}
|
|
80
|
+
isBefore(d1, d2) { return new Date(d1).getTime() < new Date(d2).getTime(); }
|
|
81
|
+
isAfter(d1, d2) { return new Date(d1).getTime() > new Date(d2).getTime(); }
|
|
82
|
+
isSame(d1, d2) { return this.isBefore(d1, d2) === false && this.isAfter(d1, d2) === false; }
|
|
83
|
+
parse(dateStr) {
|
|
84
|
+
const d = new Date(dateStr);
|
|
85
|
+
return isNaN(d.getTime()) ? null : d;
|
|
86
|
+
}
|
|
87
|
+
now() { return Date.now(); }
|
|
88
|
+
timestamp() { return Date.now(); }
|
|
89
|
+
}
|
|
90
|
+
exports.DateUtils = DateUtils;
|
package/dist/id.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare class IdUtils {
|
|
2
|
+
private readonly base36Chars;
|
|
3
|
+
uuid(): string;
|
|
4
|
+
nanoid(size?: number): string;
|
|
5
|
+
shortId(length?: number): string;
|
|
6
|
+
snowflake(): string;
|
|
7
|
+
hash(input: string): string;
|
|
8
|
+
generateCode(length?: number, charset?: 'numeric' | 'alpha' | 'alphanumeric'): string;
|
|
9
|
+
generateToken(length?: number): string;
|
|
10
|
+
}
|
package/dist/id.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IdUtils = void 0;
|
|
4
|
+
class IdUtils {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.base36Chars = '0123456789abcdefghijklmnopqrstuvwxyz';
|
|
7
|
+
}
|
|
8
|
+
uuid() {
|
|
9
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
|
10
|
+
const r = Math.random() * 16 | 0;
|
|
11
|
+
const v = c === 'x' ? r : (r & 0x3 | 0x8);
|
|
12
|
+
return v.toString(16);
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
nanoid(size = 21) {
|
|
16
|
+
const bytes = new Uint8Array(size);
|
|
17
|
+
crypto.getRandomValues(bytes);
|
|
18
|
+
return Array.from(bytes, b => this.base36Chars[b & 63]).join('');
|
|
19
|
+
}
|
|
20
|
+
shortId(length = 10) {
|
|
21
|
+
return this.nanoid(length).replace(/[_=-]/g, '');
|
|
22
|
+
}
|
|
23
|
+
snowflake() {
|
|
24
|
+
const timestamp = BigInt(Date.now() - 1700000000000);
|
|
25
|
+
const machineId = BigInt(Math.floor(Math.random() * 1024));
|
|
26
|
+
const sequence = BigInt(Math.floor(Math.random() * 4096));
|
|
27
|
+
const id = (timestamp << 22n) | (machineId << 12n) | sequence;
|
|
28
|
+
return id.toString();
|
|
29
|
+
}
|
|
30
|
+
hash(input) {
|
|
31
|
+
let hash = 0;
|
|
32
|
+
for (let i = 0; i < input.length; i++) {
|
|
33
|
+
const char = input.charCodeAt(i);
|
|
34
|
+
hash = ((hash << 5) - hash) + char;
|
|
35
|
+
hash = hash & hash;
|
|
36
|
+
}
|
|
37
|
+
return Math.abs(hash).toString(36);
|
|
38
|
+
}
|
|
39
|
+
generateCode(length = 6, charset = 'numeric') {
|
|
40
|
+
const charsets = {
|
|
41
|
+
numeric: '0123456789',
|
|
42
|
+
alpha: 'abcdefghijklmnopqrstuvwxyz',
|
|
43
|
+
alphanumeric: '0123456789abcdefghijklmnopqrstuvwxyz'
|
|
44
|
+
};
|
|
45
|
+
const chars = charsets[charset];
|
|
46
|
+
return Array(length).fill(0).map(() => chars[Math.floor(Math.random() * chars.length)]).join('');
|
|
47
|
+
}
|
|
48
|
+
generateToken(length = 32) {
|
|
49
|
+
const bytes = new Uint8Array(length);
|
|
50
|
+
crypto.getRandomValues(bytes);
|
|
51
|
+
return Array.from(bytes, b => b.toString(16).padStart(2, '0')).join('');
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
exports.IdUtils = IdUtils;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { StringUtils } from './string';
|
|
2
|
+
import { ArrayUtils } from './array';
|
|
3
|
+
import { ObjectUtils } from './object';
|
|
4
|
+
import { DateUtils } from './date';
|
|
5
|
+
import { NumberUtils } from './number';
|
|
6
|
+
import { ValidationUtils } from './validation';
|
|
7
|
+
import { IdUtils } from './id';
|
|
8
|
+
import { CookieUtils } from './cookie';
|
|
9
|
+
export * from './string';
|
|
10
|
+
export * from './array';
|
|
11
|
+
export * from './object';
|
|
12
|
+
export * from './date';
|
|
13
|
+
export * from './number';
|
|
14
|
+
export * from './validation';
|
|
15
|
+
export * from './id';
|
|
16
|
+
export * from './cookie';
|
|
17
|
+
export { StringUtils, ArrayUtils, ObjectUtils, DateUtils, NumberUtils, ValidationUtils, IdUtils, CookieUtils };
|
|
18
|
+
export declare const Utils: {
|
|
19
|
+
string: typeof StringUtils;
|
|
20
|
+
array: typeof ArrayUtils;
|
|
21
|
+
object: typeof ObjectUtils;
|
|
22
|
+
date: typeof DateUtils;
|
|
23
|
+
number: typeof NumberUtils;
|
|
24
|
+
validation: typeof ValidationUtils;
|
|
25
|
+
id: typeof IdUtils;
|
|
26
|
+
cookie: typeof CookieUtils;
|
|
27
|
+
};
|
|
28
|
+
export default Utils;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
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
|
+
exports.Utils = exports.CookieUtils = exports.IdUtils = exports.ValidationUtils = exports.NumberUtils = exports.DateUtils = exports.ObjectUtils = exports.ArrayUtils = exports.StringUtils = void 0;
|
|
18
|
+
const string_1 = require("./string");
|
|
19
|
+
Object.defineProperty(exports, "StringUtils", { enumerable: true, get: function () { return string_1.StringUtils; } });
|
|
20
|
+
const array_1 = require("./array");
|
|
21
|
+
Object.defineProperty(exports, "ArrayUtils", { enumerable: true, get: function () { return array_1.ArrayUtils; } });
|
|
22
|
+
const object_1 = require("./object");
|
|
23
|
+
Object.defineProperty(exports, "ObjectUtils", { enumerable: true, get: function () { return object_1.ObjectUtils; } });
|
|
24
|
+
const date_1 = require("./date");
|
|
25
|
+
Object.defineProperty(exports, "DateUtils", { enumerable: true, get: function () { return date_1.DateUtils; } });
|
|
26
|
+
const number_1 = require("./number");
|
|
27
|
+
Object.defineProperty(exports, "NumberUtils", { enumerable: true, get: function () { return number_1.NumberUtils; } });
|
|
28
|
+
const validation_1 = require("./validation");
|
|
29
|
+
Object.defineProperty(exports, "ValidationUtils", { enumerable: true, get: function () { return validation_1.ValidationUtils; } });
|
|
30
|
+
const id_1 = require("./id");
|
|
31
|
+
Object.defineProperty(exports, "IdUtils", { enumerable: true, get: function () { return id_1.IdUtils; } });
|
|
32
|
+
const cookie_1 = require("./cookie");
|
|
33
|
+
Object.defineProperty(exports, "CookieUtils", { enumerable: true, get: function () { return cookie_1.CookieUtils; } });
|
|
34
|
+
__exportStar(require("./string"), exports);
|
|
35
|
+
__exportStar(require("./array"), exports);
|
|
36
|
+
__exportStar(require("./object"), exports);
|
|
37
|
+
__exportStar(require("./date"), exports);
|
|
38
|
+
__exportStar(require("./number"), exports);
|
|
39
|
+
__exportStar(require("./validation"), exports);
|
|
40
|
+
__exportStar(require("./id"), exports);
|
|
41
|
+
__exportStar(require("./cookie"), exports);
|
|
42
|
+
exports.Utils = {
|
|
43
|
+
string: string_1.StringUtils,
|
|
44
|
+
array: array_1.ArrayUtils,
|
|
45
|
+
object: object_1.ObjectUtils,
|
|
46
|
+
date: date_1.DateUtils,
|
|
47
|
+
number: number_1.NumberUtils,
|
|
48
|
+
validation: validation_1.ValidationUtils,
|
|
49
|
+
id: id_1.IdUtils,
|
|
50
|
+
cookie: cookie_1.CookieUtils,
|
|
51
|
+
};
|
|
52
|
+
exports.default = exports.Utils;
|
package/dist/number.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export declare class NumberUtils {
|
|
2
|
+
formatCurrency(value: number, currency?: string, locale?: string): string;
|
|
3
|
+
formatBytes(bytes: number, decimals?: number): string;
|
|
4
|
+
formatPercent(value: number, decimals?: number): string;
|
|
5
|
+
clamp(value: number, min: number, max: number): number;
|
|
6
|
+
round(value: number, decimals?: number): number;
|
|
7
|
+
randomInt(min: number, max: number): number;
|
|
8
|
+
randomFloat(min: number, max: number): number;
|
|
9
|
+
isEven(n: number): boolean;
|
|
10
|
+
isOdd(n: number): boolean;
|
|
11
|
+
isPositive(n: number): boolean;
|
|
12
|
+
isNegative(n: number): boolean;
|
|
13
|
+
isInteger(n: number): boolean;
|
|
14
|
+
isNaN(n: number): boolean;
|
|
15
|
+
isFinite(n: number): boolean;
|
|
16
|
+
abs(n: number): number;
|
|
17
|
+
floor(n: number): number;
|
|
18
|
+
ceil(n: number): number;
|
|
19
|
+
sqrt(n: number): number;
|
|
20
|
+
pow(n: number, exp: number): number;
|
|
21
|
+
sum(...nums: number[]): number;
|
|
22
|
+
avg(...nums: number[]): number;
|
|
23
|
+
min(...nums: number[]): number;
|
|
24
|
+
max(...nums: number[]): number;
|
|
25
|
+
range(start: number, end: number, step?: number): number[];
|
|
26
|
+
}
|
package/dist/number.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NumberUtils = void 0;
|
|
4
|
+
class NumberUtils {
|
|
5
|
+
formatCurrency(value, currency = 'USD', locale = 'en-US') {
|
|
6
|
+
return new Intl.NumberFormat(locale, { style: 'currency', currency }).format(value);
|
|
7
|
+
}
|
|
8
|
+
formatBytes(bytes, decimals = 2) {
|
|
9
|
+
if (bytes === 0)
|
|
10
|
+
return '0 Bytes';
|
|
11
|
+
const k = 1024;
|
|
12
|
+
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
|
|
13
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
14
|
+
return parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)) + ' ' + sizes[i];
|
|
15
|
+
}
|
|
16
|
+
formatPercent(value, decimals = 2) {
|
|
17
|
+
return (value * 100).toFixed(decimals) + '%';
|
|
18
|
+
}
|
|
19
|
+
clamp(value, min, max) {
|
|
20
|
+
return Math.min(Math.max(value, min), max);
|
|
21
|
+
}
|
|
22
|
+
round(value, decimals = 0) {
|
|
23
|
+
const factor = Math.pow(10, decimals);
|
|
24
|
+
return Math.round(value * factor) / factor;
|
|
25
|
+
}
|
|
26
|
+
randomInt(min, max) {
|
|
27
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
28
|
+
}
|
|
29
|
+
randomFloat(min, max) {
|
|
30
|
+
return Math.random() * (max - min) + min;
|
|
31
|
+
}
|
|
32
|
+
isEven(n) { return n % 2 === 0; }
|
|
33
|
+
isOdd(n) { return n % 2 !== 0; }
|
|
34
|
+
isPositive(n) { return n > 0; }
|
|
35
|
+
isNegative(n) { return n < 0; }
|
|
36
|
+
isInteger(n) { return Number.isInteger(n); }
|
|
37
|
+
isNaN(n) { return Number.isNaN(n); }
|
|
38
|
+
isFinite(n) { return Number.isFinite(n); }
|
|
39
|
+
abs(n) { return Math.abs(n); }
|
|
40
|
+
floor(n) { return Math.floor(n); }
|
|
41
|
+
ceil(n) { return Math.ceil(n); }
|
|
42
|
+
sqrt(n) { return Math.sqrt(n); }
|
|
43
|
+
pow(n, exp) { return Math.pow(n, exp); }
|
|
44
|
+
sum(...nums) { return nums.reduce((a, b) => a + b, 0); }
|
|
45
|
+
avg(...nums) { return nums.length ? this.sum(...nums) / nums.length : 0; }
|
|
46
|
+
min(...nums) { return Math.min(...nums); }
|
|
47
|
+
max(...nums) { return Math.max(...nums); }
|
|
48
|
+
range(start, end, step = 1) {
|
|
49
|
+
const result = [];
|
|
50
|
+
for (let i = start; i < end; i += step)
|
|
51
|
+
result.push(i);
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
exports.NumberUtils = NumberUtils;
|
package/dist/object.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare class ObjectUtils {
|
|
2
|
+
deepGet(obj: unknown, path: string, defaultValue?: unknown): unknown;
|
|
3
|
+
deepSet(obj: Record<string, unknown>, path: string, value: unknown): void;
|
|
4
|
+
deepClone<T>(obj: T): T;
|
|
5
|
+
merge<T extends Record<string, unknown>>(target: T, ...sources: (Partial<T> | undefined | null)[]): T;
|
|
6
|
+
omit<T extends Record<string, unknown>, K extends keyof T>(obj: T, keys: K[]): Partial<T>;
|
|
7
|
+
pick<T extends Record<string, unknown>, K extends keyof T>(obj: T, keys: K[]): Partial<T>;
|
|
8
|
+
isEmpty(value: unknown): boolean;
|
|
9
|
+
isEqual(a: unknown, b: unknown): boolean;
|
|
10
|
+
keys(obj: Record<string, unknown>): string[];
|
|
11
|
+
values(obj: Record<string, unknown>): unknown[];
|
|
12
|
+
entries(obj: Record<string, unknown>): [string, unknown][];
|
|
13
|
+
has(obj: Record<string, unknown>, path: string): boolean;
|
|
14
|
+
size(obj: Record<string, unknown>): number;
|
|
15
|
+
mapValues(obj: Record<string, unknown>, fn: (v: unknown, k: string) => unknown): Record<string, unknown>;
|
|
16
|
+
invert(obj: Record<string, string>): Record<string, string>;
|
|
17
|
+
clone<T>(obj: T): T;
|
|
18
|
+
}
|
package/dist/object.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ObjectUtils = void 0;
|
|
4
|
+
class ObjectUtils {
|
|
5
|
+
deepGet(obj, path, defaultValue) {
|
|
6
|
+
if (!obj || !path)
|
|
7
|
+
return defaultValue;
|
|
8
|
+
const keys = path.split('.');
|
|
9
|
+
let result = obj;
|
|
10
|
+
for (const key of keys) {
|
|
11
|
+
if (result === null || result === undefined)
|
|
12
|
+
return defaultValue;
|
|
13
|
+
result = result[key];
|
|
14
|
+
}
|
|
15
|
+
return result ?? defaultValue;
|
|
16
|
+
}
|
|
17
|
+
deepSet(obj, path, value) {
|
|
18
|
+
const keys = path.split('.');
|
|
19
|
+
let current = obj;
|
|
20
|
+
for (let i = 0; i < keys.length - 1; i++) {
|
|
21
|
+
if (!(keys[i] in current) || current[keys[i]] === null)
|
|
22
|
+
current[keys[i]] = {};
|
|
23
|
+
current = current[keys[i]];
|
|
24
|
+
}
|
|
25
|
+
current[keys[keys.length - 1]] = value;
|
|
26
|
+
}
|
|
27
|
+
deepClone(obj) {
|
|
28
|
+
if (obj === null || typeof obj !== 'object')
|
|
29
|
+
return obj;
|
|
30
|
+
if (obj instanceof Date)
|
|
31
|
+
return new Date(obj.getTime());
|
|
32
|
+
if (Array.isArray(obj))
|
|
33
|
+
return obj.map(item => this.deepClone(item));
|
|
34
|
+
const cloned = {};
|
|
35
|
+
for (const key in obj)
|
|
36
|
+
cloned[key] = this.deepClone(obj[key]);
|
|
37
|
+
return cloned;
|
|
38
|
+
}
|
|
39
|
+
merge(target, ...sources) {
|
|
40
|
+
for (const source of sources) {
|
|
41
|
+
if (!source)
|
|
42
|
+
continue;
|
|
43
|
+
for (const key of Object.keys(source)) {
|
|
44
|
+
const tv = target[key], sv = source[key];
|
|
45
|
+
if (tv && typeof tv === 'object' && sv && typeof sv === 'object' && !Array.isArray(tv) && !Array.isArray(sv)) {
|
|
46
|
+
target[key] = this.merge(tv, sv);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
target[key] = sv;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return target;
|
|
54
|
+
}
|
|
55
|
+
omit(obj, keys) {
|
|
56
|
+
const result = { ...obj };
|
|
57
|
+
keys.forEach(k => delete result[k]);
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
pick(obj, keys) {
|
|
61
|
+
const result = {};
|
|
62
|
+
keys.forEach(k => { if (k in obj)
|
|
63
|
+
result[k] = obj[k]; });
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
isEmpty(value) {
|
|
67
|
+
if (value === null || value === undefined)
|
|
68
|
+
return true;
|
|
69
|
+
if (typeof value === 'string')
|
|
70
|
+
return value.trim().length === 0;
|
|
71
|
+
if (Array.isArray(value))
|
|
72
|
+
return value.length === 0;
|
|
73
|
+
if (typeof value === 'object')
|
|
74
|
+
return Object.keys(value).length === 0;
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
isEqual(a, b) {
|
|
78
|
+
if (a === b)
|
|
79
|
+
return true;
|
|
80
|
+
if (!a || !b || typeof a !== 'object' || typeof b !== 'object')
|
|
81
|
+
return false;
|
|
82
|
+
const ak = Object.keys(a), bk = Object.keys(b);
|
|
83
|
+
return ak.length === bk.length && ak.every(k => this.isEqual(a[k], b[k]));
|
|
84
|
+
}
|
|
85
|
+
keys(obj) { return Object.keys(obj); }
|
|
86
|
+
values(obj) { return Object.values(obj); }
|
|
87
|
+
entries(obj) { return Object.entries(obj); }
|
|
88
|
+
has(obj, path) { return this.deepGet(obj, path) !== undefined; }
|
|
89
|
+
size(obj) { return Object.keys(obj).length; }
|
|
90
|
+
mapValues(obj, fn) {
|
|
91
|
+
const result = {};
|
|
92
|
+
for (const k of Object.keys(obj))
|
|
93
|
+
result[k] = fn(obj[k], k);
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
invert(obj) {
|
|
97
|
+
const result = {};
|
|
98
|
+
for (const k of Object.keys(obj))
|
|
99
|
+
result[obj[k]] = k;
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
clone(obj) { return this.deepClone(obj); }
|
|
103
|
+
}
|
|
104
|
+
exports.ObjectUtils = ObjectUtils;
|
package/dist/string.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export declare class StringUtils {
|
|
2
|
+
private readonly _html_escape_map;
|
|
3
|
+
slugify(input_text: string): string;
|
|
4
|
+
truncate(input_text: string, max_length: number, suffix?: string): string;
|
|
5
|
+
capitalize(input_text: string): string;
|
|
6
|
+
titleCase(input_text: string): string;
|
|
7
|
+
camelCase(input_text: string): string;
|
|
8
|
+
kebabCase(input_text: string): string;
|
|
9
|
+
snakeCase(input_text: string): string;
|
|
10
|
+
pascalCase(input_text: string): string;
|
|
11
|
+
escapeHtml(input_text: string): string;
|
|
12
|
+
unescapeHtml(input_text: string): string;
|
|
13
|
+
repeat(input_text: string, times: number): string;
|
|
14
|
+
reverse(input_text: string): string;
|
|
15
|
+
base64Encode(input_text: string): string;
|
|
16
|
+
base64Decode(encoded: string): string;
|
|
17
|
+
randomString(length: number): string;
|
|
18
|
+
trimStart(text: string, chars?: string): string;
|
|
19
|
+
trimEnd(text: string, chars?: string): string;
|
|
20
|
+
words(text: string, delimiter?: RegExp): string[];
|
|
21
|
+
countWords(text: string): number;
|
|
22
|
+
levenshtein(a: string, b: string): number;
|
|
23
|
+
}
|
package/dist/string.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StringUtils = void 0;
|
|
4
|
+
class StringUtils {
|
|
5
|
+
constructor() {
|
|
6
|
+
this._html_escape_map = {
|
|
7
|
+
'&': '&', '<': '<', '>': '>', '"': '"', "'": '''
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
slugify(input_text) {
|
|
11
|
+
if (!input_text)
|
|
12
|
+
return '';
|
|
13
|
+
return input_text.toLowerCase().trim()
|
|
14
|
+
.replace(/[^\w\s-]/g, '')
|
|
15
|
+
.replace(/[\s_-]+/g, '-')
|
|
16
|
+
.replace(/^-+|-+$/g, '');
|
|
17
|
+
}
|
|
18
|
+
truncate(input_text, max_length, suffix = '...') {
|
|
19
|
+
if (!input_text || input_text.length <= max_length)
|
|
20
|
+
return input_text;
|
|
21
|
+
return input_text.slice(0, max_length - suffix.length) + suffix;
|
|
22
|
+
}
|
|
23
|
+
capitalize(input_text) {
|
|
24
|
+
if (!input_text)
|
|
25
|
+
return '';
|
|
26
|
+
return input_text.charAt(0).toUpperCase() + input_text.slice(1).toLowerCase();
|
|
27
|
+
}
|
|
28
|
+
titleCase(input_text) {
|
|
29
|
+
if (!input_text)
|
|
30
|
+
return '';
|
|
31
|
+
return input_text.replace(/\w\S*/g, (word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase());
|
|
32
|
+
}
|
|
33
|
+
camelCase(input_text) {
|
|
34
|
+
if (!input_text)
|
|
35
|
+
return '';
|
|
36
|
+
return input_text.replace(/(?:^\w|[A-Z]|\b\w)/g, (word, index) => index === 0 ? word.toLowerCase() : word.toUpperCase()).replace(/[\s_-]+/g, '');
|
|
37
|
+
}
|
|
38
|
+
kebabCase(input_text) {
|
|
39
|
+
if (!input_text)
|
|
40
|
+
return '';
|
|
41
|
+
return input_text.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/[\s_]+/g, '-').toLowerCase();
|
|
42
|
+
}
|
|
43
|
+
snakeCase(input_text) {
|
|
44
|
+
if (!input_text)
|
|
45
|
+
return '';
|
|
46
|
+
return input_text.replace(/([a-z])([A-Z])/g, '$1_$2').replace(/[\s-]+/g, '_').toLowerCase();
|
|
47
|
+
}
|
|
48
|
+
pascalCase(input_text) {
|
|
49
|
+
if (!input_text)
|
|
50
|
+
return '';
|
|
51
|
+
return input_text.replace(/(?:^\w|[A-Z]|\b\w)/g, (word) => word.toUpperCase()).replace(/[\s_-]+/g, '');
|
|
52
|
+
}
|
|
53
|
+
escapeHtml(input_text) {
|
|
54
|
+
if (!input_text)
|
|
55
|
+
return '';
|
|
56
|
+
return input_text.replace(/[&<>"']/g, (m) => this._html_escape_map[m] || m);
|
|
57
|
+
}
|
|
58
|
+
unescapeHtml(input_text) {
|
|
59
|
+
if (!input_text)
|
|
60
|
+
return '';
|
|
61
|
+
return input_text.replace(/&(amp|lt|gt|quot|#39);/g, (m) => this._html_escape_map[m] || m);
|
|
62
|
+
}
|
|
63
|
+
repeat(input_text, times) {
|
|
64
|
+
return input_text.repeat(Math.max(0, times));
|
|
65
|
+
}
|
|
66
|
+
reverse(input_text) {
|
|
67
|
+
return input_text.split('').reverse().join('');
|
|
68
|
+
}
|
|
69
|
+
base64Encode(input_text) {
|
|
70
|
+
return btoa(unescape(encodeURIComponent(input_text)));
|
|
71
|
+
}
|
|
72
|
+
base64Decode(encoded) {
|
|
73
|
+
try {
|
|
74
|
+
return decodeURIComponent(escape(atob(encoded)));
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
return '';
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
randomString(length) {
|
|
81
|
+
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
82
|
+
return Array(length).fill(0).map(() => chars[Math.floor(Math.random() * chars.length)]).join('');
|
|
83
|
+
}
|
|
84
|
+
trimStart(text, chars) {
|
|
85
|
+
return chars ? text.replace(new RegExp(`^[${chars}]+`, 'g'), '') : text.trimStart();
|
|
86
|
+
}
|
|
87
|
+
trimEnd(text, chars) {
|
|
88
|
+
return chars ? text.replace(new RegExp(`[${chars}]+$`, 'g'), '') : text.trimEnd();
|
|
89
|
+
}
|
|
90
|
+
words(text, delimiter = /\s+/) {
|
|
91
|
+
return text.split(delimiter).filter(Boolean);
|
|
92
|
+
}
|
|
93
|
+
countWords(text) {
|
|
94
|
+
return this.words(text).length;
|
|
95
|
+
}
|
|
96
|
+
levenshtein(a, b) {
|
|
97
|
+
if (!a || !b)
|
|
98
|
+
return Math.max(a?.length || 0, b?.length || 0);
|
|
99
|
+
const matrix = Array(b.length + 1).fill(null).map(() => Array(a.length + 1).fill(0));
|
|
100
|
+
for (let i = 0; i <= a.length; i++)
|
|
101
|
+
matrix[0][i] = i;
|
|
102
|
+
for (let j = 0; j <= b.length; j++)
|
|
103
|
+
matrix[j][0] = j;
|
|
104
|
+
for (let j = 1; j <= b.length; j++) {
|
|
105
|
+
for (let i = 1; i <= a.length; i++) {
|
|
106
|
+
matrix[j][i] = Math.min(matrix[j][i - 1] + 1, matrix[j - 1][i] + 1, matrix[j - 1][i - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return matrix[b.length][a.length];
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
exports.StringUtils = StringUtils;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare class ValidationUtils {
|
|
2
|
+
private readonly emailRegex;
|
|
3
|
+
private readonly urlRegex;
|
|
4
|
+
private readonly phoneRegex;
|
|
5
|
+
private readonly postalCodeRegex;
|
|
6
|
+
isEmail(value: string): boolean;
|
|
7
|
+
isUrl(value: string): boolean;
|
|
8
|
+
isPhone(value: string): boolean;
|
|
9
|
+
isPostalCode(value: string): boolean;
|
|
10
|
+
isStrongPassword(password: string): {
|
|
11
|
+
valid: boolean;
|
|
12
|
+
score: number;
|
|
13
|
+
issues: string[];
|
|
14
|
+
};
|
|
15
|
+
isNumeric(value: string): boolean;
|
|
16
|
+
isAlpha(value: string): boolean;
|
|
17
|
+
isAlphanumeric(value: string): boolean;
|
|
18
|
+
isDate(value: string): boolean;
|
|
19
|
+
isJSON(value: string): boolean;
|
|
20
|
+
isCreditCard(value: string): boolean;
|
|
21
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ValidationUtils = void 0;
|
|
4
|
+
class ValidationUtils {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
7
|
+
this.urlRegex = /^(https?:\/\/)?([\da-z.-]+)\.([a-z.]{2,6})([/\w .-]*)*\/?$/;
|
|
8
|
+
this.phoneRegex = /^\+?[\d\s\-()]{10,}$/;
|
|
9
|
+
this.postalCodeRegex = /^\d{5}(-\d{4})?$/;
|
|
10
|
+
}
|
|
11
|
+
isEmail(value) {
|
|
12
|
+
return this.emailRegex.test(value);
|
|
13
|
+
}
|
|
14
|
+
isUrl(value) {
|
|
15
|
+
return this.urlRegex.test(value);
|
|
16
|
+
}
|
|
17
|
+
isPhone(value) {
|
|
18
|
+
return this.phoneRegex.test(value);
|
|
19
|
+
}
|
|
20
|
+
isPostalCode(value) {
|
|
21
|
+
return this.postalCodeRegex.test(value);
|
|
22
|
+
}
|
|
23
|
+
isStrongPassword(password) {
|
|
24
|
+
const issues = [];
|
|
25
|
+
let score = 0;
|
|
26
|
+
if (password.length >= 8)
|
|
27
|
+
score++;
|
|
28
|
+
else
|
|
29
|
+
issues.push('Min 8 characters');
|
|
30
|
+
if (/[a-z]/.test(password))
|
|
31
|
+
score++;
|
|
32
|
+
else
|
|
33
|
+
issues.push('Lowercase letter');
|
|
34
|
+
if (/[A-Z]/.test(password))
|
|
35
|
+
score++;
|
|
36
|
+
else
|
|
37
|
+
issues.push('Uppercase letter');
|
|
38
|
+
if (/[0-9]/.test(password))
|
|
39
|
+
score++;
|
|
40
|
+
else
|
|
41
|
+
issues.push('Number');
|
|
42
|
+
if (/[^a-zA-Z0-9]/.test(password))
|
|
43
|
+
score++;
|
|
44
|
+
else
|
|
45
|
+
issues.push('Special character');
|
|
46
|
+
return { valid: score >= 4, score, issues };
|
|
47
|
+
}
|
|
48
|
+
isNumeric(value) {
|
|
49
|
+
return /^\d+$/.test(value);
|
|
50
|
+
}
|
|
51
|
+
isAlpha(value) {
|
|
52
|
+
return /^[a-zA-Z]+$/.test(value);
|
|
53
|
+
}
|
|
54
|
+
isAlphanumeric(value) {
|
|
55
|
+
return /^[a-zA-Z0-9]+$/.test(value);
|
|
56
|
+
}
|
|
57
|
+
isDate(value) {
|
|
58
|
+
return !isNaN(Date.parse(value));
|
|
59
|
+
}
|
|
60
|
+
isJSON(value) {
|
|
61
|
+
try {
|
|
62
|
+
JSON.parse(value);
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
isCreditCard(value) {
|
|
70
|
+
const cleaned = value.replace(/\s/g, '');
|
|
71
|
+
if (!/^\d{13,19}$/.test(cleaned))
|
|
72
|
+
return false;
|
|
73
|
+
let sum = 0;
|
|
74
|
+
let isEven = false;
|
|
75
|
+
for (let i = cleaned.length - 1; i >= 0; i--) {
|
|
76
|
+
let digit = parseInt(cleaned[i]);
|
|
77
|
+
if (isEven) {
|
|
78
|
+
digit *= 2;
|
|
79
|
+
if (digit > 9)
|
|
80
|
+
digit -= 9;
|
|
81
|
+
}
|
|
82
|
+
sum += digit;
|
|
83
|
+
isEven = !isEven;
|
|
84
|
+
}
|
|
85
|
+
return sum % 10 === 0;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
exports.ValidationUtils = ValidationUtils;
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "t1s-utils-lite",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Lightweight, type-safe utility library for web developers",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"keywords": ["utils", "typescript", "javascript", "helpers", "lodash", "underscore"],
|
|
19
|
+
"author": "Gaurang Mody - G8X",
|
|
20
|
+
"license": "MIT"
|
|
21
|
+
}
|