This is Charming1. For Charming2, go to charmingjs.org. This is v1. For v2, go to charmingjs.org.

Process

Preparing data to be rendered.

cm.each

Calls the specified function on each datum of a flow, and returns a new flow that contains the data. The function is being passed the current datum(d), the current index(i), the current group(data) and the flow(flow).

const data = [
  { name: "Locke", number: 4 },
  { name: "Reyes", number: 8 },
  { name: "Ford", number: 15 },
  { name: "Jarrah", number: 16 },
  { name: "Shephard", number: 23 },
  { name: "Kwon", number: 42 },
];

app.data(data).process(cm.each, (d) => d.number * 2);

cm.eachRight

Like cm.each, except iterates from right to left.

const data = [
  { name: "Locke", number: 4 },
  { name: "Reyes", number: 8 },
  { name: "Ford", number: 15 },
  { name: "Jarrah", number: 16 },
  { name: "Shephard", number: 23 },
  { name: "Kwon", number: 42 },
];

app.data(data).process(cm.eachRight, (d, i, data) => {
  if (d.number > 30) data.splice(i, 1);
});

cm.filter

Calls the specified function on each datum of a flow, and returns a new flow that contains the data meeting true condition. The function is being passed the current datum(d), the current index(i), the current group(data) and the flow(flow).

const data = [
  { name: "Locke", number: 4 },
  { name: "Reyes", number: 8 },
  { name: "Ford", number: 15 },
  { name: "Jarrah", number: 16 },
  { name: "Shephard", number: 23 },
  { name: "Kwon", number: 42 },
];

app.data(data).process(cm.filter, (d) => d.number % 2 === 0);

cm.map

Calls the specified function on each datum of a flow, and returns a new flow that contains the new data. The function is being passed the current datum(d), the current index(i), the current group(data) and the flow(flow).

const data = [
  { name: "Locke", number: 4 },
  { name: "Reyes", number: 8 },
  { name: "Ford", number: 15 },
  { name: "Jarrah", number: 16 },
  { name: "Shephard", number: 23 },
  { name: "Kwon", number: 42 },
];

app.data(data).process(cm.map, (d) => ({ ...d, number: d.number * 2 }));

cm.derive

Calls value on each datum of a flow to derive a new field key for each entries of the specified object, and returns a new flow that contains the new data. The value is being passed the current datum(d), the current index(i), the current group(data) and the flow(flow).

const data = [
  { name: "Locke", number: 4 },
  { name: "Reyes", number: 8 },
  { name: "Ford", number: 15 },
  { name: "Jarrah", number: 16 },
  { name: "Shephard", number: 23 },
  { name: "Kwon", number: 42 },
];

app.data(data).process(cm.derive, {
  double: (d) => d.name * 2,
  upper: (d) => d.name.toUpperCase(),
});

cm.push

Appends the specified datum to a flow, and returns a new flow that contains the new datum.

If datum is not a function, appends it to all groups.

const data = [
  { name: "Locke", number: 4 },
  { name: "Reyes", number: 8 },
  { name: "Ford", number: 15 },
  { name: "Jarrah", number: 16 },
  { name: "Shephard", number: 23 },
  { name: "Kwon", number: 42 },
];

app.data(data).process(cm.push, { name: "Jim", number: 25 });

If the flow has multiple groups(such as flow.data followed by app.data), then datum should typically be specified as a function. The function will be evaluated for each group in order, being passed the group(group), the group index(i), all the groups(data) and this flow(flow).

const matrix = [
  [" +", "-", "+ "],
  [" |", cm.wch("🚀"), "| "],
  [" +", "-", "+ "],
];

app
  .data(matrix)
  .append(cm.group, { y: (_, i) => i })
  .data((d) => d)
  .process(cm.push, (group, i, groups) => ` `);