Helpers functions for Inferno
npx @tessl/cli install tessl/npm-inferno-shared@9.0.00
# Inferno Shared
1
2
Inferno Shared provides essential utility functions for the Inferno React-like library ecosystem. This zero-dependency package offers type guards, validation functions, error handling utilities, and component helper functions designed for maximum performance and reusability within the Inferno framework.
3
4
## Package Information
5
6
- **Package Name**: inferno-shared
7
- **Package Type**: npm
8
- **Language**: TypeScript
9
- **Installation**: `npm install inferno-shared`
10
11
## Core Imports
12
13
```typescript
14
import { isArray, isString, isNumber, isFunction, isNull, isUndefined, isNullOrUndef, isInvalid, isStringOrNumber, throwError, warning, hoistStaticProperties } from "inferno-shared";
15
```
16
17
For CommonJS:
18
19
```javascript
20
const { isArray, isString, isNumber, isFunction, isNull, isUndefined, isNullOrUndef, isInvalid, isStringOrNumber, throwError, warning, hoistStaticProperties } = require("inferno-shared");
21
```
22
23
## Basic Usage
24
25
```typescript
26
import { isString, isNumber, throwError, hoistStaticProperties } from "inferno-shared";
27
28
// Type checking
29
if (isString(userInput)) {
30
// TypeScript knows userInput is string here
31
console.log(userInput.toUpperCase());
32
}
33
34
if (isNumber(value)) {
35
// TypeScript knows value is number here
36
console.log(value.toFixed(2));
37
}
38
39
// Error handling
40
if (invalidCondition) {
41
throwError("Custom error message");
42
}
43
44
// Component utilities
45
class MyComponent {}
46
MyComponent.displayName = "MyComponent";
47
48
class ExtendedComponent {}
49
hoistStaticProperties(ExtendedComponent, MyComponent);
50
// ExtendedComponent now has displayName property
51
```
52
53
## Capabilities
54
55
### Type Guard Functions
56
57
Runtime type checking functions with TypeScript type narrowing support.
58
59
```typescript { .api }
60
/**
61
* Checks if value is an array (alias for Array.isArray)
62
*/
63
const isArray: (arg: any) => arg is any[];
64
65
/**
66
* Type guard to check if value is string or number
67
* @param o - Value to check
68
* @returns true if value is string or number
69
*/
70
function isStringOrNumber(o: unknown): o is string | number;
71
72
/**
73
* Type guard to check if value is null or undefined
74
* @param o - Value to check
75
* @returns true if value is null or undefined
76
*/
77
function isNullOrUndef(o: unknown): o is undefined | null;
78
79
/**
80
* Type guard to check if value is invalid (null, boolean, or undefined)
81
* @param o - Value to check
82
* @returns true if value is null, true, false, or undefined
83
*/
84
function isInvalid(o: unknown): o is null | boolean | undefined;
85
86
/**
87
* Type guard to check if value is a function
88
* @param o - Value to check
89
* @returns true if value is a function
90
*/
91
function isFunction(o: unknown): o is Function;
92
93
/**
94
* Type guard to check if value is a string
95
* @param o - Value to check
96
* @returns true if value is a string
97
*/
98
function isString(o: unknown): o is string;
99
100
/**
101
* Type guard to check if value is a number
102
* @param o - Value to check
103
* @returns true if value is a number
104
*/
105
function isNumber(o: unknown): o is number;
106
107
/**
108
* Type guard to check if value is null
109
* @param o - Value to check
110
* @returns true if value is null
111
*/
112
function isNull(o: unknown): o is null;
113
114
/**
115
* Type guard to check if value is undefined
116
* @param o - Value to check
117
* @returns true if value is undefined
118
*/
119
function isUndefined(o: unknown): o is undefined;
120
```
121
122
### Error Handling
123
124
Error throwing and warning utilities with standardized Inferno formatting.
125
126
```typescript { .api }
127
/**
128
* Default error message constant for runtime errors
129
*/
130
const ERROR_MSG: string;
131
132
/**
133
* Throws an error with Inferno-specific formatting
134
* @param message - Optional custom error message. Defaults to ERROR_MSG if not provided
135
* @throws Error with message prefixed by "Inferno Error: "
136
*/
137
function throwError(message?: string): void;
138
139
/**
140
* Outputs a warning message to console
141
* @param message - Warning message to display
142
*/
143
function warning(message: string): void;
144
```
145
146
### Component Utilities
147
148
Helper functions for component property management and static property copying.
149
150
```typescript { .api }
151
/**
152
* Copies static properties from source component to target component
153
* Skips standard React/component properties like displayName, propTypes, etc.
154
* @param targetComponent - Component to receive the static properties
155
* @param sourceComponent - Component from which to copy static properties
156
*/
157
function hoistStaticProperties(
158
targetComponent: any,
159
sourceComponent: any,
160
): void;
161
```
162
163
## Usage Examples
164
165
### Type Guards with TypeScript
166
167
```typescript
168
import { isString, isNumber, isArray, isFunction, isNullOrUndef } from "inferno-shared";
169
170
function processUserInput(input: unknown) {
171
if (isString(input)) {
172
// TypeScript knows input is string
173
return input.trim().toLowerCase();
174
}
175
176
if (isNumber(input)) {
177
// TypeScript knows input is number
178
return input.toString();
179
}
180
181
if (isArray(input)) {
182
// TypeScript knows input is array
183
return input.join(', ');
184
}
185
186
if (isNullOrUndef(input)) {
187
return "No value provided";
188
}
189
190
return "Unknown type";
191
}
192
193
// Runtime validation
194
function safeExecute(fn: unknown, ...args: any[]) {
195
if (isFunction(fn)) {
196
// TypeScript knows fn is Function
197
return fn(...args);
198
}
199
throw new Error("Expected a function");
200
}
201
```
202
203
### Error Handling
204
205
```typescript
206
import { throwError, warning, ERROR_MSG } from "inferno-shared";
207
208
function validateAge(age: number) {
209
if (age < 0) {
210
throwError("Age cannot be negative");
211
// Throws: Error: Inferno Error: Age cannot be negative
212
}
213
214
if (age > 150) {
215
warning("Age seems unusually high");
216
// Logs warning to console.error
217
}
218
}
219
220
// Using default error message
221
function criticalFailure() {
222
throwError(); // Uses ERROR_MSG constant
223
// Throws: Error: Inferno Error: a runtime error occured! Use Inferno in development environment to find the error.
224
}
225
```
226
227
### Component Property Hoisting
228
229
```typescript
230
import { hoistStaticProperties } from "inferno-shared";
231
232
// Source component with static properties
233
function OriginalComponent() {
234
return <div>Original</div>;
235
}
236
OriginalComponent.displayName = "OriginalComponent";
237
OriginalComponent.propTypes = { /* ... */ };
238
OriginalComponent.customStatic = "custom value";
239
240
// Target component
241
function EnhancedComponent() {
242
return <div>Enhanced</div>;
243
}
244
245
// Copy static properties (excluding known React statics)
246
hoistStaticProperties(EnhancedComponent, OriginalComponent);
247
248
// EnhancedComponent now has:
249
// - customStatic: "custom value" ✓ (copied)
250
// - displayName, propTypes are NOT copied (filtered out)
251
252
console.log(EnhancedComponent.customStatic); // "custom value"
253
```
254
255
### Validation Patterns
256
257
```typescript
258
import { isInvalid, isStringOrNumber, isNullOrUndef } from "inferno-shared";
259
260
interface UserData {
261
id: string | number;
262
name?: string;
263
email?: string;
264
}
265
266
function validateUserData(data: any): UserData | null {
267
// Check for completely invalid values
268
if (isInvalid(data) || typeof data !== 'object') {
269
return null;
270
}
271
272
// Validate required ID field
273
if (!isStringOrNumber(data.id)) {
274
return null;
275
}
276
277
// Handle optional fields
278
const result: UserData = { id: data.id };
279
280
if (!isNullOrUndef(data.name) && isString(data.name)) {
281
result.name = data.name;
282
}
283
284
if (!isNullOrUndef(data.email) && isString(data.email)) {
285
result.email = data.email;
286
}
287
288
return result;
289
}
290
```