slice.js 696 B

1234567891011121314151617181920212223242526272829303132333435
  1. /**
  2. * Create slice of source array or array-like object
  3. */
  4. function slice(arr, start, end){
  5. var len = arr.length;
  6. if (start == null) {
  7. start = 0;
  8. } else if (start < 0) {
  9. start = Math.max(len + start, 0);
  10. } else {
  11. start = Math.min(start, len);
  12. }
  13. if (end == null) {
  14. end = len;
  15. } else if (end < 0) {
  16. end = Math.max(len + end, 0);
  17. } else {
  18. end = Math.min(end, len);
  19. }
  20. var result = [];
  21. while (start < end) {
  22. result.push(arr[start++]);
  23. }
  24. return result;
  25. }
  26. module.exports = slice;