@newdash/newdash
Version:
javascript/typescript utility library
40 lines (39 loc) • 1.07 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.dropRight = void 0;
const slice_1 = __importDefault(require("./slice"));
const toInteger_1 = __importDefault(require("./toInteger"));
/**
* Creates a slice of `array` with `n` elements dropped from the end.
*
* @since 5.16.0
* @category Array
* @param array The array to query.
* @param n The number of elements to drop.
* @returns Returns the slice of `array`.
* @example
*
* ```js
* dropRight([1, 2, 3])
* // => [1, 2]
*
* dropRight([1, 2, 3], 2)
* // => [1]
*
* dropRight([1, 2, 3], 5)
* // => []
*
* dropRight([1, 2, 3], 0)
* // => [1, 2, 3]
* ```
*/
function dropRight(array, n = 1) {
const length = array == null ? 0 : array.length;
n = length - (0, toInteger_1.default)(n);
return length ? (0, slice_1.default)(array, 0, n < 0 ? 0 : n) : [];
}
exports.dropRight = dropRight;
exports.default = dropRight;