Skip to content

Commit

Permalink
feat: release 0.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
lavyun committed Nov 17, 2019
1 parent 495323b commit e0aa997
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 50 deletions.
48 changes: 37 additions & 11 deletions dist/mock.browser.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ var __spreadArrays = undefined && undefined.__spreadArrays || function () {
return r;
};

var objectAssign = function objectAssign(target, varArgs) {
var objectAssign = function objectAssign(target, args) {
// TypeError if undefined or null
if (target == null) {
throw new TypeError('Cannot convert undefined or null to object');
}

var to = Object(target);

for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];
for (var i = 1; i < arguments.length; i++) {
var nextSource = arguments[i];

if (nextSource != null) {
// Skip over if undefined or null
Expand Down Expand Up @@ -95,8 +95,8 @@ var each = function each(obj, iterator, context) {
}
}
};
var type = function type(obj) {
return isDef(obj) ? Object.prototype.toString.call(obj).match(/\[object (\w+)\]/)[1].toLowerCase() : String(obj);
var type = function type(value) {
return isDef(value) ? Object.prototype.toString.call(value).match(/\[object (\w+)\]/)[1].toLowerCase() : String(value);
};
var isDef = function isDef(value) {
return value !== undefined && value !== null;
Expand Down Expand Up @@ -9088,21 +9088,47 @@ function find(options) {
for (var sUrlType in MockXMLHttpRequest.Mock.mocked) {
var item = MockXMLHttpRequest.Mock.mocked[sUrlType];

if ((!item.rurl || match(item.rurl, options.url)) && (!item.rtype || match(item.rtype, options.type.toLowerCase()))) {
// console.log('[mock]', options.url, '>', item.rurl)
if ((!item.rurl || matchUrl(item.rurl, options.url)) && (!item.rtype || matchType(item.rtype, options.type))) {
return item;
}
}

function match(expected, actual) {
if (type(expected) === 'string') {
return expected === actual;
function matchUrl(expected, actual) {
if (isString(expected)) {
if (expected === actual) {
return true;
} // expected: /hello/world
// actual: /hello/world?type=1


if (actual.indexOf(expected) === 0 && actual[expected.length] === '?') {
return true;
}
}

if (type(expected) === 'regexp') {
if (isRegExp(expected)) {
return expected.test(actual);
}

return false;
}

function matchType(expected, actual) {
if (isString(expected) || isRegExp(expected)) {
return new RegExp(expected, 'i').test(actual);
}

return false;
} // function match(expected: string | RegExp, actual: string): boolean {
// if (util.isString(expected)) {
// return expected === actual
// }
// if (util.isRegExp(expected)) {
// return new RegExp(expected, 'i').test(actual)
// }
// return false
// }

} // 数据模板 => 响应数据


Expand Down
48 changes: 37 additions & 11 deletions dist/mock.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@
return r;
};

var objectAssign = function objectAssign(target, varArgs) {
var objectAssign = function objectAssign(target, args) {
// TypeError if undefined or null
if (target == null) {
throw new TypeError('Cannot convert undefined or null to object');
}

var to = Object(target);

for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];
for (var i = 1; i < arguments.length; i++) {
var nextSource = arguments[i];

if (nextSource != null) {
// Skip over if undefined or null
Expand Down Expand Up @@ -101,8 +101,8 @@
}
}
};
var type = function type(obj) {
return isDef(obj) ? Object.prototype.toString.call(obj).match(/\[object (\w+)\]/)[1].toLowerCase() : String(obj);
var type = function type(value) {
return isDef(value) ? Object.prototype.toString.call(value).match(/\[object (\w+)\]/)[1].toLowerCase() : String(value);
};
var isDef = function isDef(value) {
return value !== undefined && value !== null;
Expand Down Expand Up @@ -9094,21 +9094,47 @@
for (var sUrlType in MockXMLHttpRequest.Mock.mocked) {
var item = MockXMLHttpRequest.Mock.mocked[sUrlType];

if ((!item.rurl || match(item.rurl, options.url)) && (!item.rtype || match(item.rtype, options.type.toLowerCase()))) {
// console.log('[mock]', options.url, '>', item.rurl)
if ((!item.rurl || matchUrl(item.rurl, options.url)) && (!item.rtype || matchType(item.rtype, options.type))) {
return item;
}
}

function match(expected, actual) {
if (type(expected) === 'string') {
return expected === actual;
function matchUrl(expected, actual) {
if (isString(expected)) {
if (expected === actual) {
return true;
} // expected: /hello/world
// actual: /hello/world?type=1


if (actual.indexOf(expected) === 0 && actual[expected.length] === '?') {
return true;
}
}

if (type(expected) === 'regexp') {
if (isRegExp(expected)) {
return expected.test(actual);
}

return false;
}

function matchType(expected, actual) {
if (isString(expected) || isRegExp(expected)) {
return new RegExp(expected, 'i').test(actual);
}

return false;
} // function match(expected: string | RegExp, actual: string): boolean {
// if (util.isString(expected)) {
// return expected === actual
// }
// if (util.isRegExp(expected)) {
// return new RegExp(expected, 'i').test(actual)
// }
// return false
// }

} // 数据模板 => 响应数据


Expand Down
2 changes: 1 addition & 1 deletion dist/mock.browser.min.js

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions dist/mock.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ var __spreadArrays = undefined && undefined.__spreadArrays || function () {
return r;
};

var objectAssign = function objectAssign(target, varArgs) {
var objectAssign = function objectAssign(target, args) {
// TypeError if undefined or null
if (target == null) {
throw new TypeError('Cannot convert undefined or null to object');
}

var to = Object(target);

for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];
for (var i = 1; i < arguments.length; i++) {
var nextSource = arguments[i];

if (nextSource != null) {
// Skip over if undefined or null
Expand Down Expand Up @@ -97,8 +97,8 @@ var each = function each(obj, iterator, context) {
}
}
};
var type = function type(obj) {
return isDef(obj) ? Object.prototype.toString.call(obj).match(/\[object (\w+)\]/)[1].toLowerCase() : String(obj);
var type = function type(value) {
return isDef(value) ? Object.prototype.toString.call(value).match(/\[object (\w+)\]/)[1].toLowerCase() : String(value);
};
var isDef = function isDef(value) {
return value !== undefined && value !== null;
Expand Down
2 changes: 1 addition & 1 deletion doc/.vuepress/dist
Submodule dist updated from 72a1b2 to 3b1df5
20 changes: 19 additions & 1 deletion doc/changelog/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
# 更新日志

## v0.0.3

发布日期:2019-11-17

### Features

* 增加 d.ts 声明文件。
* 新增 Random.phone 方法,生成一个随机的手机号。

### Optimize

* 优化 Random.dataImage 方法,移除对 node-canvas 的依赖,改为获取远端图片的 buffer,在转为base64。

### Fix

* 修复无法拦截带参数的 GET,[Mock.js#109](https://github.com/nuysoft/Mock/issues/109)
* 修复 Mock.mock 方法中, rtype 无需区分大小写。即:`get``GET` 都可以被成功匹配到。

## v0.0.2

发布日期:2019-11-13

#### Features
### Features

* 新增 Random.timestamp 方法 [Mock.js#372](https://github.com/nuysoft/Mock/issues/372),随机生成一个时间戳。
* 使用 [china-location](https://github.com/JasonBoy/china-location) 库作为省市区数据源。
Expand Down
26 changes: 14 additions & 12 deletions src/lib/util.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export const objectAssign = function (target, varArgs) {
export const objectAssign = function (target, args) {
// TypeError if undefined or null
if (target == null) {
throw new TypeError('Cannot convert undefined or null to object')
}

let to = Object(target)

for (let index = 1; index < arguments.length; index++) {
const nextSource = arguments[index]
for (let i = 1; i < arguments.length; i++) {
const nextSource = arguments[i]

if (nextSource != null) { // Skip over if undefined or null
for (let nextKey in nextSource) {
Expand Down Expand Up @@ -38,43 +38,45 @@ export const each = function (obj, iterator, context?) {
}
}

export const type = function (obj) {
return isDef(obj) ? Object.prototype.toString.call(obj).match(/\[object (\w+)\]/)![1].toLowerCase() : String(obj)
export const type = function(value: any): string {
return isDef(value)
? Object.prototype.toString.call(value).match(/\[object (\w+)\]/)![1].toLowerCase()
: String(value)
}

export const isDef = function (value: any): boolean {
return value !== undefined && value !== null
}

export const isString = function (value) {
export const isString = function (value: any): value is string {
return type(value) === 'string'
}

export const isObject = function (value) {
export const isObject = function (value: any): value is object {
return type(value) === 'object'
}

export const isArray = function (value) {
export const isArray = function (value: any): value is Array<any> {
return type(value) === 'array'
}

export const isRegExp = function (value) {
export const isRegExp = function (value: any): value is RegExp {
return type(value) === 'regexp'
}

export const isFunction = function (value) {
export const isFunction = function (value: any): value is Function {
return type(value) === 'function'
}

export const isObjectOrArray = function (value) {
export const isObjectOrArray = function (value: any): value is object | Array<any> {
return isObject(value) || isArray(value)
}

export const isNumeric = function (value) {
return !isNaN(parseFloat(value)) && isFinite(value)
}

export const keys = function (obj: object) {
export const keys = function (obj: object): string[] {
const keys: string[] = []
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
Expand Down
38 changes: 31 additions & 7 deletions src/lib/xhr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,22 +443,46 @@ function find(options) {
for (let sUrlType in MockXMLHttpRequest.Mock.mocked) {
const item = MockXMLHttpRequest.Mock.mocked[sUrlType]
if (
(!item.rurl || match(item.rurl, options.url)) &&
(!item.rtype || match(item.rtype, options.type.toLowerCase()))
(!item.rurl || matchUrl(item.rurl, options.url)) &&
(!item.rtype || matchType(item.rtype, options.type))
) {
// console.log('[mock]', options.url, '>', item.rurl)
return item
}
}

function match(expected, actual) {
if (util.type(expected) === 'string') {
return expected === actual
function matchUrl(expected: string | RegExp, actual: string): boolean {
if (util.isString(expected)) {
if (expected === actual) {
return true
}
// expected: /hello/world
// actual: /hello/world?type=1
if (actual.indexOf(expected) === 0 && actual[expected.length] === '?') {
return true
}
}
if (util.type(expected) === 'regexp') {
if (util.isRegExp(expected)) {
return expected.test(actual)
}
return false
}

function matchType(expected: string | RegExp, actual: string): boolean {
if (util.isString(expected) || util.isRegExp(expected)) {
return new RegExp(expected, 'i').test(actual)
}
return false
}

// function match(expected: string | RegExp, actual: string): boolean {
// if (util.isString(expected)) {
// return expected === actual
// }
// if (util.isRegExp(expected)) {
// return new RegExp(expected, 'i').test(actual)
// }
// return false
// }
}

// 数据模板 => 响应数据
Expand Down
2 changes: 1 addition & 1 deletion test/test.mock.random.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ describe('Random', function () {
})
})

describe.only('Image', function () {
describe('Image', function () {
doit('Random.image()', function (data) {
expect(data).to.include('https://dummyimage.com')
})
Expand Down
Loading

0 comments on commit e0aa997

Please sign in to comment.