@marcura/dadesk-ui-common
v2.0.3
Published
Basic set of most common tools used inside DA-Desk projects.
Downloads
296
Maintainers
Keywords
Readme
@marcura/dadesk-ui-common
Basic set of most common tools used inside DA-Desk projects.
This version is expected to work with Angular 15.x.
@Models
Common
Debouncer
import {Debouncer} from '@marcura/dadesk-ui-common';
const debouncer = new Debouncer(window, 500);
debouncer.debounce('key', () => console.log('debounced operation'));
Destroyables
Registry of objects implementing IDestroyable interface, which should all be destroyed when registry does.
import {Destroyables} from '@marcura/dadesk-ui-common';
const destroyables = new Destroyables();
destroyables.add(new SomeDestroyable());
destroyables.add(new AnotherDestroyable());
destroyables.destroy();
Forms
Typed versions of Ng controls.
LazyProperty
Lazy loading utility for things we don't want to spawn before anybody really accesses.
import {LazyProperty} from '@marcura/dadesk-ui-common';
class Test {
private readonly _sample: LazyProperty<number> = new LazyProperty(() => 123);
public get sample(): number {
return this._sample.value;
}
}
ObservableSet
RX version of normal Set
import {ObservableSet} from '@marcura/dadesk-ui-common';
const observableSet = new ObservableSet<number>();
observableSet.changes$.subscribe(() => console.log('Set changed'));
observableSet.add(1); // Console: 'Set changed'
observableSet.delete(1); // Console: 'Set changed'
Observer
Observing utility, with few handy methods for listening on Observables. Extends Destroyables.
import {Observer} from '@marcura/dadesk-ui-common';
const observer = new Observer();
observer.listen(someObservable, (value) => console.log(value));
observer.bind(anotherObservable, someSubject);
observer.bindControl(yetAnotherObservable, someFormControl);
observer.destroy();
Popup
import {Popup} from '@marcura/dadesk-ui-common';
const popup = new Popup();
popup.opened$.subscribe(() => console.log('Popup opened'));
popup.closed$.subscribe(() => console.log('Popup closed'));
popup.open();
QueryParams, QueryParamsBuilder
import {QueryParams, QueryParamsBuilder} from '@marcura/dadesk-ui-common';
const builder: QueryParamsBuilder = new QueryParamsBuilder();
builder.add('param1', 'value1').add('param2', [1, 2, 3]);
const queryParams: QueryParams = builder.build();
Range
import {Range} from '@marcura/dadesk-ui-common';
const values: Range<number> = {from: 0, to: 100};
RemoteSubject
A class that provides a subject that loads its value asynchronously. The value is loaded only once and is shared among all subscribers.
import {RemoteSubject} from '@marcura/dadesk-ui-common';
const subject: RemoteSubject<string> = new RemoteSubject(() => of('Hello, world!'));
subject.subscribe((value) => console.log(value));
Request
Represents a request with a payload, result, and error type.
import {Request} from '@marcura/dadesk-ui-common';
const request: Request<string, number, Error> = new Request<string, number, Error>(
'payload',
(result) => console.log(result),
(error) => console.error(error)
);
Rx
A utility class providing various RxJS operators and helper functions.
Snapshot
Represents a snapshot of an observable value that can be subscribed to, but also allows sync access to recent value via .get() method.
import {Snapshot} from '@marcura/dadesk-ui-common';
const source: BehaviorSubject<number> = new BehaviorSubject<number>(0);
const snapshot$: Snapshot<number> = new Snapshot(source);
snapshot$.subscribe((value) => console.log(value));
source.next(1);
console.log(snapshot$.get()); // 1
Sort
Sorting utilities.
State
Represents an observable state with sync get()/set() methods.
import {State} from '@marcura/dadesk-ui-common';
const state$: State<number> = new State<number>(0);
state$.subscribe((value) => console.log(value));
state$.set(1);
console.log(state$.get()); // 1
Worker
Worker that handles only one task at the same time. If new task is assigned via .handle(), the previous one is cancelled.
import {worker} from '@marcura/dadesk-ui-common';
const worker = new Worker();
worker.handle(
someObservableTask(),
(result) => console.log(result),
(error) => console.error(error)
);
Testing
DevDebug
A collection of debugging utilities for development purposes.
import {DevDebug} from '@marcura/dadesk-ui-common';
const form = new FormGroup({
name: new FormControl('', Validators.required),
email: new FormControl('', Validators.email),
});
console.log(DevDebug.getFormErrors(form));
DevFormTesting
A collection of form testing utilities for unit tests.
import {DevFormTesting} from '@marcura/dadesk-ui-common';
const form = new FormGroup({
name: new FormControl('', Validators.required),
email: new FormControl('', Validators.email),
});
const error = DevFormTesting.allChildControls(form, (control) => control.valid);
expect(error).toBeNull();
DevTesting
Namespace containing utilities for development testing.
import {DevTesting} from '@marcura/dadesk-ui-common';
class SomeType {
someProp: string = '';
}
const prop: DevTesting.PropNameValidator<SomeType> = DevTesting.createPropNameValidator<SomeType>();
desribe(SomeType.name, () => {
describe(prop('someProp'), () => {
// is fine
});
describe(prop('notExistingProp'), () => {
// shows an error
});
});