Size

stdtraits~ Size

Source:

Trait to de­term­ine the size of a con­tainer.

const as­sert = re­quire('assert');
const { stric­tEqual: as­sertIs } = re­quire('assert');
const { Size, size, empty } = re­quire('fer­rum');

// Im­ple­ment­ing size
class Foo {
  con­structor(len) {
    this.len = len || 42;
  }

  [Size.sym­bol]() {
    re­turn this.len;
  }
}

// Al­tern­at­ively
//Size.impl(Foo, (v) => v.len);

// De­term­ine the size
as­sertIs(size(new Map()),         0);
as­sertIs(size("foobar"),          6);
as­sertIs(size({foo: 42, bar: 5}), 2);
as­sert(empty(new Set()));
as­sert(!empty([1,2]));

Im­ple­men­ted at least for Ob­ject, String, Ar­ray, Map, Set.

In­ter­face

In­voc­a­tion takes the form (c: Con­tainer) => i: In­teger

Laws

  • i >= 0
  • i !== null && i !== un­defined.
  • Must be ef­fi­cient to ex­ecute. No IO, avoid bad al­gorithmic com­plex­it­ies.

See: size See: empty