Skip to content

Plugins

Santhosh Kumar edited this page Jan 2, 2020 · 10 revisions

You can add more custom units and function via fcal API

Custom units

You can add a new unit type to the existing Unit using its unit ID, for that you need to provide ratio to its base unit ratio, in this case, it will be seconds.

The ratio of the base unit type usually is 1 scalar value.

Fcal.UseUnit({
  id: Unit.TIMEID,
  ratio: 2592000,
  phrases: ["month", "months"],
  singular: "month",
  plural: "months"
});

We can also set singular and plural of unit type for value formatting

To add new Unit

const units = [];

const id = "CONTENT";
units.push({
  id: id,
  ratio: 1,
  type: "char",
  phrases: ["char", "chars"],
  singular: "character",
  plural: "characters"
});

units.push({
  id: id,
  ratio: 5,
  type: "word",
  phrases: ["word", "words"],
  singular: "word",
  plural: "words"
});

units.push({
  id: id,
  ratio: 100,
  type: "page",
  phrases: ["page", "pages"],
  singular: "page",
  plural: "pages"
});

Fcal.UseUnits(units);

Custom functions

You can add new functionality to fcal using UseFunction. You need to provide function name, no of arguments it will take and function body

Function body will take two arguments, env is the environment of fcal through which you can get local variables and other functions. Args will give access to function arguments

const func = function(env, args) {
  return Math.random();
};

Fcal.UseFunction({ name: "random", arity: 0, func: func });

The return type of the function can be Fcal.Type, number or Decimal

Use -1 to specify no limit on the number of arguments the function will take. Max argument limit is 254

Constants

You can also define constants in Fcal whose value can not be replaced.

Fcal.useConstants({ newConstant: 123 });

Converters

Converter convert one value into another, It is just function takes a single argument and convert it into another value or type

Fcal.useConverter("incre", a => {
  a.n = a.n.add(1);
  return a;
});

To use converter in expression

45 cm as incre

Scales

To add custom scales to Fcal , use setScales function

Fcal.useScales({ tens: 10 });

Clone this wiki locally