Lodash source code parsing — Slice

I have always heard that LoDash source code is written very well, in my spare time from work, READ the source code of LoDash, feel very clear to write, consider very comprehensive. Today, I will interpret slice source code and record my learning process and problems.

// Array is an array passed in, which is more fault-tolerant than slice
// start is the truncated start subscript
// end is the truncated tail index
function slice(array, start, end) {
  // Take the length of an array depending on whether it is an array, or return an empty array if not
  let length = array == null ? 0 : array.length;
  if(! length) {return [];
  }
  // if start is not passed, start from index 0
  start = start == null ? 0 : start;
  // If end is not passed, the array length index is truncated
  end = end === undefined ? length : end;
  // Start is negative when -start>length and 0 when -start>length, otherwise length + start
  if (start < 0) {
    start = -start > length ? 0 : length + start;
  }
  // If the user input exceeds the array length, end should be assigned length, plus length if the input is negative
  end = end > length ? length : end;
  if (end < 0) {
    end += length;
  }
  // Start < end
  // So when the start index is greater than the end index, the unreasonable interval, the interception length is set to 0
  // If it is reasonable to convert the result of end-start to a decimal number, assign the value to length

  // >>> here is an unsigned shift the point of moving 0 bits is to convert it to a decimal integer
  length = start > end ? 0 : (end - start) >>> 0;
  start >>>= 0;
  // New array length, and create a new array length of length
  let index = -1;
  const result = new Array(length);
  // Loop to start interception
  while (++index < length) {
    result[index] = array[index + start];
  }
  return result;
}

export default slice;
Copy the code

I see that I’m at the end of this, but I’m sure there are a lot of people who are confused by the ‘>>>’ operation notation, so here’s a bit more explanation.

Js bit operation, how to deal with the decimal part?

  • The answer is simply throw it away

How do non-numeric js types perform bitwise operations?

  • When js requires bit operations, for non-numeric types, the operand is first converted to an integer (i.e., 0) and then performed.

What’s special about unsigned right shift in JS?

  • Js is no different from other languages when moving bits greater than 0
  • When the number of moving bits is 0 and the value is negative, js will have a unique >>>0 actually does not change digits, but JS will replace the sign bit with 0, which is the shift characteristic of JS
// java
8 >>> 0  / / 8
5 >>> 1  / / 2
-1 >>> 1 / / 2147483647
-1 >>> 0 // -1

// js
8 >>> 0  / / 8
5 >>> 1  / / 2
-1 >>> 1 / / 2147483647
// Compare Java to find a big difference
-1 >>> 0 / / 4294967295
Copy the code

Js unsigned right shift, regardless of positive or negative numbers will first replace the sign bit with 0, and then shift. That is, the operator always returns a positive integer.

At this point, I’m sure you understand why we use the >>> operator in our code.

To summarize

The main advantage of slice wrapped in LoDash over native Slice is that the array passed in does not throw an exception, even if it is not an array. Instead, it returns an empty array. This is a minor advantage when working with typescript. Start and end are also processed, so that there will not be chaos during the operation of the error. Of course, functional call is also a major advantage of using the method, at least you can learn the programming ideas here, but it is undeniable that the source code if the use of ES6 syntax will be more concise, such as es6 can use the default parameter values, if added ts the method will be more robust, the user will be more clear. Well, that's all for now. Thank you for watching and learning together.Copy the code