lifecycleion 0.0.1
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/LICENSE +22 -0
- package/README.md +125 -0
- package/dist/index.cjs +7 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/arrays.cjs +95 -0
- package/dist/lib/arrays.cjs.map +1 -0
- package/dist/lib/arrays.d.cts +15 -0
- package/dist/lib/arrays.d.ts +15 -0
- package/dist/lib/arrays.js +63 -0
- package/dist/lib/arrays.js.map +1 -0
- package/dist/lib/ascii-tables/index.cjs +642 -0
- package/dist/lib/ascii-tables/index.cjs.map +1 -0
- package/dist/lib/ascii-tables/index.d.cts +66 -0
- package/dist/lib/ascii-tables/index.d.ts +66 -0
- package/dist/lib/ascii-tables/index.js +603 -0
- package/dist/lib/ascii-tables/index.js.map +1 -0
- package/dist/lib/clamp.cjs +41 -0
- package/dist/lib/clamp.cjs.map +1 -0
- package/dist/lib/clamp.d.cts +26 -0
- package/dist/lib/clamp.d.ts +26 -0
- package/dist/lib/clamp.js +15 -0
- package/dist/lib/clamp.js.map +1 -0
- package/dist/lib/constants.cjs +73 -0
- package/dist/lib/constants.cjs.map +1 -0
- package/dist/lib/constants.d.cts +17 -0
- package/dist/lib/constants.d.ts +17 -0
- package/dist/lib/constants.js +34 -0
- package/dist/lib/constants.js.map +1 -0
- package/dist/lib/curly-brackets.cjs +77 -0
- package/dist/lib/curly-brackets.cjs.map +1 -0
- package/dist/lib/curly-brackets.d.cts +17 -0
- package/dist/lib/curly-brackets.d.ts +17 -0
- package/dist/lib/curly-brackets.js +52 -0
- package/dist/lib/curly-brackets.js.map +1 -0
- package/dist/lib/deep-clone.cjs +87 -0
- package/dist/lib/deep-clone.cjs.map +1 -0
- package/dist/lib/deep-clone.d.cts +19 -0
- package/dist/lib/deep-clone.d.ts +19 -0
- package/dist/lib/deep-clone.js +62 -0
- package/dist/lib/deep-clone.js.map +1 -0
- package/dist/lib/error-to-string.cjs +743 -0
- package/dist/lib/error-to-string.cjs.map +1 -0
- package/dist/lib/error-to-string.d.cts +3 -0
- package/dist/lib/error-to-string.d.ts +3 -0
- package/dist/lib/error-to-string.js +706 -0
- package/dist/lib/error-to-string.js.map +1 -0
- package/dist/lib/event-emitter.cjs +899 -0
- package/dist/lib/event-emitter.cjs.map +1 -0
- package/dist/lib/event-emitter.d.cts +78 -0
- package/dist/lib/event-emitter.d.ts +78 -0
- package/dist/lib/event-emitter.js +861 -0
- package/dist/lib/event-emitter.js.map +1 -0
- package/dist/lib/id-helpers.cjs +205 -0
- package/dist/lib/id-helpers.cjs.map +1 -0
- package/dist/lib/id-helpers.d.cts +198 -0
- package/dist/lib/id-helpers.d.ts +198 -0
- package/dist/lib/id-helpers.js +170 -0
- package/dist/lib/id-helpers.js.map +1 -0
- package/dist/lib/is-boolean.cjs +33 -0
- package/dist/lib/is-boolean.cjs.map +1 -0
- package/dist/lib/is-boolean.d.cts +19 -0
- package/dist/lib/is-boolean.d.ts +19 -0
- package/dist/lib/is-boolean.js +8 -0
- package/dist/lib/is-boolean.js.map +1 -0
- package/dist/lib/is-function.cjs +33 -0
- package/dist/lib/is-function.cjs.map +1 -0
- package/dist/lib/is-function.d.cts +3 -0
- package/dist/lib/is-function.d.ts +3 -0
- package/dist/lib/is-function.js +8 -0
- package/dist/lib/is-function.js.map +1 -0
- package/dist/lib/is-number.cjs +38 -0
- package/dist/lib/is-number.cjs.map +1 -0
- package/dist/lib/is-number.d.cts +38 -0
- package/dist/lib/is-number.d.ts +38 -0
- package/dist/lib/is-number.js +12 -0
- package/dist/lib/is-number.js.map +1 -0
- package/dist/lib/is-plain-object.cjs +33 -0
- package/dist/lib/is-plain-object.cjs.map +1 -0
- package/dist/lib/is-plain-object.d.cts +20 -0
- package/dist/lib/is-plain-object.d.ts +20 -0
- package/dist/lib/is-plain-object.js +8 -0
- package/dist/lib/is-plain-object.js.map +1 -0
- package/dist/lib/is-promise.cjs +34 -0
- package/dist/lib/is-promise.cjs.map +1 -0
- package/dist/lib/is-promise.d.cts +3 -0
- package/dist/lib/is-promise.d.ts +3 -0
- package/dist/lib/is-promise.js +9 -0
- package/dist/lib/is-promise.js.map +1 -0
- package/dist/lib/json-helpers.cjs +49 -0
- package/dist/lib/json-helpers.cjs.map +1 -0
- package/dist/lib/json-helpers.d.cts +10 -0
- package/dist/lib/json-helpers.d.ts +10 -0
- package/dist/lib/json-helpers.js +22 -0
- package/dist/lib/json-helpers.js.map +1 -0
- package/dist/lib/lifecycle-manager/index.cjs +5594 -0
- package/dist/lib/lifecycle-manager/index.cjs.map +1 -0
- package/dist/lib/lifecycle-manager/index.d.cts +2044 -0
- package/dist/lib/lifecycle-manager/index.d.ts +2044 -0
- package/dist/lib/lifecycle-manager/index.js +5543 -0
- package/dist/lib/lifecycle-manager/index.js.map +1 -0
- package/dist/lib/logger/index.cjs +2514 -0
- package/dist/lib/logger/index.cjs.map +1 -0
- package/dist/lib/logger/index.d.cts +630 -0
- package/dist/lib/logger/index.d.ts +630 -0
- package/dist/lib/logger/index.js +2470 -0
- package/dist/lib/logger/index.js.map +1 -0
- package/dist/lib/padding-utils.cjs +77 -0
- package/dist/lib/padding-utils.cjs.map +1 -0
- package/dist/lib/padding-utils.d.cts +44 -0
- package/dist/lib/padding-utils.d.ts +44 -0
- package/dist/lib/padding-utils.js +46 -0
- package/dist/lib/padding-utils.js.map +1 -0
- package/dist/lib/process-signal-manager.cjs +1306 -0
- package/dist/lib/process-signal-manager.cjs.map +1 -0
- package/dist/lib/process-signal-manager.d.cts +305 -0
- package/dist/lib/process-signal-manager.d.ts +305 -0
- package/dist/lib/process-signal-manager.js +1269 -0
- package/dist/lib/process-signal-manager.js.map +1 -0
- package/dist/lib/promise-protected-resolver.cjs +828 -0
- package/dist/lib/promise-protected-resolver.cjs.map +1 -0
- package/dist/lib/promise-protected-resolver.d.cts +17 -0
- package/dist/lib/promise-protected-resolver.d.ts +17 -0
- package/dist/lib/promise-protected-resolver.js +791 -0
- package/dist/lib/promise-protected-resolver.js.map +1 -0
- package/dist/lib/retry-utils/index.cjs +2183 -0
- package/dist/lib/retry-utils/index.cjs.map +1 -0
- package/dist/lib/retry-utils/index.d.cts +321 -0
- package/dist/lib/retry-utils/index.d.ts +321 -0
- package/dist/lib/retry-utils/index.js +2133 -0
- package/dist/lib/retry-utils/index.js.map +1 -0
- package/dist/lib/safe-handle-callback.cjs +818 -0
- package/dist/lib/safe-handle-callback.cjs.map +1 -0
- package/dist/lib/safe-handle-callback.d.cts +43 -0
- package/dist/lib/safe-handle-callback.d.ts +43 -0
- package/dist/lib/safe-handle-callback.js +780 -0
- package/dist/lib/safe-handle-callback.js.map +1 -0
- package/dist/lib/serialize-error/index.cjs +93 -0
- package/dist/lib/serialize-error/index.cjs.map +1 -0
- package/dist/lib/serialize-error/index.d.cts +26 -0
- package/dist/lib/serialize-error/index.d.ts +26 -0
- package/dist/lib/serialize-error/index.js +64 -0
- package/dist/lib/serialize-error/index.js.map +1 -0
- package/dist/lib/single-event-observer.cjs +841 -0
- package/dist/lib/single-event-observer.cjs.map +1 -0
- package/dist/lib/single-event-observer.d.cts +54 -0
- package/dist/lib/single-event-observer.d.ts +54 -0
- package/dist/lib/single-event-observer.js +803 -0
- package/dist/lib/single-event-observer.js.map +1 -0
- package/dist/lib/sleep.cjs +37 -0
- package/dist/lib/sleep.cjs.map +1 -0
- package/dist/lib/sleep.d.cts +11 -0
- package/dist/lib/sleep.d.ts +11 -0
- package/dist/lib/sleep.js +12 -0
- package/dist/lib/sleep.js.map +1 -0
- package/dist/lib/strings.cjs +186 -0
- package/dist/lib/strings.cjs.map +1 -0
- package/dist/lib/strings.d.cts +107 -0
- package/dist/lib/strings.d.ts +107 -0
- package/dist/lib/strings.js +149 -0
- package/dist/lib/strings.js.map +1 -0
- package/dist/lib/tmp-dir.cjs +254 -0
- package/dist/lib/tmp-dir.cjs.map +1 -0
- package/dist/lib/tmp-dir.d.cts +63 -0
- package/dist/lib/tmp-dir.d.ts +63 -0
- package/dist/lib/tmp-dir.js +211 -0
- package/dist/lib/tmp-dir.js.map +1 -0
- package/dist/lib/unix-time-helpers.cjs +53 -0
- package/dist/lib/unix-time-helpers.cjs.map +1 -0
- package/dist/lib/unix-time-helpers.d.cts +56 -0
- package/dist/lib/unix-time-helpers.d.ts +56 -0
- package/dist/lib/unix-time-helpers.js +24 -0
- package/dist/lib/unix-time-helpers.js.map +1 -0
- package/package.json +220 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# Lifecycleion v0.0.1
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/js/lifecycleion)
|
|
4
|
+
|
|
5
|
+
A collection of foundational TypeScript utilities for managing application lifecycle, logging, retries, events, and common programming patterns.
|
|
6
|
+
|
|
7
|
+
<!-- toc -->
|
|
8
|
+
|
|
9
|
+
- [Why Lifecycleion?](#why-lifecycleion)
|
|
10
|
+
- [Key Features](#key-features)
|
|
11
|
+
- [Installation](#installation)
|
|
12
|
+
- [Quick Example](#quick-example)
|
|
13
|
+
- [Available Libraries](#available-libraries)
|
|
14
|
+
- [Change Log](#change-log)
|
|
15
|
+
- [Contributing](#contributing)
|
|
16
|
+
- [License](#license)
|
|
17
|
+
|
|
18
|
+
<!-- tocstop -->
|
|
19
|
+
|
|
20
|
+
## Why Lifecycleion?
|
|
21
|
+
|
|
22
|
+
Lifecycleion provides battle-tested, production-ready utilities that handle the complex orchestration of modern applications. Whether you need graceful shutdowns, robust retry logic, flexible logging, or just reliable helper functions, Lifecycleion has you covered.
|
|
23
|
+
|
|
24
|
+
### Key Features
|
|
25
|
+
|
|
26
|
+
- 🚀 **Lifecycle Management** - Orchestrate startup, shutdown, and runtime control of application components with dependency resolution
|
|
27
|
+
- 🪵 **Flexible Logger** - Sink-based logger with log levels, redaction, and service scoping
|
|
28
|
+
- 🔄 **Retry Utilities** - Fixed and exponential backoff strategies with cancellation support
|
|
29
|
+
- 📡 **Event Systems** - Event emitters for multiple event types and single-event observers with type safety
|
|
30
|
+
- 🛡️ **Error Handling** - Serialize errors for IPC/RPC, format them as readable tables, and handle callbacks safely
|
|
31
|
+
- 🔧 **Common Utilities** - ID generation (UUID, ULID, ObjectID), string manipulation, deep cloning, and more
|
|
32
|
+
- 📦 **Tree-shakeable** - Import only what you need via subpath exports
|
|
33
|
+
- 💪 **TypeScript-first** - Full type safety with comprehensive TypeScript definitions
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
npm install lifecycleion
|
|
39
|
+
# or
|
|
40
|
+
yarn add lifecycleion
|
|
41
|
+
# or
|
|
42
|
+
bun add lifecycleion
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Quick Example
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import {
|
|
49
|
+
LifecycleManager,
|
|
50
|
+
BaseComponent,
|
|
51
|
+
} from 'lifecycleion/lifecycle-manager';
|
|
52
|
+
import { createLogger } from 'lifecycleion/logger';
|
|
53
|
+
import { RetryRunner } from 'lifecycleion/retry-utils';
|
|
54
|
+
|
|
55
|
+
// Create a logger
|
|
56
|
+
const logger = createLogger({ service: 'my-app' });
|
|
57
|
+
|
|
58
|
+
// Set up retry logic
|
|
59
|
+
const runner = new RetryRunner({ maxAttempts: 3, baseDelayMs: 1000 });
|
|
60
|
+
await runner.run(async () => {
|
|
61
|
+
// Your operation here
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// Manage component lifecycle
|
|
65
|
+
class MyComponent extends BaseComponent {
|
|
66
|
+
async start() {
|
|
67
|
+
logger.info('Starting component');
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async stop() {
|
|
71
|
+
logger.info('Stopping component');
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const manager = new LifecycleManager();
|
|
76
|
+
manager.registerComponent(new MyComponent('my-component'));
|
|
77
|
+
await manager.startAllComponents();
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Available Libraries
|
|
81
|
+
|
|
82
|
+
Each library has comprehensive documentation in the [docs](./docs) folder. Click on any library name in the table below to view detailed usage examples, API references, and best practices.
|
|
83
|
+
|
|
84
|
+
| Library | Import Path | Description |
|
|
85
|
+
| ------------------------------------------------------------------ | ----------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
|
|
86
|
+
| [arrays](./docs/arrays.md) | `lifecycleion/arrays` | Array utility functions for filtering, comparison, deduplication, and string manipulation |
|
|
87
|
+
| [ascii-tables](./docs/ascii-tables.md) | `lifecycleion/ascii-tables` | Key-value and multi-column ASCII tables with word wrapping, nesting, and emoji support |
|
|
88
|
+
| [clamp](./docs/clamp.md) | `lifecycleion/clamp` | Number clamping utilities with safe handling for non-finite values and nullish inputs |
|
|
89
|
+
| [constants](./docs/constants.md) | `lifecycleion/constants` | Common string constants including whitespace helpers and Python-style character sets |
|
|
90
|
+
| [curly-brackets](./docs/curly-brackets.md) | `lifecycleion/curly-brackets` | String templating with `{{placeholders}}`, fallbacks, escaping, and compiled templates |
|
|
91
|
+
| [deep-clone](./docs/deep-clone.md) | `lifecycleion/deep-clone` | Deep clone utility with circular reference detection for objects, arrays, Maps, Sets, and more |
|
|
92
|
+
| [error-to-string](./docs/error-to-string.md) | `lifecycleion/error-to-string` | Format errors into readable ASCII tables with support for nested info and sensitive field masking |
|
|
93
|
+
| [event-emitter](./docs/event-emitter.md) | `lifecycleion/event-emitter` | Lightweight event emitter with protected and public variants, type safety, and memory management |
|
|
94
|
+
| [id-helpers](./docs/id-helpers.md) | `lifecycleion/id-helpers` | Unified ID generation and validation for ObjectID, UUID v4, UUID v7, and ULID |
|
|
95
|
+
| [is-boolean](./docs/is-boolean.md) | `lifecycleion/is-boolean` | Type guard to check if a value is a boolean |
|
|
96
|
+
| [is-function](./docs/is-function.md) | `lifecycleion/is-function` | Check whether a value is a function |
|
|
97
|
+
| [is-number](./docs/is-number.md) | `lifecycleion/is-number` | Type guards to check whether a value is a valid number, with and without finite enforcement |
|
|
98
|
+
| [is-plain-object](./docs/is-plain-object.md) | `lifecycleion/is-plain-object` | Type guard to check if a value is a plain object (not null, not an array) |
|
|
99
|
+
| [is-promise](./docs/is-promise.md) | `lifecycleion/is-promise` | Thenable/promise detection following the Promises/A+ specification |
|
|
100
|
+
| [json-helpers](./docs/json-helpers.md) | `lifecycleion/json-helpers` | JSON formatting utilities |
|
|
101
|
+
| [lifecycle-manager](./docs/lifecycle-manager.md) | `lifecycleion/lifecycle-manager` | Lifecycle orchestration for managing startup, shutdown, and runtime control of application components |
|
|
102
|
+
| [logger](./docs/logger.md) | `lifecycleion/logger` | Flexible sink-based logger with log levels, redaction, template strings, and service scoping |
|
|
103
|
+
| [padding-utils](./docs/padding-utils.md) | `lifecycleion/padding-utils` | String padding utilities for left, right, and center alignment with a configurable pad character |
|
|
104
|
+
| [process-signal-manager](./docs/process-signal-manager.md) | `lifecycleion/process-signal-manager` | Unified handler for process signals (SIGINT, SIGTERM, SIGHUP, etc.) and keyboard shortcuts with graceful shutdown and hot-reload support |
|
|
105
|
+
| [promise-protected-resolver](./docs/promise-protected-resolver.md) | `lifecycleion/promise-protected-resolver` | Promise wrapper with `resolveOnce` and `rejectOnce` that guarantee a promise is only settled once |
|
|
106
|
+
| [retry-utils](./docs/retry-utils.md) | `lifecycleion/retry-utils` | Retry logic with fixed and exponential backoff via `RetryPolicy` (low-level) and `RetryRunner` (high-level with events and cancellation) |
|
|
107
|
+
| [safe-handle-callback](./docs/safe-handle-callback.md) | `lifecycleion/safe-handle-callback` | Safely execute sync or async callbacks with automatic error reporting via the standard `reportError` event API |
|
|
108
|
+
| [serialize-error](./docs/serialize-error.md) | `lifecycleion/serialize-error` | Convert any `Error` into a plain JSON-serializable object and back again for IPC, RPCs, and database storage |
|
|
109
|
+
| [single-event-observer](./docs/single-event-observer.md) | `lifecycleion/single-event-observer` | Lightweight type-safe observer pattern for a single event type, with public and protected notify variants |
|
|
110
|
+
| [sleep](./docs/sleep.md) | `lifecycleion/sleep` | Pause async execution for a given number of milliseconds |
|
|
111
|
+
| [strings](./docs/strings.md) | `lifecycleion/strings` | String type guard, case conversion (PascalCase, camelCase, CONSTANT_CASE), grapheme splitting, character filtering, and chopping helpers |
|
|
112
|
+
| [tmp-dir](./docs/tmp-dir.md) | `lifecycleion/tmp-dir` | Create and automatically clean up uniquely-named temporary directories with configurable prefix, postfix, and unsafe cleanup support |
|
|
113
|
+
| [unix-time-helpers](./docs/unix-time-helpers.md) | `lifecycleion/unix-time-helpers` | Unix timestamp utilities for seconds, milliseconds, high-resolution timing, and unit conversion |
|
|
114
|
+
|
|
115
|
+
## Change Log
|
|
116
|
+
|
|
117
|
+
See [changelog.md](./changelog.md) for version history and release notes.
|
|
118
|
+
|
|
119
|
+
## Contributing
|
|
120
|
+
|
|
121
|
+
Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests on [GitHub](https://github.com/keverw/lifecycleion).
|
|
122
|
+
|
|
123
|
+
## License
|
|
124
|
+
|
|
125
|
+
MIT License - see [LICENSE](./LICENSE) file for details.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
throw new Error(
|
|
5
|
+
'Lifecycleion does not support default imports. Import from specific subpaths like "lifecycleion/arrays", "lifecycleion/logger", etc. See README for all available imports: https://github.com/keverw/lifecycleion#readme'
|
|
6
|
+
);
|
|
7
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Lifecycleion is a collection of utility libraries.\n *\n * Import from specific subpaths instead of the package root:\n *\n * @example\n * ```typescript\n * // ✅ Correct - Import from subpaths\n * import { chunk } from \"lifecycleion/arrays\"\n * import { Logger } from \"lifecycleion/logger\"\n * import { sleep } from \"lifecycleion/sleep\"\n *\n * // ❌ Wrong - Don't import from package root\n * import { something } from \"lifecycleion\"\n * ```\n *\n * See the README for a complete list of available imports:\n * https://github.com/keverw/lifecycleion#readme\n */\n\nthrow new Error(\n 'Lifecycleion does not support default imports. ' +\n 'Import from specific subpaths like \"lifecycleion/arrays\", \"lifecycleion/logger\", etc. ' +\n 'See README for all available imports: https://github.com/keverw/lifecycleion#readme',\n);\n"],"mappings":";;;AAoBA,MAAM,IAAI;AAAA,EACR;AAGF;","names":[]}
|
package/dist/index.d.cts
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
throw new Error(
|
|
3
|
+
'Lifecycleion does not support default imports. Import from specific subpaths like "lifecycleion/arrays", "lifecycleion/logger", etc. See README for all available imports: https://github.com/keverw/lifecycleion#readme'
|
|
4
|
+
);
|
|
5
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Lifecycleion is a collection of utility libraries.\n *\n * Import from specific subpaths instead of the package root:\n *\n * @example\n * ```typescript\n * // ✅ Correct - Import from subpaths\n * import { chunk } from \"lifecycleion/arrays\"\n * import { Logger } from \"lifecycleion/logger\"\n * import { sleep } from \"lifecycleion/sleep\"\n *\n * // ❌ Wrong - Don't import from package root\n * import { something } from \"lifecycleion\"\n * ```\n *\n * See the README for a complete list of available imports:\n * https://github.com/keverw/lifecycleion#readme\n */\n\nthrow new Error(\n 'Lifecycleion does not support default imports. ' +\n 'Import from specific subpaths like \"lifecycleion/arrays\", \"lifecycleion/logger\", etc. ' +\n 'See README for all available imports: https://github.com/keverw/lifecycleion#readme',\n);\n"],"mappings":";AAoBA,MAAM,IAAI;AAAA,EACR;AAGF;","names":[]}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/lib/arrays.ts
|
|
21
|
+
var arrays_exports = {};
|
|
22
|
+
__export(arrays_exports, {
|
|
23
|
+
areArraysEqual: () => areArraysEqual,
|
|
24
|
+
isArray: () => isArray,
|
|
25
|
+
isEveryArrayItemAnString: () => isEveryArrayItemAnString,
|
|
26
|
+
prependStringToArrayItems: () => prependStringToArrayItems,
|
|
27
|
+
pushWithoutDuplicates: () => pushWithoutDuplicates,
|
|
28
|
+
removeEmptyStringsFromArray: () => removeEmptyStringsFromArray
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(arrays_exports);
|
|
31
|
+
|
|
32
|
+
// src/lib/strings.ts
|
|
33
|
+
function isString(value) {
|
|
34
|
+
return typeof value === "string";
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// src/lib/arrays.ts
|
|
38
|
+
function isArray(value) {
|
|
39
|
+
return Array.isArray(value);
|
|
40
|
+
}
|
|
41
|
+
function removeEmptyStringsFromArray(array) {
|
|
42
|
+
const newArray = [];
|
|
43
|
+
for (let item of array) {
|
|
44
|
+
item = item.trim();
|
|
45
|
+
if (item.length > 0) {
|
|
46
|
+
newArray.push(item);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return newArray;
|
|
50
|
+
}
|
|
51
|
+
function prependStringToArrayItems(array, value) {
|
|
52
|
+
const newArray = [];
|
|
53
|
+
for (const item of array) {
|
|
54
|
+
newArray.push(value + item);
|
|
55
|
+
}
|
|
56
|
+
return newArray;
|
|
57
|
+
}
|
|
58
|
+
function isEveryArrayItemAnString(value) {
|
|
59
|
+
if (Array.isArray(value)) {
|
|
60
|
+
for (const item of value) {
|
|
61
|
+
if (!isString(item)) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return true;
|
|
66
|
+
} else {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function areArraysEqual(arr1, arr2) {
|
|
71
|
+
if (arr1.length !== arr2.length) {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
for (const [i, element] of arr1.entries()) {
|
|
75
|
+
if (element !== arr2[i]) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
function pushWithoutDuplicates(array, value) {
|
|
82
|
+
if (!array.includes(value)) {
|
|
83
|
+
array.push(value);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
87
|
+
0 && (module.exports = {
|
|
88
|
+
areArraysEqual,
|
|
89
|
+
isArray,
|
|
90
|
+
isEveryArrayItemAnString,
|
|
91
|
+
prependStringToArrayItems,
|
|
92
|
+
pushWithoutDuplicates,
|
|
93
|
+
removeEmptyStringsFromArray
|
|
94
|
+
});
|
|
95
|
+
//# sourceMappingURL=arrays.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/lib/arrays.ts","../../src/lib/strings.ts"],"sourcesContent":["import { isString } from './strings';\n\nexport function isArray(value: unknown): value is unknown[] {\n return Array.isArray(value);\n}\n\nexport function removeEmptyStringsFromArray(array: string[]): string[] {\n const newArray: string[] = [];\n\n for (let item of array) {\n item = item.trim();\n\n if (item.length > 0) {\n newArray.push(item);\n }\n }\n\n return newArray;\n}\n\n/**\n * Will take an array and return a new array with a value prepended to each item\n *\n * @param array\n * @param value\n * @returns\n */\n\nexport function prependStringToArrayItems(\n array: string[],\n value: string,\n): string[] {\n const newArray: string[] = [];\n\n for (const item of array) {\n newArray.push(value + item);\n }\n\n return newArray;\n}\n\nexport function isEveryArrayItemAnString(value: unknown): boolean {\n if (Array.isArray(value)) {\n for (const item of value) {\n if (!isString(item)) {\n return false;\n }\n }\n\n return true;\n } else {\n return false;\n }\n}\n\nexport function areArraysEqual<T>(arr1: T[], arr2: T[]): boolean {\n // Check if the arrays are the same length\n if (arr1.length !== arr2.length) {\n return false;\n }\n\n // Check each element in the arrays\n for (const [i, element] of arr1.entries()) {\n if (element !== arr2[i]) {\n return false;\n }\n }\n\n // If all elements are equal\n return true;\n}\n\nexport function pushWithoutDuplicates(array: unknown[], value: unknown): void {\n if (!array.includes(value)) {\n array.push(value);\n }\n}\n","import { removeEmptyStringsFromArray } from './arrays';\n\nexport function isString(value: unknown): value is string {\n return typeof value === 'string';\n}\n\n/**\n * Converts a string or an array of strings to Pascal Case.\n *\n * This function takes an input string or an array of strings, each potentially containing hyphens,\n * and converts them to Pascal Case. It removes any characters that are not letters or numbers,\n * capitalizes the first letter of each substring, and ensures the rest of the substring\n * is in lowercase. Finally, it concatenates all these substrings to produce a Pascal Case\n * output.\n *\n * @param {string | string[]} input - The input string or array of strings to be converted to Pascal Case.\n * @returns {string} The converted string in Pascal Case.\n *\n * Examples:\n * toPascalCase(\"hello-world-123!$\") will return \"HelloWorld123\"\n * toPascalCase([\"hello\", \"world-123!$\"]) will return \"HelloWorld123\"\n */\n\nexport function toPascalCase(input: string | string[]): string {\n // Ensure input is an array\n const inputArray = Array.isArray(input) ? input : [input];\n\n // Process each string in the array\n const parts: string[] = [];\n\n for (const item of inputArray) {\n // Clean the string, split by hyphen, and remove empty strings\n const cleanedItem = item.replace(/[^a-zA-Z0-9-]/g, '');\n parts.push(...removeEmptyStringsFromArray(cleanedItem.split('-')));\n }\n\n // Process and rejoin the input strings\n return parts\n .map(\n (subString) =>\n subString.charAt(0).toUpperCase() + subString.slice(1).toLowerCase(),\n )\n .join('');\n}\n\n/**\n * Converts a string or an array of strings to Camel Case.\n *\n * This function takes an input string or an array of strings, each potentially containing hyphens,\n * and converts them to Camel Case. It removes any characters that are not letters or numbers,\n * capitalizes the first letter of each substring after the first one, and ensures the rest of the substring\n * is in lowercase. For the first substring, it ensures the entire substring is in lowercase.\n * Finally, it concatenates all these substrings to produce a Camel Case output.\n *\n * @param {string | string[]} input - The input string or array of strings to be converted to Camel Case.\n * @returns {string} The converted string in Camel Case.\n *\n * Examples:\n * toCamelCase(\"hello-world-123!$\") will return \"helloWorld123\"\n * toCamelCase([\"hello\", \"world-123!$\"]) will return \"helloWorld123\"\n */\n\nexport function toCamelCase(input: string | string[]): string {\n // Ensure input is an array\n const inputArray = Array.isArray(input) ? input : [input];\n\n // Process each string in the array\n const parts: string[] = [];\n\n for (const item of inputArray) {\n // Clean the string, split by hyphen, and remove empty strings\n const cleanedItem = item.replace(/[^a-zA-Z0-9-]/g, '');\n parts.push(...removeEmptyStringsFromArray(cleanedItem.split('-')));\n }\n\n // Process and rejoin the input strings\n return parts\n .map((subString, index) =>\n index === 0\n ? subString.toLowerCase()\n : subString.charAt(0).toUpperCase() + subString.slice(1).toLowerCase(),\n )\n .join('');\n}\n\n/**\n * This method converts a string or an array of strings to camel case,\n * but if starting with a leading hyphen, it will convert to Pascal case.\n */\n\nexport function toCamelCaseWithPascalOverride(\n input: string | string[],\n): string {\n if (isString(input) && input.startsWith('-')) {\n return toPascalCase(input);\n } else if (\n Array.isArray(input) &&\n input.length > 0 &&\n input[0].startsWith('-')\n ) {\n return toPascalCase(input);\n } else {\n return toCamelCase(input);\n }\n}\n\n/**\n * Converts a string or an array of strings to constant case.\n *\n * The function takes a string or an array of strings, where each string can be separated by a '-',\n * and converts them into a constant case format (all uppercase with underscores between words).\n * It first cleans the input by removing non-alphanumeric characters (except for hyphens), splits the\n * string into parts on hyphens, and then joins these parts with underscores, converting the entire\n * result to uppercase.\n *\n * @param {string | string[]} input - The input string or array of strings to be converted.\n * @returns {string} The converted string in constant case.\n *\n * Example:\n * toConstantCase(\"hello-world\") will return \"HELLO_WORLD\"\n * toConstantCase([\"hello\", \"world\"]) will return \"HELLO_WORLD\"\n */\n\nexport function toConstantCase(input: string | string[]): string {\n // Ensure input is an array\n const inputArray = Array.isArray(input) ? input : [input];\n\n // Process each string in the array\n let parts: string[] = [];\n\n for (const item of inputArray) {\n // Clean the string and split by hyphen\n const cleanedItem = item.replace(/[^a-zA-Z0-9-]/g, '');\n parts.push(...cleanedItem.split('-'));\n }\n\n // Remove empty strings from the array\n parts = removeEmptyStringsFromArray(parts);\n\n // Join parts with underscore and convert to uppercase\n return parts.join('_').toUpperCase();\n}\n\nexport function splitGraphemes(text: string): string[] {\n const graphemes: string[] = [];\n let grapheme = '';\n let zwjSequence = '';\n\n for (let i = 0; i < text.length; i++) {\n const char = text[i];\n const nextChar = text[i + 1] || '';\n const code = char.charCodeAt(0);\n\n // Handling combining marks and zero width joiner\n if (\n (code >= 0x0300 && code <= 0x036f) || // Combining Diacritical Marks\n (code >= 0x1ab0 && code <= 0x1aff) || // Combining Diacritical Marks Extended\n (code >= 0x1dc0 && code <= 0x1dff) || // Combining Diacritical Marks Supplement\n (code >= 0xfe20 && code <= 0xfe2f) || // Combining Half Marks\n (code >= 0x0e31 && code <= 0x0e3a) || // Thai combining marks\n (code >= 0x0e47 && code <= 0x0e4e)\n ) {\n // Thai combining marks\n grapheme += char;\n } else if (char === '\\u200d') {\n // Zero Width Joiner (ZWJ)\n zwjSequence += grapheme + char;\n grapheme = '';\n } else {\n if (grapheme) {\n if (zwjSequence) {\n graphemes.push(zwjSequence + grapheme);\n zwjSequence = '';\n } else {\n graphemes.push(grapheme);\n }\n }\n grapheme = char;\n\n // Handle surrogate pairs (needed for certain characters including emojis)\n if (\n char >= '\\ud800' &&\n char <= '\\udbff' &&\n nextChar >= '\\udc00' &&\n nextChar <= '\\udfff'\n ) {\n grapheme += nextChar;\n i++;\n }\n }\n }\n\n if (grapheme) {\n if (zwjSequence) {\n graphemes.push(zwjSequence + grapheme);\n } else {\n graphemes.push(grapheme);\n }\n }\n\n return graphemes;\n}\n\nexport function skipTrailingNewLines(str: string): string {\n return str.replace(/\\n+$/, '');\n}\n\n/**\n * Filters a string to include only specified characters, optionally replacing disallowed characters.\n *\n * @param str - The input string to be filtered.\n * @param list - An array of allowed characters.\n * @param caseInsensitive - Optional. If true, the filtering is case-insensitive. Default is false.\n * @param replacementChar - Optional. Character to replace disallowed characters. If empty, disallowed characters are removed. Default is ''.\n * @returns A new string containing only the allowed characters from the input string, with disallowed characters optionally replaced.\n *\n * @example\n * // Case-sensitive usage, removing disallowed characters\n * characterAllowedOnly(\"Hello123!\", [\"H\", \"e\", \"l\", \"o\"]);\n * // Returns: \"Hello\"\n *\n * @example\n * // Case-insensitive usage, removing disallowed characters\n * characterAllowedOnly(\"Hello123!\", [\"h\", \"E\", \"L\", \"O\"], true);\n * // Returns: \"Hello\"\n *\n * @example\n * // Using replacement character\n * characterAllowedOnly(\"Hello123!\", [\"H\", \"e\", \"l\", \"o\"], false, \"-\");\n * // Returns: \"Hello---\"\n */\n\nexport function characterAllowedOnly(\n str: string,\n list: string[],\n // eslint-disable-next-line @typescript-eslint/naming-convention\n caseInsensitive = false,\n replacementChar = '',\n): string {\n let newStr = '';\n\n // Convert the allowed list to lowercase if case-insensitive\n if (caseInsensitive) {\n list = Array.from(new Set(list.map((item) => item.toLowerCase())));\n }\n\n // Convert the entire input string to lowercase if case-insensitive\n const processedStr = caseInsensitive ? str.toLowerCase() : str;\n\n for (const c of processedStr) {\n if (list.includes(c)) {\n newStr += c;\n } else if (replacementChar !== '') {\n newStr += replacementChar;\n }\n }\n\n return newStr;\n}\n\n// functions to chop characters from a string.\n\n/**\n * Will remove the matching first character from a string\n * @param str\n * @param char\n * @returns\n */\n\nexport function chopBeginningCharacter(str: string, char: string): string {\n if (str.startsWith(char)) {\n return str.slice(1);\n } else {\n return str;\n }\n}\n\n/**\n * Will remove the matching last character from the string\n * @param str\n * @param char\n * @returns\n */\nexport function chopEndingCharacter(str: string, char: string): string {\n if (str.endsWith(char)) {\n return str.slice(0, -1);\n } else {\n return str;\n }\n}\n\n/**\n * Will remove the matching character, from the beginning and/or end of the string if matching\n * @param str\n * @param char\n * @returns\n */\n\nexport function chopBothBeginningAndEndingCharacters(\n str: string,\n char: string,\n): string {\n return chopBeginningCharacter(chopEndingCharacter(str, char), char);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEO,SAAS,SAAS,OAAiC;AACxD,SAAO,OAAO,UAAU;AAC1B;;;ADFO,SAAS,QAAQ,OAAoC;AAC1D,SAAO,MAAM,QAAQ,KAAK;AAC5B;AAEO,SAAS,4BAA4B,OAA2B;AACrE,QAAM,WAAqB,CAAC;AAE5B,WAAS,QAAQ,OAAO;AACtB,WAAO,KAAK,KAAK;AAEjB,QAAI,KAAK,SAAS,GAAG;AACnB,eAAS,KAAK,IAAI;AAAA,IACpB;AAAA,EACF;AAEA,SAAO;AACT;AAUO,SAAS,0BACd,OACA,OACU;AACV,QAAM,WAAqB,CAAC;AAE5B,aAAW,QAAQ,OAAO;AACxB,aAAS,KAAK,QAAQ,IAAI;AAAA,EAC5B;AAEA,SAAO;AACT;AAEO,SAAS,yBAAyB,OAAyB;AAChE,MAAI,MAAM,QAAQ,KAAK,GAAG;AACxB,eAAW,QAAQ,OAAO;AACxB,UAAI,CAAC,SAAS,IAAI,GAAG;AACnB,eAAO;AAAA,MACT;AAAA,IACF;AAEA,WAAO;AAAA,EACT,OAAO;AACL,WAAO;AAAA,EACT;AACF;AAEO,SAAS,eAAkB,MAAW,MAAoB;AAE/D,MAAI,KAAK,WAAW,KAAK,QAAQ;AAC/B,WAAO;AAAA,EACT;AAGA,aAAW,CAAC,GAAG,OAAO,KAAK,KAAK,QAAQ,GAAG;AACzC,QAAI,YAAY,KAAK,CAAC,GAAG;AACvB,aAAO;AAAA,IACT;AAAA,EACF;AAGA,SAAO;AACT;AAEO,SAAS,sBAAsB,OAAkB,OAAsB;AAC5E,MAAI,CAAC,MAAM,SAAS,KAAK,GAAG;AAC1B,UAAM,KAAK,KAAK;AAAA,EAClB;AACF;","names":[]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
declare function isArray(value: unknown): value is unknown[];
|
|
2
|
+
declare function removeEmptyStringsFromArray(array: string[]): string[];
|
|
3
|
+
/**
|
|
4
|
+
* Will take an array and return a new array with a value prepended to each item
|
|
5
|
+
*
|
|
6
|
+
* @param array
|
|
7
|
+
* @param value
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
declare function prependStringToArrayItems(array: string[], value: string): string[];
|
|
11
|
+
declare function isEveryArrayItemAnString(value: unknown): boolean;
|
|
12
|
+
declare function areArraysEqual<T>(arr1: T[], arr2: T[]): boolean;
|
|
13
|
+
declare function pushWithoutDuplicates(array: unknown[], value: unknown): void;
|
|
14
|
+
|
|
15
|
+
export { areArraysEqual, isArray, isEveryArrayItemAnString, prependStringToArrayItems, pushWithoutDuplicates, removeEmptyStringsFromArray };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
declare function isArray(value: unknown): value is unknown[];
|
|
2
|
+
declare function removeEmptyStringsFromArray(array: string[]): string[];
|
|
3
|
+
/**
|
|
4
|
+
* Will take an array and return a new array with a value prepended to each item
|
|
5
|
+
*
|
|
6
|
+
* @param array
|
|
7
|
+
* @param value
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
declare function prependStringToArrayItems(array: string[], value: string): string[];
|
|
11
|
+
declare function isEveryArrayItemAnString(value: unknown): boolean;
|
|
12
|
+
declare function areArraysEqual<T>(arr1: T[], arr2: T[]): boolean;
|
|
13
|
+
declare function pushWithoutDuplicates(array: unknown[], value: unknown): void;
|
|
14
|
+
|
|
15
|
+
export { areArraysEqual, isArray, isEveryArrayItemAnString, prependStringToArrayItems, pushWithoutDuplicates, removeEmptyStringsFromArray };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// src/lib/strings.ts
|
|
2
|
+
function isString(value) {
|
|
3
|
+
return typeof value === "string";
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
// src/lib/arrays.ts
|
|
7
|
+
function isArray(value) {
|
|
8
|
+
return Array.isArray(value);
|
|
9
|
+
}
|
|
10
|
+
function removeEmptyStringsFromArray(array) {
|
|
11
|
+
const newArray = [];
|
|
12
|
+
for (let item of array) {
|
|
13
|
+
item = item.trim();
|
|
14
|
+
if (item.length > 0) {
|
|
15
|
+
newArray.push(item);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return newArray;
|
|
19
|
+
}
|
|
20
|
+
function prependStringToArrayItems(array, value) {
|
|
21
|
+
const newArray = [];
|
|
22
|
+
for (const item of array) {
|
|
23
|
+
newArray.push(value + item);
|
|
24
|
+
}
|
|
25
|
+
return newArray;
|
|
26
|
+
}
|
|
27
|
+
function isEveryArrayItemAnString(value) {
|
|
28
|
+
if (Array.isArray(value)) {
|
|
29
|
+
for (const item of value) {
|
|
30
|
+
if (!isString(item)) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return true;
|
|
35
|
+
} else {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function areArraysEqual(arr1, arr2) {
|
|
40
|
+
if (arr1.length !== arr2.length) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
for (const [i, element] of arr1.entries()) {
|
|
44
|
+
if (element !== arr2[i]) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
function pushWithoutDuplicates(array, value) {
|
|
51
|
+
if (!array.includes(value)) {
|
|
52
|
+
array.push(value);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
export {
|
|
56
|
+
areArraysEqual,
|
|
57
|
+
isArray,
|
|
58
|
+
isEveryArrayItemAnString,
|
|
59
|
+
prependStringToArrayItems,
|
|
60
|
+
pushWithoutDuplicates,
|
|
61
|
+
removeEmptyStringsFromArray
|
|
62
|
+
};
|
|
63
|
+
//# sourceMappingURL=arrays.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/lib/strings.ts","../../src/lib/arrays.ts"],"sourcesContent":["import { removeEmptyStringsFromArray } from './arrays';\n\nexport function isString(value: unknown): value is string {\n return typeof value === 'string';\n}\n\n/**\n * Converts a string or an array of strings to Pascal Case.\n *\n * This function takes an input string or an array of strings, each potentially containing hyphens,\n * and converts them to Pascal Case. It removes any characters that are not letters or numbers,\n * capitalizes the first letter of each substring, and ensures the rest of the substring\n * is in lowercase. Finally, it concatenates all these substrings to produce a Pascal Case\n * output.\n *\n * @param {string | string[]} input - The input string or array of strings to be converted to Pascal Case.\n * @returns {string} The converted string in Pascal Case.\n *\n * Examples:\n * toPascalCase(\"hello-world-123!$\") will return \"HelloWorld123\"\n * toPascalCase([\"hello\", \"world-123!$\"]) will return \"HelloWorld123\"\n */\n\nexport function toPascalCase(input: string | string[]): string {\n // Ensure input is an array\n const inputArray = Array.isArray(input) ? input : [input];\n\n // Process each string in the array\n const parts: string[] = [];\n\n for (const item of inputArray) {\n // Clean the string, split by hyphen, and remove empty strings\n const cleanedItem = item.replace(/[^a-zA-Z0-9-]/g, '');\n parts.push(...removeEmptyStringsFromArray(cleanedItem.split('-')));\n }\n\n // Process and rejoin the input strings\n return parts\n .map(\n (subString) =>\n subString.charAt(0).toUpperCase() + subString.slice(1).toLowerCase(),\n )\n .join('');\n}\n\n/**\n * Converts a string or an array of strings to Camel Case.\n *\n * This function takes an input string or an array of strings, each potentially containing hyphens,\n * and converts them to Camel Case. It removes any characters that are not letters or numbers,\n * capitalizes the first letter of each substring after the first one, and ensures the rest of the substring\n * is in lowercase. For the first substring, it ensures the entire substring is in lowercase.\n * Finally, it concatenates all these substrings to produce a Camel Case output.\n *\n * @param {string | string[]} input - The input string or array of strings to be converted to Camel Case.\n * @returns {string} The converted string in Camel Case.\n *\n * Examples:\n * toCamelCase(\"hello-world-123!$\") will return \"helloWorld123\"\n * toCamelCase([\"hello\", \"world-123!$\"]) will return \"helloWorld123\"\n */\n\nexport function toCamelCase(input: string | string[]): string {\n // Ensure input is an array\n const inputArray = Array.isArray(input) ? input : [input];\n\n // Process each string in the array\n const parts: string[] = [];\n\n for (const item of inputArray) {\n // Clean the string, split by hyphen, and remove empty strings\n const cleanedItem = item.replace(/[^a-zA-Z0-9-]/g, '');\n parts.push(...removeEmptyStringsFromArray(cleanedItem.split('-')));\n }\n\n // Process and rejoin the input strings\n return parts\n .map((subString, index) =>\n index === 0\n ? subString.toLowerCase()\n : subString.charAt(0).toUpperCase() + subString.slice(1).toLowerCase(),\n )\n .join('');\n}\n\n/**\n * This method converts a string or an array of strings to camel case,\n * but if starting with a leading hyphen, it will convert to Pascal case.\n */\n\nexport function toCamelCaseWithPascalOverride(\n input: string | string[],\n): string {\n if (isString(input) && input.startsWith('-')) {\n return toPascalCase(input);\n } else if (\n Array.isArray(input) &&\n input.length > 0 &&\n input[0].startsWith('-')\n ) {\n return toPascalCase(input);\n } else {\n return toCamelCase(input);\n }\n}\n\n/**\n * Converts a string or an array of strings to constant case.\n *\n * The function takes a string or an array of strings, where each string can be separated by a '-',\n * and converts them into a constant case format (all uppercase with underscores between words).\n * It first cleans the input by removing non-alphanumeric characters (except for hyphens), splits the\n * string into parts on hyphens, and then joins these parts with underscores, converting the entire\n * result to uppercase.\n *\n * @param {string | string[]} input - The input string or array of strings to be converted.\n * @returns {string} The converted string in constant case.\n *\n * Example:\n * toConstantCase(\"hello-world\") will return \"HELLO_WORLD\"\n * toConstantCase([\"hello\", \"world\"]) will return \"HELLO_WORLD\"\n */\n\nexport function toConstantCase(input: string | string[]): string {\n // Ensure input is an array\n const inputArray = Array.isArray(input) ? input : [input];\n\n // Process each string in the array\n let parts: string[] = [];\n\n for (const item of inputArray) {\n // Clean the string and split by hyphen\n const cleanedItem = item.replace(/[^a-zA-Z0-9-]/g, '');\n parts.push(...cleanedItem.split('-'));\n }\n\n // Remove empty strings from the array\n parts = removeEmptyStringsFromArray(parts);\n\n // Join parts with underscore and convert to uppercase\n return parts.join('_').toUpperCase();\n}\n\nexport function splitGraphemes(text: string): string[] {\n const graphemes: string[] = [];\n let grapheme = '';\n let zwjSequence = '';\n\n for (let i = 0; i < text.length; i++) {\n const char = text[i];\n const nextChar = text[i + 1] || '';\n const code = char.charCodeAt(0);\n\n // Handling combining marks and zero width joiner\n if (\n (code >= 0x0300 && code <= 0x036f) || // Combining Diacritical Marks\n (code >= 0x1ab0 && code <= 0x1aff) || // Combining Diacritical Marks Extended\n (code >= 0x1dc0 && code <= 0x1dff) || // Combining Diacritical Marks Supplement\n (code >= 0xfe20 && code <= 0xfe2f) || // Combining Half Marks\n (code >= 0x0e31 && code <= 0x0e3a) || // Thai combining marks\n (code >= 0x0e47 && code <= 0x0e4e)\n ) {\n // Thai combining marks\n grapheme += char;\n } else if (char === '\\u200d') {\n // Zero Width Joiner (ZWJ)\n zwjSequence += grapheme + char;\n grapheme = '';\n } else {\n if (grapheme) {\n if (zwjSequence) {\n graphemes.push(zwjSequence + grapheme);\n zwjSequence = '';\n } else {\n graphemes.push(grapheme);\n }\n }\n grapheme = char;\n\n // Handle surrogate pairs (needed for certain characters including emojis)\n if (\n char >= '\\ud800' &&\n char <= '\\udbff' &&\n nextChar >= '\\udc00' &&\n nextChar <= '\\udfff'\n ) {\n grapheme += nextChar;\n i++;\n }\n }\n }\n\n if (grapheme) {\n if (zwjSequence) {\n graphemes.push(zwjSequence + grapheme);\n } else {\n graphemes.push(grapheme);\n }\n }\n\n return graphemes;\n}\n\nexport function skipTrailingNewLines(str: string): string {\n return str.replace(/\\n+$/, '');\n}\n\n/**\n * Filters a string to include only specified characters, optionally replacing disallowed characters.\n *\n * @param str - The input string to be filtered.\n * @param list - An array of allowed characters.\n * @param caseInsensitive - Optional. If true, the filtering is case-insensitive. Default is false.\n * @param replacementChar - Optional. Character to replace disallowed characters. If empty, disallowed characters are removed. Default is ''.\n * @returns A new string containing only the allowed characters from the input string, with disallowed characters optionally replaced.\n *\n * @example\n * // Case-sensitive usage, removing disallowed characters\n * characterAllowedOnly(\"Hello123!\", [\"H\", \"e\", \"l\", \"o\"]);\n * // Returns: \"Hello\"\n *\n * @example\n * // Case-insensitive usage, removing disallowed characters\n * characterAllowedOnly(\"Hello123!\", [\"h\", \"E\", \"L\", \"O\"], true);\n * // Returns: \"Hello\"\n *\n * @example\n * // Using replacement character\n * characterAllowedOnly(\"Hello123!\", [\"H\", \"e\", \"l\", \"o\"], false, \"-\");\n * // Returns: \"Hello---\"\n */\n\nexport function characterAllowedOnly(\n str: string,\n list: string[],\n // eslint-disable-next-line @typescript-eslint/naming-convention\n caseInsensitive = false,\n replacementChar = '',\n): string {\n let newStr = '';\n\n // Convert the allowed list to lowercase if case-insensitive\n if (caseInsensitive) {\n list = Array.from(new Set(list.map((item) => item.toLowerCase())));\n }\n\n // Convert the entire input string to lowercase if case-insensitive\n const processedStr = caseInsensitive ? str.toLowerCase() : str;\n\n for (const c of processedStr) {\n if (list.includes(c)) {\n newStr += c;\n } else if (replacementChar !== '') {\n newStr += replacementChar;\n }\n }\n\n return newStr;\n}\n\n// functions to chop characters from a string.\n\n/**\n * Will remove the matching first character from a string\n * @param str\n * @param char\n * @returns\n */\n\nexport function chopBeginningCharacter(str: string, char: string): string {\n if (str.startsWith(char)) {\n return str.slice(1);\n } else {\n return str;\n }\n}\n\n/**\n * Will remove the matching last character from the string\n * @param str\n * @param char\n * @returns\n */\nexport function chopEndingCharacter(str: string, char: string): string {\n if (str.endsWith(char)) {\n return str.slice(0, -1);\n } else {\n return str;\n }\n}\n\n/**\n * Will remove the matching character, from the beginning and/or end of the string if matching\n * @param str\n * @param char\n * @returns\n */\n\nexport function chopBothBeginningAndEndingCharacters(\n str: string,\n char: string,\n): string {\n return chopBeginningCharacter(chopEndingCharacter(str, char), char);\n}\n","import { isString } from './strings';\n\nexport function isArray(value: unknown): value is unknown[] {\n return Array.isArray(value);\n}\n\nexport function removeEmptyStringsFromArray(array: string[]): string[] {\n const newArray: string[] = [];\n\n for (let item of array) {\n item = item.trim();\n\n if (item.length > 0) {\n newArray.push(item);\n }\n }\n\n return newArray;\n}\n\n/**\n * Will take an array and return a new array with a value prepended to each item\n *\n * @param array\n * @param value\n * @returns\n */\n\nexport function prependStringToArrayItems(\n array: string[],\n value: string,\n): string[] {\n const newArray: string[] = [];\n\n for (const item of array) {\n newArray.push(value + item);\n }\n\n return newArray;\n}\n\nexport function isEveryArrayItemAnString(value: unknown): boolean {\n if (Array.isArray(value)) {\n for (const item of value) {\n if (!isString(item)) {\n return false;\n }\n }\n\n return true;\n } else {\n return false;\n }\n}\n\nexport function areArraysEqual<T>(arr1: T[], arr2: T[]): boolean {\n // Check if the arrays are the same length\n if (arr1.length !== arr2.length) {\n return false;\n }\n\n // Check each element in the arrays\n for (const [i, element] of arr1.entries()) {\n if (element !== arr2[i]) {\n return false;\n }\n }\n\n // If all elements are equal\n return true;\n}\n\nexport function pushWithoutDuplicates(array: unknown[], value: unknown): void {\n if (!array.includes(value)) {\n array.push(value);\n }\n}\n"],"mappings":";AAEO,SAAS,SAAS,OAAiC;AACxD,SAAO,OAAO,UAAU;AAC1B;;;ACFO,SAAS,QAAQ,OAAoC;AAC1D,SAAO,MAAM,QAAQ,KAAK;AAC5B;AAEO,SAAS,4BAA4B,OAA2B;AACrE,QAAM,WAAqB,CAAC;AAE5B,WAAS,QAAQ,OAAO;AACtB,WAAO,KAAK,KAAK;AAEjB,QAAI,KAAK,SAAS,GAAG;AACnB,eAAS,KAAK,IAAI;AAAA,IACpB;AAAA,EACF;AAEA,SAAO;AACT;AAUO,SAAS,0BACd,OACA,OACU;AACV,QAAM,WAAqB,CAAC;AAE5B,aAAW,QAAQ,OAAO;AACxB,aAAS,KAAK,QAAQ,IAAI;AAAA,EAC5B;AAEA,SAAO;AACT;AAEO,SAAS,yBAAyB,OAAyB;AAChE,MAAI,MAAM,QAAQ,KAAK,GAAG;AACxB,eAAW,QAAQ,OAAO;AACxB,UAAI,CAAC,SAAS,IAAI,GAAG;AACnB,eAAO;AAAA,MACT;AAAA,IACF;AAEA,WAAO;AAAA,EACT,OAAO;AACL,WAAO;AAAA,EACT;AACF;AAEO,SAAS,eAAkB,MAAW,MAAoB;AAE/D,MAAI,KAAK,WAAW,KAAK,QAAQ;AAC/B,WAAO;AAAA,EACT;AAGA,aAAW,CAAC,GAAG,OAAO,KAAK,KAAK,QAAQ,GAAG;AACzC,QAAI,YAAY,KAAK,CAAC,GAAG;AACvB,aAAO;AAAA,IACT;AAAA,EACF;AAGA,SAAO;AACT;AAEO,SAAS,sBAAsB,OAAkB,OAAsB;AAC5E,MAAI,CAAC,MAAM,SAAS,KAAK,GAAG;AAC1B,UAAM,KAAK,KAAK;AAAA,EAClB;AACF;","names":[]}
|