airtable-formula-builder
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

airtable-formula-builder Build Status

Helper utility to create Airtable's Formula strings.

Install

npm install airtable-formula-builder

Usage

Use a helper library to generate the formulas, like lodash or Ramda: Can be used with Airtable's API field filterByFormula.

    import f from "airtable-formula-builder";
    import R from "ramda";
 
    const idsFormula = R.compose(
      R.flatten,
      R.map(id => [
        f.CONTAINS(id, COLUMN.AIRTABLE_PLAY_URL),
        f.CONTAINS(id, COLUMN.RECORD_ID)
      ])
    )(ids);

TypeScript advanced sample:

    const containsReducer = (value: ConstantCollection, field: string) => (
      accstring[],
      curstring
    )string[] => R.append(f.CONTAINS(R.prop(cur, value), field), acc);
 
    const normalizedCategories = R.map(
      paramCaseAndRmAccents,
      R.defaultTo([], categories as string[])
    );
 
    const categoriesReducer = containsReducer(CATEGORY, COLUMN.CATEGORIES);
    const categoriesFormula = R.compose(f._str, R.reduce(categoriesReducer, []))(
      normalizedCategories
    );
 
    const normalizedModifiers = R.map(
      paramCaseAndRmAccents,
      R.defaultTo([], modifiers as string[])
    );
    const modifiersReducer = containsReducer(MODIFIER, COLUMN.MODIFIERS);
    const modifiersFormula = R.compose(f._str, R.reduce(modifiersReducer, []))(
      normalizedModifiers
    );
 
    const normalizedType: string = paramCaseAndRmAccents(type);
    const curType: string = R.prop(normalizedType, TYPE);
    const typeFormula: string = f.EQUALS(COLUMN.TYPE, curType);
 
    const formula: string = f.AND([
      typeFormula,
      f.OR([categoriesFormula, modifiersFormula])
    ]);

API

    AND: (query: ReadonlyArray<string>) => string;
    OR: (query: ReadonlyArray<string>) => string;
    EQUALS: (column: string, value: string) => any;
    FIND: (query: string, column: string, operator: string, value: any) => string;
    CONTAINS: (query: string, column: string) => string;
    _str: (arr: ReadonlyArray<string>) => string;

License

MIT © Gabriel Almeida

Package Sidebar

Install

npm i airtable-formula-builder

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

4.66 kB

Total Files

6

Last publish

Collaborators

  • gbr