OverviewFilesCommitsIssuesDocsReleasesPeers

keet-deobfuscated / @holepunchto / keet-indexing / plugins / word-count / apply.js

22 lines · javascript ref:
const e = require("sub-encoder"),
  t = require("compact-encoding"),
  { ChatSchema: i } = require("@holepunchto/keet-core-schemas"),
  { AddChatMessageOperation: o } = i.Operations
module.exports = class {
  static PLUGIN_NAME = "@holepunchto/word-count"
  constructor(i, o) {
    ;((this.view = i),
      (this.prefix = o),
      (this._enc = { keyEncoding: new e(o, t.raw.string), valueEncoding: t.uint }))
  }
  async apply(e) {
    if (e.type !== o.TYPE || !e.value.text) return
    const { text: t } = e.value
    for (const e of t.split(" ")) {
      const t = e.toLowerCase(),
        i = await this.view.bee.get(e, this._enc)
      await this.view.bee.put(t, i ? i.value + 1 : 1, this._enc)
    }
  }
}