Skip to main content

Library API

The command line runs on these functions. Import them from @vitness/fds-encoder once the package is installed in your project. The values in the comments are what each call returns.

Keys​

checkKey, writeKey, keyType and compareKeys work on all four key types.

import { checkKey, compareKeys, KeyError, keyType, writeKey } from '@vitness/fds-encoder';

checkKey('FXI=1.0S/o:standing/m:el.flex/l:bell.dumb@hands');
// { ok: true, type: 'FXI', written: 'FXI=1.0S/m:el.flex/o:standing/l:bell.dumb@hands', issues: [] }

checkKey('FXE=1.0S/k:robot').issues;
// [{ code: 'unknown-kind', message: '"robot" is not an FXE kind.', at: 'k:robot' }]

writeKey('FXMG=1.0/thigh.posterior+hamstring'); // 'FXMG=1.0/hamstring'
keyType('FXM=1.0/deltoid.clavicular'); // 'FXM'

try {
writeKey('FXE=1.0S/k:robot');
} catch (err) {
if (err instanceof KeyError) console.log(err.issues[0].code); // 'unknown-kind'
}

compareKeys('FXM=1.0/biceps-brachii.long', 'FXM=1.0/biceps-brachii');
// { type: 'FXM', result: 'more-specific', specific: 'a', shared: ['biceps-brachii.long'] }

compareKeys('FXI=1.0S/m:el.flex/o:standing/l:bell.dumb@hands', 'FXI=1.0S/m:el.flex/o:?/l:bell.dumb@hands').layers?.o;
// 'narrower'
FunctionReturns
checkKey(text){ ok, type, written, issues }. written is the standard written form. Each issue has a code, a message and, where it applies, at
writeKey(text)The written form, or throws a KeyError that lists every issue
keyType(text)'FXI', 'FXE', 'FXM', 'FXMG', or undefined
compareKeys(a, b){ type, result, specific?, layers?, shared? }. result is same, more-specific, overlap or different. Exercise and equipment keys give layers; muscle and category keys give shared. Throws a KeyError when a key doesn't check or the types differ
KeyErrorThe error writeKey and compareKeys throw. Its issues list what is wrong

parseFxi, parseFxe, parseFxm and parseFxmg give the parsed layers and terms of one key type. standardFxi and standardFxe drop the extended layers and give the standard part of a key.

import { parseFxi, parseFxm, standardFxe } from '@vitness/fds-encoder';

parseFxi('FXI=1.0S/o:standing/m:el.flex/l:bell.dumb@hands').layers;
// { m: 'el.flex', o: 'standing', l: 'bell.dumb@hands' }

parseFxm('FXM=1.0S/biceps-brachii.long').key;
// { kind: 'part', muscle: 'biceps-brachii', part: 'long' }

standardFxe('FXE=1.0N/k:mac/l:ankles/s:prone/m:kn.flex/r:plate/c:arc'); // 'FXE=1.0S/k:mac/l:ankles/s:prone/m:kn.flex'
FunctionReturns
parseFxi(text){ ok, issues, canonical, layers, flag }. canonical is the written form and layers holds each layer's value
parseFxe(text){ ok, issues, canonical, configs, flag }. configs lists each configuration's kind k and layers; a combination machine has several
parseFxm(text){ ok, issues, canonical, key }. key is a muscle, { kind: 'muscle', muscle }, or a part, { kind: 'part', muscle, part }
parseFxmg(text){ ok, issues, canonical, key }. key.terms lists the categories and muscles of the union
standardFxi(text), standardFxe(text)The standard part of a key: the written form without its extended layers. Throws for a key that doesn't check
formatFxm(key)The text of a parsed muscle key: formatFxm(parseFxm('FXM=1.0/deltoid.acromial').key) is 'FXM=1.0/deltoid.acromial'
FXM_PREFIX, FXMG_PREFIXThe headers every muscle and category key starts with: 'FXM=1.0/' and 'FXMG=1.0/'

Exercise and equipment​

crossCheck(fxi, fxe) checks an exercise key against the key of its equipment. An empty list means they agree. The warning codes are invalid, main-equipment, posture, load-interface and off-design. Checking an exercise against its equipment, on the equipment key page, says when each check fails.

import { crossCheck, mainEquipment, parseFxi } from '@vitness/fds-encoder';

const squat = 'FXI=1.0S/m:hip.ext+kn.ext/o:standing/l:bar.straight@back';

crossCheck(squat, 'FXE=1.0S/k:imp/f:bar.straight'); // []
crossCheck(squat, 'FXE=1.0S/k:imp/f:bell.dumb')[0].code; // 'main-equipment'

mainEquipment(parseFxi(squat).layers ?? {});
// { token: 'bar.straight', fxe: 'FXE=1.0S/k:imp/f:bar.straight' }
FunctionReturns
crossCheck(fxi, fxe)A list of { code, message }, empty when the two keys agree
mainEquipment(layers)An exercise's main equipment from its l and s layers: { token, fxe }, the exercise-key code that names it, when one does, and the equipment key it stands for

Muscles and categories​

import { fxmgCategoriesOf, fxmgMembers, fxmInFxmg } from '@vitness/fds-encoder';

fxmInFxmg('FXM=1.0/rectus-femoris', 'FXMG=1.0/a:hip.flex');
// { relation: 'in', shared: ['rectus-femoris'] }

fxmgMembers('FXMG=1.0/hamstring');
// ['FXM=1.0/biceps-femoris', 'FXM=1.0/semimembranosus', 'FXM=1.0/semitendinosus']

fxmgCategoriesOf('FXM=1.0/semitendinosus');
// ['FXMG=1.0/a:hip.ext', 'FXMG=1.0/a:kn.flex', 'FXMG=1.0/entire', 'FXMG=1.0/hamstring', 'FXMG=1.0/thigh', 'FXMG=1.0/thigh.posterior']
FunctionReturns
fxmInFxmg(fxm, fxmg){ relation, shared }. relation is in, partly-in or not-in
fxmgMembers(fxmg)A category's members as muscle keys
fxmgCategoriesOf(fxm, index?)Every category the muscle is wholly in. Pass an index from buildFxmgIndex() when you call it many times
buildFxmgIndex()A map from each muscle and part to every category that holds it
expandFxm(fxm), expandFxmg(fxmg)The muscles and parts a key covers, without the prefix: expandFxm('FXM=1.0/biceps-brachii') is ['biceps-brachii.long', 'biceps-brachii.short']
expandMuscleKey(key)The same for either a muscle key or a category key
fxmTerm(fxm)The term a muscle key names: { name, anchors, uberonNote? }. anchors are its FMA and Uberon terms; uberonNote says what Uberon has when it has no term for the structure
fxmgTerm(fxmg)The term a key of one category names: { name, anchors, members }
fxmgKeys()Every category key, sorted: 66 of them, from 'FXMG=1.0/a:ank.df'

The data behind them is exported too: FXM_MUSCLES (the muscles and their parts, with their anatomy ontology terms), FXMG_SETS (the named sets), FXMG_ACTIONS (one category for each joint action) and ACTION_PRIME_MOVERS (each joint action's prime movers). FXM_VOCABULARY and FXMG_VOCABULARY are the same data indexed for lookup; every muscle and category function takes one as its last, optional argument. The anatomy labels and ids come from FMA and Uberon under CC BY: Sources and licences gives each source and its licence.

Building keys​

QUESTIONS and FLOW are the question flow that fds-encoder questions prints. build turns one entry of answers into keys.

import { AnswerError, build, QUESTIONS } from '@vitness/fds-encoder';

QUESTIONS.length; // 201
QUESTIONS[0].id; // 'subject'

build({ id: 'eq-7', answers: { subject: 'equipment', 'eq.kind': 'imp', 'eq.form.imp': 'bar.ez', 'eq.details': 'usual' } });
// { id: 'eq-7', fxi: null, fxe: 'FXE=1.0S/k:imp/f:bar.ez', path: ['subject', 'eq.kind', 'eq.form.imp', 'eq.details'] }

try {
build({ id: 'ex-103', answers: { subject: 'usual' } });
} catch (err) {
if (err instanceof AnswerError) console.log(err.problems[0]);
// 'changes: no answer. Which of these does the exercise state, beyond its motion, body position, load and sides?'
}
ExportDoes
build(entry)Builds { id, fxi, fxe, path }. path lists the questions asked, in order. Throws an AnswerError whose problems say what to fix
AnswerErrorThe error build throws for answers that are missing, invalid or contradictory. entryId and problems say which entry and what to fix
InternalErrorThe error build throws when it writes a key its own check refuses. It is a defect in the Encoder, never in the answers
buildWith(id, provide)Builds from any answer source: provide(question, reading) returns the answer to each question on the path
openOptions(question, reading)The options of a question that apply, given the answers so far. A form shows only these
QUESTIONS, QUESTION_BY_ID, FLOW, FACTSThe questions, by order and by id, the whole flow as one JSON-ready object, and the facts its conditions use
startAsk(rows, existing?), stepAsk(state, line)The ask session as a pure stepper: each call returns the next state, the lines to print and whether to save
askRows(data), answersText(state)Read a rows file, and write the answers file for a state
import { startAsk, stepAsk } from '@vitness/fds-encoder';

const first = startAsk([{ id: 'ex-101', name: 'Dumbbell Curl' }]);
first.state.question; // 'subject'

const next = stepAsk(first.state, '1');
next.state.question; // 'changes'
next.state.answers; // { subject: 'usual' }

Records​

These read and write only the record's own key field and the key fields of the references it holds: fxi on an exercise reference (exercise, alternatives[]), fxe on an equipment reference (equipment, equipment.required[], equipment.optional[]), and fxm and fxmg on a muscle reference (targets.primary[], targets.secondary[]). A field with one of those names anywhere else is not a key and is never read or written, and nothing under metadata, attributes or extensions is. Anything else a platform keeps about a key, such as who wrote it or a confirmed link, belongs in attributes or extensions.

import { checkRecord, matchEntryOf, recordKeys } from '@vitness/fds-encoder';

const record = {
exerciseId: 'ex-201',
fxi: 'FXI=1.0S/o:standing/m:el.flex/l:bell.dumb@hands',
canonical: { name: 'Dumbbell Curl' },
equipment: { required: [{ id: 'eq-1', name: 'Dumbbell', fxe: 'FXE=1.0S/k:imp/f:bell.dumb' }] },
metadata: { fxi: 'not a key' },
};

recordKeys(record).map((k) => k.path); // ['fxi', 'equipment.required[0].fxe']

const checked = checkRecord(record);
checked.problems; // []
checked.warnings; // []
checked.rewritten;
// [{ path: 'fxi', from: 'FXI=1.0S/o:standing/m:el.flex/l:bell.dumb@hands', to: 'FXI=1.0S/m:el.flex/o:standing/l:bell.dumb@hands' }]

matchEntryOf(record);
// { id: 'ex-201', key: 'FXI=1.0S/o:standing/m:el.flex/l:bell.dumb@hands', name: 'Dumbbell Curl', fxe: 'FXE=1.0S/k:imp/f:bell.dumb' }
FunctionReturns
recordKeys(record)Every key in the record, as { path, field, key }, in document order. A property name that isn't a plain word is quoted in the path: ["a.b"]
checkRecord(record){ problems, warnings, rewritten, record }. record is a copy with every key that checks in its written form
matchEntryOf(record)The record as a match entry, or undefined when it has no key of its own or no id. Only the record's own properties count
KEY_FIELDSThe four key fields and the key type each holds

Matching​

match(entry, library, options?) suggests library entries for one entry. Give it library entries with the same type of key.

import { match, nameSimilarity } from '@vitness/fds-encoder';

const library = [
{ id: 'lib-1', key: 'FXI=1.0S/m:el.flex/o:standing/l:bell.dumb@hands', name: 'Dumbbell Biceps Curl' },
{ id: 'lib-2', key: 'FXI=1.0S/m:el.flex/o:seated/l:bell.dumb@hands', name: 'Seated Dumbbell Curl' },
];

match({ id: 'imp-2', key: 'FXI=1.0S/m:el.flex/o:?/l:bell.dumb@hands', name: 'Dumbbell Curl' }, library, { top: 2 });
// {
// kind: 'confirm',
// candidates: [
// { id: 'lib-1', via: 'key', result: 'more-specific', specific: 'b' },
// { id: 'lib-2', via: 'key', result: 'more-specific', specific: 'b' },
// ],
// why: 'no library entry has the same key',
// }

nameSimilarity('Dumbbell Curl', 'Dumbbell Biceps Curl'); // 0.8

An entry whose key doesn't check matches nothing by its key, so its candidates come by name alone. The result then carries warnings, which say so:

match({ id: 'imp-3', key: 'FXI=1.0S/m:el.bend/o:standing/l:bell.dumb@hands', name: 'Dumbbell Curl' }, library, { top: 1 });
// {
// kind: 'confirm',
// candidates: [{ id: 'lib-1', via: 'name' }],
// why: 'the key does not check',
// warnings: ['The key FXI=1.0S/m:el.bend/o:standing/l:bell.dumb@hands does not check, so it matches by name only: "el.bend" is not a joint action.'],
// }

The result's kind is identical, confirm or absent, as in fds-encoder match. top is the length of the candidate list, 3 by default. nameSimilarity scores two names from 0 to 1 and orders candidates that tie on their keys.

Code lists and registries​

codes holds every code list the keys draw on, with a short description of each code. buildRegistries() generates the code registries published under specification/registries/, by file name.

import { buildRegistries, codes } from '@vitness/fds-encoder';

codes.FXE_KINDS.mac;
// 'Strength machine: a guided mechanism with fixed body contacts, built for one movement.'

Object.keys(buildRegistries());
// ['exercise-key.registry.json', 'equipment-key.registry.json', 'body-points.registry.json',
// 'body-positions.registry.json', 'muscle-key.registry.json', 'muscle-category-key.registry.json']

Types​

The package ships the TypeScript types of everything above:

AreaTypes
KeysCheckedKey, KeyComparison, KeyResult, KeyType, Issue, LayerRelation
Exercise and equipment keysParsedFxi, ParsedFxe, FxeConfig, MainEquipment
Muscle and category keysFxmKey, ParsedFxm, FxmVocabulary, FxmgKey, FxmgTerm, ParsedFxmg, FxmInFxmg, Anchor, FxmMuscle, FxmPart, FxmgSet, FxmgMember, FxmgAction
Records and matchingRecordKey, RecordCheck, RecordIssue, KeyField, MatchEntry, MatchOptions, MatchResult, Candidate
Building keysAnswer, Answers, Built, Entry, Provider, Cond, FactId, FlowOption, FlowQuestion, AskRow, AskState, AskStep
RegistriesRegistry

See also​