@naman_deep_singh/utils 2.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 +218 -0
- package/dist/cjs/array/array-extensions.d.ts +1 -0
- package/dist/cjs/array/array-extensions.js +152 -0
- package/dist/cjs/array/index.d.ts +1 -0
- package/dist/cjs/array/index.js +5 -0
- package/dist/cjs/core/index.d.ts +2 -0
- package/dist/cjs/core/index.js +19 -0
- package/dist/cjs/core/performance.d.ts +17 -0
- package/dist/cjs/core/performance.js +82 -0
- package/dist/cjs/core/validation.d.ts +4 -0
- package/dist/cjs/core/validation.js +31 -0
- package/dist/cjs/core/version.d.ts +1 -0
- package/dist/cjs/core/version.js +9 -0
- package/dist/cjs/extensions/index.d.ts +4 -0
- package/dist/cjs/extensions/index.js +21 -0
- package/dist/cjs/index.d.ts +61 -0
- package/dist/cjs/index.js +77 -0
- package/dist/cjs/init/index.d.ts +2 -0
- package/dist/cjs/init/index.js +19 -0
- package/dist/cjs/init/initializer.d.ts +3 -0
- package/dist/cjs/init/initializer.js +28 -0
- package/dist/cjs/init/options.d.ts +3 -0
- package/dist/cjs/init/options.js +23 -0
- package/dist/cjs/number/index.d.ts +1 -0
- package/dist/cjs/number/index.js +5 -0
- package/dist/cjs/number/number-extensions.d.ts +1 -0
- package/dist/cjs/number/number-extensions.js +142 -0
- package/dist/cjs/object/index.d.ts +1 -0
- package/dist/cjs/object/index.js +5 -0
- package/dist/cjs/object/object-extensions.d.ts +1 -0
- package/dist/cjs/object/object-extensions.js +148 -0
- package/dist/cjs/string/index.d.ts +1 -0
- package/dist/cjs/string/index.js +5 -0
- package/dist/cjs/string/string-extensions.d.ts +1 -0
- package/dist/cjs/string/string-extensions.js +127 -0
- package/dist/cjs/types/extension-types.d.ts +9 -0
- package/dist/cjs/types/extension-types.js +2 -0
- package/dist/cjs/types/global-augmentations.d.ts +72 -0
- package/dist/cjs/types/global-augmentations.js +2 -0
- package/dist/cjs/types/index.d.ts +2 -0
- package/dist/cjs/types/index.js +19 -0
- package/dist/cjs/utils/config.d.ts +4 -0
- package/dist/cjs/utils/config.js +25 -0
- package/dist/cjs/utils/defineExtension.d.ts +1 -0
- package/dist/cjs/utils/defineExtension.js +13 -0
- package/dist/cjs/utils/helpers.d.ts +5 -0
- package/dist/cjs/utils/helpers.js +37 -0
- package/dist/cjs/utils/index.d.ts +3 -0
- package/dist/cjs/utils/index.js +20 -0
- package/dist/esm/array/array-extensions.d.ts +1 -0
- package/dist/esm/array/array-extensions.js +149 -0
- package/dist/esm/array/index.d.ts +1 -0
- package/dist/esm/array/index.js +1 -0
- package/dist/esm/core/index.d.ts +2 -0
- package/dist/esm/core/index.js +3 -0
- package/dist/esm/core/performance.d.ts +17 -0
- package/dist/esm/core/performance.js +74 -0
- package/dist/esm/core/validation.d.ts +4 -0
- package/dist/esm/core/validation.js +25 -0
- package/dist/esm/core/version.d.ts +1 -0
- package/dist/esm/core/version.js +5 -0
- package/dist/esm/extensions/index.d.ts +4 -0
- package/dist/esm/extensions/index.js +5 -0
- package/dist/esm/index.d.ts +61 -0
- package/dist/esm/index.js +68 -0
- package/dist/esm/init/index.d.ts +2 -0
- package/dist/esm/init/index.js +3 -0
- package/dist/esm/init/initializer.d.ts +3 -0
- package/dist/esm/init/initializer.js +24 -0
- package/dist/esm/init/options.d.ts +3 -0
- package/dist/esm/init/options.js +19 -0
- package/dist/esm/number/index.d.ts +1 -0
- package/dist/esm/number/index.js +1 -0
- package/dist/esm/number/number-extensions.d.ts +1 -0
- package/dist/esm/number/number-extensions.js +139 -0
- package/dist/esm/object/index.d.ts +1 -0
- package/dist/esm/object/index.js +1 -0
- package/dist/esm/object/object-extensions.d.ts +1 -0
- package/dist/esm/object/object-extensions.js +145 -0
- package/dist/esm/string/index.d.ts +1 -0
- package/dist/esm/string/index.js +1 -0
- package/dist/esm/string/string-extensions.d.ts +1 -0
- package/dist/esm/string/string-extensions.js +124 -0
- package/dist/esm/types/extension-types.d.ts +9 -0
- package/dist/esm/types/extension-types.js +1 -0
- package/dist/esm/types/global-augmentations.d.ts +72 -0
- package/dist/esm/types/global-augmentations.js +1 -0
- package/dist/esm/types/index.d.ts +2 -0
- package/dist/esm/types/index.js +3 -0
- package/dist/esm/utils/config.d.ts +4 -0
- package/dist/esm/utils/config.js +20 -0
- package/dist/esm/utils/defineExtension.d.ts +1 -0
- package/dist/esm/utils/defineExtension.js +10 -0
- package/dist/esm/utils/helpers.d.ts +5 -0
- package/dist/esm/utils/helpers.js +30 -0
- package/dist/esm/utils/index.d.ts +3 -0
- package/dist/esm/utils/index.js +4 -0
- package/dist/types/array/array-extensions.d.ts +1 -0
- package/dist/types/array/index.d.ts +1 -0
- package/dist/types/core/index.d.ts +2 -0
- package/dist/types/core/performance.d.ts +17 -0
- package/dist/types/core/validation.d.ts +4 -0
- package/dist/types/core/version.d.ts +1 -0
- package/dist/types/extensions/index.d.ts +4 -0
- package/dist/types/index.d.ts +61 -0
- package/dist/types/init/index.d.ts +2 -0
- package/dist/types/init/initializer.d.ts +3 -0
- package/dist/types/init/options.d.ts +3 -0
- package/dist/types/number/index.d.ts +1 -0
- package/dist/types/number/number-extensions.d.ts +1 -0
- package/dist/types/object/index.d.ts +1 -0
- package/dist/types/object/object-extensions.d.ts +1 -0
- package/dist/types/string/index.d.ts +1 -0
- package/dist/types/string/string-extensions.d.ts +1 -0
- package/dist/types/types/extension-types.d.ts +9 -0
- package/dist/types/types/global-augmentations.d.ts +72 -0
- package/dist/types/types/index.d.ts +2 -0
- package/dist/types/utils/config.d.ts +4 -0
- package/dist/types/utils/defineExtension.d.ts +1 -0
- package/dist/types/utils/helpers.d.ts +5 -0
- package/dist/types/utils/index.d.ts +3 -0
- package/package.json +46 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getPerformanceConfig = exports.setPerformanceConfig = exports.LRUCache = exports.hasOwnProperty = exports.getPathSegments = exports.safeClone = exports.ensurePositiveInteger = exports.isValidArrayIndex = exports.mergeConfigs = exports.validatePerformanceSettings = exports.validateConfig = exports.validatePositiveInteger = exports.validateNumberRange = exports.validateArrayInput = exports.validateExtensionInput = exports.extend = exports.extendAll = exports.withCache = void 0;
|
|
4
|
+
exports.initializeExtensions = initializeExtensions;
|
|
5
|
+
// Import types first
|
|
6
|
+
require("../types/global-augmentations");
|
|
7
|
+
const array_1 = require("./array");
|
|
8
|
+
const number_1 = require("./number");
|
|
9
|
+
const object_1 = require("./object");
|
|
10
|
+
// Import extension modules
|
|
11
|
+
const string_1 = require("./string");
|
|
12
|
+
// Import core utilities
|
|
13
|
+
const performance_1 = require("./core/performance");
|
|
14
|
+
Object.defineProperty(exports, "LRUCache", { enumerable: true, get: function () { return performance_1.LRUCache; } });
|
|
15
|
+
Object.defineProperty(exports, "getPerformanceConfig", { enumerable: true, get: function () { return performance_1.getPerformanceConfig; } });
|
|
16
|
+
Object.defineProperty(exports, "setPerformanceConfig", { enumerable: true, get: function () { return performance_1.setPerformanceConfig; } });
|
|
17
|
+
Object.defineProperty(exports, "withCache", { enumerable: true, get: function () { return performance_1.withCache; } });
|
|
18
|
+
// Import validation utilities
|
|
19
|
+
const validation_1 = require("./core/validation");
|
|
20
|
+
Object.defineProperty(exports, "validateArrayInput", { enumerable: true, get: function () { return validation_1.validateArrayInput; } });
|
|
21
|
+
Object.defineProperty(exports, "validateExtensionInput", { enumerable: true, get: function () { return validation_1.validateExtensionInput; } });
|
|
22
|
+
Object.defineProperty(exports, "validateNumberRange", { enumerable: true, get: function () { return validation_1.validateNumberRange; } });
|
|
23
|
+
Object.defineProperty(exports, "validatePositiveInteger", { enumerable: true, get: function () { return validation_1.validatePositiveInteger; } });
|
|
24
|
+
// Import utility functions
|
|
25
|
+
const config_1 = require("./utils/config");
|
|
26
|
+
Object.defineProperty(exports, "mergeConfigs", { enumerable: true, get: function () { return config_1.mergeConfigs; } });
|
|
27
|
+
Object.defineProperty(exports, "validateConfig", { enumerable: true, get: function () { return config_1.validateConfig; } });
|
|
28
|
+
Object.defineProperty(exports, "validatePerformanceSettings", { enumerable: true, get: function () { return config_1.validatePerformanceSettings; } });
|
|
29
|
+
const helpers_1 = require("./utils/helpers");
|
|
30
|
+
Object.defineProperty(exports, "ensurePositiveInteger", { enumerable: true, get: function () { return helpers_1.ensurePositiveInteger; } });
|
|
31
|
+
Object.defineProperty(exports, "getPathSegments", { enumerable: true, get: function () { return helpers_1.getPathSegments; } });
|
|
32
|
+
Object.defineProperty(exports, "hasOwnProperty", { enumerable: true, get: function () { return helpers_1.hasOwnProperty; } });
|
|
33
|
+
Object.defineProperty(exports, "isValidArrayIndex", { enumerable: true, get: function () { return helpers_1.isValidArrayIndex; } });
|
|
34
|
+
Object.defineProperty(exports, "safeClone", { enumerable: true, get: function () { return helpers_1.safeClone; } });
|
|
35
|
+
// Import initialization functions
|
|
36
|
+
const initializer_1 = require("./init/initializer");
|
|
37
|
+
Object.defineProperty(exports, "extendAll", { enumerable: true, get: function () { return initializer_1.extendAll; } });
|
|
38
|
+
/**
|
|
39
|
+
* Initialize JavaScript prototype extensions
|
|
40
|
+
* @param options - Configure which extensions to enable (default: all enabled)
|
|
41
|
+
*/
|
|
42
|
+
function initializeExtensions(options = {}) {
|
|
43
|
+
(0, initializer_1.initExtensions)(options);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Selective prototype extension helpers
|
|
47
|
+
* Initialize only specific extensions
|
|
48
|
+
*/
|
|
49
|
+
exports.extend = {
|
|
50
|
+
string: string_1.extendString,
|
|
51
|
+
array: array_1.extendArray,
|
|
52
|
+
object: object_1.extendObject,
|
|
53
|
+
number: number_1.extendNumber,
|
|
54
|
+
};
|
|
55
|
+
// Default export
|
|
56
|
+
exports.default = {
|
|
57
|
+
initializeExtensions,
|
|
58
|
+
extendAll: initializer_1.extendAll,
|
|
59
|
+
extend: exports.extend,
|
|
60
|
+
LRUCache: performance_1.LRUCache,
|
|
61
|
+
setPerformanceConfig: performance_1.setPerformanceConfig,
|
|
62
|
+
getPerformanceConfig: performance_1.getPerformanceConfig,
|
|
63
|
+
// Validation utilities
|
|
64
|
+
validateExtensionInput: validation_1.validateExtensionInput,
|
|
65
|
+
validateArrayInput: validation_1.validateArrayInput,
|
|
66
|
+
validateNumberRange: validation_1.validateNumberRange,
|
|
67
|
+
validatePositiveInteger: validation_1.validatePositiveInteger,
|
|
68
|
+
// Utility functions
|
|
69
|
+
validateConfig: config_1.validateConfig,
|
|
70
|
+
validatePerformanceSettings: config_1.validatePerformanceSettings,
|
|
71
|
+
mergeConfigs: config_1.mergeConfigs,
|
|
72
|
+
isValidArrayIndex: helpers_1.isValidArrayIndex,
|
|
73
|
+
ensurePositiveInteger: helpers_1.ensurePositiveInteger,
|
|
74
|
+
safeClone: helpers_1.safeClone,
|
|
75
|
+
getPathSegments: helpers_1.getPathSegments,
|
|
76
|
+
hasOwnProperty: helpers_1.hasOwnProperty,
|
|
77
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
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
|
+
// Re-export initialization logic
|
|
18
|
+
__exportStar(require("./initializer"), exports);
|
|
19
|
+
__exportStar(require("./options"), exports);
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.initExtensions = initExtensions;
|
|
4
|
+
exports.extendAll = extendAll;
|
|
5
|
+
// Extension initialization logic
|
|
6
|
+
const array_1 = require("../array");
|
|
7
|
+
const performance_1 = require("../core/performance");
|
|
8
|
+
const number_1 = require("../number");
|
|
9
|
+
const object_1 = require("../object");
|
|
10
|
+
const string_1 = require("../string");
|
|
11
|
+
function initExtensions(options = {}) {
|
|
12
|
+
const { string = true, array = true, object = true, number = true, performance, } = options;
|
|
13
|
+
if (performance) {
|
|
14
|
+
// Set performance config if provided
|
|
15
|
+
(0, performance_1.setPerformanceConfig)(performance);
|
|
16
|
+
}
|
|
17
|
+
if (string)
|
|
18
|
+
(0, string_1.extendString)();
|
|
19
|
+
if (array)
|
|
20
|
+
(0, array_1.extendArray)();
|
|
21
|
+
if (object)
|
|
22
|
+
(0, object_1.extendObject)();
|
|
23
|
+
if (number)
|
|
24
|
+
(0, number_1.extendNumber)();
|
|
25
|
+
}
|
|
26
|
+
function extendAll() {
|
|
27
|
+
initExtensions();
|
|
28
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { ExtensionOptions } from '../types/extension-types';
|
|
2
|
+
export declare function validateExtensionOptions(options: Partial<ExtensionOptions>): ExtensionOptions;
|
|
3
|
+
export declare function createExtensionOptions(enableString?: boolean, enableArray?: boolean, enableObject?: boolean, enableNumber?: boolean, performanceConfig?: any): ExtensionOptions;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateExtensionOptions = validateExtensionOptions;
|
|
4
|
+
exports.createExtensionOptions = createExtensionOptions;
|
|
5
|
+
function validateExtensionOptions(options) {
|
|
6
|
+
const validated = {
|
|
7
|
+
string: options.string !== false,
|
|
8
|
+
array: options.array !== false,
|
|
9
|
+
object: options.object !== false,
|
|
10
|
+
number: options.number !== false,
|
|
11
|
+
performance: options.performance,
|
|
12
|
+
};
|
|
13
|
+
return validated;
|
|
14
|
+
}
|
|
15
|
+
function createExtensionOptions(enableString = true, enableArray = true, enableObject = true, enableNumber = true, performanceConfig) {
|
|
16
|
+
return {
|
|
17
|
+
string: enableString,
|
|
18
|
+
array: enableArray,
|
|
19
|
+
object: enableObject,
|
|
20
|
+
number: enableNumber,
|
|
21
|
+
performance: performanceConfig,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { extendNumber } from './number-extensions';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extendNumber = void 0;
|
|
4
|
+
var number_extensions_1 = require("./number-extensions");
|
|
5
|
+
Object.defineProperty(exports, "extendNumber", { enumerable: true, get: function () { return number_extensions_1.extendNumber; } });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function extendNumber(): void;
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extendNumber = extendNumber;
|
|
4
|
+
const utils_1 = require("src/utils");
|
|
5
|
+
const performance_1 = require("../core/performance");
|
|
6
|
+
let numberExtended = false;
|
|
7
|
+
function extendNumber() {
|
|
8
|
+
if (numberExtended)
|
|
9
|
+
return;
|
|
10
|
+
numberExtended = true;
|
|
11
|
+
(0, utils_1.defineExtension)(Number.prototype, 'toPercent', function (decimals = 2) {
|
|
12
|
+
return `${(this * 100).toFixed(decimals)}%`;
|
|
13
|
+
});
|
|
14
|
+
(0, utils_1.defineExtension)(Number.prototype, 'toCurrency', function (currency = 'USD', locale = 'en-US') {
|
|
15
|
+
return new Intl.NumberFormat(locale, {
|
|
16
|
+
style: 'currency',
|
|
17
|
+
currency,
|
|
18
|
+
}).format(this);
|
|
19
|
+
});
|
|
20
|
+
(0, utils_1.defineExtension)(Number.prototype, 'clamp', function (min, max) {
|
|
21
|
+
if (min > max)
|
|
22
|
+
throw new RangeError(`clamp: min (${min}) cannot be greater than max (${max})`);
|
|
23
|
+
return Math.min(Math.max(this, min), max);
|
|
24
|
+
});
|
|
25
|
+
(0, utils_1.defineExtension)(Number.prototype, 'isEven', function () {
|
|
26
|
+
return this % 2 === 0;
|
|
27
|
+
});
|
|
28
|
+
(0, utils_1.defineExtension)(Number.prototype, 'isOdd', function () {
|
|
29
|
+
return this % 2 !== 0;
|
|
30
|
+
});
|
|
31
|
+
(0, utils_1.defineExtension)(Number.prototype, 'isPrime', function () {
|
|
32
|
+
const num = this;
|
|
33
|
+
return (0, performance_1.withCache)((0, performance_1.makeInternalCacheKey)('prime', num), () => {
|
|
34
|
+
if (num < 2)
|
|
35
|
+
return false;
|
|
36
|
+
for (let i = 2; i <= Math.sqrt(num); i++)
|
|
37
|
+
if (num % i === 0)
|
|
38
|
+
return false;
|
|
39
|
+
return true;
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
(0, utils_1.defineExtension)(Number.prototype, 'factorial', function () {
|
|
43
|
+
const num = Math.floor(this);
|
|
44
|
+
return (0, performance_1.withCache)((0, performance_1.makeInternalCacheKey)('factorial', num), () => {
|
|
45
|
+
if (num < 0)
|
|
46
|
+
return NaN;
|
|
47
|
+
if (num <= 1)
|
|
48
|
+
return 1;
|
|
49
|
+
let result = 1;
|
|
50
|
+
for (let i = 2; i <= num; i++)
|
|
51
|
+
result *= i;
|
|
52
|
+
return result;
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
(0, utils_1.defineExtension)(Number.prototype, 'toOrdinal', function () {
|
|
56
|
+
const num = Math.floor(this);
|
|
57
|
+
const suffix = ['th', 'st', 'nd', 'rd'];
|
|
58
|
+
const v = num % 100;
|
|
59
|
+
return num + (suffix[(v - 20) % 10] || suffix[v] || suffix[0]);
|
|
60
|
+
});
|
|
61
|
+
(0, utils_1.defineExtension)(Number.prototype, 'toRoman', function () {
|
|
62
|
+
const num = Math.floor(this);
|
|
63
|
+
if (num <= 0)
|
|
64
|
+
throw new RangeError('toRoman: number must be positive');
|
|
65
|
+
if (num >= 4000)
|
|
66
|
+
throw new RangeError('toRoman: number must be less than 4000');
|
|
67
|
+
return (0, performance_1.withCache)((0, performance_1.makeInternalCacheKey)('roman', num), () => {
|
|
68
|
+
const values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
|
|
69
|
+
const symbols = [
|
|
70
|
+
'M',
|
|
71
|
+
'CM',
|
|
72
|
+
'D',
|
|
73
|
+
'CD',
|
|
74
|
+
'C',
|
|
75
|
+
'XC',
|
|
76
|
+
'L',
|
|
77
|
+
'XL',
|
|
78
|
+
'X',
|
|
79
|
+
'IX',
|
|
80
|
+
'V',
|
|
81
|
+
'IV',
|
|
82
|
+
'I',
|
|
83
|
+
];
|
|
84
|
+
let n = num;
|
|
85
|
+
let result = '';
|
|
86
|
+
for (let i = 0; i < values.length; i++) {
|
|
87
|
+
while (n >= values[i]) {
|
|
88
|
+
result += symbols[i];
|
|
89
|
+
n -= values[i];
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return result;
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
(0, utils_1.defineExtension)(Number.prototype, 'inRange', function (min, max) {
|
|
96
|
+
return this >= min && this <= max;
|
|
97
|
+
});
|
|
98
|
+
(0, utils_1.defineExtension)(Number.prototype, 'round', function (decimals = 0) {
|
|
99
|
+
if (!Number.isInteger(decimals) || decimals < 0)
|
|
100
|
+
throw new TypeError('round: decimals must be non-negative integer');
|
|
101
|
+
const factor = Math.pow(10, decimals);
|
|
102
|
+
return Math.round(this * factor) / factor;
|
|
103
|
+
});
|
|
104
|
+
(0, utils_1.defineExtension)(Number.prototype, 'ceil', function (decimals = 0) {
|
|
105
|
+
if (!Number.isInteger(decimals) || decimals < 0)
|
|
106
|
+
throw new TypeError('ceil: decimals must be non-negative integer');
|
|
107
|
+
const factor = Math.pow(10, decimals);
|
|
108
|
+
return Math.ceil(this * factor) / factor;
|
|
109
|
+
});
|
|
110
|
+
(0, utils_1.defineExtension)(Number.prototype, 'floor', function (decimals = 0) {
|
|
111
|
+
if (!Number.isInteger(decimals) || decimals < 0)
|
|
112
|
+
throw new TypeError('floor: decimals must be non-negative integer');
|
|
113
|
+
const factor = Math.pow(10, decimals);
|
|
114
|
+
return Math.floor(this * factor) / factor;
|
|
115
|
+
});
|
|
116
|
+
(0, utils_1.defineExtension)(Number.prototype, 'abs', function () {
|
|
117
|
+
return Math.abs(this);
|
|
118
|
+
});
|
|
119
|
+
(0, utils_1.defineExtension)(Number.prototype, 'sign', function () {
|
|
120
|
+
return Math.sign(this);
|
|
121
|
+
});
|
|
122
|
+
(0, utils_1.defineExtension)(Number.prototype, 'times', function (callback) {
|
|
123
|
+
if (typeof callback !== 'function')
|
|
124
|
+
throw new TypeError('times: callback must be a function');
|
|
125
|
+
for (let i = 0; i < Math.floor(this); i++)
|
|
126
|
+
callback(i);
|
|
127
|
+
});
|
|
128
|
+
(0, utils_1.defineExtension)(Number.prototype, 'toFixedNumber', function (decimals = 0) {
|
|
129
|
+
if (!Number.isInteger(decimals) || decimals < 0) {
|
|
130
|
+
throw new TypeError(`toFixedNumber: decimals must be a non-negative integer, got ${decimals}`);
|
|
131
|
+
}
|
|
132
|
+
const factor = Math.pow(10, decimals);
|
|
133
|
+
return Math.round(this.valueOf() * factor) / factor;
|
|
134
|
+
});
|
|
135
|
+
(0, utils_1.defineExtension)(Number.prototype, 'randomUpTo', function () {
|
|
136
|
+
const max = this.valueOf();
|
|
137
|
+
if (!Number.isFinite(max)) {
|
|
138
|
+
throw new TypeError(`randomUpTo: number must be finite, got ${max}`);
|
|
139
|
+
}
|
|
140
|
+
return Math.random() * max;
|
|
141
|
+
});
|
|
142
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { extendObject } from './object-extensions';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extendObject = void 0;
|
|
4
|
+
var object_extensions_1 = require("./object-extensions");
|
|
5
|
+
Object.defineProperty(exports, "extendObject", { enumerable: true, get: function () { return object_extensions_1.extendObject; } });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function extendObject(): void;
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extendObject = extendObject;
|
|
4
|
+
const utils_1 = require("src/utils");
|
|
5
|
+
let objectExtended = false;
|
|
6
|
+
function extendObject() {
|
|
7
|
+
if (objectExtended)
|
|
8
|
+
return;
|
|
9
|
+
objectExtended = true;
|
|
10
|
+
(0, utils_1.defineExtension)(Object.prototype, 'isEmpty', function () {
|
|
11
|
+
return Object.keys(this).length === 0;
|
|
12
|
+
});
|
|
13
|
+
(0, utils_1.defineExtension)(Object.prototype, 'pick', function (keys) {
|
|
14
|
+
if (!Array.isArray(keys))
|
|
15
|
+
throw new TypeError('pick: keys must be an array');
|
|
16
|
+
if (!keys.length)
|
|
17
|
+
throw new TypeError('pick: keys array cannot be empty');
|
|
18
|
+
const result = {};
|
|
19
|
+
keys.forEach((key) => {
|
|
20
|
+
if (key in this)
|
|
21
|
+
result[key] = this[key];
|
|
22
|
+
});
|
|
23
|
+
return result;
|
|
24
|
+
});
|
|
25
|
+
(0, utils_1.defineExtension)(Object.prototype, 'omit', function (keys) {
|
|
26
|
+
if (!Array.isArray(keys))
|
|
27
|
+
throw new TypeError('omit: keys must be an array');
|
|
28
|
+
if (!keys.length)
|
|
29
|
+
throw new TypeError('omit: keys array cannot be empty');
|
|
30
|
+
const result = { ...this };
|
|
31
|
+
keys.forEach((key) => delete result[key]);
|
|
32
|
+
return result;
|
|
33
|
+
});
|
|
34
|
+
(0, utils_1.defineExtension)(Object.prototype, 'deepClone', function () {
|
|
35
|
+
const visited = new WeakSet();
|
|
36
|
+
function deepCloneSafe(obj) {
|
|
37
|
+
if (obj === null || typeof obj !== 'object')
|
|
38
|
+
return obj;
|
|
39
|
+
if (visited.has(obj))
|
|
40
|
+
throw new Error('Circular reference detected in deepClone');
|
|
41
|
+
visited.add(obj);
|
|
42
|
+
if (obj instanceof Date)
|
|
43
|
+
return new Date(obj.getTime());
|
|
44
|
+
if (Array.isArray(obj))
|
|
45
|
+
return obj.map((item) => deepCloneSafe(item));
|
|
46
|
+
const cloned = {};
|
|
47
|
+
Object.keys(obj).forEach((key) => {
|
|
48
|
+
cloned[key] = deepCloneSafe(obj[key]);
|
|
49
|
+
});
|
|
50
|
+
return cloned;
|
|
51
|
+
}
|
|
52
|
+
return deepCloneSafe(this);
|
|
53
|
+
});
|
|
54
|
+
(0, utils_1.defineExtension)(Object.prototype, 'merge', function (other) {
|
|
55
|
+
return { ...this, ...other };
|
|
56
|
+
});
|
|
57
|
+
(0, utils_1.defineExtension)(Object.prototype, 'deepFreeze', function () {
|
|
58
|
+
Object.getOwnPropertyNames(this).forEach((name) => {
|
|
59
|
+
const value = this[name];
|
|
60
|
+
if (value && typeof value === 'object')
|
|
61
|
+
value.deepFreeze?.();
|
|
62
|
+
});
|
|
63
|
+
return Object.freeze(this);
|
|
64
|
+
});
|
|
65
|
+
(0, utils_1.defineExtension)(Object.prototype, 'hasPath', function (path) {
|
|
66
|
+
if (!path.trim())
|
|
67
|
+
throw new TypeError('hasPath: path cannot be empty');
|
|
68
|
+
let current = this;
|
|
69
|
+
return path.split('.').every((key) => {
|
|
70
|
+
if (current == null || !(key in current))
|
|
71
|
+
return false;
|
|
72
|
+
current = current[key];
|
|
73
|
+
return true;
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
(0, utils_1.defineExtension)(Object.prototype, 'getPath', function (path, defaultValue) {
|
|
77
|
+
if (!path.trim())
|
|
78
|
+
throw new TypeError('getPath: path cannot be empty');
|
|
79
|
+
return path
|
|
80
|
+
.split('.')
|
|
81
|
+
.reduce((acc, key) => (acc && key in acc ? acc[key] : defaultValue), this);
|
|
82
|
+
});
|
|
83
|
+
(0, utils_1.defineExtension)(Object.prototype, 'setPath', function (path, value) {
|
|
84
|
+
if (!path.trim())
|
|
85
|
+
throw new TypeError('setPath: path cannot be empty');
|
|
86
|
+
const keys = path.split('.');
|
|
87
|
+
let current = this;
|
|
88
|
+
for (let i = 0; i < keys.length - 1; i++) {
|
|
89
|
+
if (!(keys[i] in current) || typeof current[keys[i]] !== 'object')
|
|
90
|
+
current[keys[i]] = {};
|
|
91
|
+
current = current[keys[i]];
|
|
92
|
+
}
|
|
93
|
+
current[keys[keys.length - 1]] = value;
|
|
94
|
+
return this;
|
|
95
|
+
});
|
|
96
|
+
(0, utils_1.defineExtension)(Object.prototype, 'mapValues', function (fn) {
|
|
97
|
+
if (typeof fn !== 'function') {
|
|
98
|
+
throw new TypeError(`mapValues: fn must be a function, got ${typeof fn}`);
|
|
99
|
+
}
|
|
100
|
+
const result = {};
|
|
101
|
+
for (const key in this) {
|
|
102
|
+
if (Object.prototype.hasOwnProperty.call(this, key)) {
|
|
103
|
+
result[key] = fn(this[key], key);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return result;
|
|
107
|
+
});
|
|
108
|
+
(0, utils_1.defineExtension)(Object.prototype, 'mapKeys', function (fn) {
|
|
109
|
+
if (typeof fn !== 'function') {
|
|
110
|
+
throw new TypeError(`mapKeys: fn must be a function, got ${typeof fn}`);
|
|
111
|
+
}
|
|
112
|
+
const result = {};
|
|
113
|
+
for (const key in this) {
|
|
114
|
+
if (Object.prototype.hasOwnProperty.call(this, key)) {
|
|
115
|
+
const newKey = fn(key);
|
|
116
|
+
result[newKey] = this[key];
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return result;
|
|
120
|
+
});
|
|
121
|
+
(0, utils_1.defineExtension)(Object.prototype, 'filterKeys', function (keys) {
|
|
122
|
+
if (!Array.isArray(keys)) {
|
|
123
|
+
throw new TypeError(`filterKeys: keys must be an array, got ${typeof keys}`);
|
|
124
|
+
}
|
|
125
|
+
const result = {};
|
|
126
|
+
for (const key of keys) {
|
|
127
|
+
if (key in this) {
|
|
128
|
+
result[key] = this[key];
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return result;
|
|
132
|
+
});
|
|
133
|
+
(0, utils_1.defineExtension)(Object.prototype, 'filterValues', function (fn) {
|
|
134
|
+
if (typeof fn !== 'function') {
|
|
135
|
+
throw new TypeError(`filterValues: fn must be a function, got ${typeof fn}`);
|
|
136
|
+
}
|
|
137
|
+
const result = {};
|
|
138
|
+
for (const key in this) {
|
|
139
|
+
if (Object.prototype.hasOwnProperty.call(this, key)) {
|
|
140
|
+
const val = this[key];
|
|
141
|
+
if (fn(val, key)) {
|
|
142
|
+
result[key] = val;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return result;
|
|
147
|
+
});
|
|
148
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { extendString } from './string-extensions';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extendString = void 0;
|
|
4
|
+
var string_extensions_1 = require("./string-extensions");
|
|
5
|
+
Object.defineProperty(exports, "extendString", { enumerable: true, get: function () { return string_extensions_1.extendString; } });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function extendString(): void;
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extendString = extendString;
|
|
4
|
+
const utils_1 = require("src/utils");
|
|
5
|
+
let stringExtended = false;
|
|
6
|
+
function extendString() {
|
|
7
|
+
if (stringExtended)
|
|
8
|
+
return;
|
|
9
|
+
stringExtended = true;
|
|
10
|
+
(0, utils_1.defineExtension)(String.prototype, 'toCapitalize', function () {
|
|
11
|
+
return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase();
|
|
12
|
+
});
|
|
13
|
+
(0, utils_1.defineExtension)(String.prototype, 'toCamelCase', function () {
|
|
14
|
+
return this.replace(/[-_\s]+(.)?/g, (_, char) => char ? char.toUpperCase() : '');
|
|
15
|
+
});
|
|
16
|
+
(0, utils_1.defineExtension)(String.prototype, 'toKebabCase', function () {
|
|
17
|
+
return this.replace(/([a-z])([A-Z])/g, '$1-$2')
|
|
18
|
+
.replace(/[\s_]+/g, '-')
|
|
19
|
+
.toLowerCase();
|
|
20
|
+
});
|
|
21
|
+
(0, utils_1.defineExtension)(String.prototype, 'toSnakeCase', function () {
|
|
22
|
+
return this.replace(/([a-z])([A-Z])/g, '$1_$2')
|
|
23
|
+
.replace(/[\s-]+/g, '_')
|
|
24
|
+
.toLowerCase();
|
|
25
|
+
});
|
|
26
|
+
(0, utils_1.defineExtension)(String.prototype, 'truncate', function (length, suffix = '...') {
|
|
27
|
+
if (!Number.isInteger(length) || length < 0) {
|
|
28
|
+
throw new TypeError(`truncate: length must be a non-negative integer, got ${length}`);
|
|
29
|
+
}
|
|
30
|
+
return this.length > length
|
|
31
|
+
? this.substring(0, length) + suffix
|
|
32
|
+
: this.toString();
|
|
33
|
+
});
|
|
34
|
+
(0, utils_1.defineExtension)(String.prototype, 'isEmail', function () {
|
|
35
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
36
|
+
return emailRegex.test(this.toString());
|
|
37
|
+
});
|
|
38
|
+
(0, utils_1.defineExtension)(String.prototype, 'isUrl', function () {
|
|
39
|
+
try {
|
|
40
|
+
new URL(this.toString());
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
(0, utils_1.defineExtension)(String.prototype, 'removeWhitespace', function () {
|
|
48
|
+
return this.replace(/\s+/g, '');
|
|
49
|
+
});
|
|
50
|
+
(0, utils_1.defineExtension)(String.prototype, 'reverse', function () {
|
|
51
|
+
return this.split('').reverse().join('');
|
|
52
|
+
});
|
|
53
|
+
(0, utils_1.defineExtension)(String.prototype, 'isPalindrome', function () {
|
|
54
|
+
const cleaned = this.toLowerCase().replace(/[^a-z0-9]/g, '');
|
|
55
|
+
return cleaned === cleaned.split('').reverse().join('');
|
|
56
|
+
});
|
|
57
|
+
(0, utils_1.defineExtension)(String.prototype, 'toTitleCase', function () {
|
|
58
|
+
return this.replace(/\w\S*/g, (txt) => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase());
|
|
59
|
+
});
|
|
60
|
+
(0, utils_1.defineExtension)(String.prototype, 'stripHtml', function () {
|
|
61
|
+
return this.replace(/<[^>]*>/g, '');
|
|
62
|
+
});
|
|
63
|
+
(0, utils_1.defineExtension)(String.prototype, 'padStart', function (targetLength, padString = ' ') {
|
|
64
|
+
if (!Number.isInteger(targetLength) || targetLength < 0) {
|
|
65
|
+
throw new TypeError(`padStart: targetLength must be a non-negative integer, got ${targetLength}`);
|
|
66
|
+
}
|
|
67
|
+
if (typeof padString !== 'string') {
|
|
68
|
+
throw new TypeError(`padStart: padString must be a string, got ${typeof padString}`);
|
|
69
|
+
}
|
|
70
|
+
if (padString.length === 0) {
|
|
71
|
+
throw new TypeError('padStart: padString cannot be empty');
|
|
72
|
+
}
|
|
73
|
+
return this.toString().padStart(targetLength, padString);
|
|
74
|
+
});
|
|
75
|
+
(0, utils_1.defineExtension)(String.prototype, 'padEnd', function (targetLength, padString = ' ') {
|
|
76
|
+
if (!Number.isInteger(targetLength) || targetLength < 0) {
|
|
77
|
+
throw new TypeError(`padEnd: targetLength must be a non-negative integer, got ${targetLength}`);
|
|
78
|
+
}
|
|
79
|
+
if (typeof padString !== 'string') {
|
|
80
|
+
throw new TypeError(`padEnd: padString must be a string, got ${typeof padString}`);
|
|
81
|
+
}
|
|
82
|
+
if (padString.length === 0) {
|
|
83
|
+
throw new TypeError('padEnd: padString cannot be empty');
|
|
84
|
+
}
|
|
85
|
+
return this.toString().padEnd(targetLength, padString);
|
|
86
|
+
});
|
|
87
|
+
(0, utils_1.defineExtension)(String.prototype, 'count', function (substring) {
|
|
88
|
+
if (typeof substring !== 'string') {
|
|
89
|
+
throw new TypeError(`count: substring must be a string, got ${typeof substring}`);
|
|
90
|
+
}
|
|
91
|
+
if (substring === '') {
|
|
92
|
+
throw new TypeError('count: substring cannot be empty');
|
|
93
|
+
}
|
|
94
|
+
const escaped = substring.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
95
|
+
return (this.match(new RegExp(escaped, 'g')) || []).length;
|
|
96
|
+
});
|
|
97
|
+
(0, utils_1.defineExtension)(String.prototype, 'words', function () {
|
|
98
|
+
return this.trim()
|
|
99
|
+
.split(/\s+/)
|
|
100
|
+
.filter((word) => word.length > 0);
|
|
101
|
+
});
|
|
102
|
+
(0, utils_1.defineExtension)(String.prototype, 'lines', function () {
|
|
103
|
+
return this.split(/\r?\n/);
|
|
104
|
+
});
|
|
105
|
+
(0, utils_1.defineExtension)(String.prototype, 'capitalizeWords', function () {
|
|
106
|
+
return this.toString().replace(/\b\w/g, (char) => char.toUpperCase());
|
|
107
|
+
});
|
|
108
|
+
(0, utils_1.defineExtension)(String.prototype, 'reverseWords', function () {
|
|
109
|
+
return this.toString().split(/\s+/).reverse().join(' ');
|
|
110
|
+
});
|
|
111
|
+
(0, utils_1.defineExtension)(String.prototype, 'truncateWords', function (count, suffix = '...') {
|
|
112
|
+
if (!Number.isInteger(count) || count < 0) {
|
|
113
|
+
throw new TypeError(`truncateWords: count must be a non-negative integer, got ${count}`);
|
|
114
|
+
}
|
|
115
|
+
const words = this.toString().split(/\s+/);
|
|
116
|
+
if (words.length <= count)
|
|
117
|
+
return this.toString();
|
|
118
|
+
return words.slice(0, count).join(' ') + suffix;
|
|
119
|
+
});
|
|
120
|
+
(0, utils_1.defineExtension)(String.prototype, 'slugify', function () {
|
|
121
|
+
return this.toString()
|
|
122
|
+
.toLowerCase()
|
|
123
|
+
.replace(/[^\w\s-]/g, '')
|
|
124
|
+
.trim()
|
|
125
|
+
.replace(/[\s_-]+/g, '-');
|
|
126
|
+
});
|
|
127
|
+
}
|