]> gerrit.simantics Code Review - simantics/district.git/blob - shim.js
2761a1aad8340d1894759fec7338d622e2d18140
[simantics/district.git] / shim.js
1 'use strict';
2
3 var toInteger    = require('../../../number/to-integer')
4   , toPosInt     = require('../../../number/to-pos-integer')
5   , isPlainArray = require('../../is-plain-array')
6
7   , isArray = Array.isArray, slice = Array.prototype.slice
8   , hasOwnProperty = Object.prototype.hasOwnProperty, max = Math.max;
9
10 module.exports = function (start, end) {
11         var length, result, i;
12         if (!this || !isArray(this) || isPlainArray(this)) {
13                 return slice.apply(this, arguments);
14         }
15         length = toPosInt(this.length);
16         start = toInteger(start);
17         if (start < 0) start = max(length + start, 0);
18         else if (start > length) start = length;
19         if (end === undefined) {
20                 end = length;
21         } else {
22                 end = toInteger(end);
23                 if (end < 0) end = max(length + end, 0);
24                 else if (end > length) end = length;
25         }
26         if (start > end) start = end;
27         result = new this.constructor(end - start);
28         i = 0;
29         while (start !== end) {
30                 if (hasOwnProperty.call(this, start)) result[i] = this[start];
31                 ++i;
32                 ++start;
33         }
34         return result;
35 };