Skip to content

Commit 6962ca0

Browse files
authored
Merge pull request #67 from lavyun/develop
transfer syntax
2 parents 76be4f4 + baca4e7 commit 6962ca0

File tree

16 files changed

+440
-145
lines changed

16 files changed

+440
-145
lines changed

dist/mock.browser.esm.js

+54-21
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
/*!
2-
* better-mock v0.2.9 (mock.browser.esm.js)
2+
* better-mock v0.3.0 (mock.browser.esm.js)
33
* (c) 2019-2020 lavyun@163.com
44
* Released under the MIT License.
55
*/
66

77
var constant = {
88
GUID: 1,
99
RE_KEY: /(.+)\|(?:\+(\d+)|([\+\-]?\d+-?[\+\-]?\d*)?(?:\.(\d+-?\d*))?)/,
10+
RE_TRANSFER_TYPE: /#(.*)$/,
1011
RE_RANGE: /([\+\-]?\d+)-?([\+\-]?\d+)?/,
1112
RE_PLACEHOLDER: /\\*@([^@#%&()\?\s]+)(?:\((.*?)\))?/g
1213
};
@@ -6597,6 +6598,23 @@ var parse = function (name) {
65976598
return {};
65986599
};
65996600

6601+
var number = Number;
6602+
var boolean$1 = Boolean;
6603+
var string$1 = String;
6604+
var transfer = {
6605+
number: number,
6606+
boolean: boolean$1,
6607+
string: string$1,
6608+
extend: extend
6609+
};
6610+
function extend(source) {
6611+
if (isObject(source)) {
6612+
for (var key in source) {
6613+
transfer[key] = source[key];
6614+
}
6615+
}
6616+
}
6617+
66006618
// ## RegExp Handler
66016619
// ASCII printable code chart
66026620
var LOWER = ascii(97, 122);
@@ -7597,60 +7615,64 @@ var handler$1 = {
75977615
},
75987616
object: function (options) {
75997617
var result = {};
7600-
var keys$1;
7601-
var fnKeys;
7602-
var key;
7603-
var parsedKey;
7604-
var inc;
7605-
var i;
76067618
// 'obj|min-max': {}
76077619
if (options.rule.min != undefined) {
7608-
keys$1 = keys(options.template);
7620+
var keys$1 = keys(options.template);
76097621
keys$1 = random.shuffle(keys$1);
76107622
keys$1 = keys$1.slice(0, options.rule.count);
7611-
for (i = 0; i < keys$1.length; i++) {
7612-
key = keys$1[i];
7613-
parsedKey = key.replace(constant.RE_KEY, '$1');
7623+
for (var i = 0; i < keys$1.length; i++) {
7624+
var key = keys$1[i];
7625+
var parsedKey = key.replace(constant.RE_KEY, '$1');
7626+
var transferTypeCtor = handler$1.getTransferTypeCtor(key);
7627+
if (transferTypeCtor) {
7628+
parsedKey = parsedKey.replace(constant.RE_TRANSFER_TYPE, '');
7629+
}
76147630
options.context.path.push(parsedKey);
76157631
options.context.templatePath.push(key);
7616-
result[parsedKey] = handler$1.gen(options.template[key], key, {
7632+
var generatedValue = handler$1.gen(options.template[key], key, {
76177633
path: options.context.path,
76187634
templatePath: options.context.templatePath,
76197635
currentContext: result,
76207636
templateCurrentContext: options.template,
76217637
root: options.context.root || result,
76227638
templateRoot: options.context.templateRoot || options.template
76237639
});
7640+
result[parsedKey] = transferTypeCtor(generatedValue);
76247641
options.context.path.pop();
76257642
options.context.templatePath.pop();
76267643
}
76277644
}
76287645
else {
76297646
// 'obj': {}
7630-
keys$1 = [];
7631-
fnKeys = []; // Mock.js#25 改变了非函数属性的顺序,查找起来不方便
7632-
for (key in options.template) {
7647+
var keys$1 = [];
7648+
var fnKeys = []; // Mock.js#25 改变了非函数属性的顺序,查找起来不方便
7649+
for (var key in options.template) {
76337650
var target = typeof options.template[key] === 'function' ? fnKeys : keys$1;
76347651
target.push(key);
76357652
}
76367653
keys$1 = keys$1.concat(fnKeys);
7637-
for (i = 0; i < keys$1.length; i++) {
7638-
key = keys$1[i];
7639-
parsedKey = key.replace(constant.RE_KEY, '$1');
7654+
for (var i = 0; i < keys$1.length; i++) {
7655+
var key = keys$1[i];
7656+
var parsedKey = key.replace(constant.RE_KEY, '$1');
7657+
var transferTypeCtor = handler$1.getTransferTypeCtor(key);
7658+
if (transferTypeCtor) {
7659+
parsedKey = parsedKey.replace(constant.RE_TRANSFER_TYPE, '');
7660+
}
76407661
options.context.path.push(parsedKey);
76417662
options.context.templatePath.push(key);
7642-
result[parsedKey] = handler$1.gen(options.template[key], key, {
7663+
var generatedValue = handler$1.gen(options.template[key], key, {
76437664
path: options.context.path,
76447665
templatePath: options.context.templatePath,
76457666
currentContext: result,
76467667
templateCurrentContext: options.template,
76477668
root: options.context.root || result,
76487669
templateRoot: options.context.templateRoot || options.template
76497670
});
7671+
result[parsedKey] = transferTypeCtor(generatedValue);
76507672
options.context.path.pop();
76517673
options.context.templatePath.pop();
76527674
// 'id|+1': 1
7653-
inc = key.match(constant.RE_KEY);
7675+
var inc = key.match(constant.RE_KEY);
76547676
if (inc && inc[2] && type(options.template[key]) === 'number') {
76557677
options.template[key] += parseInt(inc[2], 10);
76567678
}
@@ -7722,6 +7744,7 @@ var handler$1 = {
77227744
lastIndex = index + input.length;
77237745
continue;
77247746
}
7747+
// console.log(input, options.context.currentContext, options.context.templateCurrentContext, options)
77257748
var replaced = handler$1.placeholder(input, options.context.currentContext, options.context.templateCurrentContext, options);
77267749
// 只有一个占位符,并且没有其他字符,例如:'name': '@EMAIL'
77277750
if (index === 0 && input.length === source.length) {
@@ -7771,6 +7794,7 @@ var handler$1 = {
77717794
},
77727795
// 处理占位符,转换为最终值
77737796
placeholder: function (placeholder, obj, templateContext, options) {
7797+
debugger;
77747798
// 1 key, 2 params
77757799
// regexp init
77767800
constant.RE_PLACEHOLDER.exec('');
@@ -7906,6 +7930,14 @@ var handler$1 = {
79067930
},
79077931
splitPathToArray: function (path) {
79087932
return path.split(/\/+/).filter(function (_) { return _; });
7933+
},
7934+
getTransferTypeCtor: function (key) {
7935+
var matched = key.match(constant.RE_TRANSFER_TYPE);
7936+
var type = matched && matched[1];
7937+
if (type && transfer.hasOwnProperty(type) && type !== 'extend') {
7938+
return transfer[type];
7939+
}
7940+
return function (value) { return value; };
79097941
}
79107942
};
79117943

@@ -8714,6 +8746,7 @@ function overrideFetchAndRequest() {
87148746
var Mock = {
87158747
Handler: handler$1,
87168748
Random: random,
8749+
Transfer: transfer,
87178750
Util: Util,
87188751
XHR: MockXMLHttpRequest,
87198752
RE: RE,
@@ -8723,7 +8756,7 @@ var Mock = {
87238756
heredoc: heredoc,
87248757
setup: setting.setup.bind(setting),
87258758
_mocked: mocked.getMocked(),
8726-
version: '0.2.9'
8759+
version: '0.3.0'
87278760
};
87288761
// 根据数据模板生成模拟数据。
87298762
function mock(rurl, rtype, template) {

dist/mock.browser.js

+54-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* better-mock v0.2.9 (mock.browser.js)
2+
* better-mock v0.3.0 (mock.browser.js)
33
* (c) 2019-2020 lavyun@163.com
44
* Released under the MIT License.
55
*/
@@ -13,6 +13,7 @@
1313
var constant = {
1414
GUID: 1,
1515
RE_KEY: /(.+)\|(?:\+(\d+)|([\+\-]?\d+-?[\+\-]?\d*)?(?:\.(\d+-?\d*))?)/,
16+
RE_TRANSFER_TYPE: /#(.*)$/,
1617
RE_RANGE: /([\+\-]?\d+)-?([\+\-]?\d+)?/,
1718
RE_PLACEHOLDER: /\\*@([^@#%&()\?\s]+)(?:\((.*?)\))?/g
1819
};
@@ -6603,6 +6604,23 @@
66036604
return {};
66046605
};
66056606

6607+
var number = Number;
6608+
var boolean$1 = Boolean;
6609+
var string$1 = String;
6610+
var transfer = {
6611+
number: number,
6612+
boolean: boolean$1,
6613+
string: string$1,
6614+
extend: extend
6615+
};
6616+
function extend(source) {
6617+
if (isObject(source)) {
6618+
for (var key in source) {
6619+
transfer[key] = source[key];
6620+
}
6621+
}
6622+
}
6623+
66066624
// ## RegExp Handler
66076625
// ASCII printable code chart
66086626
var LOWER = ascii(97, 122);
@@ -7603,60 +7621,64 @@
76037621
},
76047622
object: function (options) {
76057623
var result = {};
7606-
var keys$1;
7607-
var fnKeys;
7608-
var key;
7609-
var parsedKey;
7610-
var inc;
7611-
var i;
76127624
// 'obj|min-max': {}
76137625
if (options.rule.min != undefined) {
7614-
keys$1 = keys(options.template);
7626+
var keys$1 = keys(options.template);
76157627
keys$1 = random.shuffle(keys$1);
76167628
keys$1 = keys$1.slice(0, options.rule.count);
7617-
for (i = 0; i < keys$1.length; i++) {
7618-
key = keys$1[i];
7619-
parsedKey = key.replace(constant.RE_KEY, '$1');
7629+
for (var i = 0; i < keys$1.length; i++) {
7630+
var key = keys$1[i];
7631+
var parsedKey = key.replace(constant.RE_KEY, '$1');
7632+
var transferTypeCtor = handler$1.getTransferTypeCtor(key);
7633+
if (transferTypeCtor) {
7634+
parsedKey = parsedKey.replace(constant.RE_TRANSFER_TYPE, '');
7635+
}
76207636
options.context.path.push(parsedKey);
76217637
options.context.templatePath.push(key);
7622-
result[parsedKey] = handler$1.gen(options.template[key], key, {
7638+
var generatedValue = handler$1.gen(options.template[key], key, {
76237639
path: options.context.path,
76247640
templatePath: options.context.templatePath,
76257641
currentContext: result,
76267642
templateCurrentContext: options.template,
76277643
root: options.context.root || result,
76287644
templateRoot: options.context.templateRoot || options.template
76297645
});
7646+
result[parsedKey] = transferTypeCtor(generatedValue);
76307647
options.context.path.pop();
76317648
options.context.templatePath.pop();
76327649
}
76337650
}
76347651
else {
76357652
// 'obj': {}
7636-
keys$1 = [];
7637-
fnKeys = []; // Mock.js#25 改变了非函数属性的顺序,查找起来不方便
7638-
for (key in options.template) {
7653+
var keys$1 = [];
7654+
var fnKeys = []; // Mock.js#25 改变了非函数属性的顺序,查找起来不方便
7655+
for (var key in options.template) {
76397656
var target = typeof options.template[key] === 'function' ? fnKeys : keys$1;
76407657
target.push(key);
76417658
}
76427659
keys$1 = keys$1.concat(fnKeys);
7643-
for (i = 0; i < keys$1.length; i++) {
7644-
key = keys$1[i];
7645-
parsedKey = key.replace(constant.RE_KEY, '$1');
7660+
for (var i = 0; i < keys$1.length; i++) {
7661+
var key = keys$1[i];
7662+
var parsedKey = key.replace(constant.RE_KEY, '$1');
7663+
var transferTypeCtor = handler$1.getTransferTypeCtor(key);
7664+
if (transferTypeCtor) {
7665+
parsedKey = parsedKey.replace(constant.RE_TRANSFER_TYPE, '');
7666+
}
76467667
options.context.path.push(parsedKey);
76477668
options.context.templatePath.push(key);
7648-
result[parsedKey] = handler$1.gen(options.template[key], key, {
7669+
var generatedValue = handler$1.gen(options.template[key], key, {
76497670
path: options.context.path,
76507671
templatePath: options.context.templatePath,
76517672
currentContext: result,
76527673
templateCurrentContext: options.template,
76537674
root: options.context.root || result,
76547675
templateRoot: options.context.templateRoot || options.template
76557676
});
7677+
result[parsedKey] = transferTypeCtor(generatedValue);
76567678
options.context.path.pop();
76577679
options.context.templatePath.pop();
76587680
// 'id|+1': 1
7659-
inc = key.match(constant.RE_KEY);
7681+
var inc = key.match(constant.RE_KEY);
76607682
if (inc && inc[2] && type(options.template[key]) === 'number') {
76617683
options.template[key] += parseInt(inc[2], 10);
76627684
}
@@ -7728,6 +7750,7 @@
77287750
lastIndex = index + input.length;
77297751
continue;
77307752
}
7753+
// console.log(input, options.context.currentContext, options.context.templateCurrentContext, options)
77317754
var replaced = handler$1.placeholder(input, options.context.currentContext, options.context.templateCurrentContext, options);
77327755
// 只有一个占位符,并且没有其他字符,例如:'name': '@EMAIL'
77337756
if (index === 0 && input.length === source.length) {
@@ -7777,6 +7800,7 @@
77777800
},
77787801
// 处理占位符,转换为最终值
77797802
placeholder: function (placeholder, obj, templateContext, options) {
7803+
debugger;
77807804
// 1 key, 2 params
77817805
// regexp init
77827806
constant.RE_PLACEHOLDER.exec('');
@@ -7912,6 +7936,14 @@
79127936
},
79137937
splitPathToArray: function (path) {
79147938
return path.split(/\/+/).filter(function (_) { return _; });
7939+
},
7940+
getTransferTypeCtor: function (key) {
7941+
var matched = key.match(constant.RE_TRANSFER_TYPE);
7942+
var type = matched && matched[1];
7943+
if (type && transfer.hasOwnProperty(type) && type !== 'extend') {
7944+
return transfer[type];
7945+
}
7946+
return function (value) { return value; };
79157947
}
79167948
};
79177949

@@ -8720,6 +8752,7 @@
87208752
var Mock = {
87218753
Handler: handler$1,
87228754
Random: random,
8755+
Transfer: transfer,
87238756
Util: Util,
87248757
XHR: MockXMLHttpRequest,
87258758
RE: RE,
@@ -8729,7 +8762,7 @@
87298762
heredoc: heredoc,
87308763
setup: setting.setup.bind(setting),
87318764
_mocked: mocked.getMocked(),
8732-
version: '0.2.9'
8765+
version: '0.3.0'
87338766
};
87348767
// 根据数据模板生成模拟数据。
87358768
function mock(rurl, rtype, template) {

dist/mock.browser.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)