ts-packages 2.0.0 → 3.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/package.json +1 -1
- package/packages/cache/README.md +105 -9
- package/packages/cache/dist/cjs/adapters/memcache/MemcacheCache.d.ts +1 -54
- package/packages/cache/dist/cjs/adapters/memcache/MemcacheCache.js +75 -281
- package/packages/cache/dist/cjs/adapters/memory/MemoryCache.js +76 -22
- package/packages/cache/dist/cjs/adapters/redis/RedisCache.js +84 -26
- package/packages/cache/dist/cjs/core/BaseCache.js +13 -4
- package/packages/cache/dist/cjs/core/factory.js +26 -4
- package/packages/cache/dist/cjs/errors/CacheError.d.ts +10 -7
- package/packages/cache/dist/cjs/errors/CacheError.js +6 -11
- package/packages/cache/dist/cjs/errors/cacheErrorCodes.d.ts +22 -0
- package/packages/cache/dist/cjs/errors/cacheErrorCodes.js +24 -0
- package/packages/cache/dist/cjs/errors/index.js +3 -0
- package/packages/cache/dist/cjs/middleware/express/cacheMiddleware.js +8 -8
- package/packages/cache/dist/cjs/session/SessionStore.js +84 -28
- package/packages/cache/dist/esm/adapters/memcache/MemcacheCache.d.ts +1 -54
- package/packages/cache/dist/esm/adapters/memcache/MemcacheCache.js +75 -281
- package/packages/cache/dist/esm/adapters/memory/MemoryCache.js +76 -22
- package/packages/cache/dist/esm/adapters/redis/RedisCache.js +84 -26
- package/packages/cache/dist/esm/core/BaseCache.js +13 -4
- package/packages/cache/dist/esm/core/factory.js +26 -4
- package/packages/cache/dist/esm/errors/CacheError.d.ts +10 -7
- package/packages/cache/dist/esm/errors/CacheError.js +6 -11
- package/packages/cache/dist/esm/errors/cacheErrorCodes.d.ts +22 -0
- package/packages/cache/dist/esm/errors/cacheErrorCodes.js +21 -0
- package/packages/cache/dist/esm/errors/index.js +3 -0
- package/packages/cache/dist/esm/middleware/express/cacheMiddleware.js +8 -8
- package/packages/cache/dist/esm/session/SessionStore.js +84 -28
- package/packages/cache/dist/types/adapters/memcache/MemcacheCache.d.ts +1 -54
- package/packages/cache/dist/types/errors/CacheError.d.ts +10 -7
- package/packages/cache/dist/types/errors/cacheErrorCodes.d.ts +22 -0
- package/packages/cache/package.json +8 -6
- package/packages/cache/src/adapters/memcache/MemcacheCache.ts +79 -352
- package/packages/cache/src/adapters/memory/MemoryCache.ts +76 -77
- package/packages/cache/src/adapters/redis/RedisCache.ts +84 -86
- package/packages/cache/src/core/BaseCache.ts +13 -14
- package/packages/cache/src/core/factory.ts +27 -16
- package/packages/cache/src/errors/CacheError.ts +16 -17
- package/packages/cache/src/errors/cacheErrorCodes.ts +24 -0
- package/packages/cache/src/errors/index.ts +5 -0
- package/packages/cache/src/middleware/express/cacheMiddleware.ts +8 -8
- package/packages/cache/src/session/SessionStore.ts +84 -84
- package/packages/errors-utils/README.md +54 -57
- package/packages/errors-utils/dist/cjs/constants/errorCodes.d.ts +28 -23
- package/packages/errors-utils/dist/cjs/constants/errorCodes.js +57 -22
- package/packages/errors-utils/dist/cjs/constants/errorMessages.d.ts +8 -1
- package/packages/errors-utils/dist/cjs/constants/errorMessages.js +72 -29
- package/packages/errors-utils/dist/cjs/error/AppError.d.ts +2 -2
- package/packages/errors-utils/dist/cjs/error/AppError.js +2 -2
- package/packages/errors-utils/dist/cjs/error/ServiceUnavailableError.d.ts +4 -0
- package/packages/errors-utils/dist/cjs/error/ServiceUnavailableError.js +11 -0
- package/packages/errors-utils/dist/cjs/error/TokenExpiredError.d.ts +2 -2
- package/packages/errors-utils/dist/cjs/error/TokenExpiredError.js +2 -2
- package/packages/errors-utils/dist/cjs/error/TokenMalformedError.d.ts +2 -2
- package/packages/errors-utils/dist/cjs/error/TokenMalformedError.js +2 -2
- package/packages/errors-utils/dist/cjs/error/UnauthorizedError.d.ts +1 -2
- package/packages/errors-utils/dist/cjs/error/UnauthorizedError.js +2 -2
- package/packages/errors-utils/dist/cjs/error/index.d.ts +14 -0
- package/packages/errors-utils/dist/cjs/error/index.js +39 -0
- package/packages/errors-utils/dist/cjs/errorRegistry/errorRegistry.d.ts +19 -0
- package/packages/errors-utils/dist/cjs/errorRegistry/errorRegistry.js +63 -0
- package/packages/errors-utils/dist/cjs/errorRegistry/index.d.ts +3 -0
- package/packages/errors-utils/dist/cjs/errorRegistry/index.js +6 -0
- package/packages/errors-utils/dist/cjs/index.d.ts +3 -9
- package/packages/errors-utils/dist/cjs/index.js +12 -9
- package/packages/errors-utils/dist/cjs/middleware/express/{errorHandler.js → errorHandler.middleware.js} +4 -4
- package/packages/errors-utils/dist/cjs/middleware/express/index.d.ts +2 -2
- package/packages/errors-utils/dist/cjs/middleware/express/index.js +2 -2
- package/packages/errors-utils/dist/esm/constants/errorCodes.d.ts +28 -23
- package/packages/errors-utils/dist/esm/constants/errorCodes.js +57 -22
- package/packages/errors-utils/dist/esm/constants/errorMessages.d.ts +8 -1
- package/packages/errors-utils/dist/esm/constants/errorMessages.js +72 -29
- package/packages/errors-utils/dist/esm/error/AppError.d.ts +2 -2
- package/packages/errors-utils/dist/esm/error/AppError.js +2 -2
- package/packages/errors-utils/dist/esm/error/ServiceUnavailableError.d.ts +4 -0
- package/packages/errors-utils/dist/esm/error/ServiceUnavailableError.js +7 -0
- package/packages/errors-utils/dist/esm/error/TokenExpiredError.d.ts +2 -2
- package/packages/errors-utils/dist/esm/error/TokenExpiredError.js +2 -2
- package/packages/errors-utils/dist/esm/error/TokenMalformedError.d.ts +2 -2
- package/packages/errors-utils/dist/esm/error/TokenMalformedError.js +2 -2
- package/packages/errors-utils/dist/esm/error/UnauthorizedError.d.ts +1 -2
- package/packages/errors-utils/dist/esm/error/UnauthorizedError.js +2 -2
- package/packages/errors-utils/dist/esm/error/index.d.ts +14 -0
- package/packages/errors-utils/dist/esm/error/index.js +23 -0
- package/packages/errors-utils/dist/esm/errorRegistry/errorRegistry.d.ts +19 -0
- package/packages/errors-utils/dist/esm/errorRegistry/errorRegistry.js +59 -0
- package/packages/errors-utils/dist/esm/errorRegistry/index.d.ts +3 -0
- package/packages/errors-utils/dist/esm/errorRegistry/index.js +3 -0
- package/packages/errors-utils/dist/esm/index.d.ts +3 -9
- package/packages/errors-utils/dist/esm/index.js +12 -9
- package/packages/errors-utils/dist/esm/middleware/express/{errorHandler.js → errorHandler.middleware.js} +5 -5
- package/packages/errors-utils/dist/esm/middleware/express/index.d.ts +2 -2
- package/packages/errors-utils/dist/esm/middleware/express/index.js +2 -2
- package/packages/errors-utils/dist/types/constants/errorCodes.d.ts +28 -23
- package/packages/errors-utils/dist/types/constants/errorMessages.d.ts +8 -1
- package/packages/errors-utils/dist/types/error/AppError.d.ts +2 -2
- package/packages/errors-utils/dist/types/error/ServiceUnavailableError.d.ts +4 -0
- package/packages/errors-utils/dist/types/error/TokenExpiredError.d.ts +2 -2
- package/packages/errors-utils/dist/types/error/TokenMalformedError.d.ts +2 -2
- package/packages/errors-utils/dist/types/error/UnauthorizedError.d.ts +1 -2
- package/packages/errors-utils/dist/types/error/index.d.ts +14 -0
- package/packages/errors-utils/dist/types/errorRegistry/errorRegistry.d.ts +19 -0
- package/packages/errors-utils/dist/types/errorRegistry/index.d.ts +3 -0
- package/packages/errors-utils/dist/types/index.d.ts +3 -9
- package/packages/errors-utils/dist/types/middleware/express/index.d.ts +2 -2
- package/packages/errors-utils/package.json +4 -4
- package/packages/errors-utils/src/constants/errorCodes.ts +64 -23
- package/packages/errors-utils/src/constants/errorMessages.ts +91 -34
- package/packages/errors-utils/src/error/AppError.ts +3 -2
- package/packages/errors-utils/src/error/ServiceUnavailableError.ts +8 -0
- package/packages/errors-utils/src/error/TokenExpiredError.ts +2 -2
- package/packages/errors-utils/src/error/TokenMalformedError.ts +2 -2
- package/packages/errors-utils/src/error/UnauthorizedError.ts +8 -8
- package/packages/errors-utils/src/error/index.ts +26 -0
- package/packages/errors-utils/src/errorRegistry/errorRegistry.ts +74 -0
- package/packages/errors-utils/src/errorRegistry/index.ts +4 -0
- package/packages/errors-utils/src/index.ts +12 -10
- package/packages/errors-utils/src/middleware/express/{errorHandler.ts → errorHandler.middleware.ts} +5 -5
- package/packages/errors-utils/src/middleware/express/index.ts +2 -2
- package/packages/js-extensions/README.md +174 -425
- package/packages/js-extensions/dist/cjs/array/array-extensions.js +84 -50
- package/packages/js-extensions/dist/cjs/core/performance.d.ts +1 -0
- package/packages/js-extensions/dist/cjs/core/performance.js +6 -0
- package/packages/js-extensions/dist/cjs/core/version.d.ts +1 -0
- package/packages/js-extensions/dist/cjs/core/version.js +9 -0
- package/packages/js-extensions/dist/cjs/index.d.ts +1 -0
- package/packages/js-extensions/dist/cjs/index.js +1 -0
- package/packages/js-extensions/dist/cjs/number/number-extensions.js +85 -97
- package/packages/js-extensions/dist/cjs/object/object-extensions.js +102 -103
- package/packages/js-extensions/dist/cjs/string/string-extensions.js +66 -43
- package/packages/js-extensions/dist/cjs/types/global-augmentations.d.ts +1 -0
- package/packages/js-extensions/dist/cjs/utils/defineExtension.d.ts +1 -0
- package/packages/js-extensions/dist/cjs/utils/defineExtension.js +13 -0
- package/packages/js-extensions/dist/cjs/utils/index.d.ts +1 -0
- package/packages/js-extensions/dist/cjs/utils/index.js +1 -0
- package/packages/js-extensions/dist/esm/array/array-extensions.js +84 -50
- package/packages/js-extensions/dist/esm/core/performance.d.ts +1 -0
- package/packages/js-extensions/dist/esm/core/performance.js +5 -0
- package/packages/js-extensions/dist/esm/core/version.d.ts +1 -0
- package/packages/js-extensions/dist/esm/core/version.js +5 -0
- package/packages/js-extensions/dist/esm/index.d.ts +1 -0
- package/packages/js-extensions/dist/esm/index.js +1 -0
- package/packages/js-extensions/dist/esm/number/number-extensions.js +86 -98
- package/packages/js-extensions/dist/esm/object/object-extensions.js +102 -103
- package/packages/js-extensions/dist/esm/string/string-extensions.js +66 -43
- package/packages/js-extensions/dist/esm/types/global-augmentations.d.ts +1 -0
- package/packages/js-extensions/dist/esm/utils/defineExtension.d.ts +1 -0
- package/packages/js-extensions/dist/esm/utils/defineExtension.js +10 -0
- package/packages/js-extensions/dist/esm/utils/index.d.ts +1 -0
- package/packages/js-extensions/dist/esm/utils/index.js +1 -0
- package/packages/js-extensions/dist/types/core/performance.d.ts +1 -0
- package/packages/js-extensions/dist/types/core/version.d.ts +1 -0
- package/packages/js-extensions/dist/types/index.d.ts +1 -0
- package/packages/js-extensions/dist/types/types/global-augmentations.d.ts +1 -0
- package/packages/js-extensions/dist/types/utils/defineExtension.d.ts +1 -0
- package/packages/js-extensions/dist/types/utils/index.d.ts +1 -0
- package/packages/js-extensions/package.json +8 -4
- package/packages/js-extensions/src/array/array-extensions.ts +268 -150
- package/packages/js-extensions/src/core/performance.ts +11 -0
- package/packages/js-extensions/src/core/version.ts +7 -0
- package/packages/js-extensions/src/index.ts +2 -0
- package/packages/js-extensions/src/number/number-extensions.ts +90 -123
- package/packages/js-extensions/src/object/object-extensions.ts +102 -130
- package/packages/js-extensions/src/string/string-extensions.ts +80 -76
- package/packages/js-extensions/src/types/global-augmentations.ts +2 -1
- package/packages/js-extensions/src/utils/defineExtension.ts +14 -0
- package/packages/js-extensions/src/utils/index.ts +1 -0
- package/packages/response-utils/README.md +156 -198
- package/packages/response-utils/dist/cjs/core/BaseResponder.js +20 -4
- package/packages/response-utils/dist/cjs/index.d.ts +0 -1
- package/packages/response-utils/dist/cjs/index.js +1 -25
- package/packages/response-utils/dist/esm/core/BaseResponder.js +20 -4
- package/packages/response-utils/dist/esm/index.d.ts +0 -1
- package/packages/response-utils/dist/esm/index.js +0 -2
- package/packages/response-utils/dist/types/index.d.ts +0 -1
- package/packages/response-utils/package.json +1 -1
- package/packages/response-utils/src/core/BaseResponder.ts +25 -4
- package/packages/response-utils/src/index.ts +0 -3
- package/packages/response-utils/src/middleware/express/expressMiddleware.ts +1 -1
- package/packages/security/README.md +153 -355
- package/packages/security/dist/cjs/core/crypto/cryptoManager.js +34 -17
- package/packages/security/dist/cjs/core/jwt/decode.js +4 -1
- package/packages/security/dist/cjs/core/jwt/generateTokens.js +4 -1
- package/packages/security/dist/cjs/core/jwt/jwtManager.d.ts +19 -43
- package/packages/security/dist/cjs/core/jwt/jwtManager.js +84 -199
- package/packages/security/dist/cjs/core/jwt/parseDuration.js +3 -2
- package/packages/security/dist/cjs/core/jwt/signToken.js +2 -1
- package/packages/security/dist/cjs/core/jwt/validateToken.d.ts +10 -7
- package/packages/security/dist/cjs/core/jwt/validateToken.js +19 -10
- package/packages/security/dist/cjs/core/jwt/verify.d.ts +8 -9
- package/packages/security/dist/cjs/core/jwt/verify.js +59 -14
- package/packages/security/dist/cjs/core/password/hash.js +4 -4
- package/packages/security/dist/cjs/core/password/passwordManager.d.ts +1 -1
- package/packages/security/dist/cjs/core/password/passwordManager.js +36 -80
- package/packages/security/dist/cjs/core/password/strength.js +12 -6
- package/packages/security/dist/cjs/core/password/utils.d.ts +12 -0
- package/packages/security/dist/cjs/core/password/utils.js +16 -1
- package/packages/security/dist/cjs/core/password/verify.js +4 -4
- package/packages/security/dist/cjs/index.d.ts +2 -7
- package/packages/security/dist/esm/core/crypto/cryptoManager.js +34 -17
- package/packages/security/dist/esm/core/jwt/decode.js +4 -1
- package/packages/security/dist/esm/core/jwt/generateTokens.js +4 -1
- package/packages/security/dist/esm/core/jwt/jwtManager.d.ts +19 -43
- package/packages/security/dist/esm/core/jwt/jwtManager.js +85 -200
- package/packages/security/dist/esm/core/jwt/parseDuration.js +3 -2
- package/packages/security/dist/esm/core/jwt/signToken.js +2 -1
- package/packages/security/dist/esm/core/jwt/validateToken.d.ts +10 -7
- package/packages/security/dist/esm/core/jwt/validateToken.js +19 -10
- package/packages/security/dist/esm/core/jwt/verify.d.ts +8 -9
- package/packages/security/dist/esm/core/jwt/verify.js +58 -13
- package/packages/security/dist/esm/core/password/hash.js +4 -4
- package/packages/security/dist/esm/core/password/passwordManager.d.ts +1 -1
- package/packages/security/dist/esm/core/password/passwordManager.js +36 -80
- package/packages/security/dist/esm/core/password/strength.js +12 -6
- package/packages/security/dist/esm/core/password/utils.d.ts +12 -0
- package/packages/security/dist/esm/core/password/utils.js +16 -1
- package/packages/security/dist/esm/core/password/verify.js +4 -4
- package/packages/security/dist/esm/index.d.ts +2 -7
- package/packages/security/dist/types/core/jwt/jwtManager.d.ts +19 -43
- package/packages/security/dist/types/core/jwt/validateToken.d.ts +10 -7
- package/packages/security/dist/types/core/jwt/verify.d.ts +8 -9
- package/packages/security/dist/types/core/password/passwordManager.d.ts +1 -1
- package/packages/security/dist/types/core/password/utils.d.ts +12 -0
- package/packages/security/dist/types/index.d.ts +2 -7
- package/packages/security/package.json +3 -3
- package/packages/security/src/core/crypto/cryptoManager.ts +53 -21
- package/packages/security/src/core/jwt/decode.ts +4 -1
- package/packages/security/src/core/jwt/generateTokens.ts +4 -1
- package/packages/security/src/core/jwt/jwtManager.ts +105 -257
- package/packages/security/src/core/jwt/parseDuration.ts +4 -2
- package/packages/security/src/core/jwt/signToken.ts +2 -1
- package/packages/security/src/core/jwt/validateToken.ts +21 -13
- package/packages/security/src/core/jwt/verify.ts +82 -20
- package/packages/security/src/core/password/hash.ts +10 -4
- package/packages/security/src/core/password/passwordManager.ts +38 -108
- package/packages/security/src/core/password/strength.ts +13 -8
- package/packages/security/src/core/password/utils.ts +19 -3
- package/packages/security/src/core/password/verify.ts +6 -4
- package/packages/server-utils/README.md +1 -1
- package/packages/server-utils/dist/cjs/core/server.js +6 -6
- package/packages/server-utils/dist/cjs/middleware/auth.middleware.d.ts +1 -2
- package/packages/server-utils/dist/cjs/middleware/auth.middleware.js +17 -14
- package/packages/server-utils/dist/cjs/middleware/cache.middleware.js +2 -2
- package/packages/server-utils/dist/cjs/middleware/errorHandler.middleware.d.ts +1 -1
- package/packages/server-utils/dist/cjs/middleware/errorHandler.middleware.js +34 -17
- package/packages/server-utils/dist/cjs/middleware/session.middleware.js +8 -8
- package/packages/server-utils/dist/cjs/middleware/validation.middleware.js +2 -2
- package/packages/server-utils/dist/esm/core/server.js +6 -6
- package/packages/server-utils/dist/esm/middleware/auth.middleware.d.ts +1 -2
- package/packages/server-utils/dist/esm/middleware/auth.middleware.js +18 -15
- package/packages/server-utils/dist/esm/middleware/cache.middleware.js +2 -2
- package/packages/server-utils/dist/esm/middleware/errorHandler.middleware.d.ts +1 -1
- package/packages/server-utils/dist/esm/middleware/errorHandler.middleware.js +34 -17
- package/packages/server-utils/dist/esm/middleware/session.middleware.js +8 -8
- package/packages/server-utils/dist/esm/middleware/validation.middleware.js +2 -2
- package/packages/server-utils/dist/types/middleware/auth.middleware.d.ts +1 -2
- package/packages/server-utils/dist/types/middleware/errorHandler.middleware.d.ts +1 -1
- package/packages/server-utils/package.json +4 -4
- package/packages/server-utils/src/core/server.ts +6 -6
- package/packages/server-utils/src/middleware/auth.middleware.ts +40 -27
- package/packages/server-utils/src/middleware/cache.middleware.ts +2 -2
- package/packages/server-utils/src/middleware/errorHandler.middleware.ts +39 -24
- package/packages/server-utils/src/middleware/session.middleware.ts +11 -8
- package/packages/server-utils/src/middleware/validation.middleware.ts +2 -3
- package/packages/errors-utils/dist/cjs/error/RateLimitError.d.ts +0 -4
- package/packages/errors-utils/dist/cjs/error/RateLimitError.js +0 -11
- package/packages/errors-utils/dist/cjs/utils/mapAppErrorToResponder.d.ts +0 -3
- package/packages/errors-utils/dist/cjs/utils/mapAppErrorToResponder.js +0 -27
- package/packages/errors-utils/dist/esm/error/RateLimitError.d.ts +0 -4
- package/packages/errors-utils/dist/esm/error/RateLimitError.js +0 -7
- package/packages/errors-utils/dist/esm/utils/mapAppErrorToResponder.d.ts +0 -3
- package/packages/errors-utils/dist/esm/utils/mapAppErrorToResponder.js +0 -24
- package/packages/errors-utils/dist/types/error/RateLimitError.d.ts +0 -4
- package/packages/errors-utils/dist/types/utils/mapAppErrorToResponder.d.ts +0 -3
- package/packages/errors-utils/src/error/RateLimitError.ts +0 -8
- package/packages/errors-utils/src/utils/mapAppErrorToResponder.ts +0 -38
- package/packages/response-utils/src/legacy.ts +0 -30
- /package/packages/errors-utils/dist/cjs/middleware/express/{errorConverter.d.ts → errorConverter.middleware.d.ts} +0 -0
- /package/packages/errors-utils/dist/cjs/middleware/express/{errorConverter.js → errorConverter.middleware.js} +0 -0
- /package/packages/errors-utils/dist/cjs/middleware/express/{errorHandler.d.ts → errorHandler.middleware.d.ts} +0 -0
- /package/packages/errors-utils/dist/esm/middleware/express/{errorConverter.d.ts → errorConverter.middleware.d.ts} +0 -0
- /package/packages/errors-utils/dist/esm/middleware/express/{errorConverter.js → errorConverter.middleware.js} +0 -0
- /package/packages/errors-utils/dist/esm/middleware/express/{errorHandler.d.ts → errorHandler.middleware.d.ts} +0 -0
- /package/packages/errors-utils/dist/types/middleware/express/{errorConverter.d.ts → errorConverter.middleware.d.ts} +0 -0
- /package/packages/errors-utils/dist/types/middleware/express/{errorHandler.d.ts → errorHandler.middleware.d.ts} +0 -0
- /package/packages/errors-utils/src/middleware/express/{errorConverter.ts → errorConverter.middleware.ts} +0 -0
|
@@ -1,72 +1,43 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
// Object prototype extensions
|
|
3
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
3
|
exports.extendObject = extendObject;
|
|
4
|
+
const utils_1 = require("src/utils");
|
|
5
|
+
let objectExtended = false;
|
|
5
6
|
function extendObject() {
|
|
6
|
-
|
|
7
|
+
if (objectExtended)
|
|
8
|
+
return;
|
|
9
|
+
objectExtended = true;
|
|
10
|
+
(0, utils_1.defineExtension)(Object.prototype, 'isEmpty', function () {
|
|
7
11
|
return Object.keys(this).length === 0;
|
|
8
|
-
};
|
|
9
|
-
Object.prototype
|
|
10
|
-
if (
|
|
11
|
-
throw new TypeError('pick:
|
|
12
|
-
|
|
13
|
-
if (!Array.isArray(keys)) {
|
|
14
|
-
throw new TypeError(`pick: keys must be an array, got ${typeof keys}`);
|
|
15
|
-
}
|
|
16
|
-
if (keys.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
17
|
throw new TypeError('pick: keys array cannot be empty');
|
|
18
|
-
}
|
|
19
18
|
const result = {};
|
|
20
|
-
const obj = this;
|
|
21
19
|
keys.forEach((key) => {
|
|
22
|
-
if (key in
|
|
23
|
-
result[key] =
|
|
24
|
-
}
|
|
20
|
+
if (key in this)
|
|
21
|
+
result[key] = this[key];
|
|
25
22
|
});
|
|
26
23
|
return result;
|
|
27
|
-
};
|
|
28
|
-
Object.prototype
|
|
29
|
-
if (
|
|
30
|
-
throw new TypeError('omit:
|
|
31
|
-
|
|
32
|
-
if (!Array.isArray(keys)) {
|
|
33
|
-
throw new TypeError(`omit: keys must be an array, got ${typeof keys}`);
|
|
34
|
-
}
|
|
35
|
-
if (keys.length === 0) {
|
|
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)
|
|
36
29
|
throw new TypeError('omit: keys array cannot be empty');
|
|
37
|
-
}
|
|
38
30
|
const result = { ...this };
|
|
39
|
-
keys.forEach((key) =>
|
|
40
|
-
delete result[key];
|
|
41
|
-
});
|
|
31
|
+
keys.forEach((key) => delete result[key]);
|
|
42
32
|
return result;
|
|
43
|
-
};
|
|
44
|
-
Object.prototype
|
|
45
|
-
// Simple cycle detection without caching key generation
|
|
46
|
-
if (this === null || typeof this !== 'object')
|
|
47
|
-
return this;
|
|
48
|
-
// Handle Date objects
|
|
49
|
-
if (this instanceof Date)
|
|
50
|
-
return new Date(this.getTime());
|
|
51
|
-
// Handle Array objects
|
|
52
|
-
if (Array.isArray(this)) {
|
|
53
|
-
return this.map((item) => {
|
|
54
|
-
if (item &&
|
|
55
|
-
typeof item === 'object' &&
|
|
56
|
-
typeof item.deepClone === 'function') {
|
|
57
|
-
return item.deepClone();
|
|
58
|
-
}
|
|
59
|
-
return item;
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
// Handle regular objects with better cycle detection
|
|
33
|
+
});
|
|
34
|
+
(0, utils_1.defineExtension)(Object.prototype, 'deepClone', function () {
|
|
63
35
|
const visited = new WeakSet();
|
|
64
36
|
function deepCloneSafe(obj) {
|
|
65
37
|
if (obj === null || typeof obj !== 'object')
|
|
66
38
|
return obj;
|
|
67
|
-
if (visited.has(obj))
|
|
39
|
+
if (visited.has(obj))
|
|
68
40
|
throw new Error('Circular reference detected in deepClone');
|
|
69
|
-
}
|
|
70
41
|
visited.add(obj);
|
|
71
42
|
if (obj instanceof Date)
|
|
72
43
|
return new Date(obj.getTime());
|
|
@@ -79,69 +50,97 @@ function extendObject() {
|
|
|
79
50
|
return cloned;
|
|
80
51
|
}
|
|
81
52
|
return deepCloneSafe(this);
|
|
82
|
-
};
|
|
83
|
-
Object.prototype
|
|
53
|
+
});
|
|
54
|
+
(0, utils_1.defineExtension)(Object.prototype, 'merge', function (other) {
|
|
84
55
|
return { ...this, ...other };
|
|
85
|
-
};
|
|
86
|
-
Object.prototype
|
|
87
|
-
|
|
88
|
-
for (const name of propNames) {
|
|
56
|
+
});
|
|
57
|
+
(0, utils_1.defineExtension)(Object.prototype, 'deepFreeze', function () {
|
|
58
|
+
Object.getOwnPropertyNames(this).forEach((name) => {
|
|
89
59
|
const value = this[name];
|
|
90
|
-
if (value && typeof value === 'object')
|
|
91
|
-
value.deepFreeze();
|
|
92
|
-
|
|
93
|
-
}
|
|
60
|
+
if (value && typeof value === 'object')
|
|
61
|
+
value.deepFreeze?.();
|
|
62
|
+
});
|
|
94
63
|
return Object.freeze(this);
|
|
95
|
-
};
|
|
96
|
-
Object.prototype
|
|
97
|
-
if (
|
|
98
|
-
throw new TypeError(
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
|
+
return path.split('.').every((key) => {
|
|
69
|
+
if (this == null || !(key in this))
|
|
70
|
+
return false;
|
|
71
|
+
// @ts-ignore
|
|
72
|
+
this = this[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.split('.').reduce((acc, key) => (acc && key in acc ? acc[key] : defaultValue), this);
|
|
80
|
+
});
|
|
81
|
+
(0, utils_1.defineExtension)(Object.prototype, 'setPath', function (path, value) {
|
|
82
|
+
if (!path.trim())
|
|
83
|
+
throw new TypeError('setPath: path cannot be empty');
|
|
103
84
|
const keys = path.split('.');
|
|
104
85
|
let current = this;
|
|
105
|
-
for (
|
|
106
|
-
if (
|
|
107
|
-
|
|
108
|
-
current = current[
|
|
86
|
+
for (let i = 0; i < keys.length - 1; i++) {
|
|
87
|
+
if (!(keys[i] in current) || typeof current[keys[i]] !== 'object')
|
|
88
|
+
current[keys[i]] = {};
|
|
89
|
+
current = current[keys[i]];
|
|
109
90
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
91
|
+
current[keys[keys.length - 1]] = value;
|
|
92
|
+
return this;
|
|
93
|
+
});
|
|
94
|
+
(0, utils_1.defineExtension)(Object.prototype, 'mapValues', function (fn) {
|
|
95
|
+
if (typeof fn !== 'function') {
|
|
96
|
+
throw new TypeError(`mapValues: fn must be a function, got ${typeof fn}`);
|
|
115
97
|
}
|
|
116
|
-
|
|
117
|
-
|
|
98
|
+
const result = {};
|
|
99
|
+
for (const key in this) {
|
|
100
|
+
if (Object.prototype.hasOwnProperty.call(this, key)) {
|
|
101
|
+
result[key] = fn(this[key], key);
|
|
102
|
+
}
|
|
118
103
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
current = current[key];
|
|
104
|
+
return result;
|
|
105
|
+
});
|
|
106
|
+
(0, utils_1.defineExtension)(Object.prototype, 'mapKeys', function (fn) {
|
|
107
|
+
if (typeof fn !== 'function') {
|
|
108
|
+
throw new TypeError(`mapKeys: fn must be a function, got ${typeof fn}`);
|
|
125
109
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
110
|
+
const result = {};
|
|
111
|
+
for (const key in this) {
|
|
112
|
+
if (Object.prototype.hasOwnProperty.call(this, key)) {
|
|
113
|
+
const newKey = fn(key);
|
|
114
|
+
result[newKey] = this[key];
|
|
115
|
+
}
|
|
131
116
|
}
|
|
132
|
-
|
|
133
|
-
|
|
117
|
+
return result;
|
|
118
|
+
});
|
|
119
|
+
(0, utils_1.defineExtension)(Object.prototype, 'filterKeys', function (keys) {
|
|
120
|
+
if (!Array.isArray(keys)) {
|
|
121
|
+
throw new TypeError(`filterKeys: keys must be an array, got ${typeof keys}`);
|
|
134
122
|
}
|
|
135
|
-
const
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
if (!(key in current) || typeof current[key] !== 'object') {
|
|
140
|
-
current[key] = {};
|
|
123
|
+
const result = {};
|
|
124
|
+
for (const key of keys) {
|
|
125
|
+
if (key in this) {
|
|
126
|
+
result[key] = this[key];
|
|
141
127
|
}
|
|
142
|
-
current = current[key];
|
|
143
128
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
129
|
+
return result;
|
|
130
|
+
});
|
|
131
|
+
(0, utils_1.defineExtension)(Object.prototype, 'filterValues', function (fn) {
|
|
132
|
+
if (typeof fn !== 'function') {
|
|
133
|
+
throw new TypeError(`filterValues: fn must be a function, got ${typeof fn}`);
|
|
134
|
+
}
|
|
135
|
+
const result = {};
|
|
136
|
+
for (const key in this) {
|
|
137
|
+
if (Object.prototype.hasOwnProperty.call(this, key)) {
|
|
138
|
+
const val = this[key];
|
|
139
|
+
if (fn(val, key)) {
|
|
140
|
+
result[key] = val;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return result;
|
|
145
|
+
});
|
|
147
146
|
}
|
|
@@ -1,37 +1,39 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.extendString = extendString;
|
|
4
|
-
|
|
4
|
+
const utils_1 = require("src/utils");
|
|
5
|
+
let stringExtended = false;
|
|
5
6
|
function extendString() {
|
|
6
|
-
|
|
7
|
+
if (stringExtended)
|
|
8
|
+
return;
|
|
9
|
+
stringExtended = true;
|
|
10
|
+
(0, utils_1.defineExtension)(String.prototype, 'toCapitalize', function () {
|
|
7
11
|
return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase();
|
|
8
|
-
};
|
|
9
|
-
String.prototype
|
|
10
|
-
return this.replace(/[-_\s]+(.)?/g, (_, char) => char ? char.toUpperCase() : '');
|
|
11
|
-
};
|
|
12
|
-
String.prototype
|
|
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 () {
|
|
13
17
|
return this.replace(/([a-z])([A-Z])/g, '$1-$2')
|
|
14
18
|
.replace(/[\s_]+/g, '-')
|
|
15
19
|
.toLowerCase();
|
|
16
|
-
};
|
|
17
|
-
String.prototype
|
|
20
|
+
});
|
|
21
|
+
(0, utils_1.defineExtension)(String.prototype, 'toSnakeCase', function () {
|
|
18
22
|
return this.replace(/([a-z])([A-Z])/g, '$1_$2')
|
|
19
23
|
.replace(/[\s-]+/g, '_')
|
|
20
24
|
.toLowerCase();
|
|
21
|
-
};
|
|
22
|
-
String.prototype
|
|
25
|
+
});
|
|
26
|
+
(0, utils_1.defineExtension)(String.prototype, 'truncate', function (length, suffix = '...') {
|
|
23
27
|
if (!Number.isInteger(length) || length < 0) {
|
|
24
28
|
throw new TypeError(`truncate: length must be a non-negative integer, got ${length}`);
|
|
25
29
|
}
|
|
26
|
-
return this.length > length
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
};
|
|
30
|
-
String.prototype.isEmail = function () {
|
|
30
|
+
return this.length > length ? this.substring(0, length) + suffix : this.toString();
|
|
31
|
+
});
|
|
32
|
+
(0, utils_1.defineExtension)(String.prototype, 'isEmail', function () {
|
|
31
33
|
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
32
34
|
return emailRegex.test(this.toString());
|
|
33
|
-
};
|
|
34
|
-
String.prototype
|
|
35
|
+
});
|
|
36
|
+
(0, utils_1.defineExtension)(String.prototype, 'isUrl', function () {
|
|
35
37
|
try {
|
|
36
38
|
new URL(this.toString());
|
|
37
39
|
return true;
|
|
@@ -39,24 +41,24 @@ function extendString() {
|
|
|
39
41
|
catch {
|
|
40
42
|
return false;
|
|
41
43
|
}
|
|
42
|
-
};
|
|
43
|
-
String.prototype
|
|
44
|
+
});
|
|
45
|
+
(0, utils_1.defineExtension)(String.prototype, 'removeWhitespace', function () {
|
|
44
46
|
return this.replace(/\s+/g, '');
|
|
45
|
-
};
|
|
46
|
-
String.prototype
|
|
47
|
+
});
|
|
48
|
+
(0, utils_1.defineExtension)(String.prototype, 'reverse', function () {
|
|
47
49
|
return this.split('').reverse().join('');
|
|
48
|
-
};
|
|
49
|
-
String.prototype
|
|
50
|
+
});
|
|
51
|
+
(0, utils_1.defineExtension)(String.prototype, 'isPalindrome', function () {
|
|
50
52
|
const cleaned = this.toLowerCase().replace(/[^a-z0-9]/g, '');
|
|
51
53
|
return cleaned === cleaned.split('').reverse().join('');
|
|
52
|
-
};
|
|
53
|
-
String.prototype
|
|
54
|
+
});
|
|
55
|
+
(0, utils_1.defineExtension)(String.prototype, 'toTitleCase', function () {
|
|
54
56
|
return this.replace(/\w\S*/g, (txt) => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase());
|
|
55
|
-
};
|
|
56
|
-
String.prototype
|
|
57
|
+
});
|
|
58
|
+
(0, utils_1.defineExtension)(String.prototype, 'stripHtml', function () {
|
|
57
59
|
return this.replace(/<[^>]*>/g, '');
|
|
58
|
-
};
|
|
59
|
-
String.prototype
|
|
60
|
+
});
|
|
61
|
+
(0, utils_1.defineExtension)(String.prototype, 'padStart', function (targetLength, padString = ' ') {
|
|
60
62
|
if (!Number.isInteger(targetLength) || targetLength < 0) {
|
|
61
63
|
throw new TypeError(`padStart: targetLength must be a non-negative integer, got ${targetLength}`);
|
|
62
64
|
}
|
|
@@ -67,8 +69,8 @@ function extendString() {
|
|
|
67
69
|
throw new TypeError('padStart: padString cannot be empty');
|
|
68
70
|
}
|
|
69
71
|
return this.toString().padStart(targetLength, padString);
|
|
70
|
-
};
|
|
71
|
-
String.prototype
|
|
72
|
+
});
|
|
73
|
+
(0, utils_1.defineExtension)(String.prototype, 'padEnd', function (targetLength, padString = ' ') {
|
|
72
74
|
if (!Number.isInteger(targetLength) || targetLength < 0) {
|
|
73
75
|
throw new TypeError(`padEnd: targetLength must be a non-negative integer, got ${targetLength}`);
|
|
74
76
|
}
|
|
@@ -79,22 +81,43 @@ function extendString() {
|
|
|
79
81
|
throw new TypeError('padEnd: padString cannot be empty');
|
|
80
82
|
}
|
|
81
83
|
return this.toString().padEnd(targetLength, padString);
|
|
82
|
-
};
|
|
83
|
-
String.prototype
|
|
84
|
+
});
|
|
85
|
+
(0, utils_1.defineExtension)(String.prototype, 'count', function (substring) {
|
|
84
86
|
if (typeof substring !== 'string') {
|
|
85
87
|
throw new TypeError(`count: substring must be a string, got ${typeof substring}`);
|
|
86
88
|
}
|
|
87
89
|
if (substring === '') {
|
|
88
90
|
throw new TypeError('count: substring cannot be empty');
|
|
89
91
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
String.prototype.lines = function () {
|
|
92
|
+
const escaped = substring.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
93
|
+
return (this.match(new RegExp(escaped, 'g')) || []).length;
|
|
94
|
+
});
|
|
95
|
+
(0, utils_1.defineExtension)(String.prototype, 'words', function () {
|
|
96
|
+
return this.trim().split(/\s+/).filter((word) => word.length > 0);
|
|
97
|
+
});
|
|
98
|
+
(0, utils_1.defineExtension)(String.prototype, 'lines', function () {
|
|
98
99
|
return this.split(/\r?\n/);
|
|
99
|
-
};
|
|
100
|
+
});
|
|
101
|
+
(0, utils_1.defineExtension)(String.prototype, 'capitalizeWords', function () {
|
|
102
|
+
return this.toString().replace(/\b\w/g, (char) => char.toUpperCase());
|
|
103
|
+
});
|
|
104
|
+
(0, utils_1.defineExtension)(String.prototype, 'reverseWords', function () {
|
|
105
|
+
return this.toString().split(/\s+/).reverse().join(' ');
|
|
106
|
+
});
|
|
107
|
+
(0, utils_1.defineExtension)(String.prototype, 'truncateWords', function (count, suffix = '...') {
|
|
108
|
+
if (!Number.isInteger(count) || count < 0) {
|
|
109
|
+
throw new TypeError(`truncateWords: count must be a non-negative integer, got ${count}`);
|
|
110
|
+
}
|
|
111
|
+
const words = this.toString().split(/\s+/);
|
|
112
|
+
if (words.length <= count)
|
|
113
|
+
return this.toString();
|
|
114
|
+
return words.slice(0, count).join(' ') + suffix;
|
|
115
|
+
});
|
|
116
|
+
(0, utils_1.defineExtension)(String.prototype, 'slugify', function () {
|
|
117
|
+
return this.toString()
|
|
118
|
+
.toLowerCase()
|
|
119
|
+
.replace(/[^\w\s-]/g, '')
|
|
120
|
+
.trim()
|
|
121
|
+
.replace(/[\s_-]+/g, '-');
|
|
122
|
+
});
|
|
100
123
|
}
|
|
@@ -26,6 +26,7 @@ declare global {
|
|
|
26
26
|
sum(): number;
|
|
27
27
|
average(): number;
|
|
28
28
|
compact(): T[];
|
|
29
|
+
compactTruthy(): T[];
|
|
29
30
|
pluck<K extends keyof T>(key: K): T[K][];
|
|
30
31
|
findLast(predicate: (item: T) => boolean): T | undefined;
|
|
31
32
|
partition(predicate: (item: T) => boolean): [T[], T[]];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function defineExtension<T extends object>(prototype: T, name: string, fn: Function): void;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defineExtension = defineExtension;
|
|
4
|
+
function defineExtension(prototype, name, fn) {
|
|
5
|
+
if (!Object.prototype.hasOwnProperty.call(prototype, name)) {
|
|
6
|
+
Object.defineProperty(prototype, name, {
|
|
7
|
+
value: fn,
|
|
8
|
+
writable: false,
|
|
9
|
+
configurable: false,
|
|
10
|
+
enumerable: false,
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -1,17 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
import { defineExtension } from "src/utils";
|
|
2
|
+
let arrayExtended = false;
|
|
2
3
|
export function extendArray() {
|
|
3
|
-
|
|
4
|
+
if (arrayExtended)
|
|
5
|
+
return;
|
|
6
|
+
arrayExtended = true;
|
|
7
|
+
defineExtension(Array.prototype, 'unique', function () {
|
|
4
8
|
return [...new Set(this)];
|
|
5
|
-
};
|
|
6
|
-
Array.prototype
|
|
9
|
+
});
|
|
10
|
+
defineExtension(Array.prototype, 'shuffle', function () {
|
|
7
11
|
const arr = [...this];
|
|
8
12
|
for (let i = arr.length - 1; i > 0; i--) {
|
|
9
13
|
const j = Math.floor(Math.random() * (i + 1));
|
|
10
14
|
[arr[i], arr[j]] = [arr[j], arr[i]];
|
|
11
15
|
}
|
|
12
16
|
return arr;
|
|
13
|
-
};
|
|
14
|
-
Array.prototype
|
|
17
|
+
});
|
|
18
|
+
defineExtension(Array.prototype, 'chunk', function (size) {
|
|
15
19
|
if (!Number.isInteger(size) || size <= 0) {
|
|
16
20
|
throw new TypeError(`chunk: size must be a positive integer, got ${size}`);
|
|
17
21
|
}
|
|
@@ -20,8 +24,8 @@ export function extendArray() {
|
|
|
20
24
|
chunks.push(this.slice(i, i + size));
|
|
21
25
|
}
|
|
22
26
|
return chunks;
|
|
23
|
-
};
|
|
24
|
-
Array.prototype
|
|
27
|
+
});
|
|
28
|
+
defineExtension(Array.prototype, 'groupBy', function (keyFn) {
|
|
25
29
|
if (typeof keyFn !== 'function') {
|
|
26
30
|
throw new TypeError(`groupBy: keyFn must be a function, got ${typeof keyFn}`);
|
|
27
31
|
}
|
|
@@ -32,33 +36,31 @@ export function extendArray() {
|
|
|
32
36
|
groups[key].push(item);
|
|
33
37
|
return groups;
|
|
34
38
|
}, {});
|
|
35
|
-
};
|
|
36
|
-
Array.prototype
|
|
37
|
-
|
|
38
|
-
if (numbers.length === 0) {
|
|
39
|
+
});
|
|
40
|
+
defineExtension(Array.prototype, 'sum', function () {
|
|
41
|
+
if (this.length === 0) {
|
|
39
42
|
throw new TypeError('sum: array must contain at least one number');
|
|
40
43
|
}
|
|
41
|
-
return
|
|
42
|
-
};
|
|
43
|
-
Array.prototype
|
|
44
|
-
|
|
45
|
-
if (numbers.length === 0) {
|
|
44
|
+
return this.reduce((sum, num) => sum + num, 0);
|
|
45
|
+
});
|
|
46
|
+
defineExtension(Array.prototype, 'average', function () {
|
|
47
|
+
if (this.length === 0) {
|
|
46
48
|
throw new TypeError('average: array must contain at least one number');
|
|
47
49
|
}
|
|
48
|
-
return
|
|
49
|
-
};
|
|
50
|
-
Array.prototype
|
|
51
|
-
return this.filter((item) => item
|
|
52
|
-
};
|
|
53
|
-
Array.prototype
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
};
|
|
61
|
-
Array.prototype
|
|
50
|
+
return this.reduce((sum, num) => sum + num, 0) / this.length;
|
|
51
|
+
});
|
|
52
|
+
defineExtension(Array.prototype, 'compact', function () {
|
|
53
|
+
return this.filter((item) => item !== null && item !== undefined && item !== '');
|
|
54
|
+
});
|
|
55
|
+
defineExtension(Array.prototype, 'compactTruthy', function () {
|
|
56
|
+
return this.filter(Boolean);
|
|
57
|
+
});
|
|
58
|
+
defineExtension(Array.prototype, 'pluck', function (key) {
|
|
59
|
+
return this
|
|
60
|
+
.map((item) => item[key])
|
|
61
|
+
.filter((val) => val !== undefined);
|
|
62
|
+
});
|
|
63
|
+
defineExtension(Array.prototype, 'findLast', function (predicate) {
|
|
62
64
|
if (typeof predicate !== 'function') {
|
|
63
65
|
throw new TypeError(`findLast: predicate must be a function, got ${typeof predicate}`);
|
|
64
66
|
}
|
|
@@ -67,8 +69,8 @@ export function extendArray() {
|
|
|
67
69
|
return this[i];
|
|
68
70
|
}
|
|
69
71
|
return undefined;
|
|
70
|
-
};
|
|
71
|
-
Array.prototype
|
|
72
|
+
});
|
|
73
|
+
defineExtension(Array.prototype, 'partition', function (predicate) {
|
|
72
74
|
if (typeof predicate !== 'function') {
|
|
73
75
|
throw new TypeError(`partition: predicate must be a function, got ${typeof predicate}`);
|
|
74
76
|
}
|
|
@@ -76,42 +78,74 @@ export function extendArray() {
|
|
|
76
78
|
const falsy = [];
|
|
77
79
|
this.forEach((item) => predicate(item) ? truthy.push(item) : falsy.push(item));
|
|
78
80
|
return [truthy, falsy];
|
|
79
|
-
};
|
|
80
|
-
Array.prototype
|
|
81
|
+
});
|
|
82
|
+
defineExtension(Array.prototype, 'flatten', function (depth = 1) {
|
|
81
83
|
return depth > 0
|
|
82
84
|
? this.reduce((acc, val) => acc.concat(Array.isArray(val) ? val.flatten(depth - 1) : val), [])
|
|
83
85
|
: this.slice();
|
|
84
|
-
};
|
|
85
|
-
Array.prototype
|
|
86
|
+
});
|
|
87
|
+
defineExtension(Array.prototype, 'deepFlatten', function () {
|
|
86
88
|
return this.reduce((acc, val) => acc.concat(Array.isArray(val) ? val.deepFlatten() : val), []);
|
|
87
|
-
};
|
|
88
|
-
Array.prototype
|
|
89
|
+
});
|
|
90
|
+
defineExtension(Array.prototype, 'difference', function (other) {
|
|
89
91
|
if (!Array.isArray(other)) {
|
|
90
92
|
throw new TypeError(`difference: other must be an array, got ${typeof other}`);
|
|
91
93
|
}
|
|
92
94
|
return this.filter((item) => !other.includes(item));
|
|
93
|
-
};
|
|
94
|
-
Array.prototype
|
|
95
|
+
});
|
|
96
|
+
defineExtension(Array.prototype, 'intersection', function (other) {
|
|
95
97
|
if (!Array.isArray(other)) {
|
|
96
98
|
throw new TypeError(`intersection: other must be an array, got ${typeof other}`);
|
|
97
99
|
}
|
|
98
100
|
return this.filter((item) => other.includes(item));
|
|
99
|
-
};
|
|
100
|
-
Array.prototype
|
|
101
|
+
});
|
|
102
|
+
defineExtension(Array.prototype, 'union', function (other) {
|
|
101
103
|
if (!Array.isArray(other)) {
|
|
102
104
|
throw new TypeError(`union: other must be an array, got ${typeof other}`);
|
|
103
105
|
}
|
|
104
106
|
return [...new Set([...this, ...other])];
|
|
105
|
-
};
|
|
106
|
-
Array.prototype
|
|
107
|
+
});
|
|
108
|
+
defineExtension(Array.prototype, 'sample', function () {
|
|
107
109
|
return this.length > 0
|
|
108
110
|
? this[Math.floor(Math.random() * this.length)]
|
|
109
111
|
: undefined;
|
|
110
|
-
};
|
|
111
|
-
Array.prototype
|
|
112
|
+
});
|
|
113
|
+
defineExtension(Array.prototype, 'take', function (count) {
|
|
112
114
|
return this.slice(0, Math.max(0, count));
|
|
113
|
-
};
|
|
114
|
-
Array.prototype
|
|
115
|
+
});
|
|
116
|
+
defineExtension(Array.prototype, 'drop', function (count) {
|
|
115
117
|
return this.slice(Math.max(0, count));
|
|
116
|
-
};
|
|
118
|
+
});
|
|
119
|
+
defineExtension(Array.prototype, 'uniqueBy', function (keyFn) {
|
|
120
|
+
if (typeof keyFn !== 'function') {
|
|
121
|
+
throw new TypeError(`uniqueBy: keyFn must be a function, got ${typeof keyFn}`);
|
|
122
|
+
}
|
|
123
|
+
const seen = new Set();
|
|
124
|
+
const result = [];
|
|
125
|
+
for (const item of this) {
|
|
126
|
+
const key = keyFn(item);
|
|
127
|
+
if (!seen.has(key)) {
|
|
128
|
+
seen.add(key);
|
|
129
|
+
result.push(item);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return result;
|
|
133
|
+
});
|
|
134
|
+
defineExtension(Array.prototype, 'sortBy', function (keyFn) {
|
|
135
|
+
if (typeof keyFn !== 'function') {
|
|
136
|
+
throw new TypeError(`sortBy: keyFn must be a function, got ${typeof keyFn}`);
|
|
137
|
+
}
|
|
138
|
+
return [...this].sort((a, b) => {
|
|
139
|
+
const aVal = keyFn(a);
|
|
140
|
+
const bVal = keyFn(b);
|
|
141
|
+
if (aVal < bVal)
|
|
142
|
+
return -1;
|
|
143
|
+
if (aVal > bVal)
|
|
144
|
+
return 1;
|
|
145
|
+
return 0;
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
defineExtension(Array.prototype, 'last', function () {
|
|
149
|
+
return this.length > 0 ? this[this.length - 1] : undefined;
|
|
150
|
+
});
|
|
117
151
|
}
|