lodash框架中的chunk与drop函数源码逐行分析
lodash是一个工具库,跟underscore差不多
chunk函数的作用: 把一维数组,按照固定的长度分段成二维数组
如:
function chunk(array, size) {
size = Math.max(size, 0);
const length = array == null ? 0 : array.length;
if (!length || size < 1) {
return [];
}
let index = 0;
let resIndex = 0;
const result = new Array(Math.ceil(length / size));
while (index < length) {
result[resIndex++] = array.slice(index, (index += size));
}
return result;
}
第2行: size=Math.max( size, 0 ); 获取0和size之间的较大值, 纵观整个函数, 通俗点讲就是,如果size是负数,把size变为0,其实整个函数 有个小bug, 那就是没有对size是否传递参数做判断,
如果这样用 chunk( [ 10, 20, 30 ] ) 这样会报错,因为size没有传,默认为undefined, 在第9行 length / size的时候就会报错, 所以我认为更加严谨的做法是在第二行代码之间,再加一句判断:
size = ( typeof size === 'undefined' ) ? 0 : size;
for( ; index < length; ) {result[resIndex] = array.slice( index, index + size );resIndex++;index = index + size;}
function drop(array, n = 1) {
const length = array == null ? 0 : array.length;
return length
? array.slice( n < 0 ? 0 : n, length)
: [];
}
console.log( drop( [ 10, 20, 30 ] ) ); //[ 20, 30 ]
console.log( drop( [ 10, 20, 30 ], 3 ) ); //[]
console.log( drop( [ 10, 20, 30 ], 2 ) ); //[30]
lodash框架中的chunk与drop函数源码逐行分析的更多相关文章
- Vue中之nextTick函数源码分析
Vue中之nextTick函数源码分析 1. 什么是Vue.nextTick()?官方文档解释如下:在下次DOM更新循环结束之后执行的延迟回调.在修改数据之后立即使用这个方法,获取更新后的DOM. 2 ...
- PHP 源码 — intval 函数源码分析
PHP 源码 - intval 函数源码分析 文章来源: https://github.com/suhanyujie/learn-computer/ 作者:suhanyujie 基于PHP 7.3.3 ...
- PHP 源码 —— is_array 函数源码分析
is_array 函数源码分析 本文首发于 https://github.com/suhanyujie/learn-computer/blob/master/src/function/array/is ...
- MapReduce中TextInputFormat分片和读取分片数据源码级分析
InputFormat主要用于描述输入数据的格式(我们只分析新API,即org.apache.hadoop.mapreduce.lib.input.InputFormat),提供以下两个功能: (1) ...
- SPRING框架中ModelAndView、Model、ModelMap区别及详细分析
转载内容:http://www.cnblogs.com/google4y/p/3421017.html 1. Model Model 是一个接口, 其实现类为ExtendedModelMap,继承了M ...
- 序列化器中钩子函数源码分析、many关键字源码分析
局部钩子和全局钩子源码分析(2星) # 入口是 ser.is_valid(),是BaseSerializer的方法 # 最核心的代码 self._validated_data = self.run_v ...
- Go语言fmt库的print函数源码解析
// Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a B ...
- Spark GraphX的函数源码分析及应用实例
1. outerJoinVertices函数 首先给出源代码 override def outerJoinVertices[U: ClassTag, VD2: ClassTag] (other: RD ...
- [转]delphi 有授权许可的字符串拷贝函数源码
一段看上去“貌不惊人”的Delphi插入汇编代码,却需要授权许可,但是与经典的同类型函数比较,确实“身手不凡”. 研究代码的目的在于借鉴,本文通过分析,并用C++重写代码进行比较,再次证明这段代码效率 ...
随机推荐
- usaco 2002 月赛 Chores 题解
Description Farmer John's family pitches in with the chores during milking, doing all the chores as ...
- Spring3 MVC DispatcherServlet 配置问题
1. Spring MVC 是通过servlet来进行转发的,一般在web.xml中配置如下: <servlet> <servlet-name>dispatcher</s ...
- Codeforces_776B: Sherlock and his girlfriend(素数筛)
题目链接 题意:对2~n+1染色,一个数不能与其素因子同色. 故而只需两种颜色即可,素数染1,合数染2便可满足条件 #include<bits/stdc++.h> using namesp ...
- IDEA 单元测试testng入门及testng.xml
直接进入正题: 1.TestNG的运行方式如下: With a testng.xml file 直接run as test suite With ant 使用ant From the command ...
- 了解Java并学会创建Java项目(一个菜鸟的成长历程)
计算机语言分类:了解 1)低级语言:更接近于计算机的语言 1.1)机器语言:由0和1组成的 1.2)汇编语言:有一些助记符号2)高级语言:更接近于人的语言 2.1)面向过程的:C... 2.2)面向对 ...
- HDU 6047 Maximum Sequence(线段树)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=6047 题目: Maximum Sequence Time Limit: 4000/2000 MS (J ...
- 【HTML】table元素
1.最简单的table <table> <tr> <th></th> </tr> <tr> <td></td& ...
- 关于position
一.position 一)语法: 看了那么久的语法,终于到了讲正题的时间了. 二)定位 1.相对定位:相对元素自己在没有定位之前的位置进行位移,元素仍然保留还没有原来的位置. 特性: 1)不脱离文档流 ...
- angularjs 动态显示内容适用于$modal
1.创建指令 angular.module('app').directive('dynamicElement', ["$compile", function ($compile) ...
- centos6.8 静默安装 oracle 11.2.0.4
安装环境及系统要求 (下文具体参数值与路径根据自己的环境调整)操作系统:Red Hat Enterprise Linux 6 (x86) 或者CentOS 6 (x64) 数据库:Oracle ...