@rt-tools/core 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/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # @rt-tools/core
2
+
3
+ Core utilities for Angular applications.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @rt-tools/core
9
+ # or
10
+ pnpm add @rt-tools/core
11
+ ```
12
+
13
+ ## Features
14
+
15
+ ### isNil
16
+ Type guard function for null/undefined checks.
17
+
18
+ ```typescript
19
+ import { isNil } from '@rt-tools/core';
20
+
21
+ if (isNil(value)) {
22
+ // value is null or undefined
23
+ }
24
+ ```
25
+
26
+ ### MessageBus
27
+ Event bus for component communication using RxJS.
28
+
29
+ ```typescript
30
+ import { MessageBus } from '@rt-tools/core';
31
+
32
+ const bus = new MessageBus<'USER_LOGGED_IN' | 'USER_LOGGED_OUT'>();
33
+
34
+ // Subscribe to events
35
+ bus.ofType('USER_LOGGED_IN').subscribe(event => {
36
+ console.log('User logged in');
37
+ });
38
+
39
+ // Emit events
40
+ bus.emit({ type: 'USER_LOGGED_IN' });
41
+ ```
42
+
43
+ ### PlatformService
44
+ Service for detecting browser/server platform.
45
+
46
+ ```typescript
47
+ import { PlatformService } from '@rt-tools/core';
48
+
49
+ @Component({...})
50
+ export class MyComponent {
51
+ private platform = inject(PlatformService);
52
+
53
+ ngOnInit() {
54
+ if (this.platform.isPlatformBrowser) {
55
+ // Browser-only code
56
+ }
57
+ }
58
+ }
59
+ ```
60
+
61
+ ### WINDOW Token
62
+ Injection token for safe window access.
63
+
64
+ ```typescript
65
+ import { WINDOW } from '@rt-tools/core';
66
+
67
+ @Component({...})
68
+ export class MyComponent {
69
+ private window = inject(WINDOW);
70
+
71
+ scrollToTop() {
72
+ this.window.scrollTo(0, 0);
73
+ }
74
+ }
75
+ ```
76
+
77
+ ## Requirements
78
+
79
+ - Angular 21+
80
+ - RxJS 7.8+
81
+
82
+ ## License
83
+
84
+ Apache-2.0
@@ -0,0 +1,59 @@
1
+ import { Subject } from 'rxjs';
2
+ import { filter, map } from 'rxjs/operators';
3
+ import { isPlatformBrowser, DOCUMENT } from '@angular/common';
4
+ import * as i0 from '@angular/core';
5
+ import { inject, PLATFORM_ID, Injectable, InjectionToken } from '@angular/core';
6
+
7
+ function isNil(entity) {
8
+ return entity === null || entity === undefined;
9
+ }
10
+
11
+ class MessageBus {
12
+ #eventSource = new Subject();
13
+ emit(event) {
14
+ this.#eventSource.next(event);
15
+ }
16
+ onEmit() {
17
+ return this.#eventSource.asObservable();
18
+ }
19
+ ofType(eventType) {
20
+ return this.onEmit().pipe(filter((event) => event.type === eventType), map((event) => event));
21
+ }
22
+ }
23
+
24
+ /**
25
+ * A service for detecting the platform on which the application is running.
26
+ * This service is useful for checking if the application is running in a browser
27
+ * environment or on the server-side.
28
+ */
29
+ class PlatformService {
30
+ #platformId = inject(PLATFORM_ID);
31
+ constructor() {
32
+ this.isPlatformBrowser = isPlatformBrowser(this.#platformId);
33
+ }
34
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.0.1", ngImport: i0, type: PlatformService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
35
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.0.1", ngImport: i0, type: PlatformService, providedIn: 'root' }); }
36
+ }
37
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.0.1", ngImport: i0, type: PlatformService, decorators: [{
38
+ type: Injectable,
39
+ args: [{ providedIn: 'root' }]
40
+ }], ctorParameters: () => [] });
41
+
42
+ const WINDOW = new InjectionToken('An injection token for global window object', {
43
+ factory: () => {
44
+ const { defaultView } = inject(DOCUMENT);
45
+ if (!defaultView) {
46
+ throw new Error('Window is not available');
47
+ }
48
+ return defaultView;
49
+ },
50
+ });
51
+
52
+ // functions
53
+
54
+ /**
55
+ * Generated bundle index. Do not edit.
56
+ */
57
+
58
+ export { MessageBus, PlatformService, WINDOW, isNil };
59
+ //# sourceMappingURL=rt-tools-core.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rt-tools-core.mjs","sources":["../../../projects/core/src/lib/functions/is-nil.ts","../../../projects/core/src/lib/services/message-bus.ts","../../../projects/core/src/lib/services/platform.service.ts","../../../projects/core/src/lib/tokens/window.token.ts","../../../projects/core/src/index.ts","../../../projects/core/src/rt-tools-core.ts"],"sourcesContent":["export function isNil<T>(entity: T | null | undefined): entity is null | undefined {\n return entity === null || entity === undefined;\n}\n","import { Observable, Subject } from 'rxjs';\nimport { filter, map } from 'rxjs/operators';\n\nexport interface MessageBusEvent<T = string> {\n readonly type: T;\n}\n\nexport class MessageBus<M> {\n readonly #eventSource: Subject<MessageBusEvent<M>> = new Subject<MessageBusEvent<M>>();\n\n public emit(event: MessageBusEvent<M>): void {\n this.#eventSource.next(event);\n }\n\n public onEmit(): Observable<MessageBusEvent<M>> {\n return this.#eventSource.asObservable();\n }\n\n public ofType(eventType: M): Observable<MessageBusEvent<M>> {\n return this.onEmit().pipe(\n filter((event: MessageBusEvent<M>): event is MessageBusEvent<M> => event.type === eventType),\n map((event: MessageBusEvent<M>) => event)\n );\n }\n}\n","import { isPlatformBrowser } from '@angular/common';\nimport { Injectable, PLATFORM_ID, inject } from '@angular/core';\n\n/**\n * A service for detecting the platform on which the application is running.\n * This service is useful for checking if the application is running in a browser\n * environment or on the server-side.\n */\n@Injectable({ providedIn: 'root' })\nexport class PlatformService {\n readonly #platformId: object = inject(PLATFORM_ID);\n public readonly isPlatformBrowser: boolean;\n\n constructor() {\n this.isPlatformBrowser = isPlatformBrowser(this.#platformId);\n }\n}\n","import { DOCUMENT } from '@angular/common';\nimport { InjectionToken, inject } from '@angular/core';\n\nexport const WINDOW: InjectionToken<Window> = new InjectionToken<Window>('An injection token for global window object', {\n factory: (): Window => {\n const { defaultView }: Document = inject(DOCUMENT);\n\n if (!defaultView) {\n throw new Error('Window is not available');\n }\n\n return defaultView;\n },\n});\n","// functions\nexport * from './lib/functions';\n\n// services\nexport * from './lib/services';\n\n// tokens\nexport * from './lib/tokens';\n\n// types\nexport * from './lib/types';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;;;AAAM,SAAU,KAAK,CAAI,MAA4B,EAAA;AACjD,IAAA,OAAO,MAAM,KAAK,IAAI,IAAI,MAAM,KAAK,SAAS;AAClD;;MCKa,UAAU,CAAA;AACV,IAAA,YAAY,GAAgC,IAAI,OAAO,EAAsB;AAE/E,IAAA,IAAI,CAAC,KAAyB,EAAA;AACjC,QAAA,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC;IACjC;IAEO,MAAM,GAAA;AACT,QAAA,OAAO,IAAI,CAAC,YAAY,CAAC,YAAY,EAAE;IAC3C;AAEO,IAAA,MAAM,CAAC,SAAY,EAAA;AACtB,QAAA,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,CACrB,MAAM,CAAC,CAAC,KAAyB,KAAkC,KAAK,CAAC,IAAI,KAAK,SAAS,CAAC,EAC5F,GAAG,CAAC,CAAC,KAAyB,KAAK,KAAK,CAAC,CAC5C;IACL;AACH;;ACrBD;;;;AAIG;MAEU,eAAe,CAAA;AACf,IAAA,WAAW,GAAW,MAAM,CAAC,WAAW,CAAC;AAGlD,IAAA,WAAA,GAAA;QACI,IAAI,CAAC,iBAAiB,GAAG,iBAAiB,CAAC,IAAI,CAAC,WAAW,CAAC;IAChE;8GANS,eAAe,EAAA,IAAA,EAAA,EAAA,EAAA,MAAA,EAAA,EAAA,CAAA,eAAA,CAAA,UAAA,EAAA,CAAA,CAAA;AAAf,IAAA,SAAA,IAAA,CAAA,KAAA,GAAA,EAAA,CAAA,qBAAA,CAAA,EAAA,UAAA,EAAA,QAAA,EAAA,OAAA,EAAA,QAAA,EAAA,QAAA,EAAA,EAAA,EAAA,IAAA,EAAA,eAAe,cADF,MAAM,EAAA,CAAA,CAAA;;2FACnB,eAAe,EAAA,UAAA,EAAA,CAAA;kBAD3B,UAAU;mBAAC,EAAE,UAAU,EAAE,MAAM,EAAE;;;MCLrB,MAAM,GAA2B,IAAI,cAAc,CAAS,6CAA6C,EAAE;IACpH,OAAO,EAAE,MAAa;QAClB,MAAM,EAAE,WAAW,EAAE,GAAa,MAAM,CAAC,QAAQ,CAAC;QAElD,IAAI,CAAC,WAAW,EAAE;AACd,YAAA,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC;QAC9C;AAEA,QAAA,OAAO,WAAW;IACtB,CAAC;AACJ,CAAA;;ACbD;;ACAA;;AAEG;;;;"}
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@rt-tools/core",
3
+ "version": "0.0.1",
4
+ "description": "Core utilities for Angular applications - MessageBus, PlatformService, WINDOW token, and helper functions",
5
+ "license": "Apache-2.0",
6
+ "author": "Yauheni Krumin",
7
+ "type": "module",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/Eyhenij/rt-tools",
11
+ "directory": "projects/core"
12
+ },
13
+ "keywords": [
14
+ "Angular",
15
+ "Core",
16
+ "Utils",
17
+ "MessageBus",
18
+ "Signals"
19
+ ],
20
+ "peerDependencies": {
21
+ "@angular/common": "^21.0.0",
22
+ "@angular/core": "^21.0.0",
23
+ "rxjs": "^7.8.0"
24
+ },
25
+ "sideEffects": false,
26
+ "main": "src/index.ts",
27
+ "types": "src/index.ts",
28
+ "module": "fesm2022/rt-tools-core.mjs",
29
+ "typings": "types/rt-tools-core.d.ts",
30
+ "exports": {
31
+ "./package.json": {
32
+ "default": "./package.json"
33
+ },
34
+ ".": {
35
+ "types": "./types/rt-tools-core.d.ts",
36
+ "default": "./fesm2022/rt-tools-core.mjs"
37
+ }
38
+ },
39
+ "dependencies": {
40
+ "tslib": "^2.3.0"
41
+ }
42
+ }
@@ -0,0 +1,41 @@
1
+ import { Observable } from 'rxjs';
2
+ import * as i0 from '@angular/core';
3
+ import { InjectionToken } from '@angular/core';
4
+
5
+ declare function isNil<T>(entity: T | null | undefined): entity is null | undefined;
6
+
7
+ interface MessageBusEvent<T = string> {
8
+ readonly type: T;
9
+ }
10
+ declare class MessageBus<M> {
11
+ #private;
12
+ emit(event: MessageBusEvent<M>): void;
13
+ onEmit(): Observable<MessageBusEvent<M>>;
14
+ ofType(eventType: M): Observable<MessageBusEvent<M>>;
15
+ }
16
+
17
+ /**
18
+ * A service for detecting the platform on which the application is running.
19
+ * This service is useful for checking if the application is running in a browser
20
+ * environment or on the server-side.
21
+ */
22
+ declare class PlatformService {
23
+ #private;
24
+ readonly isPlatformBrowser: boolean;
25
+ constructor();
26
+ static ɵfac: i0.ɵɵFactoryDeclaration<PlatformService, never>;
27
+ static ɵprov: i0.ɵɵInjectableDeclaration<PlatformService>;
28
+ }
29
+
30
+ declare const WINDOW: InjectionToken<Window>;
31
+
32
+ interface IDictionary<T> {
33
+ [Key: string]: T;
34
+ }
35
+
36
+ type Nullable<T> = T | undefined | null;
37
+
38
+ type Primitive = string | number | bigint | boolean | symbol | null | undefined;
39
+
40
+ export { MessageBus, PlatformService, WINDOW, isNil };
41
+ export type { IDictionary, MessageBusEvent, Nullable, Primitive };