Skip to content

Commit

Permalink
refactor(Expand):
Browse files Browse the repository at this point in the history
- небольшие оптимизации
- убраны комментарии и старый код
  • Loading branch information
wmakeev committed Aug 2, 2021
1 parent 43578a2 commit 6f45f8d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
28 changes: 5 additions & 23 deletions src/model/utils/Expand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import type {
MetaType
} from '../'

type t1 = EntityRef<'account'> extends CollectionRef<'account'> ? true : false

// prettier-ignore

/**
Expand Down Expand Up @@ -96,17 +94,14 @@ export type ExpandField<T, K extends keyof T> = {
*/
export type ExpandPath<T, K extends string> =
string extends K
? '[Error] ExpandPath: Неизвестный путь - string'
? never

// i.e. `attributes`
: T extends Array<infer M>
? Array<ExpandPath<M, K>>

// i.e. `positions`
: T extends Collection<infer U>
// ? T & { rows: ExpandPath<T['rows'], K> }
// ? CollectionRef<M> & { rows: ExpandPath<EntityByMetaType[M], K>[] }
// TODO Протестировано?
? {
[P in keyof T]: P extends 'rows'
? ExpandPath<U, K>[]
Expand All @@ -115,12 +110,6 @@ export type ExpandPath<T, K extends string> =

// 'foo.bar'
: K extends `${infer Field}.${infer Rest}`
// ? Field extends keyof T
// ? ExpandField<T, Field> &
// {
// [Key in Field]: ExpandPath<ExpandField<T, Field>[Field], Rest>
// }
// : never
? {
[P in keyof T]: Field extends P
? ExpandPath<ExpandedField<T, Field>, Rest>
Expand All @@ -131,19 +120,15 @@ export type ExpandPath<T, K extends string> =
: K extends keyof T
? ExpandField<T, K>

: never // `[Error] ExpandPath: Неизвестный путь - ${K}`
: never

// prettier-ignore

/**
* Разворачивает поля типа по строке expand в формате API МойСклад
*/
export type Expand<T, U extends string | undefined> =
// Исходный тип если expand не задан
U extends undefined
? T

: string extends U
string extends U
? never

// 'foo.bar,baz'
Expand All @@ -154,8 +139,5 @@ export type Expand<T, U extends string | undefined> =
: U extends string
? ExpandPath<T, U>

: `[Error] Expand: Неизвестный путь - ${U}`

// TODO 'positions.assortment,agent.attributes.value'
// TODO 'attributes.value,agent.attributes.value'
// TODO 'agent,operations,operations.customerOrder'
// Исходный тип если expand не задан
: T
15 changes: 13 additions & 2 deletions test/model/utils/Expand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
Counterparty,
Organization,
AttributeBase,
Entity
Entity,
Product
} from '../../../src'

//#region
Expand Down Expand Up @@ -172,11 +173,17 @@ const t40_name: string = t40.agent.group.name // OK: 2-nd level expanded
//#endregion

//#region Многоуровневый Expand CollectionRef
const t41 = {} as Expand<CustomerOrder, 'positions.assortment'>
const t41 = {} as Expand<CustomerOrder, 'positions.assortment.country'>

t41.positions

const t41_id: string = t41.positions.rows[0].assortment.id

const t41_asort = t41.positions.rows[0].assortment

if ('country' in t41_asort) {
const p: string | undefined = t41_asort.country?.id
}
//#endregion

//#region Многоуровневый Expand Array
Expand Down Expand Up @@ -307,3 +314,7 @@ if (t60_agentAttr?.type === AttributeType.Counterparty) {
const t60_assortmentId: string = t60.positions.rows[0].assortment.id

//#endregion

// TODO 'positions.assortment,agent.attributes.value'
// TODO 'attributes.value,agent.attributes.value'
// TODO 'agent,operations,operations.customerOrder'

0 comments on commit 6f45f8d

Please sign in to comment.