Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement(angular/core): Refactoring localization service and SORT_COMPARE_FUNC #19700

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
masum-ulu marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class LocalizationService {

return local;
}),
filter(Boolean)
filter(Boolean),
)
.subscribe(val => this.localizations$.next(val));
}
Expand Down Expand Up @@ -211,9 +211,12 @@ export class LocalizationService {
key: string | LocalizationWithDefault,
...interpolateParams: string[]
) {
if (!key) key = '';
let defaultValue = '';

if (!key) {
return defaultValue;
}

if (typeof key !== 'string') {
defaultValue = key.defaultValue;
key = key.key;
Expand Down Expand Up @@ -265,7 +268,10 @@ export class LocalizationService {
}
}

function recursivelyMergeBaseResources(baseResourceName: string, source: ResourceDto): ApplicationLocalizationResourceDto {
function recursivelyMergeBaseResources(
baseResourceName: string,
source: ResourceDto,
): ApplicationLocalizationResourceDto {
const item = source[baseResourceName];

if (item.baseResources.length === 0) {
Expand All @@ -280,10 +286,12 @@ function recursivelyMergeBaseResources(baseResourceName: string, source: Resourc
}

function mergeResourcesWithBaseResource(resource: ResourceDto): ResourceDto {
const entities: Array<[string, ApplicationLocalizationResourceDto]> = Object.keys(resource).map(key => {
const newValue = recursivelyMergeBaseResources(key, resource);
return [key, newValue];
});
const entities: Array<[string, ApplicationLocalizationResourceDto]> = Object.keys(resource).map(
key => {
const newValue = recursivelyMergeBaseResources(key, resource);
return [key, newValue];
},
);
return entities.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {});
}

Expand Down
21 changes: 13 additions & 8 deletions npm/ng-packs/packages/core/src/lib/tokens/compare-func.token.ts
masum-ulu marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InjectionToken, inject } from '@angular/core';
import { SortableItem } from '../models';
import type { SortableItem } from '../models';
import { LocalizationService } from '../services';

export const SORT_COMPARE_FUNC = new InjectionToken<(a: SortableItem, b: SortableItem) => number>(
Expand All @@ -9,23 +9,28 @@ export const SORT_COMPARE_FUNC = new InjectionToken<(a: SortableItem, b: Sortabl
export function compareFuncFactory() {
const localizationService = inject(LocalizationService);
const fn = (a: SortableItem, b: SortableItem) => {
const aName = localizationService.instant(a.name);
const bName = localizationService.instant(b.name);
const aNumber = a.order;
const bNumber = b.order;

if (aNumber > bNumber) return 1;
if (aNumber < bNumber) return -1;

if (a.id > b.id) return 1;
if (a.id < b.id) return -1;

if (!Number.isInteger(aNumber)) return 1;
if (!Number.isInteger(bNumber)) return -1;

if (aNumber > bNumber) return 1;
if (aNumber < bNumber) return -1;
if (!a?.name || !b?.name) {
return -1;
}

const aName = localizationService.instant(a.name);
const bName = localizationService.instant(b.name);

if (aName > bName) return 1;
if (aName < bName) return -1;

if (a.id > b.id) return 1;
if (a.id < b.id) return -1;

return 0;
};

Expand Down
Loading