Stringifies single keys in string or Key from, as well as chords, chains, full shortcuts, and commands.
In the case of string key ids and Shortcuts, some additional information is required to be able to convert the keys and commands back into the full commands and consistently stringify them.
ProtectedstringifyProtectedstringifyStringifies commands by name (stringify interprets single strings as key ids.).
Optionalcommand: Command<string, CommandExecute<string>, Condition>ProtectedstringifyOptionalcondition: ConditionProtectedstringifyStringifies lists of keys, shortcuts, or commands.
The type must be specified because we can't magically tell what type something is for string lists and/or empty lists.
Stringifies the property values of items when there is an error (e.g. "You cannot change prop x from a to b because of y." a and b here being the property values).
Protectedstringify
The default class based implementation of the IStringifier interface.
It can be passed (and a default instance is passed by default) to most functions to specify how to stringify items in errors.
The default method
stringifycan be called with any key, chord, chain, shortcut, etc. and calls the respective DefaultStringifierOptions method depending on the type property or in the case of chains, chords, and keys, the array depth.That method called then calls any others it needs (e.g. if you pass a shortcut, it will call
stringifyShortcutwhich will callstringifyCommand,stringifyCondition, andstringifyChain, which will callstringifyChordand so on)These can be customized by changing the options of the default instance (see Customizing below). These options are methods that describe in simpler term how items should be joined, without handling all the logic (the class pieces it all together)
For chains the default method uses a key's label and combines keys inside chords with
+and the chords of shortcut chains with a space.For shortcuts, the default is:
If the condition or command are undefined: - For command it will still say it's undefined. - The condition is removed entirely if it's undefined.
stringifyListsreturns a lists joined by a comma and new line:Customizing
Ideally a single stringifier should be created and shared amongst all instances. This is already taken care of if you do not pass a custom stringifier, a default stringifier instance is re-used throughout. Unless you're implementing your own IStringifier, you should not need to pass the default one around.
You can just import it early and change it's options.