Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
xuliangzhan committed Sep 18, 2020
1 parent dcb77a5 commit 206dc97
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
10 changes: 9 additions & 1 deletion methods/array/slice.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var toNumber = require('../number/toNumber')

/**
* 裁剪 Arguments 或数组 array,从 start 位置开始到 end 结束,但不包括 end 本身的位置
* @param {Array/Arguments} array 数组或Arguments
Expand All @@ -6,8 +8,14 @@
*/
function slice (array, startIndex, endIndex) {
var result = []
var argsSize = arguments.length
if (array) {
for (startIndex = startIndex || 0, endIndex = endIndex || array.length; startIndex < endIndex; startIndex++) {
startIndex = argsSize >= 2 ? toNumber(startIndex) : 0
endIndex = argsSize >= 3 ? toNumber(endIndex) : array.length
if (array.slice) {
return array.slice(startIndex, endIndex)
}
for (; startIndex < endIndex; startIndex++) {
result.push(array[startIndex])
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xe-utils",
"version": "2.7.11",
"version": "2.7.12",
"description": "JavaScript 函数库、工具类",
"main": "index.js",
"unpkg": "dist/xe-utils.umd.min.js",
Expand Down
9 changes: 9 additions & 0 deletions test/array.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,21 @@ describe('Array functions', () => {
expect(
XEUtils.slice([11, 22])
).toEqual([11, 22])
expect(
XEUtils.slice([11, 22, 33, 44], 0)
).toEqual([11, 22, 33, 44])
expect(
XEUtils.slice([11, 22, 33, 44], 0, 0)
).toEqual([])
expect(
XEUtils.slice([11, 22, 33, 44], 1)
).toEqual([22, 33, 44])
expect(
XEUtils.slice([11, 22, 33, 44], 1, 3)
).toEqual([22, 33])
expect(
XEUtils.slice([11, 22, 33, 44], 0, 1)
).toEqual([11])
let method = function () {
expect(
XEUtils.slice(arguments, 1, 3)
Expand Down

0 comments on commit 206dc97

Please sign in to comment.