LostKobrakai

LostKobrakai

Numerator - calculate paginations without creating any markup

For pagination many people (like me) use scrivener and scrivener_ecto, which works great for offset based pagination. What never really fit my work though was scrivener_html, because I often don’t use frameworks and it took over link generation and styles so extensively.

A thread here on the forums today made me clean up some code I wrote a few days ago for a new project and release it on hex:

The package does not generate any markup at all, but instead creates a list of elements, which describe the pagination. You need to keep GET parameters on the urls, because the paginated content is filtered? Numerator doesn’t care. You want to use custom styles? Go ahead. You need to display the pagination in a CLI? No problem.

iex(1)> Numerator.build(%{page: 6, last: 17}, show_first: true, show_last: true)
[
  %{page: 5, type: :prev},
  %{page: 1, type: :page},
  %{type: :ellipsis},
  %{page: 4, type: :page},
  %{page: 5, type: :page},
  %{page: 6, type: :current},
  %{page: 7, type: :page},
  %{page: 8, type: :page},
  %{type: :ellipsis},
  %{page: 17, type: :page},
  %{page: 7, type: :next}
]

It does support a scrivener page struct as first argument as well, so there’s no need to convert the map parameters manually.

I’ve also added an example implementation for bootstrap in the readme: https://github.com/madeitGmbH/numerator/blob/master/README.md#bootstrap

Most Liked

LostKobrakai

LostKobrakai

I just pushed an update with a breaking change (0.3.0) where :show_first and :show_last pages are tagged :first and :last respectively – instead of just :page – when they’re separated from the normal pagination pages by an :ellipsis. So now they can be styled differently once the ellipsis starts appearing.

LostKobrakai

LostKobrakai

I just released an update to the package:

0.2.1

  • Extracted the chunk generation; The part calculating which pages around the current one to show.
  • Improved the performance especially when paginating a large number of pages

Small number: 1..100; Large number: 1..100_000; Number of pages to show: 5

Before:

Name                            ips        average  deviation         median         99th %
Small number of pages       23.33 K      0.0429 ms     ±7.68%      0.0420 ms      0.0520 ms
Large number of pages      0.0169 K       59.31 ms     ±9.67%       59.26 ms       73.17 ms

Comparison:
Small number of pages       23.33 K
Large number of pages      0.0169 K - 1383.32x slower +59.26 ms

After:

Name                            ips        average  deviation         median         99th %
Small number of pages      219.65 K        4.55 μs   ±290.11%        3.99 μs        5.99 μs
Large number of pages      210.09 K        4.76 μs   ±298.51%        3.99 μs        5.99 μs

Comparison:
Small number of pages      219.65 K
Large number of pages      210.09 K - 1.05x slower +0.21 μs

Additionally I’d like to note, that for all of you using liveview this library might be worth looking into. It will allow proper change tracking as all the markup is handled in the template and not in some function call the template engine has no way to look into.

LostKobrakai

LostKobrakai

I haven’t been in the need for implementing pagination in a while, but I’d still use this. The returned list should play well with liveviews change tracking :smiley:

lucaong

lucaong

Nice library! I was looking for something similar and was about to build something myself. Your update message comes at the perfect time, as I missed your first announcement. Great job!

Where Next?

Popular in Libraries Top

kevinlang
Hey all, We have made an Ecto3 Adapter for SQLite3, ecto_sqlite3! We have successfully on-boarded the full suite of integration tests (...
New
treble37
Just looking for a little feedback on a tiny helper library I built - Sometimes I find the need to convert maps with atom keys to maps...
New
praveenperera
FastRSS Parse RSS feeds very quickly: This is rust NIF built using rustler Uses the RSS rust crate to do the actual RSS parsing Speed...
New
New
riverrun
I’ve just released version 3 of Comeonin, a password hashing library. The following small changes have been made: changes to the NIF c...
New
zorbash
I created Kitto a framework for dashboards inspired by Dashing. [demo] The distributed characteristics of Elixir and the low memory foo...
New
Crowdhailer
Experimenting with this code. OK.try do user <- fetch_user(1) cart <- fetch_cart(1) order = checkout(cart, user) save_or...
New
mtrudel
Bandit is an HTTP server for Plug and WebSock apps. Bandit is written entirely in Elixir and is built atop Thousand Island. It can serve...
New
anshuman23
Hello all, I have been working on my proposed project called Tensorflex as part of Google Summer of Code 2018.. Tensorflex can be used f...
New
ostinelli
Let’s write a database! Well not really, but I think it’s a little sad that there doesn’t seem to be a simple in-memory distributed KV da...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New

Sub Categories:

We're in Beta

About us Mission Statement