@hichchi/utils 0.0.1-alpha.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/CHANGELOG.md +3 -0
- package/README.md +1382 -0
- package/package.json +22 -0
- package/readme-top.md +11 -0
- package/src/index.d.ts +1 -0
- package/src/index.js +5 -0
- package/src/index.js.map +1 -0
- package/src/lib/assertions.utils.d.ts +55 -0
- package/src/lib/assertions.utils.js +68 -0
- package/src/lib/assertions.utils.js.map +1 -0
- package/src/lib/file.utils.d.ts +25 -0
- package/src/lib/file.utils.js +1263 -0
- package/src/lib/file.utils.js.map +1 -0
- package/src/lib/interfaces.d.ts +6 -0
- package/src/lib/interfaces.js +3 -0
- package/src/lib/interfaces.js.map +1 -0
- package/src/lib/object.utils.d.ts +265 -0
- package/src/lib/object.utils.js +414 -0
- package/src/lib/object.utils.js.map +1 -0
- package/src/lib/string-template.utils.d.ts +25 -0
- package/src/lib/string-template.utils.js +40 -0
- package/src/lib/string-template.utils.js.map +1 -0
- package/src/lib/string.utils.d.ts +217 -0
- package/src/lib/string.utils.js +314 -0
- package/src/lib/string.utils.js.map +1 -0
- package/src/lib/types.d.ts +20 -0
- package/src/lib/types.js +4 -0
- package/src/lib/types.js.map +1 -0
- package/src/lib/utils.d.ts +7 -0
- package/src/lib/utils.js +11 -0
- package/src/lib/utils.js.map +1 -0
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hichchi/utils",
|
|
3
|
+
"version": "0.0.1-alpha.0",
|
|
4
|
+
"publishConfig": {
|
|
5
|
+
"access": "public"
|
|
6
|
+
},
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/hichchidev/hichchi.git"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [],
|
|
12
|
+
"author": "Waruna Udayanga",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"main": "./src/index.ts",
|
|
15
|
+
"typings": "./src/index.d.ts",
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"tslib": "^2.3.0"
|
|
19
|
+
},
|
|
20
|
+
"types": "./src/index.d.ts",
|
|
21
|
+
"type": "commonjs"
|
|
22
|
+
}
|
package/readme-top.md
ADDED
package/src/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./lib/utils";
|
package/src/index.js
ADDED
package/src/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../libs/utils/src/index.ts"],"names":[],"mappings":";;;AAAA,sDAA4B"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check if the value is an array while asserting it's an array of generic type T
|
|
3
|
+
* @template T The type of the array
|
|
4
|
+
* @param {T | T[] | undefined} value The value to check
|
|
5
|
+
* @returns {value is T[]} True if the value is an array, false otherwise
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```TypeScript
|
|
9
|
+
* async function createUser(userOrUsers: UserDto | UserDto[] | undefined): User {
|
|
10
|
+
* if (isArray<UserDto>(userOrUsers)) {
|
|
11
|
+
* return userOrUsers.map(async user => await userService.createUser(user));
|
|
12
|
+
* } else {
|
|
13
|
+
* return await userService.createUser(userOrUsers);
|
|
14
|
+
* }
|
|
15
|
+
* }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function isArray<T>(value: T | T[] | undefined): value is T[];
|
|
19
|
+
/**
|
|
20
|
+
* Check if the value is an object while asserting it's an object of generic type T
|
|
21
|
+
* @template T The type of the object
|
|
22
|
+
* @param {T | T[] | undefined} [value] The value to check
|
|
23
|
+
* @returns {value is T} True if the value is an object, false otherwise
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```TypeScript
|
|
27
|
+
* async function getUserInfo(userIdOrUser: EntityId | User | undefined): UserInfo {
|
|
28
|
+
* if (isObject<User>(userIdOrUser)) {
|
|
29
|
+
* return await userService.getUserInfo(userIdOrUser.id);
|
|
30
|
+
* } else {
|
|
31
|
+
* return await userService.getUserInfo(userIdOrUser);
|
|
32
|
+
* }
|
|
33
|
+
* }
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare function isObject<T>(value?: T | T[] | undefined): value is T;
|
|
37
|
+
/**
|
|
38
|
+
* Check if the value is an object with a given property name and asset it's an object of generic type T
|
|
39
|
+
* @template T The type of the object
|
|
40
|
+
* @param {any} value The value to check
|
|
41
|
+
* @param {keyof T} propertyName The property name to check
|
|
42
|
+
* @returns {value is T} True if the value is an object with the given property name, false otherwise
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```TypeScript
|
|
46
|
+
* async function getUserInfo(userIdOrUser: EntityId | User | undefined): UserInfo {
|
|
47
|
+
* if (isObjectWith<User>(userIdOrUser, "id")) {
|
|
48
|
+
* return await userService.getUserInfo(userIdOrUser.id);
|
|
49
|
+
* } else {
|
|
50
|
+
* return await userService.getUserInfo(userIdOrUser);
|
|
51
|
+
* }
|
|
52
|
+
* }
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export declare function isObjectWith<T extends object>(value: unknown, propertyName: keyof T): value is T;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// noinspection JSUnusedGlobalSymbols
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.isArray = isArray;
|
|
5
|
+
exports.isObject = isObject;
|
|
6
|
+
exports.isObjectWith = isObjectWith;
|
|
7
|
+
/**
|
|
8
|
+
* Check if the value is an array while asserting it's an array of generic type T
|
|
9
|
+
* @template T The type of the array
|
|
10
|
+
* @param {T | T[] | undefined} value The value to check
|
|
11
|
+
* @returns {value is T[]} True if the value is an array, false otherwise
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```TypeScript
|
|
15
|
+
* async function createUser(userOrUsers: UserDto | UserDto[] | undefined): User {
|
|
16
|
+
* if (isArray<UserDto>(userOrUsers)) {
|
|
17
|
+
* return userOrUsers.map(async user => await userService.createUser(user));
|
|
18
|
+
* } else {
|
|
19
|
+
* return await userService.createUser(userOrUsers);
|
|
20
|
+
* }
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
function isArray(value) {
|
|
25
|
+
return Array.isArray(value);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Check if the value is an object while asserting it's an object of generic type T
|
|
29
|
+
* @template T The type of the object
|
|
30
|
+
* @param {T | T[] | undefined} [value] The value to check
|
|
31
|
+
* @returns {value is T} True if the value is an object, false otherwise
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```TypeScript
|
|
35
|
+
* async function getUserInfo(userIdOrUser: EntityId | User | undefined): UserInfo {
|
|
36
|
+
* if (isObject<User>(userIdOrUser)) {
|
|
37
|
+
* return await userService.getUserInfo(userIdOrUser.id);
|
|
38
|
+
* } else {
|
|
39
|
+
* return await userService.getUserInfo(userIdOrUser);
|
|
40
|
+
* }
|
|
41
|
+
* }
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
function isObject(value) {
|
|
45
|
+
return !Array.isArray(value) && typeof value === "object" && value !== null;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Check if the value is an object with a given property name and asset it's an object of generic type T
|
|
49
|
+
* @template T The type of the object
|
|
50
|
+
* @param {any} value The value to check
|
|
51
|
+
* @param {keyof T} propertyName The property name to check
|
|
52
|
+
* @returns {value is T} True if the value is an object with the given property name, false otherwise
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```TypeScript
|
|
56
|
+
* async function getUserInfo(userIdOrUser: EntityId | User | undefined): UserInfo {
|
|
57
|
+
* if (isObjectWith<User>(userIdOrUser, "id")) {
|
|
58
|
+
* return await userService.getUserInfo(userIdOrUser.id);
|
|
59
|
+
* } else {
|
|
60
|
+
* return await userService.getUserInfo(userIdOrUser);
|
|
61
|
+
* }
|
|
62
|
+
* }
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
function isObjectWith(value, propertyName) {
|
|
66
|
+
return Object.prototype.hasOwnProperty.call(value, propertyName);
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=assertions.utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assertions.utils.js","sourceRoot":"","sources":["../../../../../libs/utils/src/lib/assertions.utils.ts"],"names":[],"mappings":";AAAA,qCAAqC;;AAmBrC,0BAEC;AAmBD,4BAEC;AAoBD,oCAEC;AA9DD;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,OAAO,CAAI,KAA0B;IACjD,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAChC,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,QAAQ,CAAI,KAA2B;IACnD,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,CAAC;AAChF,CAAC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAgB,YAAY,CAAmB,KAAc,EAAE,YAAqB;IAChF,OAAO,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC;AACrE,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Map of mime types to all file extensions.
|
|
3
|
+
*/
|
|
4
|
+
export declare const mimeTypes: Map<string, string>;
|
|
5
|
+
/**
|
|
6
|
+
* Get the file extension of the given mime type.
|
|
7
|
+
* @param mimeType - Mime type.
|
|
8
|
+
* @param allowedMimeTypes - Allowed mime types.
|
|
9
|
+
* @returns File extension.
|
|
10
|
+
*/
|
|
11
|
+
export declare const getFileExt: (mimeType: string, allowedMimeTypes?: Map<string, string>) => string | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* Get file size in human-readable format.
|
|
14
|
+
* @param size - File size in bytes.
|
|
15
|
+
* @param round - Whether to round the size.
|
|
16
|
+
* @returns File size in human-readable format.
|
|
17
|
+
*/
|
|
18
|
+
export declare const getFileSize: (size: number, round?: boolean) => string;
|
|
19
|
+
/**
|
|
20
|
+
* Save a StreamableBlob as a file.
|
|
21
|
+
* @param blob - Blob to save.
|
|
22
|
+
* @param filename - File name.
|
|
23
|
+
* @throws {Error} - Throws an error if used in a Node.js environment.
|
|
24
|
+
*/
|
|
25
|
+
export declare const saveAsFile: (blob: Blob, filename: string) => void;
|