@hichchi/utils 0.0.1-beta.2 → 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/index.cjs.default.js +1 -0
- package/index.cjs.js +3645 -0
- package/index.cjs.mjs +2 -0
- package/index.d.ts +1 -5
- package/index.esm.js +3575 -0
- package/package.json +13 -8
- package/src/index.d.ts +5 -0
- package/{interfaces → src/interfaces}/infinite-object.interface.d.ts +3 -3
- package/{interfaces → src/interfaces}/path-value-set.interface.d.ts +12 -12
- package/{types → src/types}/deep-partial.type.d.ts +6 -0
- package/{types → src/types}/literal-object.type.d.ts +2 -2
- package/{utils → src/utils}/index.d.ts +1 -0
- package/{utils → src/utils}/object.utils.d.ts +23 -19
- package/CHANGELOG.md +0 -19
- package/README.md +0 -7944
- package/constants/constants.js +0 -227
- package/constants/constants.js.map +0 -1
- package/constants/english-inflection-rules.js +0 -360
- package/constants/english-inflection-rules.js.map +0 -1
- package/constants/index.js +0 -6
- package/constants/index.js.map +0 -1
- package/enums/index.js +0 -5
- package/enums/index.js.map +0 -1
- package/enums/template-tag.enum.js +0 -39
- package/enums/template-tag.enum.js.map +0 -1
- package/index.js +0 -9
- package/index.js.map +0 -1
- package/interfaces/index.js +0 -7
- package/interfaces/index.js.map +0 -1
- package/interfaces/infinite-object.interface.js +0 -3
- package/interfaces/infinite-object.interface.js.map +0 -1
- package/interfaces/inflection-rule.interfaces.js +0 -3
- package/interfaces/inflection-rule.interfaces.js.map +0 -1
- package/interfaces/path-value-set.interface.js +0 -3
- package/interfaces/path-value-set.interface.js.map +0 -1
- package/readme-top.md +0 -187
- package/types/deep-partial.type.js +0 -3
- package/types/deep-partial.type.js.map +0 -1
- package/types/index.js +0 -38
- package/types/index.js.map +0 -1
- package/types/is-already-in-path.type.js +0 -3
- package/types/is-already-in-path.type.js.map +0 -1
- package/types/is-empty.type.js +0 -3
- package/types/is-empty.type.js.map +0 -1
- package/types/is-primitive.type.js +0 -4
- package/types/is-primitive.type.js.map +0 -1
- package/types/literal-object.type.js +0 -3
- package/types/literal-object.type.js.map +0 -1
- package/types/loose-autocomplete.type.js +0 -3
- package/types/loose-autocomplete.type.js.map +0 -1
- package/types/partial-with-null.type.js +0 -3
- package/types/partial-with-null.type.js.map +0 -1
- package/types/prettify.type.js +0 -3
- package/types/prettify.type.js.map +0 -1
- package/types/type.type.js +0 -3
- package/types/type.type.js.map +0 -1
- package/utils/assertions.utils.js +0 -163
- package/utils/assertions.utils.js.map +0 -1
- package/utils/file.utils.js +0 -1315
- package/utils/file.utils.js.map +0 -1
- package/utils/index.js +0 -9
- package/utils/index.js.map +0 -1
- package/utils/object.utils.js +0 -1069
- package/utils/object.utils.js.map +0 -1
- package/utils/string-template.utils.js +0 -269
- package/utils/string-template.utils.js.map +0 -1
- package/utils/string.utils.js +0 -1255
- package/utils/string.utils.js.map +0 -1
- package/utils/url.utils.js +0 -112
- package/utils/url.utils.js.map +0 -1
- /package/{constants → src/constants}/constants.d.ts +0 -0
- /package/{constants → src/constants}/english-inflection-rules.d.ts +0 -0
- /package/{constants → src/constants}/index.d.ts +0 -0
- /package/{enums → src/enums}/index.d.ts +0 -0
- /package/{enums → src/enums}/template-tag.enum.d.ts +0 -0
- /package/{interfaces → src/interfaces}/index.d.ts +0 -0
- /package/{interfaces → src/interfaces}/inflection-rule.interfaces.d.ts +0 -0
- /package/{types → src/types}/index.d.ts +0 -0
- /package/{types → src/types}/is-already-in-path.type.d.ts +0 -0
- /package/{types → src/types}/is-empty.type.d.ts +0 -0
- /package/{types → src/types}/is-primitive.type.d.ts +0 -0
- /package/{types → src/types}/loose-autocomplete.type.d.ts +0 -0
- /package/{types → src/types}/partial-with-null.type.d.ts +0 -0
- /package/{types → src/types}/prettify.type.d.ts +0 -0
- /package/{types → src/types}/type.type.d.ts +0 -0
- /package/{utils → src/utils}/assertions.utils.d.ts +0 -0
- /package/{utils → src/utils}/file.utils.d.ts +0 -0
- /package/{utils → src/utils}/string-template.utils.d.ts +0 -0
- /package/{utils → src/utils}/string.utils.d.ts +0 -0
- /package/{utils → src/utils}/url.utils.d.ts +0 -0
package/index.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
tslib_1.__exportStar(require("./utils"), exports);
|
|
5
|
-
tslib_1.__exportStar(require("./interfaces"), exports);
|
|
6
|
-
tslib_1.__exportStar(require("./types"), exports);
|
|
7
|
-
tslib_1.__exportStar(require("./enums"), exports);
|
|
8
|
-
tslib_1.__exportStar(require("./constants"), exports);
|
|
9
|
-
//# sourceMappingURL=index.js.map
|
package/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../libs/utils/src/index.ts"],"names":[],"mappings":";;;AAAA,kDAAwB;AACxB,uDAA6B;AAC7B,kDAAwB;AACxB,kDAAwB;AACxB,sDAA4B"}
|
package/interfaces/index.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
tslib_1.__exportStar(require("./path-value-set.interface"), exports);
|
|
5
|
-
tslib_1.__exportStar(require("./infinite-object.interface"), exports);
|
|
6
|
-
tslib_1.__exportStar(require("./inflection-rule.interfaces"), exports);
|
|
7
|
-
//# sourceMappingURL=index.js.map
|
package/interfaces/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../libs/utils/src/interfaces/index.ts"],"names":[],"mappings":";;;AAAA,qEAA2C;AAC3C,sEAA4C;AAC5C,uEAA6C"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"infinite-object.interface.js","sourceRoot":"","sources":["../../../../libs/utils/src/interfaces/infinite-object.interface.ts"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"inflection-rule.interfaces.js","sourceRoot":"","sources":["../../../../libs/utils/src/interfaces/inflection-rule.interfaces.ts"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"path-value-set.interface.js","sourceRoot":"","sources":["../../../../libs/utils/src/interfaces/path-value-set.interface.ts"],"names":[],"mappings":""}
|
package/readme-top.md
DELETED
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
<!--suppress ALL -->
|
|
2
|
-
<div align="center">
|
|
3
|
-
|
|
4
|
-
# 🛠️ @hichchi/utils
|
|
5
|
-
|
|
6
|
-
**A comprehensive utility library with essential helper functions for JavaScript/TypeScript applications**
|
|
7
|
-
|
|
8
|
-
[](https://www.npmjs.com/package/@hichchi/utils)
|
|
9
|
-
[](https://www.npmjs.com/package/@hichchi/utils)
|
|
10
|
-
[](https://github.com/hichchidev/hichchi/blob/main/LICENSE)
|
|
11
|
-
[](https://www.typescriptlang.org/)
|
|
12
|
-
|
|
13
|
-
*Part of the [Hichchi](https://github.com/hichchidev/hichchi) ecosystem - A powerful, scalable application built with Nx workspace*
|
|
14
|
-
|
|
15
|
-
[📚 Jump to Documentation](#-api-documentation)
|
|
16
|
-
|
|
17
|
-
</div>
|
|
18
|
-
|
|
19
|
-
---
|
|
20
|
-
|
|
21
|
-
## 📋 Table Of Contents
|
|
22
|
-
|
|
23
|
-
- [📦 Installation](#-installation)
|
|
24
|
-
- [⚡ Quick Start](#-quick-start)
|
|
25
|
-
- [📋 Prerequisites](#-prerequisites)
|
|
26
|
-
- [🌟 Overview](#-overview)
|
|
27
|
-
- [✨ Features](#-features)
|
|
28
|
-
- [🛠️ Troubleshooting](#️-troubleshooting)
|
|
29
|
-
- [🔧 Development](#-development)
|
|
30
|
-
- [📖 API Documentation](#-api-documentation)
|
|
31
|
-
|
|
32
|
-
---
|
|
33
|
-
|
|
34
|
-
## 📦 Installation
|
|
35
|
-
|
|
36
|
-
```bash
|
|
37
|
-
npm install @hichchi/utils
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
## ⚡ Quick Start
|
|
41
|
-
|
|
42
|
-
Get up and running with powerful utility functions in just a few minutes:
|
|
43
|
-
|
|
44
|
-
```typescript
|
|
45
|
-
// 1. Install the package
|
|
46
|
-
npm install @hichchi/utils
|
|
47
|
-
|
|
48
|
-
// 2. Import utility functions
|
|
49
|
-
import { pathValueSetToObject } from '@hichchi/utils';
|
|
50
|
-
|
|
51
|
-
// 3. Use utility functions in your application
|
|
52
|
-
const nestedObject = pathValueSetToObject({
|
|
53
|
-
"id": 123,
|
|
54
|
-
"name": "John Doe",
|
|
55
|
-
"profile.age": 30,
|
|
56
|
-
"profile.address.city": "New York",
|
|
57
|
-
"profile.address.zip": "10001"
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
Result:
|
|
61
|
-
// {
|
|
62
|
-
// id: 123,
|
|
63
|
-
// name: "John Doe",
|
|
64
|
-
// profile: {
|
|
65
|
-
// age: 30,
|
|
66
|
-
// address: {
|
|
67
|
-
// city: "New York",
|
|
68
|
-
// zip: "10001"
|
|
69
|
-
// }
|
|
70
|
-
// }
|
|
71
|
-
// }
|
|
72
|
-
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
## 📋 Prerequisites
|
|
76
|
-
|
|
77
|
-
Before installing @hichchi/utils, ensure you have:
|
|
78
|
-
|
|
79
|
-
### Required Dependencies
|
|
80
|
-
- **Node.js**: >= 18.0.0
|
|
81
|
-
- **TypeScript**: >= 5.0.0 (for TypeScript projects)
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
## 🌟 Overview
|
|
85
|
-
|
|
86
|
-
🎯 **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.
|
|
87
|
-
|
|
88
|
-
## ✨ Features
|
|
89
|
-
|
|
90
|
-
### 🔤 String Utilities
|
|
91
|
-
- 🎨 **Case Conversion** - camelCase, PascalCase, snake_case, kebab-case transformations
|
|
92
|
-
- 🔍 **String Analysis** - Pattern matching, validation, and content analysis
|
|
93
|
-
- ✂️ **String Manipulation** - Truncation, padding, cleaning, and formatting
|
|
94
|
-
- 🏷️ **Slug Generation** - URL-friendly string generation with customizable options
|
|
95
|
-
- 📝 **Template Processing** - Advanced string templating with variable substitution
|
|
96
|
-
|
|
97
|
-
### 🗂️ Object Utilities
|
|
98
|
-
- 🔄 **Deep Operations** - Deep merge, clone, compare, and transformation
|
|
99
|
-
- 🎯 **Property Access** - Safe property getting/setting with dot notation
|
|
100
|
-
- 🔍 **Object Analysis** - Type checking, validation, and structure analysis
|
|
101
|
-
- 📊 **Data Transformation** - Object flattening, nesting, and restructuring
|
|
102
|
-
- 🧹 **Object Cleaning** - Remove null/undefined values, empty objects
|
|
103
|
-
|
|
104
|
-
### 📝 String Template Utilities
|
|
105
|
-
- 🎨 **Template Processing** - Advanced string templating with variable substitution
|
|
106
|
-
- 🔄 **Dynamic Content** - Apply templates with custom prefixes and transformations
|
|
107
|
-
- 🏷️ **Tag-based Processing** - Support for various template tags and formats
|
|
108
|
-
|
|
109
|
-
### 🌐 URL Utilities
|
|
110
|
-
- 🔒 **Redirect Validation** - Secure redirect URL validation against allowed domains
|
|
111
|
-
- 🛡️ **Security Features** - Prevent open redirect vulnerabilities
|
|
112
|
-
|
|
113
|
-
### ✅ Assertion Utilities
|
|
114
|
-
- 🔍 **Type Guards** - TypeScript type guards for arrays and objects
|
|
115
|
-
- 🎯 **Property Checking** - Check if objects have specific properties
|
|
116
|
-
- 🛡️ **Safe Type Narrowing** - Runtime type validation with TypeScript support
|
|
117
|
-
|
|
118
|
-
### 🎨 Developer Experience
|
|
119
|
-
- 📝 **Full TypeScript Support** - Complete type definitions and IntelliSense
|
|
120
|
-
- 🔧 **Tree Shaking** - Import only what you need for optimal bundle size
|
|
121
|
-
- 📚 **Comprehensive Documentation** - Detailed JSDoc comments for all functions
|
|
122
|
-
- 🧪 **Well Tested** - Extensive test coverage for reliability
|
|
123
|
-
- 🚀 **Performance Optimized** - Efficient algorithms and minimal overhead
|
|
124
|
-
|
|
125
|
-
## 🔒 Security Best Practices
|
|
126
|
-
|
|
127
|
-
Detailed security best practices will be added here
|
|
128
|
-
|
|
129
|
-
## 🛠️ Troubleshooting
|
|
130
|
-
|
|
131
|
-
### Common Issues
|
|
132
|
-
|
|
133
|
-
#### TypeScript Compilation Issues
|
|
134
|
-
```bash
|
|
135
|
-
# Ensure you have the correct TypeScript version
|
|
136
|
-
npm install typescript@^5.0.0 --save-dev
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
## 🔧 Development
|
|
140
|
-
|
|
141
|
-
### Building the Library
|
|
142
|
-
```bash
|
|
143
|
-
nx build utils
|
|
144
|
-
```
|
|
145
|
-
|
|
146
|
-
### Running Tests
|
|
147
|
-
```bash
|
|
148
|
-
nx test utils
|
|
149
|
-
```
|
|
150
|
-
|
|
151
|
-
### Linting
|
|
152
|
-
```bash
|
|
153
|
-
nx lint utils
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
### Performance Testing
|
|
157
|
-
```bash
|
|
158
|
-
nx run utils:benchmark
|
|
159
|
-
```
|
|
160
|
-
|
|
161
|
-
---
|
|
162
|
-
|
|
163
|
-
<div align="center">
|
|
164
|
-
|
|
165
|
-
**Made with ❤️ by [Hichchi Dev](https://github.com/hichchidev)**
|
|
166
|
-
|
|
167
|
-
[](https://github.com/hichchidev/hichchi)
|
|
168
|
-
[](https://github.com/hichchidev/hichchi/issues)
|
|
169
|
-
[](https://github.com/hichchidev/hichchi/issues)
|
|
170
|
-
|
|
171
|
-
*Building the future of development utilities, one commit at a time*
|
|
172
|
-
|
|
173
|
-
</div>
|
|
174
|
-
|
|
175
|
-
---
|
|
176
|
-
|
|
177
|
-
# 📖 API Documentation
|
|
178
|
-
|
|
179
|
-
Complete technical reference for all classes, interfaces, methods, and types in this library.
|
|
180
|
-
|
|
181
|
-
**Auto-generated by TypeDoc** - Browse through detailed API references, code examples, and implementation guides below.
|
|
182
|
-
|
|
183
|
-
<!-- TypeDoc generated documentation will be appended below this point -->
|
|
184
|
-
|
|
185
|
-
---
|
|
186
|
-
|
|
187
|
-
## 📋 API Table of Contents
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"deep-partial.type.js","sourceRoot":"","sources":["../../../../libs/utils/src/types/deep-partial.type.ts"],"names":[],"mappings":""}
|
package/types/index.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
/**
|
|
5
|
-
* Utility types for TypeScript development
|
|
6
|
-
*
|
|
7
|
-
* This module exports a collection of utility types that enhance TypeScript's
|
|
8
|
-
* type system with additional functionality. These types help with common
|
|
9
|
-
* type manipulation tasks and provide solutions for advanced typing scenarios.
|
|
10
|
-
*
|
|
11
|
-
* Key categories of types included:
|
|
12
|
-
* - Object type utilities (DeepPartial, PartialWithNull, LiteralObject)
|
|
13
|
-
* - Type predicates (IsEmpty, IsPrimitive, IsAlreadyInPath)
|
|
14
|
-
* - Type transformation utilities (Prettify, LooseAutocomplete)
|
|
15
|
-
* - Constructor and class utilities (Type)
|
|
16
|
-
*
|
|
17
|
-
* Import these types individually or from this barrel file to enhance
|
|
18
|
-
* your TypeScript code with more expressive and precise type definitions.
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* ```typescript
|
|
22
|
-
* // Import all types
|
|
23
|
-
* import * as Types from '@hichchi/utils/types';
|
|
24
|
-
*
|
|
25
|
-
* // Or import specific types
|
|
26
|
-
* import { DeepPartial, LiteralObject } from '@hichchi/utils/types';
|
|
27
|
-
* ```
|
|
28
|
-
*/
|
|
29
|
-
tslib_1.__exportStar(require("./type.type"), exports);
|
|
30
|
-
tslib_1.__exportStar(require("./is-empty.type"), exports);
|
|
31
|
-
tslib_1.__exportStar(require("./prettify.type"), exports);
|
|
32
|
-
tslib_1.__exportStar(require("./deep-partial.type"), exports);
|
|
33
|
-
tslib_1.__exportStar(require("./is-primitive.type"), exports);
|
|
34
|
-
tslib_1.__exportStar(require("./literal-object.type"), exports);
|
|
35
|
-
tslib_1.__exportStar(require("./partial-with-null.type"), exports);
|
|
36
|
-
tslib_1.__exportStar(require("./is-already-in-path.type"), exports);
|
|
37
|
-
tslib_1.__exportStar(require("./loose-autocomplete.type"), exports);
|
|
38
|
-
//# sourceMappingURL=index.js.map
|
package/types/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../libs/utils/src/types/index.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,sDAA4B;AAC5B,0DAAgC;AAChC,0DAAgC;AAChC,8DAAoC;AACpC,8DAAoC;AACpC,gEAAsC;AACtC,mEAAyC;AACzC,oEAA0C;AAC1C,oEAA0C"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"is-already-in-path.type.js","sourceRoot":"","sources":["../../../../libs/utils/src/types/is-already-in-path.type.ts"],"names":[],"mappings":""}
|
package/types/is-empty.type.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"is-empty.type.js","sourceRoot":"","sources":["../../../../libs/utils/src/types/is-empty.type.ts"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"is-primitive.type.js","sourceRoot":"","sources":["../../../../libs/utils/src/types/is-primitive.type.ts"],"names":[],"mappings":";AAAA,qCAAqC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"literal-object.type.js","sourceRoot":"","sources":["../../../../libs/utils/src/types/literal-object.type.ts"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"loose-autocomplete.type.js","sourceRoot":"","sources":["../../../../libs/utils/src/types/loose-autocomplete.type.ts"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"partial-with-null.type.js","sourceRoot":"","sources":["../../../../libs/utils/src/types/partial-with-null.type.ts"],"names":[],"mappings":""}
|
package/types/prettify.type.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"prettify.type.js","sourceRoot":"","sources":["../../../../libs/utils/src/types/prettify.type.ts"],"names":[],"mappings":""}
|
package/types/type.type.js
DELETED
package/types/type.type.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"type.type.js","sourceRoot":"","sources":["../../../../libs/utils/src/types/type.type.ts"],"names":[],"mappings":""}
|
|
@@ -1,163 +0,0 @@
|
|
|
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
|
-
* Type-safe utility to check if a value is an array of a specific type.
|
|
9
|
-
*
|
|
10
|
-
* This function acts as a type guard that not only checks if the provided value
|
|
11
|
-
* is an array (using the native Array.isArray method), but also narrows the TypeScript
|
|
12
|
-
* type to an array of the generic type T. This enables safer handling of potentially
|
|
13
|
-
* array-typed values with full type inference in the conditional branches.
|
|
14
|
-
*
|
|
15
|
-
* @template T The expected element type of the array
|
|
16
|
-
* @param {T | T[] | undefined} value The value to check, which could be a single item,
|
|
17
|
-
* an array of items, or undefined
|
|
18
|
-
* @returns {value is T[]} Type predicate that narrows the type to T[] when true
|
|
19
|
-
*
|
|
20
|
-
* @remarks
|
|
21
|
-
* While this function essentially wraps Array.isArray, its value comes from the
|
|
22
|
-
* TypeScript type narrowing it provides, making it especially useful in code that
|
|
23
|
-
* needs to handle both single items and collections of items with type safety.
|
|
24
|
-
*
|
|
25
|
-
* The function properly handles undefined values by returning false, making it safe
|
|
26
|
-
* to use with optional parameters or potentially undefined values.
|
|
27
|
-
*
|
|
28
|
-
* @example
|
|
29
|
-
* ```typescript
|
|
30
|
-
* // Function that can accept either a single user or multiple users
|
|
31
|
-
* async function createUser(userOrUsers: UserDto | UserDto[] | undefined): Promise<User | User[]> {
|
|
32
|
-
* // TypeScript knows userOrUsers is UserDto[] in this branch
|
|
33
|
-
* if (isArray<UserDto>(userOrUsers)) {
|
|
34
|
-
* return Promise.all(userOrUsers.map(user => userService.createUser(user)));
|
|
35
|
-
* }
|
|
36
|
-
* // TypeScript knows userOrUsers is UserDto or undefined in this branch
|
|
37
|
-
* else if (userOrUsers) {
|
|
38
|
-
* return userService.createUser(userOrUsers);
|
|
39
|
-
* }
|
|
40
|
-
* else {
|
|
41
|
-
* throw new BadRequestException('User data is required');
|
|
42
|
-
* }
|
|
43
|
-
* }
|
|
44
|
-
* ```
|
|
45
|
-
*/
|
|
46
|
-
function isArray(value) {
|
|
47
|
-
return Array.isArray(value);
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Type-safe utility to check if a value is a non-array object of a specific type.
|
|
51
|
-
*
|
|
52
|
-
* This function serves as a type guard that checks if the provided value is an object
|
|
53
|
-
* (but not an array) and narrows the TypeScript type to the generic type T. It properly
|
|
54
|
-
* excludes null, arrays, and primitive values, focusing only on object instances.
|
|
55
|
-
*
|
|
56
|
-
* @template T The expected object type to narrow to when the check passes
|
|
57
|
-
* @param {T | T[] | undefined} [value] The value to check, which could be a single object,
|
|
58
|
-
* an array of objects, or undefined
|
|
59
|
-
* @returns {value is T} Type predicate that narrows the type to T when true
|
|
60
|
-
*
|
|
61
|
-
* @remarks
|
|
62
|
-
* This function performs three checks to ensure the value is a proper object:
|
|
63
|
-
* 1. It's not an array (using Array.isArray)
|
|
64
|
-
* 2. It's of type "object" according to JavaScript's typeof operator
|
|
65
|
-
* 3. It's not null (which would pass the typeof check but isn't a valid object)
|
|
66
|
-
*
|
|
67
|
-
* This is particularly useful when handling parameters that could be either an object
|
|
68
|
-
* or a simpler identifier (like an ID), allowing for type-safe conditional logic.
|
|
69
|
-
*
|
|
70
|
-
* @example
|
|
71
|
-
* ```typescript
|
|
72
|
-
* // Function that can accept either a user ID or a user object
|
|
73
|
-
* async function getUserInfo(userIdOrUser: number | User | undefined): Promise<UserInfo> {
|
|
74
|
-
* // TypeScript knows userIdOrUser is User in this branch
|
|
75
|
-
* if (isObject<User>(userIdOrUser)) {
|
|
76
|
-
* // Can safely access object properties
|
|
77
|
-
* return await userService.getUserInfo(userIdOrUser.id);
|
|
78
|
-
* }
|
|
79
|
-
* // TypeScript knows userIdOrUser is number or undefined in this branch
|
|
80
|
-
* else {
|
|
81
|
-
* return await userService.getUserInfo(userIdOrUser);
|
|
82
|
-
* }
|
|
83
|
-
* }
|
|
84
|
-
*
|
|
85
|
-
* // Works with optional parameters too
|
|
86
|
-
* function formatUserName(user?: User | string): string {
|
|
87
|
-
* if (isObject<User>(user)) {
|
|
88
|
-
* return `${user.firstName} ${user.lastName}`;
|
|
89
|
-
* }
|
|
90
|
-
* return user || 'Guest';
|
|
91
|
-
* }
|
|
92
|
-
* ```
|
|
93
|
-
*/
|
|
94
|
-
function isObject(value) {
|
|
95
|
-
return !Array.isArray(value) && typeof value === "object" && value !== null;
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Type-safe utility to check if a value is an object with a specific property.
|
|
99
|
-
*
|
|
100
|
-
* This function acts as a type guard that determines if an unknown value is an object
|
|
101
|
-
* that contains a specified property, and narrows the TypeScript type to the generic
|
|
102
|
-
* type T when true. This provides a robust way to implement duck typing in TypeScript,
|
|
103
|
-
* allowing for safe property access in the conditional branches.
|
|
104
|
-
*
|
|
105
|
-
* @template T The expected object type that should contain the property
|
|
106
|
-
* @param {unknown} value Any value to check, with no type constraints
|
|
107
|
-
* @param {keyof T} propertyName The name of the property that should exist on the object
|
|
108
|
-
* @returns {value is T} Type predicate that narrows the type to T when true
|
|
109
|
-
*
|
|
110
|
-
* @remarks
|
|
111
|
-
* This function uses Object.prototype.hasOwnProperty.call to safely check for property
|
|
112
|
-
* existence, even if the object has a custom implementation of hasOwnProperty or if the
|
|
113
|
-
* property is named 'hasOwnProperty'.
|
|
114
|
-
*
|
|
115
|
-
* Unlike the simpler isObject utility, this function can distinguish between different
|
|
116
|
-
* object types based on their properties, making it ideal for discriminating between
|
|
117
|
-
* different interface implementations or handling union types.
|
|
118
|
-
*
|
|
119
|
-
* @example
|
|
120
|
-
* ```typescript
|
|
121
|
-
* // Check if an object has the properties of a User interface
|
|
122
|
-
* interface User {
|
|
123
|
-
* id: number;
|
|
124
|
-
* name: string;
|
|
125
|
-
* }
|
|
126
|
-
*
|
|
127
|
-
* interface Order {
|
|
128
|
-
* orderNumber: string;
|
|
129
|
-
* items: string[];
|
|
130
|
-
* }
|
|
131
|
-
*
|
|
132
|
-
* // Function that can handle different object types
|
|
133
|
-
* function processEntity(entity: unknown): void {
|
|
134
|
-
* // Check if entity has 'id' property, indicating it's a User
|
|
135
|
-
* if (isObjectWith<User>(entity, 'id')) {
|
|
136
|
-
* // TypeScript knows entity is User in this branch
|
|
137
|
-
* console.log(`Processing user: ${entity.name}`);
|
|
138
|
-
* }
|
|
139
|
-
* // Check if entity has 'orderNumber' property, indicating it's an Order
|
|
140
|
-
* else if (isObjectWith<Order>(entity, 'orderNumber')) {
|
|
141
|
-
* // TypeScript knows entity is Order in this branch
|
|
142
|
-
* console.log(`Processing order: ${entity.orderNumber} with ${entity.items.length} items`);
|
|
143
|
-
* }
|
|
144
|
-
* else {
|
|
145
|
-
* console.log('Unknown entity type');
|
|
146
|
-
* }
|
|
147
|
-
* }
|
|
148
|
-
*
|
|
149
|
-
* // Useful for API responses where types may vary
|
|
150
|
-
* async function handleResponse(response: unknown): Promise<void> {
|
|
151
|
-
* if (isObjectWith<ErrorResponse>(response, 'errorCode')) {
|
|
152
|
-
* throw new ApiException(response.errorCode, response.message);
|
|
153
|
-
* }
|
|
154
|
-
* else if (isObjectWith<SuccessResponse>(response, 'data')) {
|
|
155
|
-
* await processData(response.data);
|
|
156
|
-
* }
|
|
157
|
-
* }
|
|
158
|
-
* ```
|
|
159
|
-
*/
|
|
160
|
-
function isObjectWith(value, propertyName) {
|
|
161
|
-
return Object.prototype.hasOwnProperty.call(value, propertyName);
|
|
162
|
-
}
|
|
163
|
-
//# sourceMappingURL=assertions.utils.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"assertions.utils.js","sourceRoot":"","sources":["../../../../libs/utils/src/utils/assertions.utils.ts"],"names":[],"mappings":";AAAA,qCAAqC;;AAyCrC,0BAEC;AA+CD,4BAEC;AAiED,oCAEC;AA7JD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,SAAgB,OAAO,CAAI,KAA0B;IACjD,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAChC,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8DG;AACH,SAAgB,YAAY,CAAmB,KAAc,EAAE,YAAqB;IAChF,OAAO,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC;AACrE,CAAC"}
|