dragoman 0.7.17

Server for scholarly metadata in commonmeta format stored in a SQLite database.
/**
 * The place a list in an entity's page keeps in the URL.
 *
 * An entity's page can hold two lists at once — an organization's own works and
 * the works it funded — and each is paged, narrowed and searched on its own. So
 * each keeps its own parameters, told apart by a prefix: the works above read
 * `page`, `type`, `year` and `q`, the works funded below `fpage`, `ftype`,
 * `fyear` and `fq`. A link can then say what both lists are showing, and
 * narrowing one leaves the other where the reader left it.
 *
 * That last part is the whole reason this is a module of its own rather than
 * two functions beside the component: writing one list's parameters means
 * keeping every parameter that is not its own, and `type=` and `ftype=` are
 * easy to confuse in either direction. The rules are small and the mistakes are
 * quiet, which is what a test is for.
 */

/** The works list's parameters, which carry no prefix. */
export const LIST_SCOPE = ''

/** The funded works' parameters, prefixed so both lists fit in one address. */
export const FUNDED_SCOPE = 'f'

/** The parameters one list owns, in the order they are written. */
export const listScopeKeys = (prefix = LIST_SCOPE) =>
  ['type', 'year', 'q', 'page'].map(key => `${prefix}${key}`)

/** What a list holds, as a list whatever it was handed — a link written before
 *  a facet could hold several carries one value, not a list. */
export const asList = value =>
  Array.isArray(value) ? value.filter(v => v || v === 0) : (value || value === 0 ? [value] : [])

/**
 * What a query string says one list is showing.
 *
 * Every occurrence of a parameter is read, since a facet may be picked more
 * than once; a blank one is no value, and the same value twice selects the same
 * works, so it is kept once. A page that is not a counting number is the first
 * one: `?page=0` and `?page=nonsense` would otherwise ask for a negative
 * offset. A year is four digits or more — `?year=12` is not a publication year,
 * and reading it as one would narrow the list to nothing.
 */
export function listScopeFromSearch(search, prefix = LIST_SCOPE) {
  const params = new URLSearchParams(search ?? '')
  const once = values => [...new Set(values)]
  const page = parseInt(params.get(`${prefix}page`) ?? '', 10)
  return {
    page: Number.isFinite(page) && page > 0 ? page : 1,
    type: once(params.getAll(`${prefix}type`).map(t => t.trim()).filter(Boolean)),
    year: once(
      params
        .getAll(`${prefix}year`)
        .map(value => parseInt(value, 10))
        .filter(year => Number.isFinite(year) && year > 999),
    ),
    q: (params.get(`${prefix}q`) ?? '').trim(),
  }
}

/**
 * The query string one list reads as, with everything that is not its own kept
 * as it stands — the other list's parameters among it.
 *
 * Only what differs from the default is written: nothing for a facet with
 * nothing picked, no query where there is none, and no page for the first one.
 * One parameter per value picked, in the order they were picked, because a
 * comma-separated list would not do: a commonmeta type and a subject both have
 * commas in their names.
 *
 * Returned without the leading `?`, and empty when nothing is left — which is
 * what makes a cleared list's address the page's own again.
 */
export function listScopeSearch(search, { page = 1, type = [], year = [], q = '' } = {}, prefix = LIST_SCOPE) {
  const mine = listScopeKeys(prefix).map(key => `${key}=`)
  // A part is this list's own only when its whole name matches: `type=` must
  // not claim `ftype=`, and `page=` must not claim `fpage=`.
  const kept = (search ?? '')
    .replace(/^\?/, '')
    .split('&')
    .filter(part => part && !mine.includes(`${part.split('=')[0]}=`))
  for (const value of asList(type)) kept.push(`${prefix}type=${encodeURIComponent(value)}`)
  for (const value of asList(year)) kept.push(`${prefix}year=${value}`)
  if (q) kept.push(`${prefix}q=${encodeURIComponent(q)}`)
  if (page > 1) kept.push(`${prefix}page=${page}`)
  return kept.join('&')
}