Home Reference Source Test Repository

lib/init.js

"use strict";

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.init = init;

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

/**
 * Returns all the elements of a list except the last one.
 *
 * @since v0.1.0
 * @param {Array} list - The array
 * @return {Array} - All elements except the last one.
 * @example
 *   init([1, 2, 3]) // => [1, 2]
 *   init([29, 10]) // => [29]
 *   init([300]) // => [300]
 *
 *   init('abc') //=> ['a', 'b']
 *   init('ze') // => ['z']
 *   init('q') // => ['q']
 *   init('') //=> undefined
 */
function init(list) {
    if (list.length === 0) return undefined;
    var results = [];
    list.length > 1 ? results.push.apply(results, _toConsumableArray(list.slice(0, -1))) : results.push.apply(results, _toConsumableArray(list));
    return results;
}