@naman_deep_singh/js-extensions 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +388 -39
- package/dist/{array-extensions.js → cjs/array-extensions.js} +18 -0
- package/dist/{index.d.ts → cjs/index.d.ts} +6 -0
- package/dist/{index.js → cjs/index.js} +12 -3
- package/dist/cjs/number-extensions.js +103 -0
- package/dist/cjs/object-extensions.js +90 -0
- package/dist/cjs/performance.d.ts +18 -0
- package/dist/cjs/performance.js +63 -0
- package/dist/{string-extensions.js → cjs/string-extensions.js} +15 -0
- package/dist/{types.d.ts → cjs/types.d.ts} +20 -0
- package/dist/cjs/validation.d.ts +4 -0
- package/dist/cjs/validation.js +31 -0
- package/dist/esm/array-extensions.d.ts +1 -0
- package/dist/esm/array-extensions.js +80 -0
- package/dist/esm/index.d.ts +46 -0
- package/dist/esm/index.js +50 -0
- package/dist/esm/number-extensions.d.ts +1 -0
- package/dist/esm/number-extensions.js +100 -0
- package/dist/esm/object-extensions.d.ts +1 -0
- package/dist/esm/object-extensions.js +87 -0
- package/dist/esm/performance.d.ts +18 -0
- package/dist/esm/performance.js +57 -0
- package/dist/esm/string-extensions.d.ts +1 -0
- package/dist/esm/string-extensions.js +66 -0
- package/dist/esm/types.d.ts +71 -0
- package/dist/esm/types.js +1 -0
- package/dist/esm/validation.d.ts +4 -0
- package/dist/esm/validation.js +25 -0
- package/dist/types/array-extensions.d.ts +1 -0
- package/dist/types/index.d.ts +46 -0
- package/dist/types/number-extensions.d.ts +1 -0
- package/dist/types/object-extensions.d.ts +1 -0
- package/dist/types/performance.d.ts +18 -0
- package/dist/types/string-extensions.d.ts +1 -0
- package/dist/types/types.d.ts +71 -0
- package/dist/types/validation.d.ts +4 -0
- package/package.json +23 -8
- package/dist/number-extensions.js +0 -72
- package/dist/object-extensions.js +0 -53
- package/src/array-extensions.ts +0 -73
- package/src/index.ts +0 -55
- package/src/number-extensions.ts +0 -73
- package/src/object-extensions.ts +0 -53
- package/src/string-extensions.ts +0 -63
- package/src/types.ts +0 -56
- package/tsconfig.json +0 -22
- /package/dist/{array-extensions.d.ts → cjs/array-extensions.d.ts} +0 -0
- /package/dist/{number-extensions.d.ts → cjs/number-extensions.d.ts} +0 -0
- /package/dist/{object-extensions.d.ts → cjs/object-extensions.d.ts} +0 -0
- /package/dist/{string-extensions.d.ts → cjs/string-extensions.d.ts} +0 -0
- /package/dist/{types.js → cjs/types.js} +0 -0
package/src/index.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { extendString } from './string-extensions';
|
|
2
|
-
import { extendArray } from './array-extensions';
|
|
3
|
-
import { extendObject } from './object-extensions';
|
|
4
|
-
import { extendNumber } from './number-extensions';
|
|
5
|
-
import './types';
|
|
6
|
-
|
|
7
|
-
export interface ExtensionOptions {
|
|
8
|
-
string?: boolean;
|
|
9
|
-
array?: boolean;
|
|
10
|
-
object?: boolean;
|
|
11
|
-
number?: boolean;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Initialize JavaScript prototype extensions
|
|
16
|
-
* @param options - Configure which extensions to enable (default: all enabled)
|
|
17
|
-
*/
|
|
18
|
-
export function initExtensions(options: ExtensionOptions = {}): void {
|
|
19
|
-
const {
|
|
20
|
-
string = true,
|
|
21
|
-
array = true,
|
|
22
|
-
object = true,
|
|
23
|
-
number = true
|
|
24
|
-
} = options;
|
|
25
|
-
|
|
26
|
-
if (string) extendString();
|
|
27
|
-
if (array) extendArray();
|
|
28
|
-
if (object) extendObject();
|
|
29
|
-
if (number) extendNumber();
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Initialize all extensions (convenience function)
|
|
34
|
-
*/
|
|
35
|
-
export function extendAll(): void {
|
|
36
|
-
initExtensions();
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Initialize only specific extensions
|
|
41
|
-
*/
|
|
42
|
-
export const extend = {
|
|
43
|
-
string: extendString,
|
|
44
|
-
array: extendArray,
|
|
45
|
-
object: extendObject,
|
|
46
|
-
number: extendNumber
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
export default {
|
|
52
|
-
initExtensions,
|
|
53
|
-
extendAll,
|
|
54
|
-
extend
|
|
55
|
-
};
|
package/src/number-extensions.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
// Number prototype extensions
|
|
2
|
-
export function extendNumber() {
|
|
3
|
-
Number.prototype.toPercent = function(decimals: number = 2): string {
|
|
4
|
-
return (this.valueOf() * 100).toFixed(decimals) + '%';
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
Number.prototype.toCurrency = function(currency: string = 'USD', locale: string = 'en-US'): string {
|
|
8
|
-
return new Intl.NumberFormat(locale, {
|
|
9
|
-
style: 'currency',
|
|
10
|
-
currency: currency
|
|
11
|
-
}).format(this.valueOf());
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
Number.prototype.clamp = function(min: number, max: number): number {
|
|
15
|
-
return Math.min(Math.max(this.valueOf(), min), max);
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
Number.prototype.isEven = function(): boolean {
|
|
19
|
-
return this.valueOf() % 2 === 0;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
Number.prototype.isOdd = function(): boolean {
|
|
23
|
-
return this.valueOf() % 2 !== 0;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
Number.prototype.isPrime = function(): boolean {
|
|
27
|
-
const num = this.valueOf();
|
|
28
|
-
if (num < 2) return false;
|
|
29
|
-
for (let i = 2; i <= Math.sqrt(num); i++) {
|
|
30
|
-
if (num % i === 0) return false;
|
|
31
|
-
}
|
|
32
|
-
return true;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
Number.prototype.factorial = function(): number {
|
|
36
|
-
const num = Math.floor(this.valueOf());
|
|
37
|
-
if (num < 0) return NaN;
|
|
38
|
-
if (num === 0 || num === 1) return 1;
|
|
39
|
-
let result = 1;
|
|
40
|
-
for (let i = 2; i <= num; i++) {
|
|
41
|
-
result *= i;
|
|
42
|
-
}
|
|
43
|
-
return result;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
Number.prototype.toOrdinal = function(): string {
|
|
47
|
-
const num = Math.floor(this.valueOf());
|
|
48
|
-
const suffix = ['th', 'st', 'nd', 'rd'];
|
|
49
|
-
const v = num % 100;
|
|
50
|
-
return num + (suffix[(v - 20) % 10] || suffix[v] || suffix[0]);
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
Number.prototype.toRoman = function(): string {
|
|
54
|
-
const num = Math.floor(this.valueOf());
|
|
55
|
-
if (num <= 0 || num >= 4000) return num.toString();
|
|
56
|
-
const values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
|
|
57
|
-
const symbols = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
|
|
58
|
-
let result = '';
|
|
59
|
-
let n = num;
|
|
60
|
-
for (let i = 0; i < values.length; i++) {
|
|
61
|
-
while (n >= values[i]) {
|
|
62
|
-
result += symbols[i];
|
|
63
|
-
n -= values[i];
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return result;
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
Number.prototype.inRange = function(min: number, max: number): boolean {
|
|
70
|
-
const num = this.valueOf();
|
|
71
|
-
return num >= min && num <= max;
|
|
72
|
-
};
|
|
73
|
-
}
|
package/src/object-extensions.ts
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
// Object prototype extensions
|
|
2
|
-
export function extendObject() {
|
|
3
|
-
Object.prototype.isEmpty = function(): boolean {
|
|
4
|
-
return Object.keys(this).length === 0;
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
Object.prototype.pick = function<T extends Record<string, any>, K extends keyof T>(keys: K[]): Pick<T, K> {
|
|
8
|
-
const result = {} as Pick<T, K>;
|
|
9
|
-
const obj = this as T;
|
|
10
|
-
keys.forEach(key => {
|
|
11
|
-
if (key in obj) {
|
|
12
|
-
result[key] = obj[key];
|
|
13
|
-
}
|
|
14
|
-
});
|
|
15
|
-
return result;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
Object.prototype.omit = function<T extends Record<string, any>, K extends keyof T>(keys: K[]): Omit<T, K> {
|
|
19
|
-
const result = { ...this } as T;
|
|
20
|
-
keys.forEach(key => {
|
|
21
|
-
delete result[key];
|
|
22
|
-
});
|
|
23
|
-
return result as Omit<T, K>;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
Object.prototype.deepClone = function<T>(): T {
|
|
27
|
-
if (this === null || typeof this !== 'object') return this;
|
|
28
|
-
if (this instanceof Date) return new Date(this.getTime()) as any;
|
|
29
|
-
if (this instanceof Array) return this.map(item => item?.deepClone?.() || item) as any;
|
|
30
|
-
|
|
31
|
-
const cloned = {} as T;
|
|
32
|
-
Object.keys(this).forEach(key => {
|
|
33
|
-
const value = (this as any)[key];
|
|
34
|
-
(cloned as any)[key] = value?.deepClone?.() || value;
|
|
35
|
-
});
|
|
36
|
-
return cloned;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
Object.prototype.merge = function(other: Record<string, any>): Record<string, any> {
|
|
40
|
-
return { ...this, ...other };
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
Object.prototype.deepFreeze = function<T>(): T {
|
|
44
|
-
const propNames = Object.getOwnPropertyNames(this);
|
|
45
|
-
for (const name of propNames) {
|
|
46
|
-
const value = (this as any)[name];
|
|
47
|
-
if (value && typeof value === 'object') {
|
|
48
|
-
value.deepFreeze();
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
return Object.freeze(this) as T;
|
|
52
|
-
};
|
|
53
|
-
}
|
package/src/string-extensions.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
// String prototype extensions
|
|
2
|
-
export function extendString() {
|
|
3
|
-
String.prototype.toCapitalize = function(): string {
|
|
4
|
-
return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase();
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
String.prototype.toCamelCase = function(): string {
|
|
8
|
-
return this.replace(/[-_\s]+(.)?/g, (_, char) => char ? char.toUpperCase() : '');
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
String.prototype.toKebabCase = function(): string {
|
|
12
|
-
return this.replace(/([a-z])([A-Z])/g, '$1-$2')
|
|
13
|
-
.replace(/[\s_]+/g, '-')
|
|
14
|
-
.toLowerCase();
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
String.prototype.toSnakeCase = function(): string {
|
|
18
|
-
return this.replace(/([a-z])([A-Z])/g, '$1_$2')
|
|
19
|
-
.replace(/[\s-]+/g, '_')
|
|
20
|
-
.toLowerCase();
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
String.prototype.truncate = function(length: number, suffix: string = '...'): string {
|
|
24
|
-
return this.length > length ? this.substring(0, length) + suffix : this.toString();
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
String.prototype.isEmail = function(): boolean {
|
|
28
|
-
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
29
|
-
return emailRegex.test(this.toString());
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
String.prototype.isUrl = function(): boolean {
|
|
33
|
-
try {
|
|
34
|
-
new URL(this.toString());
|
|
35
|
-
return true;
|
|
36
|
-
} catch {
|
|
37
|
-
return false;
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
String.prototype.removeWhitespace = function(): string {
|
|
42
|
-
return this.replace(/\s+/g, '');
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
String.prototype.reverse = function(): string {
|
|
46
|
-
return this.split('').reverse().join('');
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
String.prototype.isPalindrome = function(): boolean {
|
|
50
|
-
const cleaned = this.toLowerCase().replace(/[^a-z0-9]/g, '');
|
|
51
|
-
return cleaned === cleaned.split('').reverse().join('');
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
String.prototype.toTitleCase = function(): string {
|
|
55
|
-
return this.replace(/\w\S*/g, (txt) =>
|
|
56
|
-
txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()
|
|
57
|
-
);
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
String.prototype.stripHtml = function(): string {
|
|
61
|
-
return this.replace(/<[^>]*>/g, '');
|
|
62
|
-
};
|
|
63
|
-
}
|
package/src/types.ts
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
// TypeScript declarations for all extensions
|
|
2
|
-
declare global {
|
|
3
|
-
interface String {
|
|
4
|
-
toCapitalize(): string;
|
|
5
|
-
toCamelCase(): string;
|
|
6
|
-
toKebabCase(): string;
|
|
7
|
-
toSnakeCase(): string;
|
|
8
|
-
truncate(length: number, suffix?: string): string;
|
|
9
|
-
isEmail(): boolean;
|
|
10
|
-
isUrl(): boolean;
|
|
11
|
-
removeWhitespace(): string;
|
|
12
|
-
reverse(): string;
|
|
13
|
-
isPalindrome(): boolean;
|
|
14
|
-
toTitleCase(): string;
|
|
15
|
-
stripHtml(): string;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
interface Array<T> {
|
|
19
|
-
unique(): T[];
|
|
20
|
-
shuffle(): T[];
|
|
21
|
-
chunk(size: number): T[][];
|
|
22
|
-
groupBy<K extends string | number>(keyFn: (item: T) => K): Record<K, T[]>;
|
|
23
|
-
sum(): number;
|
|
24
|
-
average(): number;
|
|
25
|
-
compact(): T[];
|
|
26
|
-
pluck<K extends keyof T>(key: K): T[K][];
|
|
27
|
-
findLast(predicate: (item: T) => boolean): T | undefined;
|
|
28
|
-
partition(predicate: (item: T) => boolean): [T[], T[]];
|
|
29
|
-
flatten(depth?: number): any[];
|
|
30
|
-
deepFlatten(): any[];
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
interface Object {
|
|
34
|
-
isEmpty(): boolean;
|
|
35
|
-
pick<T extends Record<string, any>, K extends keyof T>(keys: K[]): Pick<T, K>;
|
|
36
|
-
omit<T extends Record<string, any>, K extends keyof T>(keys: K[]): Omit<T, K>;
|
|
37
|
-
deepClone<T>(): T;
|
|
38
|
-
merge(other: Record<string, any>): Record<string, any>;
|
|
39
|
-
deepFreeze<T>(): T;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
interface Number {
|
|
43
|
-
toPercent(decimals?: number): string;
|
|
44
|
-
toCurrency(currency?: string, locale?: string): string;
|
|
45
|
-
clamp(min: number, max: number): number;
|
|
46
|
-
isEven(): boolean;
|
|
47
|
-
isOdd(): boolean;
|
|
48
|
-
isPrime(): boolean;
|
|
49
|
-
factorial(): number;
|
|
50
|
-
toOrdinal(): string;
|
|
51
|
-
toRoman(): string;
|
|
52
|
-
inRange(min: number, max: number): boolean;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export {};
|
package/tsconfig.json
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2020",
|
|
4
|
-
"module": "CommonJS",
|
|
5
|
-
"moduleResolution": "node",
|
|
6
|
-
"rootDir": "./src",
|
|
7
|
-
"outDir": "./dist",
|
|
8
|
-
"strict": true,
|
|
9
|
-
"esModuleInterop": true,
|
|
10
|
-
"allowSyntheticDefaultImports": true,
|
|
11
|
-
"skipLibCheck": true,
|
|
12
|
-
"forceConsistentCasingInFileNames": true,
|
|
13
|
-
"declaration": true,
|
|
14
|
-
"baseUrl": ".",
|
|
15
|
-
"paths": {
|
|
16
|
-
"*": ["*", "*.ts", "*.js"]
|
|
17
|
-
},
|
|
18
|
-
"lib": ["ES2020", "DOM"]
|
|
19
|
-
},
|
|
20
|
-
"include": ["src/**/*"],
|
|
21
|
-
"exclude": ["node_modules", "dist"]
|
|
22
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|