@arnonsang/utils 0.0.2
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 +44 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +20 -0
- package/dist/utils/chat.d.ts +15 -0
- package/dist/utils/chat.js +29 -0
- package/dist/utils/logger.d.ts +14 -0
- package/dist/utils/logger.js +40 -0
- package/index.js +23 -0
- package/index.ts +2 -0
- package/package.json +19 -0
- package/tsconfig.json +10 -0
- package/utils/chat.js +29 -0
- package/utils/chat.ts +28 -0
- package/utils/logger.js +40 -0
- package/utils/logger.ts +55 -0
package/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# My Utils Package
|
2
|
+
|
3
|
+
Utils package focus on web development utilities.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```sh
|
8
|
+
npm install @arnonsang/utils
|
9
|
+
```
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
### Importing Utilities
|
14
|
+
|
15
|
+
```ts
|
16
|
+
import { logger, charAt, charAtFrom } from "@arnonsang/utils";
|
17
|
+
```
|
18
|
+
|
19
|
+
### Logger Utility
|
20
|
+
|
21
|
+
```ts
|
22
|
+
logger.info("This is an info message");
|
23
|
+
logger.warn("This is a warning message");
|
24
|
+
logger.error("An error occurred");
|
25
|
+
logger.success("Operation successful");
|
26
|
+
logger.debug("Debugging info");
|
27
|
+
logger.table([{ name: "Alice" }, { name: "Bob" }], "User Data");
|
28
|
+
```
|
29
|
+
|
30
|
+
### Chat Utilities
|
31
|
+
|
32
|
+
```ts
|
33
|
+
console.log(charAt(3, "upper")); // "D"
|
34
|
+
console.log(charAtFrom("hello", 1)); // "e"
|
35
|
+
```
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
Feel free to open an issue or a pull request to contribute new utilities or improve existing ones.
|
40
|
+
|
41
|
+
## License
|
42
|
+
|
43
|
+
This project is licensed under the MIT License.
|
44
|
+
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
+
};
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
exports.logger = void 0;
|
18
|
+
__exportStar(require("./utils/chat"), exports);
|
19
|
+
var logger_1 = require("./utils/logger");
|
20
|
+
Object.defineProperty(exports, "logger", { enumerable: true, get: function () { return logger_1.default; } });
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/**
|
2
|
+
* Returns the character in the alphabet at the given index (0-based).
|
3
|
+
* @param index index of the character in the alphabet
|
4
|
+
* @param type type of character to return (lowercase or uppercase)
|
5
|
+
* @returns character at the given index
|
6
|
+
*/
|
7
|
+
declare const charAt: (index: number, type?: "lower" | "upper") => string;
|
8
|
+
/**
|
9
|
+
* Returns the character at the given index in the string.
|
10
|
+
* @param str input string
|
11
|
+
* @param index index of the character to return
|
12
|
+
* @returns character at the given index
|
13
|
+
*/
|
14
|
+
declare const charAtFrom: (str: string, index: number) => string;
|
15
|
+
export { charAt, charAtFrom };
|
@@ -0,0 +1,29 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.charAtFrom = exports.charAt = void 0;
|
4
|
+
/**
|
5
|
+
* Returns the character in the alphabet at the given index (0-based).
|
6
|
+
* @param index index of the character in the alphabet
|
7
|
+
* @param type type of character to return (lowercase or uppercase)
|
8
|
+
* @returns character at the given index
|
9
|
+
*/
|
10
|
+
const charAt = (index, type = "lower") => {
|
11
|
+
const charset = "abcdefghijklmnopqrstuvwxyz";
|
12
|
+
if (index < 0 || index >= charset.length) {
|
13
|
+
return "";
|
14
|
+
}
|
15
|
+
return type === "upper"
|
16
|
+
? charset.charAt(index).toUpperCase()
|
17
|
+
: charset.charAt(index);
|
18
|
+
};
|
19
|
+
exports.charAt = charAt;
|
20
|
+
/**
|
21
|
+
* Returns the character at the given index in the string.
|
22
|
+
* @param str input string
|
23
|
+
* @param index index of the character to return
|
24
|
+
* @returns character at the given index
|
25
|
+
*/
|
26
|
+
const charAtFrom = (str, index) => {
|
27
|
+
return str.charAt(index);
|
28
|
+
};
|
29
|
+
exports.charAtFrom = charAtFrom;
|
@@ -0,0 +1,14 @@
|
|
1
|
+
declare class Logger {
|
2
|
+
private isDev;
|
3
|
+
constructor();
|
4
|
+
private logMessage;
|
5
|
+
success(message: string, ...args: unknown[]): void;
|
6
|
+
info(message: string, ...args: unknown[]): void;
|
7
|
+
warn(message: string, ...args: unknown[]): void;
|
8
|
+
error(message: string, ...args: unknown[]): void;
|
9
|
+
debug(message: string, ...args: unknown[]): void;
|
10
|
+
log(message: string, ...args: unknown[]): void;
|
11
|
+
table(data: unknown, from?: string): void;
|
12
|
+
}
|
13
|
+
declare const logger: Logger;
|
14
|
+
export default logger;
|
@@ -0,0 +1,40 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
class Logger {
|
4
|
+
constructor() {
|
5
|
+
this.isDev = process.env.NODE_ENV === "development" || !process.env.NODE_ENV;
|
6
|
+
}
|
7
|
+
logMessage(type, message, color, ...args) {
|
8
|
+
if (!this.isDev)
|
9
|
+
return;
|
10
|
+
const styles = `color: ${color}; font-weight: bold;`;
|
11
|
+
console[type](`%c[${type.toUpperCase()}] %c${message}`, styles, "color:white;", ...args);
|
12
|
+
}
|
13
|
+
success(message, ...args) {
|
14
|
+
this.logMessage("info", message, "#28a745", ...args);
|
15
|
+
}
|
16
|
+
info(message, ...args) {
|
17
|
+
this.logMessage("info", message, "#007bff", ...args);
|
18
|
+
}
|
19
|
+
warn(message, ...args) {
|
20
|
+
this.logMessage("warn", message, "#ffc107", ...args);
|
21
|
+
}
|
22
|
+
error(message, ...args) {
|
23
|
+
this.logMessage("error", message, "#dc3545", ...args);
|
24
|
+
}
|
25
|
+
debug(message, ...args) {
|
26
|
+
this.logMessage("debug", message, "#28a745", ...args);
|
27
|
+
}
|
28
|
+
log(message, ...args) {
|
29
|
+
this.logMessage("log", message, "white", ...args);
|
30
|
+
}
|
31
|
+
table(data, from) {
|
32
|
+
if (!this.isDev)
|
33
|
+
return;
|
34
|
+
console.log(`%c[DATA TABLE] %c${from || ""}`, "color: #007bff; font-weight: bold;", "color:white;");
|
35
|
+
console.table(data);
|
36
|
+
}
|
37
|
+
}
|
38
|
+
// Export an instance
|
39
|
+
const logger = new Logger();
|
40
|
+
exports.default = logger;
|
package/index.js
ADDED
@@ -0,0 +1,23 @@
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
18
|
+
};
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
20
|
+
exports.logger = void 0;
|
21
|
+
__exportStar(require("./utils/chat"), exports);
|
22
|
+
var logger_1 = require("./utils/logger");
|
23
|
+
Object.defineProperty(exports, "logger", { enumerable: true, get: function () { return __importDefault(logger_1).default; } });
|
package/index.ts
ADDED
package/package.json
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
{
|
2
|
+
"name": "@arnonsang/utils",
|
3
|
+
"version": "0.0.2",
|
4
|
+
"description": "A collection of utility functions",
|
5
|
+
"main": "dist/index.js",
|
6
|
+
"types": "dist/index.d.ts",
|
7
|
+
"license": "MIT",
|
8
|
+
"author": "arnonsang",
|
9
|
+
"scripts": {
|
10
|
+
"prepare": "tsc",
|
11
|
+
"build": "tsc",
|
12
|
+
"patch": "npm version patch && npm publish --access public ",
|
13
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
14
|
+
},
|
15
|
+
"devDependencies": {
|
16
|
+
"@types/node": "^22.13.1",
|
17
|
+
"typescript": "^5.7.3"
|
18
|
+
}
|
19
|
+
}
|
package/tsconfig.json
ADDED
package/utils/chat.js
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.charAtFrom = exports.charAt = void 0;
|
4
|
+
/**
|
5
|
+
* Returns the character in the alphabet at the given index (0-based).
|
6
|
+
* @param index index of the character in the alphabet
|
7
|
+
* @param type type of character to return (lowercase or uppercase)
|
8
|
+
* @returns character at the given index
|
9
|
+
*/
|
10
|
+
const charAt = (index, type = "lower") => {
|
11
|
+
const charset = "abcdefghijklmnopqrstuvwxyz";
|
12
|
+
if (index < 0 || index >= charset.length) {
|
13
|
+
return "";
|
14
|
+
}
|
15
|
+
return type === "upper"
|
16
|
+
? charset.charAt(index).toUpperCase()
|
17
|
+
: charset.charAt(index);
|
18
|
+
};
|
19
|
+
exports.charAt = charAt;
|
20
|
+
/**
|
21
|
+
* Returns the character at the given index in the string.
|
22
|
+
* @param str input string
|
23
|
+
* @param index index of the character to return
|
24
|
+
* @returns character at the given index
|
25
|
+
*/
|
26
|
+
const charAtFrom = (str, index) => {
|
27
|
+
return str.charAt(index);
|
28
|
+
};
|
29
|
+
exports.charAtFrom = charAtFrom;
|
package/utils/chat.ts
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
/**
|
2
|
+
* Returns the character in the alphabet at the given index (0-based).
|
3
|
+
* @param index index of the character in the alphabet
|
4
|
+
* @param type type of character to return (lowercase or uppercase)
|
5
|
+
* @returns character at the given index
|
6
|
+
*/
|
7
|
+
const charAt = (index: number, type: "lower" | "upper" = "lower") => {
|
8
|
+
const charset = "abcdefghijklmnopqrstuvwxyz";
|
9
|
+
|
10
|
+
if (index < 0 || index >= charset.length) {
|
11
|
+
return "";
|
12
|
+
}
|
13
|
+
return type === "upper"
|
14
|
+
? charset.charAt(index).toUpperCase()
|
15
|
+
: charset.charAt(index);
|
16
|
+
};
|
17
|
+
|
18
|
+
/**
|
19
|
+
* Returns the character at the given index in the string.
|
20
|
+
* @param str input string
|
21
|
+
* @param index index of the character to return
|
22
|
+
* @returns character at the given index
|
23
|
+
*/
|
24
|
+
const charAtFrom = (str: string, index: number) => {
|
25
|
+
return str.charAt(index);
|
26
|
+
};
|
27
|
+
|
28
|
+
export { charAt, charAtFrom };
|
package/utils/logger.js
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
class Logger {
|
4
|
+
constructor() {
|
5
|
+
this.isDev = process.env.NODE_ENV === "development" || !process.env.NODE_ENV;
|
6
|
+
}
|
7
|
+
logMessage(type, message, color, ...args) {
|
8
|
+
if (!this.isDev)
|
9
|
+
return;
|
10
|
+
const styles = `color: ${color}; font-weight: bold;`;
|
11
|
+
console[type](`%c[${type.toUpperCase()}] %c${message}`, styles, "color:white;", ...args);
|
12
|
+
}
|
13
|
+
success(message, ...args) {
|
14
|
+
this.logMessage("info", message, "#28a745", ...args);
|
15
|
+
}
|
16
|
+
info(message, ...args) {
|
17
|
+
this.logMessage("info", message, "#007bff", ...args);
|
18
|
+
}
|
19
|
+
warn(message, ...args) {
|
20
|
+
this.logMessage("warn", message, "#ffc107", ...args);
|
21
|
+
}
|
22
|
+
error(message, ...args) {
|
23
|
+
this.logMessage("error", message, "#dc3545", ...args);
|
24
|
+
}
|
25
|
+
debug(message, ...args) {
|
26
|
+
this.logMessage("debug", message, "#28a745", ...args);
|
27
|
+
}
|
28
|
+
log(message, ...args) {
|
29
|
+
this.logMessage("log", message, "white", ...args);
|
30
|
+
}
|
31
|
+
table(data, from) {
|
32
|
+
if (!this.isDev)
|
33
|
+
return;
|
34
|
+
console.log(`%c[DATA TABLE] %c${from || ""}`, "color: #007bff; font-weight: bold;", "color:white;");
|
35
|
+
console.table(data);
|
36
|
+
}
|
37
|
+
}
|
38
|
+
// Export an instance
|
39
|
+
const logger = new Logger();
|
40
|
+
exports.default = logger;
|
package/utils/logger.ts
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
type LogType = "info" | "warn" | "error" | "debug" | "log" | "table";
|
2
|
+
|
3
|
+
class Logger {
|
4
|
+
private isDev: boolean;
|
5
|
+
|
6
|
+
constructor() {
|
7
|
+
this.isDev = process.env.NODE_ENV === "development" || !process.env.NODE_ENV;
|
8
|
+
}
|
9
|
+
|
10
|
+
private logMessage(
|
11
|
+
type: LogType,
|
12
|
+
message: string,
|
13
|
+
color: string,
|
14
|
+
...args: unknown[]
|
15
|
+
): void {
|
16
|
+
if (!this.isDev) return;
|
17
|
+
|
18
|
+
const styles = `color: ${color}; font-weight: bold;`;
|
19
|
+
console[type](`%c[${type.toUpperCase()}] %c${message}`, styles, "color:white;", ...args);
|
20
|
+
}
|
21
|
+
|
22
|
+
success(message: string, ...args: unknown[]): void {
|
23
|
+
this.logMessage("info", message, "#28a745", ...args);
|
24
|
+
}
|
25
|
+
|
26
|
+
info(message: string, ...args: unknown[]): void {
|
27
|
+
this.logMessage("info", message, "#007bff", ...args);
|
28
|
+
}
|
29
|
+
|
30
|
+
warn(message: string, ...args: unknown[]): void {
|
31
|
+
this.logMessage("warn", message, "#ffc107", ...args);
|
32
|
+
}
|
33
|
+
|
34
|
+
error(message: string, ...args: unknown[]): void {
|
35
|
+
this.logMessage("error", message, "#dc3545", ...args);
|
36
|
+
}
|
37
|
+
|
38
|
+
debug(message: string, ...args: unknown[]): void {
|
39
|
+
this.logMessage("debug", message, "#28a745", ...args);
|
40
|
+
}
|
41
|
+
|
42
|
+
log(message: string, ...args: unknown[]): void {
|
43
|
+
this.logMessage("log", message, "white", ...args);
|
44
|
+
}
|
45
|
+
|
46
|
+
table(data: unknown, from?: string): void {
|
47
|
+
if (!this.isDev) return;
|
48
|
+
console.log(`%c[DATA TABLE] %c${from || ""}`, "color: #007bff; font-weight: bold;", "color:white;");
|
49
|
+
console.table(data);
|
50
|
+
}
|
51
|
+
}
|
52
|
+
|
53
|
+
// Export an instance
|
54
|
+
const logger = new Logger();
|
55
|
+
export default logger;
|