@ylsoo/core 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +97 -7
  2. package/index.d.ts +91 -0
  3. package/index.js +176 -17
  4. package/package.json +15 -5
package/README.md CHANGED
@@ -1,19 +1,109 @@
1
- # @ylsoo/core
1
+ <div align="center">
2
+ <h1>@ylsoo/core</h1>
3
+ <p><b>The official, zero-dependency SDK powering all premium Ylsoo products.</b></p>
4
+
5
+ [![npm version](https://img.shields.io/npm/v/@ylsoo/core.svg?style=flat-square)](https://www.npmjs.com/package/@ylsoo/core)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square)](https://opensource.org/licenses/MIT)
7
+ [![TypeScript Ready](https://img.shields.io/badge/TypeScript-Ready-blue?style=flat-square&logo=typescript)](https://www.typescriptlang.org/)
8
+ </div>
2
9
 
3
- Core utility functions for Ylsoo projects.
10
+ <hr>
4
11
 
5
- ## Installation
12
+ ## 🚀 Overview
13
+
14
+ `@ylsoo/core` provides fundamental, blazing-fast utility functions designed specifically for integrating Ylsoo services. Built completely from the ground up without any third-party dependencies, it guarantees ultra-fast execution and zero bloat.
15
+
16
+ ## 📦 Installation
6
17
 
7
18
  ```bash
8
19
  npm i @ylsoo/core
9
20
  ```
10
21
 
11
- ## Usage
22
+ *Note: Fully typed out-of-the-box for perfect IDE intellisense!*
23
+
24
+ ## 🛠️ API Reference
25
+
26
+ ### 1. Ylsoo Console (`logger`)
27
+ A beautifully formatted console utility utilizing native ANSI escape codes for local development.
28
+
29
+ ```javascript
30
+ const ylsoo = require('@ylsoo/core');
31
+
32
+ ylsoo.logger.info('System initializing...');
33
+ ylsoo.logger.success('Database connected successfully!');
34
+ ylsoo.logger.warn('Rate limit approaching.');
35
+ ylsoo.logger.error('Critical failure detected.');
36
+ ```
37
+
38
+ ### 2. Ylsoo Request Engine (`http`)
39
+ A robust wrapper around the native `fetch` API. It automatically handles stringifying bodies, applying correct headers, parsing JSON, and safely throwing formatted errors.
12
40
 
13
41
  ```javascript
14
42
  const ylsoo = require('@ylsoo/core');
15
43
 
16
- ylsoo.log('Hello World');
17
- console.log(ylsoo.capitalize('hello'));
18
- console.log(ylsoo.isEmpty(''));
44
+ async function fetchUserData() {
45
+ try {
46
+ const data = await ylsoo.http.get('https://api.ylsoo.com/v1/user/me');
47
+ console.log(data);
48
+ } catch (error) {
49
+ ylsoo.logger.error('Failed to fetch user', error);
50
+ }
51
+ }
52
+ ```
53
+
54
+ ### 3. Ylsoo Memory Cache (`cache`)
55
+ A fast, volatile in-memory key/value store with Time-To-Live (TTL) functionality.
56
+
57
+ ```javascript
58
+ const ylsoo = require('@ylsoo/core');
59
+
60
+ // Cache a complex object for 5 seconds (5000 ms)
61
+ ylsoo.cache.set('session_token', { id: 123 }, 5000);
62
+
63
+ // Retrieve it instantly
64
+ const session = ylsoo.cache.get('session_token');
65
+
66
+ // Will return null after 5 seconds
67
+ setTimeout(() => {
68
+ console.log(ylsoo.cache.get('session_token')); // null
69
+ }, 5100);
19
70
  ```
71
+
72
+ ### 4. Advanced Core Utilities
73
+
74
+ #### `sleep(ms)`
75
+ Promise-based execution halter.
76
+ ```javascript
77
+ await ylsoo.sleep(2000); // Waits 2 seconds
78
+ ```
79
+
80
+ #### `deepClone(obj)`
81
+ Safely fully clones an object without retaining references, utilizing modern native standards where available.
82
+ ```javascript
83
+ const copy = ylsoo.deepClone(complexObject);
84
+ ```
85
+
86
+ #### `debounce(func, wait)`
87
+ Throttles function execution to ensure high performance during spam events (like window resizing or typing).
88
+ ```javascript
89
+ const search = ylsoo.debounce(() => console.log('Searching...'), 300);
90
+ ```
91
+
92
+ #### `isEmpty(value)`
93
+ Intelligently determines if strings, arrays, objects, maps, or sets are empty.
94
+ ```javascript
95
+ ylsoo.isEmpty({}); // true
96
+ ylsoo.isEmpty([]); // true
97
+ ylsoo.isEmpty("hello"); // false
98
+ ```
99
+
100
+ #### `capitalize(str)`
101
+ Easily standardize text formatting.
102
+ ```javascript
103
+ ylsoo.capitalize('ylsoo'); // 'Ylsoo'
104
+ ```
105
+
106
+ ---
107
+ <div align="center">
108
+ <p>Built with ❤️ by <b>Ylsoo</b>.</p>
109
+ </div>
package/index.d.ts ADDED
@@ -0,0 +1,91 @@
1
+ /**
2
+ * @ylsoo/core Type Definitions
3
+ */
4
+
5
+ declare module '@ylsoo/core' {
6
+
7
+ export class YlsooLogger {
8
+ /** Logs an informational message */
9
+ info(message: string | any): void;
10
+ /** Logs a success message */
11
+ success(message: string | any): void;
12
+ /** Logs a warning message */
13
+ warn(message: string | any): void;
14
+ /** Logs an error message */
15
+ error(message: string | any): void;
16
+ }
17
+
18
+ export class YlsooCache {
19
+ /**
20
+ * Set a key-value pair in memory
21
+ * @param key String identifier
22
+ * @param value The raw value to store
23
+ * @param ttlMs Time to live in milliseconds (0 for infinite)
24
+ */
25
+ set(key: string, value: any, ttlMs?: number): void;
26
+
27
+ /**
28
+ * Retrieve a value from the cache. Returns null if expired or missing.
29
+ */
30
+ get<T = any>(key: string): T | null;
31
+
32
+ /**
33
+ * Delete a specific key from the cache
34
+ */
35
+ delete(key: string): boolean;
36
+
37
+ /**
38
+ * Clear all cached data
39
+ */
40
+ clear(): void;
41
+ }
42
+
43
+ export interface YlsooHttpOptions {
44
+ method?: string;
45
+ headers?: Record<string, string>;
46
+ body?: any;
47
+ [key: string]: any;
48
+ }
49
+
50
+ export class YlsooHttp {
51
+ /** Makes a raw fetch request but automatically handles JSON mapping and errors */
52
+ request<T = any>(endpoint: string, options?: YlsooHttpOptions): Promise<T>;
53
+
54
+ /** Performs a GET request */
55
+ get<T = any>(endpoint: string, headers?: Record<string, string>): Promise<T>;
56
+
57
+ /** Performs a POST request with an automatic JSON-stringified body */
58
+ post<T = any>(endpoint: string, body: any, headers?: Record<string, string>): Promise<T>;
59
+ }
60
+
61
+ export class YlsooCore {
62
+ version: string;
63
+
64
+ /** The official Ylsoo logging console */
65
+ logger: YlsooLogger;
66
+
67
+ /** The official Ylsoo memory cache */
68
+ cache: YlsooCache;
69
+
70
+ /** The official Ylsoo request engine */
71
+ http: YlsooHttp;
72
+
73
+ /** Halt execution for a set duration */
74
+ sleep(ms: number): Promise<void>;
75
+
76
+ /** Safely deep clone an object */
77
+ deepClone<T>(obj: T): T;
78
+
79
+ /** Creates a debounced version of a function */
80
+ debounce<T extends (...args: any[]) => any>(func: T, wait: number): (...args: Parameters<T>) => void;
81
+
82
+ /** Check if an array, string, object, map, or set is semantically empty */
83
+ isEmpty(value: any): boolean;
84
+
85
+ /** Capitalizes the first letter of a string */
86
+ capitalize(str: string): string;
87
+ }
88
+
89
+ const ylsoo: YlsooCore;
90
+ export default ylsoo;
91
+ }
package/index.js CHANGED
@@ -1,43 +1,202 @@
1
1
  /**
2
- * Ylsoo Core - Basic utility functions
2
+ * @ylsoo/core
3
+ * Premium utility functions for Ylsoo projects
4
+ * Zero dependency, extremely fast, robust.
3
5
  */
4
6
 
7
+ class YlsooLogger {
8
+ constructor() {
9
+ this.colors = {
10
+ reset: "\x1b[0m",
11
+ info: "\x1b[36m", // Cyan
12
+ success: "\x1b[32m", // Green
13
+ warn: "\x1b[33m", // Yellow
14
+ error: "\x1b[31m", // Red
15
+ ylsoo: "\x1b[35m" // Magenta
16
+ };
17
+ }
18
+
19
+ _format(level, message, color) {
20
+ const time = new Date().toISOString().split('T')[1].slice(0, -1);
21
+ return `${this.colors.ylsoo}[Ylsoo]${this.colors.reset} ${color}[${level.toUpperCase()}]${this.colors.reset} ${message}`;
22
+ }
23
+
24
+ info(message) {
25
+ console.log(this._format('info', message, this.colors.info));
26
+ }
27
+
28
+ success(message) {
29
+ console.log(this._format('success', message, this.colors.success));
30
+ }
31
+
32
+ warn(message) {
33
+ console.warn(this._format('warn', message, this.colors.warn));
34
+ }
35
+
36
+ error(message) {
37
+ console.error(this._format('error', message, this.colors.error));
38
+ }
39
+ }
40
+
41
+ class YlsooCache {
42
+ constructor() {
43
+ this.store = new Map();
44
+ }
45
+
46
+ /**
47
+ * Set a key-value pair with an optional Time-To-Live
48
+ * @param {string} key
49
+ * @param {*} value
50
+ * @param {number} ttlMs - Time to live in milliseconds
51
+ */
52
+ set(key, value, ttlMs = 0) {
53
+ const expiresAt = ttlMs > 0 ? Date.now() + ttlMs : null;
54
+ this.store.set(key, { value, expiresAt });
55
+ }
56
+
57
+ get(key) {
58
+ const item = this.store.get(key);
59
+ if (!item) return null;
60
+
61
+ if (item.expiresAt && Date.now() > item.expiresAt) {
62
+ this.store.delete(key);
63
+ return null;
64
+ }
65
+ return item.value;
66
+ }
67
+
68
+ delete(key) {
69
+ return this.store.delete(key);
70
+ }
71
+
72
+ clear() {
73
+ this.store.clear();
74
+ }
75
+ }
76
+
77
+ class YlsooHttp {
78
+ /**
79
+ * Premium wrapper for fetch
80
+ * @param {string} endpoint
81
+ * @param {object} options
82
+ * @returns {Promise<any>}
83
+ */
84
+ async request(endpoint, options = {}) {
85
+ const defaultHeaders = {
86
+ 'Content-Type': 'application/json',
87
+ 'User-Agent': 'Ylsoo-Core/1.1'
88
+ };
89
+
90
+ const config = {
91
+ ...options,
92
+ headers: {
93
+ ...defaultHeaders,
94
+ ...options.headers
95
+ }
96
+ };
97
+
98
+ if (config.body && typeof config.body === 'object') {
99
+ config.body = JSON.stringify(config.body);
100
+ }
101
+
102
+ try {
103
+ const response = await fetch(endpoint, config);
104
+ const isJson = response.headers.get('content-type')?.includes('application/json');
105
+
106
+ const data = isJson ? await response.json() : await response.text();
107
+
108
+ if (!response.ok) {
109
+ throw {
110
+ status: response.status,
111
+ message: response.statusText,
112
+ data
113
+ };
114
+ }
115
+
116
+ return data;
117
+ } catch (error) {
118
+ throw new Error(`[Ylsoo HTTP Error]: ${error.message || JSON.stringify(error)}`);
119
+ }
120
+ }
121
+
122
+ get(endpoint, headers = {}) {
123
+ return this.request(endpoint, { method: 'GET', headers });
124
+ }
125
+
126
+ post(endpoint, body, headers = {}) {
127
+ return this.request(endpoint, { method: 'POST', body, headers });
128
+ }
129
+ }
130
+
5
131
  class YlsooCore {
6
132
  constructor() {
7
- this.version = '1.0.0';
133
+ this.version = '1.1.0';
134
+ this.logger = new YlsooLogger();
135
+ this.cache = new YlsooCache();
136
+ this.http = new YlsooHttp();
8
137
  }
9
138
 
10
139
  /**
11
- * Logs a message with prefix
12
- * @param {string} message - The message to log
140
+ * Sleep for a given amount of time (Promise based)
141
+ * @param {number} ms
13
142
  */
14
- log(message) {
15
- console.log(`[Ylsoo Core] ${message}`);
143
+ sleep(ms) {
144
+ return new Promise(resolve => setTimeout(resolve, ms));
16
145
  }
17
146
 
18
147
  /**
19
- * Simple utility to capitalize a string
20
- * @param {string} str - The string to capitalize
21
- * @returns {string} Capitalized string
148
+ * Deep clone an object natively
149
+ * @param {object} obj
22
150
  */
23
- capitalize(str) {
24
- if (typeof str !== 'string' || str.length === 0) {
25
- return str;
151
+ deepClone(obj) {
152
+ if (typeof structuredClone === 'function') {
153
+ return structuredClone(obj);
26
154
  }
27
- return str.charAt(0).toUpperCase() + str.slice(1);
155
+ return JSON.parse(JSON.stringify(obj));
156
+ }
157
+
158
+ /**
159
+ * Creates a debounced function that delays invoking func until after wait milliseconds have elapsed
160
+ * @param {Function} func
161
+ * @param {number} wait
162
+ */
163
+ debounce(func, wait) {
164
+ let timeout;
165
+ return function executedFunction(...args) {
166
+ const later = () => {
167
+ clearTimeout(timeout);
168
+ func(...args);
169
+ };
170
+ clearTimeout(timeout);
171
+ timeout = setTimeout(later, wait);
172
+ };
28
173
  }
29
174
 
30
175
  /**
31
- * Simple utility to check if value is empty
32
- * @param {*} value - The value to check
33
- * @returns {boolean} True if empty
176
+ * Simple utility to verify if a value is semantically empty
177
+ * @param {*} value
34
178
  */
35
179
  isEmpty(value) {
36
180
  if (value === null || value === undefined) return true;
37
181
  if (typeof value === 'string' || Array.isArray(value)) return value.length === 0;
38
- if (typeof value === 'object') return Object.keys(value).length === 0;
182
+ if (typeof value === 'object') {
183
+ if (value instanceof Map || value instanceof Set) return value.size === 0;
184
+ return Object.keys(value).length === 0;
185
+ }
39
186
  return false;
40
187
  }
188
+
189
+ /**
190
+ * Simple utility to capitalize a string
191
+ * @param {string} str - The string to capitalize
192
+ * @returns {string} Capitalized string
193
+ */
194
+ capitalize(str) {
195
+ if (typeof str !== 'string' || str.length === 0) {
196
+ return str;
197
+ }
198
+ return str.charAt(0).toUpperCase() + str.slice(1);
199
+ }
41
200
  }
42
201
 
43
202
  module.exports = new YlsooCore();
package/package.json CHANGED
@@ -1,17 +1,27 @@
1
1
  {
2
2
  "name": "@ylsoo/core",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Core utility functions for Ylsoo projects",
5
5
  "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "files": [
8
+ "index.js",
9
+ "index.d.ts",
10
+ "README.md"
11
+ ],
6
12
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
13
+ "test": "node test.js"
8
14
  },
9
15
  "keywords": [
10
16
  "ylsoo",
11
17
  "core",
12
- "utilities"
18
+ "utilities",
19
+ "sdk",
20
+ "fetch",
21
+ "logger",
22
+ "cache"
13
23
  ],
14
- "author": "",
15
- "license": "ISC",
24
+ "author": "Ylsoo",
25
+ "license": "MIT",
16
26
  "type": "commonjs"
17
27
  }