super-utils-plus
Version:
A superior alternative to Lodash with improved performance, TypeScript support, and developer experience
73 lines (72 loc) • 1.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.first = void 0;
exports.head = head;
exports.last = last;
exports.tail = tail;
exports.initial = initial;
/**
* Gets the first element of array.
*
* @param array - The array to query
* @returns The first element of array
*
* @example
* ```ts
* head([1, 2, 3]);
* // => 1
*
* head([]);
* // => undefined
* ```
*/
function head(array) {
return array && array.length ? array[0] : undefined;
}
/**
* Gets the last element of array.
*
* @param array - The array to query
* @returns The last element of array
*
* @example
* ```ts
* last([1, 2, 3]);
* // => 3
* ```
*/
function last(array) {
return array && array.length ? array[array.length - 1] : undefined;
}
/**
* Gets all but the first element of array.
*
* @param array - The array to query
* @returns The slice of array
*
* @example
* ```ts
* tail([1, 2, 3]);
* // => [2, 3]
* ```
*/
function tail(array) {
return array && array.length ? array.slice(1) : [];
}
/**
* Gets all but the last element of array.
*
* @param array - The array to query
* @returns The slice of array
*
* @example
* ```ts
* initial([1, 2, 3]);
* // => [1, 2]
* ```
*/
function initial(array) {
return array && array.length ? array.slice(0, -1) : [];
}
// Aliases
exports.first = head;