Completeable
Updated on August 22, 2024Source codeTests
Completeable is a class that enriches a string, allowing it to:
- Store a current text selection
- Extract a segment of itself
- Replace the segment or the full string with a completed string, and automatically update the text selection afterward
Construct a Completeable instance
The Completeable constructor accepts two parameters:
stringoptionsCompleteable instance. See the Completeable constructor options section for more guidance.Completeable constructor options
segment{ from: 'start', to: 'end' }Completeable instance will extract segments. See the How the Completeable instance extracts segments section for more guidance.divider/\s/Tells the Completeable instance how segments of the string are divided. Has no effect when neither segments.from nor segments.to are set to divider.
See the How the Completeable instance extracts segments section for more info.
State and methods
stringThe string passed to the Completeable constructor.
If you assign a value directly to string, a setter will pass the new value to setString.
selectionAn object that describes the current selection.
selection.start and selection.end indicate the index-based start and end positions (Numbers) of the currently selected text, and selection.direction indicates the selection direction (String, forward, backward, or none).
If you assign a value directly to selection, a setter will pass the new value to setSelection.
Completeable references selection when extracting segments of text. See the How the Completeable instance extracts segments section for more info on how that works.
statusThe status (String) of the Completeable instance.
status is constructing while the instance is constructing, and ready after the instance is constructed. status changes to completing right after the complete is called, then changes to completed after text completion is done.
segmentstring. See the How the Completeable instance extracts segments section for more info.dividerIndicesWhen you use the segment constructor option to set segment.from or segment.to to divider, then dividerIndices will be an object describing the position of detected dividers around the current segment.
dividerIndices.before and dividerIndices.after indicate the index-based position (Number) of the divider before the segment, and the divider after the segment.
If neither segment.from nor segment.to are set to divider, then dividerIndices is generally not useful.
setString(newString)Completeable instance's stringstring (String)Completeable instance (this)setSelection(selection)Sets the Completeable instance's selection.
See the How the Completeable instance extracts segments section and the How the Completeable instance completes strings and computes new selections section for more info.
selection (Object)Completeable instance (this)complete(completion, options)Completes the string, replacing segment with a completed piece of text, then computes a new selection based on the options.
For more guidance on the complete method, see the How the Completeable instance completes strings and computes new selections section.
Completeable instance (this) How the Completeable instance extracts segments
Completeable's internal workflow is:
- Analyze the
stringto determine what portion of text (thesegment) should be replaced if you call thecompletemethod. - When the
completemethod is called, replace thesegmentwith thecompletionpassed as the first argument to thecompletemethod.
When analyzing the string, your Completeable instance is specifically looking for two pieces of information:
- The index-based position (Number) of the start of the
segment - The index-based position (Number) of the end of the
segment
And the factors that influence that analysis are:
- The
fromandtoproperties of theoptions.segmentoption passed to the constructor - The
options.divideroption passed to the constructor, although this is only relevant whenoptions.segment.fromORoptions.segment.towere set to'divider' - The
startandendproperties of the currentselectionobject
Here are breakdowns of exactly how each of those factors impacts Completeable's calculation of the segment start and end indices:
segment.fromsegment's start indexstartsegment's start index is always 0selectionsegment's start index is always equal to selection.startdividerTo find the start index, Completeable goes to the selection.start position and steps backward through the string, one character at a time, looking for a match to the regular expression passed to options.divider (/\s/ by default).
The start index of the segment is the match's index plus one. If no match is found, the segment's start index will be 0.
The match's index also gets stored in dividerIndices.before in case you need to reference it, or use it in debugging.
segment.tosegment's end indexendsegment's end index is always the length of the string minus one.selectionsegment's end index is always equal to selection.enddividerTo find the end index, Completeable goes to the selection.end position and steps forward through the string, one character at a time, looking for a match to the regular expression passed to options.divider (/\s/ by default).
The end index of the segment is the match's index minus one. If no match is found, the segment's end index will be the length of the string minus one.
The match's index also gets stored in dividerIndices.after in case you need to reference it, or use it in debugging.
Finally, to finish extracting the segment, Completeable slices the string from the start index through the end index.
How the Completeable instance completes strings and computes new selections
As mentioned above, the complete method accepts two parameters: completion and options.
completion should be a completed piece of text that will replace the segment within the string (or the entire string, if you haven't changed the default constructor options).
options is an object, with just one property. Here's a breakdown:
selectcompletionControls how Completeable will set the new selection after completing text. Valid values are completion or completionEnd.
If options.select is completion, Completeable will set selection.start to the start index of the completed text, and selection.end to the end index of the completed text.
If options.select is completionEnd, Completeable will set both selection.start and selection.end to the end index of the completed text.
Using with TypeScript
Nothing special to know about using Completeable with TypeScript 🚀
API design compliance
options object.stringsetStringset<Property> methodssetSelectionstatus, segment, dividerIndicescompletestop methodable