Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a chain of methods with TypeScript

I have an array of countries defined as a const CountriesList:

[{
    name: "Afghanistan",
    id: "4",
    alpha2Code: "AF",
    alpha3Code: "AFG",
    numericCode: 4
},
...]

I have a static class Countries which is supposed to return differently formatted and filtered countries from the const above.

export class Countries {
public static getCountries() {
    return CountriesList;
}

public static getFilteredCountries(setName: string) {
    return CountriesList
        .filter(function (country, index) {
            return customFilter(setName, country, index)
        });
}

public static formatForSelectInput(items: ICountryIso3166[]) {
    return items.map(function (country) {
        return {
            title: L(country.name),
            data: country.id,
            value: country.name
        };
    })
}
}

Now, as it's TypeScript and it has its own rules, I have no idea how to chain methods to make it work like this:

var countryItems = Countries
    .getFilteredCountries('test')
    .formatForSelectInput();

Should I create a new object, so that not to return bare array, but an array inside a wrapper object with respective methods, or how do I perform the chaining properly?

Please advise.

like image 452
Sergei Basharov Avatar asked Jul 19 '16 14:07

Sergei Basharov


People also ask

Can you chain JavaScript methods?

Method chaining is an interesting feature in JavaScript, which helps to reduce the size of code and increase the readability of our code. In method chaining, we call separate methods of a single object like a chain without assigning the same object multiple times by assignment operators.

Can you chain string methods?

The second option is to use method chaining. In this case, you use all string methods you want one after another. You can do this either on a single line or multiple.

What do you mean by method chaining?

Method chaining, also known as named parameter idiom, is a common syntax for invoking multiple method calls in object-oriented programming languages. Each method returns an object, allowing the calls to be chained together in a single statement without requiring variables to store the intermediate results.

Does Python have method chaining?

Some Python libraries, such as pandas, NumPy, and Pillow (PIL), are designed so that methods can be chained together and processed in order (= method chaining). Method chaining is not a special syntax, as it is just repeating the process of calling a method directly from the return value.


1 Answers

You want to chain methods, so you want to call them on an object, not a static class. So something like removing the static from Countries and changing your code to

var countryItems = new Countries()
    .getFilteredCountries('test')
    .formatForSelectInput();

Each method where you want to allow chaining should then end with return this;. You should also declare the return types of your methods (to be the class Countries).

like image 62
WouterH Avatar answered Sep 27 '22 23:09

WouterH