AngularJs search and pagination code duplication

Some modules of the application I'm writing give the user the option to filter a dataset and paginate the results.

After getting everything to work as intended, I'm trying to refactor the code in order to remove code duplication.

Each and everyone of the afore mentioned modules share the same bit of logic for filtering and pagination.

I'm wondering which is the best practice to follow in order to remove that kind of duplication.

As of now I was thinking of creating a "filter and pagination" service with the following API:

  • Paginate(items,predicate,itemsPerPage) Returns an object: { paginatedItems nrOfPages }

Predicate is called for each and every item passed in the collection, and it is used to select items based on user preferences.

Now each module would take advantage of that API.

Is there a better, recomended way?

Thanks in advance for your help