Skip to content

Commit

Permalink
refactor!: remove redundant export of providers' factories (#2278)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsbarsukov authored Aug 4, 2022
1 parent 7141e4b commit 6765d05
Show file tree
Hide file tree
Showing 49 changed files with 491 additions and 650 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
TUI_CARD_NUMBER_TEXTS,
} from '@taiga-ui/addon-commerce/tokens';
import {tuiTypedFromEvent} from '@taiga-ui/cdk';
import {TuiMedia} from '@taiga-ui/core/interfaces';
import {TUI_MEDIA} from '@taiga-ui/core/tokens';
import {combineLatest, Observable, of} from 'rxjs';
import {map, startWith, switchMap} from 'rxjs/operators';
Expand All @@ -20,42 +19,32 @@ export interface TuiCardGroupedTexts {
export const TUI_INPUT_CARD_GROUPED_TEXTS = new InjectionToken<
Observable<TuiCardGroupedTexts>
>(`InputCardGrouped texts`, {
factory: () =>
inputGroupedTextsFactory(
inject(WINDOW),
inject(TUI_CARD_NUMBER_TEXTS),
inject(TUI_CARD_EXPIRY_TEXTS),
inject(TUI_CARD_CVC_TEXTS),
inject(TUI_MEDIA),
),
});
factory: () => {
const windowRef = inject(WINDOW);
const cardNumberTexts = inject(TUI_CARD_NUMBER_TEXTS);
const expiryTexts = inject(TUI_CARD_EXPIRY_TEXTS);
const cvcTexts = inject(TUI_CARD_CVC_TEXTS);
const {desktopSmall} = inject(TUI_MEDIA);

// eslint-disable-next-line @typescript-eslint/naming-convention
export function inputGroupedTextsFactory(
windowRef: Window,
cardNumberTexts: Observable<[string, string]>,
expiryTexts: Observable<[string, string]>,
cvcTexts: Observable<[string, string]>,
{desktopSmall}: TuiMedia,
): Observable<TuiCardGroupedTexts> {
const media = windowRef.matchMedia(
`screen and (min-width: ${(desktopSmall - 1) / 16}em)`,
);
const media = windowRef.matchMedia(
`screen and (min-width: ${(desktopSmall - 1) / 16}em)`,
);

return tuiTypedFromEvent(media, `change`).pipe(
startWith(null),
switchMap(() =>
combineLatest([
of(Number(media.matches)),
cardNumberTexts,
expiryTexts,
cvcTexts,
]),
),
map(([index, cardNumber, expiry, cvcTexts]) => ({
cardNumberText: cardNumber[index],
expiryText: expiry[index],
cvcText: cvcTexts[index],
})),
);
}
return tuiTypedFromEvent(media, `change`).pipe(
startWith(null),
switchMap(() =>
combineLatest([
of(Number(media.matches)),
cardNumberTexts,
expiryTexts,
cvcTexts,
]),
),
map(([index, cardNumber, expiry, cvcTexts]) => ({
cardNumberText: cardNumber[index],
expiryText: expiry[index],
cvcText: cvcTexts[index],
})),
);
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,6 @@ const icons: Record<TuiPaymentSystem, string> = {
maestro: `tuiIconMaestro`,
};

// eslint-disable-next-line @typescript-eslint/naming-convention
export function cardTextfieldControllerFactory(
directive: TuiTextfieldExampleTextDirective | null,
): TuiTextfieldExampleTextDirective {
directive = directive || new TuiTextfieldExampleTextDirective();
directive.exampleText = `0000 0000 0000 0000`;

return directive;
}

@Component({
selector: `tui-input-card`,
templateUrl: `./input-card.template.html`,
Expand All @@ -60,7 +50,14 @@ export function cardTextfieldControllerFactory(
{
provide: TUI_TEXTFIELD_EXAMPLE_TEXT,
deps: [[new Optional(), TuiTextfieldExampleTextDirective]],
useFactory: cardTextfieldControllerFactory,
useFactory: (
directive: TuiTextfieldExampleTextDirective | null,
): TuiTextfieldExampleTextDirective => {
directive = directive || new TuiTextfieldExampleTextDirective();
directive.exampleText = `0000 0000 0000 0000`;

return directive;
},
},
],
})
Expand Down
1 change: 0 additions & 1 deletion projects/addon-commerce/utils/is-card-length-valid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {tuiGetPaymentSystem} from './get-payment-system';
/**
* Validates card number length using payment system dictionary
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
export function tuiIsCardLengthValid(cardNumber: string): boolean {
const {length} = cardNumber;
const paymentSystem = tuiGetPaymentSystem(cardNumber);
Expand Down
1 change: 0 additions & 1 deletion projects/addon-commerce/utils/is-card-number-valid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {TUI_NON_DIGITS_REGEXP} from '@taiga-ui/core';
/**
* Validates card number using Luhn algorithm
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
export function tuiIsCardNumberValid(value: string | number): boolean {
const cardNumber = String(value).replace(TUI_NON_DIGITS_REGEXP, ``);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,21 @@ export const NAVIGATION_PROVIDERS: Provider[] = [
{
provide: NAVIGATION_TITLE,
deps: [Router, ActivatedRoute, TUI_DOC_TITLE, TuiDestroyService],
useFactory: titleProviderFactory,
useFactory: (
router: Router,
activatedRoute: ActivatedRoute,
titlePrefix: string,
destroy$: Observable<void>,
): Observable<string> => {
return router.events.pipe(
filter(event => event instanceof NavigationEnd),
map(() => activatedRoute.firstChild),
filter(tuiIsPresent),
mergeMap(({data}) => data),
map(({title}) => titlePrefix + title),
takeUntil(destroy$),
);
},
},
{
provide: NAVIGATION_LABELS,
Expand All @@ -30,41 +44,20 @@ export const NAVIGATION_PROVIDERS: Provider[] = [
{
provide: NAVIGATION_ITEMS,
deps: [TUI_DOC_PAGES],
useFactory: itemsProviderFactory,
useFactory: (pages: TuiDocPages): readonly TuiDocPages[] => {
const labels = labelsProviderFactory(pages);

return [
...labels.map(label => pages.filter(({section}) => section === label)),
pages.filter(page => !page.section),
];
},
},
];

// eslint-disable-next-line @typescript-eslint/naming-convention
export function titleProviderFactory(
router: Router,
activatedRoute: ActivatedRoute,
titlePrefix: string,
destroy$: Observable<void>,
): Observable<string> {
return router.events.pipe(
filter(event => event instanceof NavigationEnd),
map(() => activatedRoute.firstChild),
filter(tuiIsPresent),
mergeMap(({data}) => data),
map(({title}) => titlePrefix + title),
takeUntil(destroy$),
);
}

// eslint-disable-next-line @typescript-eslint/naming-convention
export function labelsProviderFactory(pages: TuiDocPages): readonly string[] {
function labelsProviderFactory(pages: TuiDocPages): readonly string[] {
return pages
.map(({section}) => section)
.filter(tuiIsPresent)
.filter((item, index, array) => array.indexOf(item) === index);
}

// eslint-disable-next-line @typescript-eslint/naming-convention
export function itemsProviderFactory(pages: TuiDocPages): readonly TuiDocPages[] {
const labels = labelsProviderFactory(pages);

return [
...labels.map(label => pages.filter(({section}) => section === label)),
pages.filter(page => !page.section),
];
}
36 changes: 17 additions & 19 deletions projects/addon-doc/src/components/page/page.providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,22 @@ export const PAGE_PROVIDERS: Provider[] = [
{
provide: PAGE_SEE_ALSO,
deps: [ElementRef, TUI_DOC_SEE_ALSO],
useFactory: seeAlsoProviderFactory,
},
];

// eslint-disable-next-line @typescript-eslint/naming-convention
export function seeAlsoProviderFactory(
{nativeElement}: ElementRef,
seeAlsoGroups: ReadonlyArray<readonly string[]>,
): readonly string[] {
const currentHeader = nativeElement.getAttribute(`header`);
const groups = seeAlsoGroups.filter(group => group.includes(currentHeader)) || [];
useFactory: (
{nativeElement}: ElementRef,
seeAlsoGroups: ReadonlyArray<readonly string[]>,
): readonly string[] => {
const currentHeader = nativeElement.getAttribute(`header`);
const groups =
seeAlsoGroups.filter(group => group.includes(currentHeader)) || [];

const seeAlsoSet = new Set(
groups
.join()
.split(`,`)
.filter(component => component && component !== currentHeader),
);
const seeAlsoSet = new Set(
groups
.join()
.split(`,`)
.filter(component => component && component !== currentHeader),
);

return Array.from(seeAlsoSet);
}
return Array.from(seeAlsoSet);
},
},
];
1 change: 0 additions & 1 deletion projects/addon-editor/abstract/editor-adapter.abstract.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/naming-convention */
import {Directive} from '@angular/core';
import type {Editor, Range} from '@tiptap/core';
import type {EditorState} from 'prosemirror-state';
Expand Down
69 changes: 30 additions & 39 deletions projects/addon-editor/components/editor/editor.providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,23 @@ export const TUI_EDITOR_PROVIDERS = [
{
provide: LAZY_EDITOR_EXTENSIONS,
deps: [TUI_EDITOR_EXTENSIONS],
useFactory: extensionsFactory,
useFactory: (
extensions: Array<Promise<Extension>>,
): Observable<ReadonlyArray<Extension | Mark | Node>> => {
const extensions$ = new ReplaySubject<ReadonlyArray<Extension | Mark | Node>>(
1,
);

// eslint-disable-next-line @typescript-eslint/no-floating-promises
Promise.all(extensions).then(extensions => extensions$.next(extensions));

return extensions$;
},
},
{
provide: INITIALIZATION_TIPTAP_CONTAINER,
deps: [Renderer2],
useFactory: initializationTipTapContainer,
useFactory: (renderer: Renderer2): HTMLElement => renderer.createElement(`div`),
},
{
provide: TIPTAP_EDITOR,
Expand All @@ -32,44 +43,24 @@ export const TUI_EDITOR_PROVIDERS = [
LAZY_EDITOR_EXTENSIONS,
LAZY_TIPTAP_EDITOR,
],
useFactory: editorFactory,
useFactory: (
element: HTMLElement,
extensions: Observable<Array<Extension | Mark | Node>>,
editor: Observable<typeof Editor>,
): Observable<Editor> => {
return combineLatest([editor, extensions]).pipe(
take(1),
map(
([LazyEditor, extensions]) =>
new LazyEditor({
element,
extensions,
}),
),
shareReplay({bufferSize: 1, refCount: true}),
);
},
},
TuiTiptapEditorService,
TuiEditorPortalService,
];

// eslint-disable-next-line @typescript-eslint/naming-convention
export function extensionsFactory(
extensions: Array<Promise<Extension>>,
): Observable<ReadonlyArray<Extension | Mark | Node>> {
const extensions$ = new ReplaySubject<ReadonlyArray<Extension | Mark | Node>>(1);

// eslint-disable-next-line @typescript-eslint/no-floating-promises
Promise.all(extensions).then(extensions => extensions$.next(extensions));

return extensions$;
}

// eslint-disable-next-line @typescript-eslint/naming-convention
export function initializationTipTapContainer(renderer: Renderer2): HTMLElement {
return renderer.createElement(`div`);
}

// eslint-disable-next-line @typescript-eslint/naming-convention
export function editorFactory(
element: HTMLElement,
extensions: Observable<Array<Extension | Mark | Node>>,
editor: Observable<typeof Editor>,
): Observable<Editor> {
return combineLatest([editor, extensions]).pipe(
take(1),
map(
([LazyEditor, extensions]) =>
new LazyEditor({
element,
extensions,
}),
),
shareReplay({bufferSize: 1, refCount: true}),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,6 @@ import {
TuiPrimitiveTextfieldComponent,
} from '@taiga-ui/core';

// eslint-disable-next-line @typescript-eslint/naming-convention
export function longDropdownControllerFactory(
directive: TuiDropdownControllerDirective | null,
): TuiDropdownControllerDirective {
directive = directive || new TuiDropdownControllerDirective();
directive.maxHeight = 600;

return directive;
}

@Component({
selector: `tui-input-color`,
templateUrl: `./input-color.template.html`,
Expand All @@ -49,7 +39,14 @@ export function longDropdownControllerFactory(
{
provide: TUI_DROPDOWN_CONTROLLER,
deps: [[new Optional(), TuiDropdownControllerDirective]],
useFactory: longDropdownControllerFactory,
useFactory: (
directive: TuiDropdownControllerDirective | null,
): TuiDropdownControllerDirective => {
directive = directive || new TuiDropdownControllerDirective();
directive.maxHeight = 600;

return directive;
},
},
],
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/naming-convention */
import {ChangeDetectionStrategy, Component, Inject} from '@angular/core';
import {AbstractTuiEditor} from '@taiga-ui/addon-editor/abstract';
import {TuiTiptapEditorService} from '@taiga-ui/addon-editor/directives';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ export const TUI_MOBILE_CALENDAR_PROVIDERS: Provider[] = [
TuiDestroyService,
ChangeDetectorRef,
],
useFactory: valueStreamFactory,
useFactory: (
value$: Observable<TuiDayRange | null> | null,
destroy$: Observable<void>,
changeDetectorRef: ChangeDetectorRef,
): Observable<TuiDayRange | null> => {
return (value$ || EMPTY).pipe(
tuiWatch(changeDetectorRef),
takeUntil(destroy$),
);
},
},
];

// eslint-disable-next-line @typescript-eslint/naming-convention
export function valueStreamFactory(
value$: Observable<TuiDayRange | null> | null,
destroy$: Observable<void>,
changeDetectorRef: ChangeDetectorRef,
): Observable<TuiDayRange | null> {
return (value$ || EMPTY).pipe(tuiWatch(changeDetectorRef), takeUntil(destroy$));
}
Loading

0 comments on commit 6765d05

Please sign in to comment.