@hichchi/utils 0.0.2 โ†’ 0.0.4

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/index.cjs.js CHANGED
@@ -107,6 +107,69 @@ function isValidRedirectUrl(url, allowedDomains) {
107
107
  return false;
108
108
  }
109
109
  }
110
+ /**
111
+ * Extract a subdomain from an origin URL
112
+ *
113
+ * This utility function parses an origin URL and extracts the subdomain portion.
114
+ * It works by splitting the origin string using a regular expression that matches
115
+ * protocol prefixes (http://, https://), dots, and the provided domain name.
116
+ *
117
+ * The function handles special cases:
118
+ * - If the origin hostname has no domain name (single-label host) or is an IP address, it returns the provided fallback value
119
+ * - If no subdomain is found or the origin is undefined, it returns undefined
120
+ *
121
+ * This is particularly useful for multi-tenant applications where different
122
+ * subdomains represent different tenants or environments.
123
+ *
124
+ * @param {string} origin - The origin URL to extract the subdomain from
125
+ * (e.g., "https://admin.example.com")
126
+ * @param {string} [splitDomain] - The main domain to use as a reference for extraction
127
+ * (e.g., "example.com")
128
+ * @param {string} [ifLocalhost] - Fallback value to return when the origin hostname
129
+ * has no domain name or is an IP address (e.g., "local" or "development")
130
+ * @returns {string|undefined} The extracted subdomain if found, the ifLocalhost value
131
+ * for single-label/IP hostnames, or undefined if no subdomain exists
132
+ * or origin is undefined
133
+ *
134
+ * @example
135
+ * ```TypeScript
136
+ * extractSubdomain("example.com", "admin.example.com", "local")
137
+ * // Returns "admin"
138
+ * ```
139
+ *
140
+ * @example
141
+ * ```TypeScript
142
+ * extractSubdomain("example.com", "localhost:3000", "local")
143
+ * // Returns "local"
144
+ * ```
145
+ *
146
+ * @example
147
+ * ```TypeScript
148
+ * extractSubdomain("example.com", "example.com", "local")
149
+ * // Returns undefined (no subdomain)
150
+ * ```
151
+ */
152
+ function extractSubdomain(origin, splitDomain, ifLocalhost) {
153
+ if (!splitDomain) return undefined;
154
+ try {
155
+ const url = new URL(origin.startsWith("http") ? origin : `http://${origin}`);
156
+ const hostname = url.hostname;
157
+ const isIpv4 = /^(25[0-5]|2[0-4]\d|1?\d?\d)(\.(25[0-5]|2[0-4]\d|1?\d?\d)){3}$/.test(hostname);
158
+ const isIpv6 = hostname.includes(":");
159
+ if (!hostname.includes(".") || isIpv4 || isIpv6) {
160
+ return ifLocalhost;
161
+ }
162
+ if (hostname.endsWith(splitDomain)) {
163
+ const subdomainPart = hostname.slice(0, hostname.length - splitDomain.length);
164
+ // Remove trailing dot if present
165
+ const subdomain = subdomainPart.replace(/\.$/, "");
166
+ return subdomain || undefined;
167
+ }
168
+ return undefined;
169
+ } catch {
170
+ return undefined;
171
+ }
172
+ }
110
173
 
111
174
  /* eslint-disable @typescript-eslint/no-explicit-any */
112
175
  // noinspection JSUnusedGlobalSymbols
@@ -3593,6 +3656,7 @@ exports.deepCopy = deepCopy;
3593
3656
  exports.dottedPathObjectToNested = dottedPathObjectToNested;
3594
3657
  exports.escapeRegExp = escapeRegExp;
3595
3658
  exports.extractEmails = extractEmails;
3659
+ exports.extractSubdomain = extractSubdomain;
3596
3660
  exports.extractUrls = extractUrls;
3597
3661
  exports.filterByObject = filterByObject;
3598
3662
  exports.format = format;
package/index.esm.js CHANGED
@@ -105,6 +105,69 @@ function isValidRedirectUrl(url, allowedDomains) {
105
105
  return false;
106
106
  }
107
107
  }
108
+ /**
109
+ * Extract a subdomain from an origin URL
110
+ *
111
+ * This utility function parses an origin URL and extracts the subdomain portion.
112
+ * It works by splitting the origin string using a regular expression that matches
113
+ * protocol prefixes (http://, https://), dots, and the provided domain name.
114
+ *
115
+ * The function handles special cases:
116
+ * - If the origin hostname has no domain name (single-label host) or is an IP address, it returns the provided fallback value
117
+ * - If no subdomain is found or the origin is undefined, it returns undefined
118
+ *
119
+ * This is particularly useful for multi-tenant applications where different
120
+ * subdomains represent different tenants or environments.
121
+ *
122
+ * @param {string} origin - The origin URL to extract the subdomain from
123
+ * (e.g., "https://admin.example.com")
124
+ * @param {string} [splitDomain] - The main domain to use as a reference for extraction
125
+ * (e.g., "example.com")
126
+ * @param {string} [ifLocalhost] - Fallback value to return when the origin hostname
127
+ * has no domain name or is an IP address (e.g., "local" or "development")
128
+ * @returns {string|undefined} The extracted subdomain if found, the ifLocalhost value
129
+ * for single-label/IP hostnames, or undefined if no subdomain exists
130
+ * or origin is undefined
131
+ *
132
+ * @example
133
+ * ```TypeScript
134
+ * extractSubdomain("example.com", "admin.example.com", "local")
135
+ * // Returns "admin"
136
+ * ```
137
+ *
138
+ * @example
139
+ * ```TypeScript
140
+ * extractSubdomain("example.com", "localhost:3000", "local")
141
+ * // Returns "local"
142
+ * ```
143
+ *
144
+ * @example
145
+ * ```TypeScript
146
+ * extractSubdomain("example.com", "example.com", "local")
147
+ * // Returns undefined (no subdomain)
148
+ * ```
149
+ */
150
+ function extractSubdomain(origin, splitDomain, ifLocalhost) {
151
+ if (!splitDomain) return undefined;
152
+ try {
153
+ const url = new URL(origin.startsWith("http") ? origin : `http://${origin}`);
154
+ const hostname = url.hostname;
155
+ const isIpv4 = /^(25[0-5]|2[0-4]\d|1?\d?\d)(\.(25[0-5]|2[0-4]\d|1?\d?\d)){3}$/.test(hostname);
156
+ const isIpv6 = hostname.includes(":");
157
+ if (!hostname.includes(".") || isIpv4 || isIpv6) {
158
+ return ifLocalhost;
159
+ }
160
+ if (hostname.endsWith(splitDomain)) {
161
+ const subdomainPart = hostname.slice(0, hostname.length - splitDomain.length);
162
+ // Remove trailing dot if present
163
+ const subdomain = subdomainPart.replace(/\.$/, "");
164
+ return subdomain || undefined;
165
+ }
166
+ return undefined;
167
+ } catch {
168
+ return undefined;
169
+ }
170
+ }
108
171
 
109
172
  /* eslint-disable @typescript-eslint/no-explicit-any */
110
173
  // noinspection JSUnusedGlobalSymbols
@@ -3572,4 +3635,4 @@ function applyTemplates(str, prefixes) {
3572
3635
  return result;
3573
3636
  }
3574
3637
 
3575
- export { CHARACTERS_TO_REMOVE, CONTEXT_MULTIPLIER, DEFAULT_BREAK_CHAR, DEFAULT_CONTEXT_LENGTH, DEFAULT_ELLIPSIS, DEFAULT_LINE_LENGTH, EnglishInflectionRules, HEX_PADDING_CHAR, HEX_PADDING_LENGTH, HEX_RADIX, TemplateTag, applyTemplate, applyTemplates, breakToWords, countOccurrences, createExcerpt, deepCopy, dottedPathObjectToNested, escapeRegExp, extractEmails, extractUrls, filterByObject, format, getEnumValues, getFileExt, getFileSize, getMapKey, getMapKeys, getValueByPath, groupBy, hasOwnAll, hashString, htmlToText, isAlphanumeric, isArray, isObject, isObjectWith, isValidRedirectUrl, maskString, mimeTypes, normalizeString, objectToDottedPathValueObject, omit, padString, plural, prune, randomString, removeWhitespace, reverse, searchMapValues, singular, slugify, stringSimilarity, toCamelCase, toFirstCase, toFirstCaseBreak, toKebabCase, toLowerCase, toLowerCaseBreak, toNumber, toPascalCase, toProperTitleCase, toSentenceCase, toSnakeCase, toTitleCase, toUpperCase, toUpperCaseBreak, toVariableName, truncate, wordWrap };
3638
+ export { CHARACTERS_TO_REMOVE, CONTEXT_MULTIPLIER, DEFAULT_BREAK_CHAR, DEFAULT_CONTEXT_LENGTH, DEFAULT_ELLIPSIS, DEFAULT_LINE_LENGTH, EnglishInflectionRules, HEX_PADDING_CHAR, HEX_PADDING_LENGTH, HEX_RADIX, TemplateTag, applyTemplate, applyTemplates, breakToWords, countOccurrences, createExcerpt, deepCopy, dottedPathObjectToNested, escapeRegExp, extractEmails, extractSubdomain, extractUrls, filterByObject, format, getEnumValues, getFileExt, getFileSize, getMapKey, getMapKeys, getValueByPath, groupBy, hasOwnAll, hashString, htmlToText, isAlphanumeric, isArray, isObject, isObjectWith, isValidRedirectUrl, maskString, mimeTypes, normalizeString, objectToDottedPathValueObject, omit, padString, plural, prune, randomString, removeWhitespace, reverse, searchMapValues, singular, slugify, stringSimilarity, toCamelCase, toFirstCase, toFirstCaseBreak, toKebabCase, toLowerCase, toLowerCaseBreak, toNumber, toPascalCase, toProperTitleCase, toSentenceCase, toSnakeCase, toTitleCase, toUpperCase, toUpperCaseBreak, toVariableName, truncate, wordWrap };
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@hichchi/utils",
3
- "version": "0.0.2",
3
+ "description": "A comprehensive utility library with essential helper functions for JavaScript/TypeScript applications",
4
+ "version": "0.0.4",
4
5
  "publishConfig": {
5
6
  "access": "public"
6
7
  },
package/readme-top.md ADDED
@@ -0,0 +1,190 @@
1
+
2
+ <!--suppress ALL -->
3
+ <div align="center">
4
+
5
+ # ๐Ÿ› ๏ธ @hichchi/utils
6
+
7
+ ## Description
8
+
9
+ **A comprehensive utility library with essential helper functions for JavaScript/TypeScript applications**
10
+
11
+ [![npm version](https://img.shields.io/npm/v/@hichchi/utils?style=flat&color=blue)](https://www.npmjs.com/package/@hichchi/utils)
12
+ [![npm downloads](https://img.shields.io/npm/dm/@hichchi/utils?style=flat&color=green)](https://www.npmjs.com/package/@hichchi/utils)
13
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/hichchidev/hichchi/blob/main/LICENSE)
14
+ [![TypeScript](https://img.shields.io/badge/typescript-5.0+-blue.svg)](https://www.typescriptlang.org/)
15
+
16
+ *Part of the [Hichchi](https://github.com/hichchidev/hichchi) ecosystem - A powerful, scalable application built with Nx workspace*
17
+
18
+ [๐Ÿ“š Jump to Documentation](#-api-documentation)
19
+
20
+ </div>
21
+
22
+ ---
23
+
24
+ ## ๐Ÿ“‹ Table Of Contents
25
+
26
+ - [๐Ÿ“ฆ Installation](#-installation)
27
+ - [โšก Quick Start](#-quick-start)
28
+ - [๐Ÿ“‹ Prerequisites](#-prerequisites)
29
+ - [๐ŸŒŸ Overview](#-overview)
30
+ - [โœจ Features](#-features)
31
+ - [๐Ÿ› ๏ธ Troubleshooting](#๏ธ-troubleshooting)
32
+ - [๐Ÿ”ง Development](#-development)
33
+ - [๐Ÿ“– API Documentation](#-api-documentation)
34
+
35
+ ---
36
+
37
+ ## ๐Ÿ“ฆ Installation
38
+
39
+ ```bash
40
+ npm install @hichchi/utils
41
+ ```
42
+
43
+ ## โšก Quick Start
44
+
45
+ Get up and running with powerful utility functions in just a few minutes:
46
+
47
+ ```typescript
48
+ // 1. Install the package
49
+ npm install @hichchi/utils
50
+
51
+ // 2. Import utility functions
52
+ import { dottedPathObjectToNested } from '@hichchi/utils';
53
+
54
+ // 3. Use utility functions in your application
55
+ const nestedObject = dottedPathObjectToNested({
56
+ "id": 123,
57
+ "name": "John Doe",
58
+ "profile.age": 30,
59
+ "profile.address.city": "New York",
60
+ "profile.address.zip": "10001"
61
+ });
62
+
63
+ Result:
64
+ // {
65
+ // id: 123,
66
+ // name: "John Doe",
67
+ // profile: {
68
+ // age: 30,
69
+ // address: {
70
+ // city: "New York",
71
+ // zip: "10001"
72
+ // }
73
+ // }
74
+ // }
75
+
76
+ ```
77
+
78
+ ## ๐Ÿ“‹ Prerequisites
79
+
80
+ Before installing @hichchi/utils, ensure you have:
81
+
82
+ ### Required Dependencies
83
+ - **Node.js**: ^18.0.0
84
+ - **TypeScript**: ^5.0.0 (for TypeScript projects)
85
+
86
+
87
+ ## ๐ŸŒŸ Overview
88
+
89
+ ๐ŸŽฏ **Your essential toolkit** for JavaScript and TypeScript development. From string manipulation to file operations, from object utilities to URL handling - everything you need to streamline your development workflow with battle-tested, performance-optimized utility functions.
90
+
91
+ ## โœจ Features
92
+
93
+ ### ๐Ÿ”ค String Utilities
94
+ - ๐ŸŽจ **Case Conversion** - camelCase, PascalCase, snake_case, kebab-case transformations
95
+ - ๐Ÿ” **String Analysis** - Pattern matching, validation, and content analysis
96
+ - โœ‚๏ธ **String Manipulation** - Truncation, padding, cleaning, and formatting
97
+ - ๐Ÿท๏ธ **Slug Generation** - URL-friendly string generation with customizable options
98
+ - ๐Ÿ“ **Template Processing** - Advanced string templating with variable substitution
99
+
100
+ ### ๐Ÿ—‚๏ธ Object Utilities
101
+ - ๐Ÿ”„ **Deep Operations** - Deep merge, clone, compare, and transformation
102
+ - ๐ŸŽฏ **Property Access** - Safe property getting/setting with dot notation
103
+ - ๐Ÿ” **Object Analysis** - Type checking, validation, and structure analysis
104
+ - ๐Ÿ“Š **Data Transformation** - Object flattening, nesting, and restructuring
105
+ - ๐Ÿงน **Object Cleaning** - Remove null/undefined values, empty objects
106
+
107
+ ### ๐Ÿ“ String Template Utilities
108
+ - ๐ŸŽจ **Template Processing** - Advanced string templating with variable substitution
109
+ - ๐Ÿ”„ **Dynamic Content** - Apply templates with custom prefixes and transformations
110
+ - ๐Ÿท๏ธ **Tag-based Processing** - Support for various template tags and formats
111
+
112
+ ### ๐ŸŒ URL Utilities
113
+ - ๐Ÿ”’ **Redirect Validation** - Secure redirect URL validation against allowed domains
114
+ - ๐Ÿ›ก๏ธ **Security Features** - Prevent open redirect vulnerabilities
115
+
116
+ ### โœ… Assertion Utilities
117
+ - ๐Ÿ” **Type Guards** - TypeScript type guards for arrays and objects
118
+ - ๐ŸŽฏ **Property Checking** - Check if objects have specific properties
119
+ - ๐Ÿ›ก๏ธ **Safe Type Narrowing** - Runtime type validation with TypeScript support
120
+
121
+ ### ๐ŸŽจ Developer Experience
122
+ - ๐Ÿ“ **Full TypeScript Support** - Complete type definitions and IntelliSense
123
+ - ๐Ÿ”ง **Tree Shaking** - Import only what you need for optimal bundle size
124
+ - ๐Ÿ“š **Comprehensive Documentation** - Detailed JSDoc comments for all functions
125
+ - ๐Ÿงช **Well Tested** - Extensive test coverage for reliability
126
+ - ๐Ÿš€ **Performance Optimized** - Efficient algorithms and minimal overhead
127
+
128
+ ## ๐Ÿ”’ Security Best Practices
129
+
130
+ Detailed security best practices will be added here
131
+
132
+ ## ๐Ÿ› ๏ธ Troubleshooting
133
+
134
+ ### Common Issues
135
+
136
+ #### TypeScript Compilation Issues
137
+ ```bash
138
+ # Ensure you have the correct TypeScript version
139
+ npm install typescript@^5.0.0 --save-dev
140
+ ```
141
+
142
+ ## ๐Ÿ”ง Development
143
+
144
+ ### Building the Library
145
+ ```bash
146
+ nx build utils
147
+ ```
148
+
149
+ ### Running Tests
150
+ ```bash
151
+ nx test utils
152
+ ```
153
+
154
+ ### Linting
155
+ ```bash
156
+ nx lint utils
157
+ ```
158
+
159
+ ### Performance Testing
160
+ ```bash
161
+ nx run utils:benchmark
162
+ ```
163
+
164
+ ---
165
+
166
+ <div align="center">
167
+
168
+ **Made with โค๏ธ by [Hichchi Dev](https://github.com/hichchidev)**
169
+
170
+ [![Hichchi Ecosystem](https://img.shields.io/badge/๐Ÿ _Hichchi_Ecosystem-blue)](https://github.com/hichchidev/hichchi)
171
+ [![Report Bug](https://img.shields.io/badge/๐Ÿ›_Report_Bug-red)](https://github.com/hichchidev/hichchi/issues)
172
+ [![Request Feature](https://img.shields.io/badge/โœจ_Request_Feature-green)](https://github.com/hichchidev/hichchi/issues)
173
+
174
+ *Building the future of development utilities, one commit at a time*
175
+
176
+ </div>
177
+
178
+ ---
179
+
180
+ # ๐Ÿ“– API Documentation
181
+
182
+ Complete technical reference for all classes, interfaces, methods, and types in this library.
183
+
184
+ **Auto-generated by TypeDoc** - Browse through detailed API references, code examples, and implementation guides below.
185
+
186
+ <!-- TypeDoc generated documentation will be appended below this point -->
187
+
188
+ ---
189
+
190
+ ## ๐Ÿ“‹ API Table of Contents
@@ -97,3 +97,46 @@
97
97
  * @throws {never} This function catches all URL parsing errors and returns false instead of throwing
98
98
  */
99
99
  export declare function isValidRedirectUrl(url: string, allowedDomains: string[]): boolean;
100
+ /**
101
+ * Extract a subdomain from an origin URL
102
+ *
103
+ * This utility function parses an origin URL and extracts the subdomain portion.
104
+ * It works by splitting the origin string using a regular expression that matches
105
+ * protocol prefixes (http://, https://), dots, and the provided domain name.
106
+ *
107
+ * The function handles special cases:
108
+ * - If the origin hostname has no domain name (single-label host) or is an IP address, it returns the provided fallback value
109
+ * - If no subdomain is found or the origin is undefined, it returns undefined
110
+ *
111
+ * This is particularly useful for multi-tenant applications where different
112
+ * subdomains represent different tenants or environments.
113
+ *
114
+ * @param {string} origin - The origin URL to extract the subdomain from
115
+ * (e.g., "https://admin.example.com")
116
+ * @param {string} [splitDomain] - The main domain to use as a reference for extraction
117
+ * (e.g., "example.com")
118
+ * @param {string} [ifLocalhost] - Fallback value to return when the origin hostname
119
+ * has no domain name or is an IP address (e.g., "local" or "development")
120
+ * @returns {string|undefined} The extracted subdomain if found, the ifLocalhost value
121
+ * for single-label/IP hostnames, or undefined if no subdomain exists
122
+ * or origin is undefined
123
+ *
124
+ * @example
125
+ * ```TypeScript
126
+ * extractSubdomain("example.com", "admin.example.com", "local")
127
+ * // Returns "admin"
128
+ * ```
129
+ *
130
+ * @example
131
+ * ```TypeScript
132
+ * extractSubdomain("example.com", "localhost:3000", "local")
133
+ * // Returns "local"
134
+ * ```
135
+ *
136
+ * @example
137
+ * ```TypeScript
138
+ * extractSubdomain("example.com", "example.com", "local")
139
+ * // Returns undefined (no subdomain)
140
+ * ```
141
+ */
142
+ export declare function extractSubdomain(origin: string, splitDomain?: string, ifLocalhost?: string): string | undefined;