Yamato DaiwaE(CMA)S(cript) extensions

getLastElementOfArray

Overload No. 1
null will be returned if target array is empty
<ArrayElement>
(
):ArrayElement | null
Overload No. 2
UnexpectedEventError will be thrown is target array is empty
<ArrayElement>
(
):ArrayElement
Options
{
mustThrowErrorIfArrayIsEmpty
}

Returns the last element of the indexed array if the array is not empty. If the target array is empty,

  • An UnexpectedEventError will be thrown if the second parameter is specified with the value { mustThrowErrorIfArrayIsEmpty: true }. Additionally, TypeScript will infer that if the function executes without errors and the type of the array element is annotated (explicitly or implicitly) as neither undefined nor null, the returned value could not be undefined or null.
  • null will be returned if the second parameter has not been specified. If TypeScript is configured strictly, a non-null check will be required before using the returned value as non-nullable.

This function is not just an alternative to expressions like targetArray[targetArray.length - 1]; it also guarantees that the last element of the array exists.

Examples

Quick Input in IntelliJ IDEA Family of IDEs

Quick input in IntelliJ IDEA family of IDEs using the "Live templates" functionality.

Using the functionality of Live templates in the IntelliJ IDEA family if IDEs allows you to quickly input code such as a function invocation expression. To get the Live templates of the YDEE library, you need to install the official plugin of this library.

Steps for Using the Live Templates

If you have not used Live templates before, do not worry if the instructions below are too complicated. Once you have developed the habit of using Live templates (similar to the habit of using keyboard shortcuts), the following operations will take a matter of seconds.

  1. Copy the variable name containing the array or array expression to the clipboard. To make it possible for the IDE to fill in the correct value for the parameter, please develop the habit of copying each time before inputting the Live template of the getLastElementOfArray function.
  2. Begin to input the function name (getLastElementOfArray). There will be 2 options for autocomplete:
    1. Circled icon with the letter: it is the autocompletion of the function name, which is the standard functionality of the IDE. If you press the Enter key, the full function name will be inputted and also the function import declaration will be inserted if required. Not bad, but better automation is possible.
    2. The icon with the cliche is the template we need. Press Enter again. The code template will be inserted, with the value of the first parameter filled with the clipboard content and selected by the cursor. If you follow this procedure, there is no need to edit the inserted value; exit the parameter editing mode by pressing Enter again.
  3. Delete the unnecessary code.

You can use the alias of this Live Template — gleoa, which consists of the first letters of all words in the function name. However, the disadvantage of such aliases is that they are harder to memorize. To get the autocomplete for getLastElementOfArray, it is enough to remember the first letters of the function name.

External Links