harn-stdlib 0.8.22

Embedded Harn standard library source catalog
Documentation
// std/collections — Collection utilities.
/**
 * Options for `pick_keys`.
 *
 * `drop_nil` removes selected keys whose value is `nil` from the result;
 * defaults to `false`.
 */
type PickKeysOptions = {drop_nil?: bool}

/**
 * Remove entries from a dict where the value is nil, an empty string, or the
 * literal "null" string. Generic over the value type so a typed input
 * (`dict<string, V>` or a structural shape) projects back to a dict with the
 * same value contract instead of collapsing to untyped `dict`.
 */
pub fn filter_nil<V>(d: dict<string, V>) -> dict<string, V> {
  return __dict_filter_nil(d ?? {})
}

/**
 * Project a dict onto an explicit list of keys, preserving the original value
 * contract. Pass `{drop_nil: true}` to omit keys whose value resolves to nil
 * after lookup. Missing keys are silently dropped.
 */
pub fn pick_keys<V>(d: dict<string, V>, selected_keys: list<string>, options: PickKeysOptions = {}) -> dict<string, V> {
  if type_of(d) != "dict" {
    return {}
  }
  let drop_nil = options.drop_nil ?? false
  return __dict_pick_keys(d, selected_keys, drop_nil)
}

// Check if a store key is stale (older than max_age_seconds).
// Uses store_get/store_set convention: timestamps stored as key + "_ts".
/** store_stale. */
pub fn store_stale(key, max_age_seconds) {
  let ts = store_get(key + "_ts")
  if ts == nil {
    return true
  }
  return timestamp() - ts > max_age_seconds
}

// Refresh a store key's timestamp to now.
/** store_refresh. */
pub fn store_refresh(key) {
  store_set(key + "_ts", timestamp())
}