Skip to content

Commit

Permalink
fix: fix the :numeric type set a wrong size config
Browse files Browse the repository at this point in the history
  • Loading branch information
fefit committed Feb 9, 2022
1 parent a4773c5 commit d17086d
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 107 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

Suchjs add changelog since `v1.0.0`.

## [v2.1.6] - 2022-02-09

### Fixed

- Fix the extend `:numeric` type's wrong `$size` config.

### Changed

- Now `:cascader` type in browser, the top level field just need the `data` config, the `root` config is not required anymore.
## [v2.1.5] - 2022-01-22

### Added
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</div>


An expandable and powerful library for generating fake data, both in nodejs & browser envirionment. You can use it to build a mock data server in nodejs, or just mocking data locally in the browser, normal built-in types can seen in [official site](https://www.suchjs.com/?locale=en-US), you can start it easily.
An expandable and powerful library for generating fake data, both in nodejs & browser envirionment. You can use it to build a mock data server in nodejs, or just mocking data locally in the browser, normal built-in types can seen in [official site](https://www.suchjs.com/?locale=en-US), more extended types can seen in the [document](https://suchjs.github.io/vp-suchjs/en/extTypes.html), you can start it easily.

## Installation

Expand Down
15 changes: 11 additions & 4 deletions __tests__/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,15 +387,12 @@ describe('test built-in types', () => {
expect(() => {
return Such.as(':cascader');
}).toThrow();
expect(() => {
return Such.as(':cascader:#[data=countries]');
}).toThrow();
expect(() => {
return Such.as(':cascader:#[root=true]');
}).toThrow();
// a cascader
const cascader = {
country: ':cascader:#[root=true,data=countries]',
country: ':cascader:#[data=countries]',
city: ':cascader:&./country',
};
const cascaderData = Such.instance(cascader);
Expand Down Expand Up @@ -643,6 +640,16 @@ describe('test built-in recommend types', () => {
).toBeTruthy();
}
});
// numeric
test(':numeric', () => {
const numeric = Such.instance(':numeric:{3}');
for (let i = 0; i < 100; i++) {
const value = numeric.a() as string;
expect(
typeof value === 'string' && /^[0-9]{3}$/.test(value),
).toBeTruthy();
}
});
// alphaNumeric
test(':alphaNumeric', () => {
const alphaNumeric = Such.instance(':alphaNumeric:{3}');
Expand Down
1 change: 1 addition & 0 deletions dist/such.2.1.6.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/such.min.js

Large diffs are not rendered by default.

135 changes: 49 additions & 86 deletions examples/browser.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,62 +8,12 @@
</head>
<body>
<!-- <script src="../dist/such.min.js"></script> -->
<script src="https://cdn.jsdelivr.net/gh/suchjs/such@master/dist/such.min.js"></script>
<script src="../dist/such.2.1.6.min.js"></script>
<script>
(function () {
// // // 示例1
const demo1 = Such.instance({
errno: ':number[0,1]:%d',
errmsg: ':string{0,20}',
url: ':url',
'count?': ':number[1e5,1e6]:%d',
'list{+1,5}': {
url: ':url',
date: ':date:%yyyy-mm-dd HH\\:MM\\:ss',
price: ':number[100,200]:%.2f',
color: ':color$rgba',
isNew: ':boolean',
},
'from:{1}': ['a.com', 'b.com'],
'more{3}': '\\:number',
});
// console.log(demo1.a());
// // 示例2
// const demo2 = Such.instance({
// // name字段可有可无
// 'title?': ':string:{3,10}',
// // list字段一定为数组,大小为0到5
// 'books{+0,5}': {
// date: ':date',
// author: ':string:{10,20}',
// },
// // tags字段非必须,如果存在,就是一个3个字符串组成的数组
// 'tags{3}?': ':string:{5,10}',
// // firm字段非必须,如果存在,就从
// 'firm:{1}?': ['Netflix', 'Disney'],
// });
// console.log(demo2.a());
// // 设置一些数据
// 示例3
// Such.assign('dict', ['a', 'b']);
// const demo3 = Such.instance(':dict:#[data=dict]');
// console.log(demo3.a());
// // 示例4
// Such.assign('city', {
// 北京市: {
// 北京市: ['朝阳区', '海淀区'],
// },
// 湖北省: {
// 武汉市: ['洪湖区', '东西湖区'],
// },
// });
// const demo4 = Such.instance({
// province: ':cascader:#[root=true,data=city]',
// city: ':cascader:&./province',
// area: ':cascader:&./city'
// });
// console.log(demo4.a());
Such.assign('city', {
const globalSuch = Such.default;
globalSuch.assign('city', {
北京市: {
北京市: ['朝阳区', '东城区', '西城区'],
},
Expand All @@ -77,46 +27,59 @@
上海市: ['闵行区', '普陀区', '静安区'],
},
});
Such.define('mobile', 'regexp', '/(\\+86\\-)?(?<service>1[3-8][0-9])\\d{8}/');
globalSuch.define(
'mobile$china',
'regexp',
'/(\\+86\\-)?(?<service>1[3-8][0-9])\\d{8}/',
);
globalSuch.define('who', ["I'm", "He's", "You're"]);
// 创建模拟实例
const instance = Such.instance({
errno: ':int:[0,1]',
errmsg: ':string{0,20}:@concat("_hahaha")',
'count?': ':number[1e5,1e6]:%d',
'list{2,5}': {
id: ':increment',
range: ':increment:#[start=0]:{3}',
position: {
province: ':cascader:#[root=true,data=city]',
city: ':cascader:&./province',
area: ':cascader:&./city',
address: ':ref:&./province,./city,./area:@join("-")',
},
regexp: ':regexp:/\\$[a-z]w*/',
email: ':email:#[domain="163.com"]',
mobile: ':mobile',
date: ':date:["+1 days","+1 years"]:%yyyy-mm-dd HH\\:MM\\:ss',
number: ':number:[100,200]:%.2f',
colors: {
hex: ':color$hex',
rgb: ':color$rgb',
rgba: ':color$rgba',
hsl: ':color$hsl',
hsla: ':color$hsla'
},
const instance = globalSuch.instance({
string: ":string:[65,121]:{10,20}:@concat('_suffix')",
number: ':number:[100,200]:%.2f',
date: ":date:['-1 week','+1 week']:%yyyy-mm-dd HH\\:MM\\:ss",
regexp: ':regexp:/[a-z]{1,3}[0-9]{2,10}/',
'id{2,4}': ':increment',
range: ':increment:{2,3}:#[start=2,step=3]',
ref: ':ref:&./number',
cascader: {
province: ':cascader:#[data=city]',
city: ':cascader:&./province',
area: ':cascader:&./city',
},
'enum:{1}': ['one', 'two'],
template:
':::`:who` coming from `:ref:&./cascader/province`-`:ref:&./cascader/city`-`:ref:&./cascader/area`',
diy: ':mobile$china',
escape: '\\:number',
extends: {
bool: ':bool',
'childs{+1,3}': {
pid: ':ref:&../id',
mobile: ':mobile:#[service="135"]',
},
int: ':int',
percent: ':percent',
uppercase: ':uppercase:{2,4}',
lowercase: ':lowercase:{2,4}',
alpha: ':alpha:{3,6}',
numeric: ':numeric:{3,6}',
alphaNumeric: ':alphaNumeric:{3,6}',
alphaNumericDash: ':alphaNumericDash:{3,6}',
tld: ':tld',
domain: ':domain',
protocol: ':protocol',
url: ':url',
email: ":email:#[domain='163.com']",
ipv4: ':ipv4',
ipv6: ':ipv6',
color$hex: ':color$hex',
color$rgb: ':color$rgb',
color$rgba: ':color$rgba',
color$hsl: ':color$hsl',
color$hsla: ':color$hsla',
},
'oneof{1}': ['hehe.com', 'haha.com'],
'no-translate': '\\:number',

});
// 生成模拟数据
const value = instance.a();
console.log(JSON.stringify(value, null, 4));
console.log(JSON.stringify(value, null, 4));
})();
</script>
</body>
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": "suchjs",
"version": "2.1.5",
"version": "2.1.6",
"description": "Generating fake data, define your own types easily, expandable and powerful.",
"main": "./lib/index.js",
"typings": "./lib/index.d.ts",
Expand Down
11 changes: 1 addition & 10 deletions src/browser/mockit/cascader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import { TStrList } from '../../types/common';
import { TMAttrs, TMParams } from '../../types/mockit';

const configOptions = {
root: {
type: Boolean,
default: false,
},
handle: {
type: Function,
},
Expand All @@ -28,18 +24,13 @@ export default class ToCascader extends Mockit<unknown> {
// validate all params
public static validator = (params: TMParams): void | never => {
const { $config, $path } = params;
if ($config.root) {
if ($config.data) {
// in browser
if ($path) {
throw new Error(
`the data type 'cascader' doesn't need a path when it's a root node`,
);
}
if (!$config.data) {
throw new Error(
`the data type 'cascader' must set a 'data' configuration in a root node`,
);
}
} else {
const pathNum = $path.length;
if (pathNum === 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/browser/mockit/dict.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ export default class ToDict extends Mockit<TMultiStr> {
public init(): void {
// in browser, get data from config
this.addRule('$config', function ($config: IPPConfig) {
if (!$config || !$config['data']) {
if (!$config || !$config.data) {
throw new Error(
"the data type 'dict' must supply a 'data' field of 'data attribute' configuration, no configuration was set",
);
}
if (!isArray($config['data'])) {
if (!isArray($config.data)) {
throw new Error(
"the data type 'dict' has a none array 'data' field in configuration 'data attribute'",
);
Expand Down
2 changes: 1 addition & 1 deletion src/extends/recommend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ const confs: TSuchSettings = {
uppercase: ['string', '[65,90]'],
lowercase: ['string', '[97,122]'],
alpha: ['string', '[65-90,97-122]'],
numeric: ['string', '[48-57]'],
numeric: ['string', '[48,57]'],
alphaNumeric: ['string', '[48-57,97-122,65-90]'],
alphaNumericDash: ['string', '[48-57,97-122,65-90,95]'],
/**
Expand Down
6 changes: 5 additions & 1 deletion src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,11 @@ export const makeCascaderData = (
// the nested max level < 10
let loop = 1;
// loop to get the root mocker
while (!$config.root && loop++ < 10) {
while (loop++ < 10) {
// once meet the root or data
if ($config.root || $config.data) {
break;
}
const refMocker = getRefMocker(lastPath, mocker);
if (!refMocker) {
// eslint-disable-next-line no-console
Expand Down

0 comments on commit d17086d

Please sign in to comment.