0

I am trying to understand what does the following piece of code does

export const extractSomeDto = ({
    _id,
    name,
    status
}: SomeDto): SomeDto =>
transformAttributes<SomeDto>({
    _id,
    name,
    status
});

The function call is like this -

extractSomeDto(JSON.parse(body))

I could make out the following -

  1. the method extractSomeDto is exported for other files to call it
  2. the method is going to return : SomeDto

I have the following queries -

What is the input for extractSomeDto?
Is it SomeDto or _id, name, status?

The method extractSomeDto is going to call another method called transformAttributes,
I can see _id, name, status passed as arguments, but I don't understand the <SomeDto> part

Ani
  • 3,452
  • 5
  • 52
  • 113
  • 1
    Input is supposed to be a `SomeDto`. `transformAttributes` is a generic function over a type. The type is specified using `<>`. – MjZac Sep 28 '20 at 15:31
  • @MjZac SomeDto is input to transformAttributes? then the fields in the curly braces? – Ani Sep 28 '20 at 15:45
  • 1
    That is destructuring assignment. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment – MjZac Sep 28 '20 at 16:02

2 Answers2

2

Let's break it down:

export

    export const extractSomeDto = ({ _id, name, status }: SomeDto): SomeDto =>
//  ^^^^^^
      transformAttributes<SomeDto>({ _id, name, status });
  1. the method extractSomeDto is exported for other files to call it

You are correct it will be export from the current module.

Minor correction, this is a function, not a method. Methods belong on objects - you always call them as something.someMethod(). Functions don't have an object they are associated with, so they are essentially "free floating" and you can call them as someFunction().

Input parameter

export const extractSomeDto = ({ _id, name, status }: SomeDto): SomeDto =>
//                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    transformAttributes<SomeDto>({ _id, name, status });

The parameter this function takes is of type SomeDto, however, the properties _id, name, and status are going to be destructured. The handbook documentation shows a variable declaration but it can also be used for parameters.

In essence, it just takes the values of those three properties from the input and assigns them to a variable with the same name. It's easier than doing input._id, input.name, and input.status.

Return type

export const extractSomeDto = ({ _id, name, status }: SomeDto): SomeDto =>
//                                                              ^^^^^^^
    transformAttributes<SomeDto>({ _id, name, status });
  1. the method is going to return : SomeDto

You are again correct - the return type is SomeDto again. (reminder it's a function, not a method).

So, the input and output are the same type of object.

Generic function call

export const extractSomeDto = ({ _id, name, status }: SomeDto): SomeDto =>
    transformAttributes<SomeDto>({ _id, name, status });
//  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The function will call a generic function called transformAttributes. The signature of that function must be something like function <T>transformAttributes() where T would be the generic type argument. In your case, the type argument is SomeDto. What exactly that determines for the function depends on the implementation although it's one of

  • The parameter(s) type, e.g., function <T>transformAttributes(input: T)
  • The return type, e.g., function <T>transformAttributes(): T
  • Both the input and the return type function <T>transformAttributes(input: T): T

Input argument for transformAttributes()

export const extractSomeDto = ({ _id, name, status }: SomeDto): SomeDto =>
    transformAttributes<SomeDto>({ _id, name, status });
//                               ^^^^^^^^^^^^^^^^^^^^^

This is object property shorthand syntax. It's equivalent to { _id: _id, name: name, status: status }. In other words, it creates properties from these three variables where for each the name is the name of the variable and the value is the value of the variable.

Return value of transformAttributes and extractSomeDto

Finally, a quick mention - since the code is using an arrow function and is body is not wrapped in curly brackets {} then implicitly the return value is whatever the body returns. This means that extractSomeDto returns the result of transformAttributes<SomeDto>({ _id, name, status }) when called.

VLAZ
  • 18,437
  • 8
  • 35
  • 54
  • why were `_id, name, status` destructured and then put into `SomeDto`? Can I just accept SomeDto as an input to extractSomeDto? Will this work `export const extractSomeDto = (dto SomeDto): SomeDto => transformAttributes<>(dto);` – Ani Sep 28 '20 at 16:50
  • 1
    Maybe it would work, maybe not. I don't really know, since I don't know what `SomeDto` contains. If it's really only just these three, then you can do it. If it has more properties, then perhaps the destructuring is to get rid of them and get a "minimally viable `SomeDto`". I also don't really know what `transformAttributes` does. I *assume* the signature is `function transformAttributes(input: any): T` and it just changes all values in an object or something but that's just a guess based on the name and how it's used. – VLAZ Sep 28 '20 at 16:54
1

Function Name: extractSomeDto

Function Parameters: {_id,name,status}: SomeDtoan. This means that the function takes in an object of type SomeDtoan and destructures it to pull out the following fields: _id, name, status.

Function Return type: SomeDto

Function body: the function is returning the result of

transformAttributes<SomeDto>({
    _id,
    name,
    status
});

Here is the same function, rewritten to be a little easier to understand.

export const extractSomeDto = (inputObject: SomeDto): SomeDto => {
   const _id = inputObject._id;
   const name = inputObject.name;
   const status = inputObject.status;

   return transformAttributes<SomeDto>({_id, name, status});
}
Dharman
  • 21,838
  • 18
  • 57
  • 107
Mittyesque
  • 31
  • 5