adobe / ferrum

Features from the rust language in javascript: Provides Traits/Type classes & a hashing infrastructure and an advanced library for working with sequences/iterators in js
https://www.ferrumjs.org
Apache License 2.0
519 stars 25 forks source link

TryGet trait (Get() with default value) #166

Open koraa opened 3 years ago

koraa commented 3 years ago

Rename the Get trait to TryGet; the signature should be tryGet(key, default). We can derive from this:

const get = (c, k) => {
  const none = Symbol();
  const r = tryGet(c, k, none);
  assert(r !== none, "No such element");
  return r;
};

const has = (c, k) => {
  const none = Symbol();
  return tryGet(c, k, none) !== none;
}

Edit: This is a breaking change.