Skip to content

Commit

Permalink
fix #90 Mock.js 1.0.0报错 @pick()
Browse files Browse the repository at this point in the history
  • Loading branch information
nuysoft committed Jan 14, 2016
1 parent f8efc90 commit ad85757
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 28 deletions.
8 changes: 4 additions & 4 deletions dist/mock-min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/mock-min.js.map

Large diffs are not rendered by default.

47 changes: 35 additions & 12 deletions dist/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ return /******/ (function(modules) { // webpackBootstrap
_mocked: {}
}

Mock.version = '1.0.0'
Mock.version = '1.0.1-beta1'

// 避免循环依赖
if (XHR) XHR.Mock = Mock
Expand Down Expand Up @@ -2050,11 +2050,14 @@ return /******/ (function(modules) { // webpackBootstrap

/***/ },
/* 14 */
/***/ function(module, exports) {
/***/ function(module, exports, __webpack_require__) {

/*
## Helpers
*/

var Util = __webpack_require__(3)

module.exports = {
// 把字符串的第一个字母转换为大写。
capitalize: function(word) {
Expand All @@ -2070,17 +2073,37 @@ return /******/ (function(modules) { // webpackBootstrap
},
// 从数组中随机选取一个元素,并返回。
pick: function pick(arr, min, max) {
arr = arr || []
switch (arguments.length) {
case 1:
return arr[this.natural(0, arr.length - 1)]
case 2:
max = min
/* falls through */
case 3:
return this.shuffle(arr, min, max)

// pick( item1, item2 ... )
if (!Util.isArray(arr)) {
arr = [].slice.call(arguments)
min = 1
max = 1
} else {
// pick( [ item1, item2 ... ] )
if (min === undefined) min = 1

// pick( [ item1, item2 ... ], count )
if (max === undefined) max = min
}

if (min === 1 && max === 1) return arr[this.natural(0, arr.length - 1)]

// pick( [ item1, item2 ... ], min, max )
return this.shuffle(arr, min, max)

// 通过参数个数判断方法签名,扩展性太差!#90
// switch (arguments.length) {
// case 1:
// // pick( [ item1, item2 ... ] )
// return arr[this.natural(0, arr.length - 1)]
// case 2:
// // pick( [ item1, item2 ... ], count )
// max = min
// /* falls through */
// case 3:
// // pick( [ item1, item2 ... ], min, max )
// return this.shuffle(arr, min, max)
// }
},
/*
打乱数组中元素的顺序,并返回。
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "mockjs",
"title": "Mock.js",
"description": "生成随机数据 & 拦截 Ajax 请求",
"version": "1.0.0",
"version": "1.0.1-beta1",
"homepage": "http://mockjs.com/",
"keywords": [
"mock",
Expand Down
Binary file modified src/dependencies.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var Mock = {
_mocked: {}
}

Mock.version = '1.0.0'
Mock.version = '1.0.1-beta1'

// 避免循环依赖
if (XHR) XHR.Mock = Mock
Expand Down
41 changes: 32 additions & 9 deletions src/mock/random/helper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/*
## Helpers
*/

var Util = require('../util')

module.exports = {
// 把字符串的第一个字母转换为大写。
capitalize: function(word) {
Expand All @@ -16,17 +19,37 @@ module.exports = {
},
// 从数组中随机选取一个元素,并返回。
pick: function pick(arr, min, max) {
arr = arr || []
switch (arguments.length) {
case 1:
return arr[this.natural(0, arr.length - 1)]
case 2:
max = min
/* falls through */
case 3:
return this.shuffle(arr, min, max)
// pick( item1, item2 ... )
if (!Util.isArray(arr)) {
arr = [].slice.call(arguments)
min = 1
max = 1
} else {
// pick( [ item1, item2 ... ] )
if (min === undefined) min = 1

// pick( [ item1, item2 ... ], count )
if (max === undefined) max = min
}

if (min === 1 && max === 1) return arr[this.natural(0, arr.length - 1)]

// pick( [ item1, item2 ... ], min, max )
return this.shuffle(arr, min, max)

// 通过参数个数判断方法签名,扩展性太差!#90
// switch (arguments.length) {
// case 1:
// // pick( [ item1, item2 ... ] )
// return arr[this.natural(0, arr.length - 1)]
// case 2:
// // pick( [ item1, item2 ... ], count )
// max = min
// /* falls through */
// case 3:
// // pick( [ item1, item2 ... ], min, max )
// return this.shuffle(arr, min, max)
// }
},
/*
打乱数组中元素的顺序,并返回。
Expand Down
3 changes: 3 additions & 0 deletions test/test.mock.random.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,9 @@ describe('Random', function() {
doit('Random.pick()', function(data) {
expect(data).to.be.undefined
})
doit('Random.pick("a", "e", "i", "o", "u")', function(data) {
expect(["a", "e", "i", "o", "u"]).to.include(data)
})
doit('Random.pick(["a", "e", "i", "o", "u"])', function(data) {
expect(["a", "e", "i", "o", "u"]).to.include(data)
})
Expand Down

0 comments on commit ad85757

Please sign in to comment.