| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | /*! |
| 2 | * array-slice <https://github.com/jonschlinkert/array-slice> |
| 3 | * |
| 4 | * Copyright (c) 2014-2017, Jon Schlinkert. |
| 5 | * Released under the MIT License. |
| 6 | */ |
| 7 | |
| 8 | 'use strict'; |
| 9 | |
| 10 | module.exports = function slice(arr, start, end) { |
| 11 | var len = arr.length; |
| 12 | var range = []; |
| 13 | |
| 14 | start = idx(len, start); |
| 15 | end = idx(len, end, len); |
| 16 | |
| 17 | while (start < end) { |
| 18 | range.push(arr[start++]); |
| 19 | } |
| 20 | return range; |
| 21 | }; |
| 22 | |
| 23 | function idx(len, pos, end) { |
| 24 | if (pos == null) { |
| 25 | pos = end || 0; |
| 26 | } else if (pos < 0) { |
| 27 | pos = Math.max(len + pos, 0); |
| 28 | } else { |
| 29 | pos = Math.min(pos, len); |
| 30 | } |
| 31 | |
| 32 | return pos; |
| 33 | } |