From f467dc75c82e17f38c2100b50f67a10f364369fd Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Wed, 19 Aug 2020 15:49:05 +0300 Subject: [PATCH] refactor: make schematics import sort fn reusable --- npm/ng-packs/packages/schematics/src/utils/import.ts | 11 +++++++++++ npm/ng-packs/packages/schematics/src/utils/index.ts | 1 + npm/ng-packs/packages/schematics/src/utils/service.ts | 11 +---------- 3 files changed, 13 insertions(+), 10 deletions(-) create mode 100644 npm/ng-packs/packages/schematics/src/utils/import.ts diff --git a/npm/ng-packs/packages/schematics/src/utils/import.ts b/npm/ng-packs/packages/schematics/src/utils/import.ts new file mode 100644 index 0000000000..26b22d44e6 --- /dev/null +++ b/npm/ng-packs/packages/schematics/src/utils/import.ts @@ -0,0 +1,11 @@ +import { Import } from '../models'; + +export function sortImports(imports: Import[]) { + imports.sort((a, b) => + removeRelative(a) > removeRelative(b) ? 1 : a.keyword > b.keyword ? 1 : -1, + ); +} + +export function removeRelative(importDef: Import) { + return importDef.path.replace(/\.\.\//g, ''); +} diff --git a/npm/ng-packs/packages/schematics/src/utils/index.ts b/npm/ng-packs/packages/schematics/src/utils/index.ts index ee38b83dc1..fc6c973b81 100644 --- a/npm/ng-packs/packages/schematics/src/utils/index.ts +++ b/npm/ng-packs/packages/schematics/src/utils/index.ts @@ -1,6 +1,7 @@ export * from './angular'; export * from './ast'; export * from './common'; +export * from './import'; export * from './model'; export * from './namespace'; export * from './path'; diff --git a/npm/ng-packs/packages/schematics/src/utils/service.ts b/npm/ng-packs/packages/schematics/src/utils/service.ts index 83e8af2235..5fc2e18e36 100644 --- a/npm/ng-packs/packages/schematics/src/utils/service.ts +++ b/npm/ng-packs/packages/schematics/src/utils/service.ts @@ -1,4 +1,5 @@ import { Action, Body, Controller, Import, Method, Property, Service, Signature } from '../models'; +import { sortImports } from './import'; import { parseNamespace } from './namespace'; import { parseGenerics } from './tree'; import { createTypeAdapter, createTypesToImportsReducer } from './type'; @@ -24,16 +25,6 @@ export function createControllerToServiceMapper(solution: string, apiName: strin }; } -function sortImports(imports: Import[]) { - imports.sort((a, b) => - removeRelative(a) > removeRelative(b) ? 1 : a.keyword > b.keyword ? 1 : -1, - ); -} - -function removeRelative(importDef: Import) { - return importDef.path.replace(/\.\.\//g, ''); -} - function sortMethods(methods: Method[]) { methods.sort((a, b) => (a.signature.name > b.signature.name ? 1 : -1)); }