diff --git a/src/addons/mod/assign/services/assign-offline.ts b/src/addons/mod/assign/services/assign-offline.ts index cf8d095f0..305092943 100644 --- a/src/addons/mod/assign/services/assign-offline.ts +++ b/src/addons/mod/assign/services/assign-offline.ts @@ -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(results); + const flatten = results.flat(); // Get assign id. let assignIds: number[] = flatten.map((assign) => assign.assignid); diff --git a/src/addons/mod/forum/pages/discussion/discussion.ts b/src/addons/mod/forum/pages/discussion/discussion.ts index bf0f77308..54b10890e 100644 --- a/src/addons/mod/forum/pages/discussion/discussion.ts +++ b/src/addons/mod/forum/pages/discussion/discussion.ts @@ -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(); } /** diff --git a/src/addons/storagemanager/pages/courses-storage/courses-storage.ts b/src/addons/storagemanager/pages/courses-storage/courses-storage.ts index 9e9eb145e..cde85d50a 100644 --- a/src/addons/storagemanager/pages/courses-storage/courses-storage.ts +++ b/src/addons/storagemanager/pages/courses-storage/courses-storage.ts @@ -231,7 +231,7 @@ export class AddonStorageManagerCoursesStoragePage implements OnInit, OnDestroy */ private async calculateDownloadedCourseSize(courseId: number): Promise { 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); diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index 9409e6768..fca7c6f00 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -24,8 +24,6 @@ import { UrlSegmentGroup, } from '@angular/router'; -import { CoreArray } from '@singletons/array'; - const modulesRoutes: WeakMap, ModuleRoutes> = new WeakMap(); /** @@ -35,7 +33,7 @@ const modulesRoutes: WeakMap, ModuleRoutes> = new WeakMa * @returns App routes. */ function buildAppRoutes(injector: Injector): Routes { - return CoreArray.flatten(injector.get(APP_ROUTES, [])); + return injector.get(APP_ROUTES, []).flat(); } /** @@ -210,8 +208,8 @@ export function resolveModuleRoutes(injector: Injector, token: InjectionToken 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); diff --git a/src/core/features/comments/services/comments-offline.ts b/src/core/features/comments/services/comments-offline.ts index 766c72b3e..8199163d3 100644 --- a/src/core/features/comments/services/comments-offline.ts +++ b/src/core/features/comments/services/comments-offline.ts @@ -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(COMMENTS_DELETED_TABLE), ]); - return CoreArray.flatten(results); + return results.flat(); } /** diff --git a/src/core/features/compile/services/compile.ts b/src/core/features/compile/services/compile.ts index 3158b6448..732cba1ff 100644 --- a/src/core/features/compile/services/compile.ts +++ b/src/core/features/compile/services/compile.ts @@ -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, diff --git a/src/core/features/course/services/course-helper.ts b/src/core/features/course/services/course-helper.ts index e2594b104..d80fa7fbb 100644 --- a/src/core/features/course/services/course-helper.ts +++ b/src/core/features/course/services/course-helper.ts @@ -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 { 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)), diff --git a/src/core/features/mainmenu/services/mainmenu.ts b/src/core/features/mainmenu/services/mainmenu.ts index e08b87f0c..d48451d56 100644 --- a/src/core/features/mainmenu/services/mainmenu.ts +++ b/src/core/features/mainmenu/services/mainmenu.ts @@ -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(); } /** diff --git a/src/core/services/sites.ts b/src/core/services/sites.ts index 4119500de..d16dbb053 100644 --- a/src/core/services/sites.ts +++ b/src/core/services/sites.ts @@ -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; diff --git a/src/core/singletons/array.ts b/src/core/singletons/array.ts index 9cae5fa13..711313ef6 100644 --- a/src/core/singletons/array.ts +++ b/src/core/singletons/array.ts @@ -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(arr: T[][]): T[] { - return ( []).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(); } /** diff --git a/src/core/singletons/tests/array.test.ts b/src/core/singletons/tests/array.test.ts index 0aee9eb92..c09225cf1 100644 --- a/src/core/singletons/tests/array.test.ts +++ b/src/core/singletons/tests/array.test.ts @@ -16,11 +16,6 @@ import { CoreArray } from '@singletons/array'; describe('CoreArray singleton', () => { - it('flattens arrays', () => { - expect(CoreArray.flatten([])).toEqual([]); - expect(CoreArray.flatten([[1, 2], [3, 4], [5, 6]])).toEqual([1, 2, 3, 4, 5, 6]); - }); - it('gets array without an item', () => { const originalArray = ['foo', 'bar', 'baz'];