git » hoplite.git » master » tree

[master] / record.mjs

import { input, transaction } from './reactives.mjs';

class Record {
  constructor(fields) {
    this.fields = fields;
    this.inputs = Object.fromEntries(fields.map(f => [f, input()]))
  }
  get(k) {
    if (this.fields.indexOf(k) === -1)
      throw new Error(`Unknown field: ${k}`);
    return this.inputs[k];
  }
  clear() {
    transaction(() => {
      Object.values(this.inputs).forEach(c => c.set());
    });
  }
  asTriples(eid) {
    return Object.entries(this.inputs)
      .filter(([f, c]) => c.value)
      .map(([f, c]) => [eid, f, c.value]);
  }
}

function record(...fields) {
  return new Record(fields);
} 

export { record };