index.js 517 B

123456789101112131415161718192021
  1. /*!
  2. * array-initial <https://github.com/jonschlinkert/array-initial>
  3. *
  4. * Copyright (c) 2014 Jon Schlinkert, contributors.
  5. * Licensed under the MIT license.
  6. */
  7. var isNumber = require('is-number');
  8. var slice = require('array-slice');
  9. module.exports = function arrayInitial(arr, num) {
  10. if (!Array.isArray(arr)) {
  11. throw new Error('array-initial expects an array as the first argument.');
  12. }
  13. if (arr.length === 0) {
  14. return null;
  15. }
  16. return slice(arr, 0, arr.length - (isNumber(num) ? num : 1));
  17. };