MOBILE-3947 chore: Deprecate CoreArray.flatten in favor native flat

main
Pau Ferrer Ocaña 2024-03-07 10:49:43 +01:00
parent ccd9c05855
commit 6f371abbdb
11 changed files with 16 additions and 26 deletions

View File

@ -28,7 +28,6 @@ import {
SUBMISSIONS_GRADES_TABLE,
SUBMISSIONS_TABLE,
} from './database/assign';
import { CoreArray } from '@singletons/array';
/**
* Service to handle offline assign.
@ -87,8 +86,7 @@ export class AddonModAssignOfflineProvider {
const results = await Promise.all(promises);
// Flatten array.
const flatten = CoreArray
.flatten<AddonModAssignSubmissionsDBRecordFormatted | AddonModAssignSubmissionsGradingDBRecordFormatted>(results);
const flatten = results.flat();
// Get assign id.
let assignIds: number[] = flatten.map((assign) => assign.assignid);

View File

@ -31,7 +31,6 @@ import { CoreSites } from '@services/sites';
import { CoreDomUtils } from '@services/utils/dom';
import { CoreUtils } from '@services/utils/utils';
import { NgZone, Translate } from '@singletons';
import { CoreArray } from '@singletons/array';
import { CoreDom } from '@singletons/dom';
import { CoreEventObserver, CoreEvents } from '@singletons/events';
import { Subscription } from 'rxjs';
@ -826,7 +825,7 @@ export class AddonModForumDiscussionPage implements OnInit, AfterViewInit, OnDes
protected getAllPosts(): Post[] {
const allPosts = this.posts.map(post => this.flattenPostHierarchy(post));
return CoreArray.flatten(allPosts);
return allPosts.flat();
}
/**

View File

@ -231,7 +231,7 @@ export class AddonStorageManagerCoursesStoragePage implements OnInit, OnDestroy
*/
private async calculateDownloadedCourseSize(courseId: number): Promise<number> {
const sections = await CoreCourse.getSections(courseId);
const modules = CoreArray.flatten(sections.map((section) => section.modules));
const modules = sections.map((section) => section.modules).flat();
const promisedModuleSizes = modules.map(async (module) => {
const size = await CoreCourseModulePrefetchDelegate.getModuleStoredSize(module, courseId);

View File

@ -24,8 +24,6 @@ import {
UrlSegmentGroup,
} from '@angular/router';
import { CoreArray } from '@singletons/array';
const modulesRoutes: WeakMap<InjectionToken<unknown>, ModuleRoutes> = new WeakMap();
/**
@ -35,7 +33,7 @@ const modulesRoutes: WeakMap<InjectionToken<unknown>, ModuleRoutes> = new WeakMa
* @returns App routes.
*/
function buildAppRoutes(injector: Injector): Routes {
return CoreArray.flatten(injector.get<Routes[]>(APP_ROUTES, []));
return injector.get<Routes[]>(APP_ROUTES, []).flat();
}
/**
@ -210,8 +208,8 @@ export function resolveModuleRoutes(injector: Injector, token: InjectionToken<Mo
});
const moduleRoutes = {
children: CoreArray.flatten(routes.map(r => r.children)),
siblings: CoreArray.flatten(routes.map(r => r.siblings)),
children: routes.map(r => r.children).flat(),
siblings: routes.map(r => r.siblings).flat(),
};
modulesRoutes.set(token, moduleRoutes);

View File

@ -17,7 +17,6 @@ import { CoreSites } from '@services/sites';
import { CoreTimeUtils } from '@services/utils/time';
import { makeSingleton } from '@singletons';
import { COMMENTS_TABLE, COMMENTS_DELETED_TABLE, CoreCommentsDBRecord, CoreCommentsDeletedDBRecord } from './database/comments';
import { CoreArray } from '@singletons/array';
/**
* Service to handle offline comments.
@ -38,7 +37,7 @@ export class CoreCommentsOfflineProvider {
site.getDb().getRecords<CoreCommentsDeletedDBRecord>(COMMENTS_DELETED_TABLE),
]);
return CoreArray.flatten<CoreCommentsDBRecord | CoreCommentsDeletedDBRecord>(results);
return results.flat();
}
/**

View File

@ -213,7 +213,7 @@ export class CoreCompileProvider {
const lazyImports = await Promise.all(this.LAZY_IMPORTS.map(getModules => getModules()));
const imports = [
...CoreArray.flatten(lazyImports),
...lazyImports.flat(),
...this.IMPORTS,
...extraImports,
TranslatePipeForCompile,

View File

@ -41,7 +41,6 @@ import {
CoreCourseSearchedData,
CoreEnrolledCourseData,
} from '@features/courses/services/courses';
import { CoreArray } from '@singletons/array';
import { CoreCourseOffline } from './course-offline';
import {
CoreCourseOptionsDelegate,
@ -1909,7 +1908,7 @@ export class CoreCourseHelperProvider {
*/
async deleteCourseFiles(courseId: number): Promise<void> {
const sections = await CoreCourse.getSections(courseId);
const modules = CoreArray.flatten(sections.map((section) => section.modules));
const modules = sections.map((section) => section.modules).flat();
await Promise.all(
modules.map((module) => this.removeModuleStoredData(module, courseId)),

View File

@ -19,7 +19,6 @@ import { CoreSites } from '@services/sites';
import { CoreConstants } from '@/core/constants';
import { CoreMainMenuDelegate, CoreMainMenuHandlerToDisplay } from './mainmenu-delegate';
import { Device, makeSingleton } from '@singletons';
import { CoreArray } from '@singletons/array';
import { CoreTextUtils } from '@services/utils/text';
import { CoreScreen } from '@services/screen';
import { CorePlatform } from '@services/platform';
@ -73,7 +72,7 @@ export class CoreMainMenuProvider {
this.getCustomItemsFromConfig(),
]);
return CoreArray.flatten(customItems);
return customItems.flat();
}
/**

View File

@ -41,7 +41,6 @@ import {
SiteDBEntry,
SchemaVersionsDBEntry,
} from '@services/database/sites';
import { CoreArray } from '../singletons/array';
import { CoreNetworkError } from '@classes/errors/network-error';
import { CoreNavigator, CoreRedirectPayload } from './navigator';
import { CoreSitesFactory } from './sites-factory';
@ -98,7 +97,7 @@ export class CoreSitesProvider {
constructor(@Optional() @Inject(CORE_SITE_SCHEMAS) siteSchemas: CoreSiteSchema[][] | null) {
this.logger = CoreLogger.getInstance('CoreSitesProvider');
this.siteSchemas = CoreArray.flatten(siteSchemas ?? []).reduce(
this.siteSchemas = (siteSchemas ?? []).flat().reduce(
(siteSchemas, schema) => {
siteSchemas[schema.name] = schema;

View File

@ -39,9 +39,13 @@ export class CoreArray {
*
* @param arr Original array.
* @returns Flattened array.
* @deprecated since 4.4 Use Array.prototype.flat() instead.
*/
static flatten<T>(arr: T[][]): T[] {
return (<T[]> []).concat(...arr);
// eslint-disable-next-line no-console
console.warn('CoreArray.flatten is deprecated and will be removed soon. Please use array \'flat\' instead.');
return arr.flat();
}
/**

View File

@ -16,11 +16,6 @@ import { CoreArray } from '@singletons/array';
describe('CoreArray singleton', () => {
it('flattens arrays', () => {
expect(CoreArray.flatten([])).toEqual([]);
expect(CoreArray.flatten<number>([[1, 2], [3, 4], [5, 6]])).toEqual([1, 2, 3, 4, 5, 6]);
});
it('gets array without an item', () => {
const originalArray = ['foo', 'bar', 'baz'];