Counting the number of times a word appears in a list

I’ve seen this desired pattern many times and the solution is always a bit complex. In my firm, we do this all the time - looking for basic counting metrics to render conclusions and ideally in charts. To overcome this complexity, I decided to create a custom Pack - a simple formula that given a list of terms (or numbers), counts the occurrences and places the results in an adjacent column.

Countify(
  <term>,
  <list>
)

The pack itself is amazingly simple; Coda has found a way to make me look like a genius.

/*

   ***********************************************************
   SmartPacks: Countify
   Copyright (c) 2021 by Global Technologies Corporation
   ALL RIGHTS RESERVED
   ***********************************************************

*/

//
// import the packs sdk
//
import * as coda from "@codahq/packs-sdk";

//
// create the pack
//
export const pack = coda.newPack();

//
// add the smartify phone number
//
pack.addFormula({
  
  //
  // pack name and description
  //
  name: "Countify",
  description: "A formula that counts occurrences of values in a field.",

  //
  // result type
  //
  resultType: coda.ValueType.Number,

  //
  // parameters
  //
  parameters: [
    coda.makeParameter({
      type: coda.ParameterType.String,
      name: "<value>",
      description: "The value to test in a column."
    }),
    coda.makeParameter({
      type: coda.ParameterType.StringArray,
      name: "<column>",
      description: "The column containing the values to be tested for occurrences."
    }),
  ],

  //
  // execute CountifySelect
  //
  execute: function ([thisText, thisList]) {
    return thisList.filter(x => ( x.toString().toLowerCase().indexOf(thisText.toString().toLowerCase()) > -1 )).length;
  },

});
3 Likes