Skip to content

Commit

Permalink
fix: πŸ› fix the get path info not correct
Browse files Browse the repository at this point in the history
BREAKING CHANGE: 🧨 N

βœ… Closes: N
  • Loading branch information
fefit committed Feb 26, 2022
1 parent 970d5dd commit 5179a40
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 38 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Suchjs add changelog since `v1.0.0`.

## [v3.0.0] - 2022-02-26
## [v3.0.1] - 2022-02-26

### Changed

Expand Down
26 changes: 26 additions & 0 deletions __tests__/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,32 @@ describe('validate built-in types in nodejs', () => {
expect(typeof data.country === 'string').toBeTruthy();
expect(typeof data.state === 'string').toBeTruthy();
expect(typeof data.city === 'string').toBeTruthy();
// use user defined type
const provinceData = Such.instance<{
errno: number;
errmsg: string;
data?: {
province: string;
city: string;
area: string;
};
}>({
'errno:{1}': [0, 1],
errmsg: ':string:{10,30}',
'data?': {
province: ':province',
city: ':cascader:&./province',
area: ':cascader:&./city',
},
});
for (let i = 0; i < 100; i++) {
const value = provinceData.a();
if (value.data) {
expect(value.data.province.length > 0).toBeTruthy();
expect(value.data.city.length > 0).toBeTruthy();
expect(value.data.area.length > 0).toBeTruthy();
}
}
});
// test async
test('asc method', async ()=> {
Expand Down
80 changes: 49 additions & 31 deletions __tests__/types.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Such, { createNsSuch } from '../src/browser';

describe('test normal data', () => {
test("array data type", () => {
test('array data type', () => {
const arr = Such.instance<[string, number]>([':string', ':number']);
for(let i = 0; i < 100; i++){
for (let i = 0; i < 100; i++) {
const value = arr.a();
expect(value.length).toEqual(2);
const [first, second] = value;
Expand All @@ -15,9 +15,9 @@ describe('test normal data', () => {

describe('test apis', () => {
/**
* test alias api
*/
test("alias", () => {
* test alias api
*/
test('alias', () => {
// wrong alias
expect(() => {
// alias short is empty
Expand Down Expand Up @@ -57,12 +57,12 @@ describe('test apis', () => {
}).toThrow();
});
/**
* test define
*/
* test define
*/
// define a template
Such.define('sayHello', "'`:number:%d`'\\``:string`");
Array.from({
length: 100
length: 100,
}).forEach(() => {
expect(/^'-?\d+?'/.test(Such.as(':sayHello:{1,3}'))).toBeTruthy();
});
Expand All @@ -72,20 +72,19 @@ describe('test apis', () => {
}).toThrow();
});


describe('test built-in types', () => {
const DICTS = ['a', 'b', 'c'];
const COUNTRIES = {
China: ['BeiJing', 'ShangHai', 'WuHan'],
America: ['Washington', 'New York', 'Los Angeles'],
};
const globalConfig = {
suffix: 'ok'
suffix: 'ok',
};
Such.assign('dict', DICTS);
Such.assign('countries', COUNTRIES);
Such.assign('globalConfig', globalConfig);
Such.assign('addSuffix', function(value: string, suffix: string){
Such.assign('addSuffix', function (value: string, suffix: string) {
return value + '_' + suffix;
});
// test string
Expand Down Expand Up @@ -173,7 +172,7 @@ describe('test built-in types', () => {
}
// use a function
const suffixString = Such.instance<string>(
":string:{3}:@addSuffix(globalConfig.suffix)"
':string:{3}:@addSuffix(globalConfig.suffix)',
);
for (let i = 0; i < 100; i++) {
const value = suffixString.a();
Expand Down Expand Up @@ -619,12 +618,20 @@ describe('test built-in types', () => {
const refTmpl = {
a: 'hello',
b: 'world',
tmpl: ':::`:ref:&./a,./b:@join(" ")`'
tmpl: ':::`<helloworld>:ref:&./a,./b:@join(" ")`',
refTmplIndex: ":ref:&./tmpl/${0}",
refTmplName: ":ref:&./tmpl/${helloworld}"
};
const refTmplData = Such.as<typeof refTmpl>(refTmpl);
expect(refTmplData.tmpl === [refTmpl.a, refTmpl.b].join(" ")).toBeTruthy();
expect(refTmplData.tmpl === [refTmpl.a, refTmpl.b].join(' ')).toBeTruthy();
expect(refTmplData.tmpl === refTmplData.refTmplIndex).toBeTruthy();
expect(refTmplData.tmpl === refTmplData.refTmplName).toBeTruthy();
// reference
expect(['truetrue', 'falsefalse'].includes(Such.template('`:bool``:ref:&//${0}`').a())).toBeTruthy();
expect(
['truetrue', 'falsefalse'].includes(
Such.template('`:bool``:ref:&//${0}`').a(),
),
).toBeTruthy();
// wrong reference
expect(() => {
Such.as(':::`:ref:&./a,&./b`');
Expand Down Expand Up @@ -662,7 +669,7 @@ describe('test built-in recommend types', () => {
let finded = false;
for (let i = 0; i < 100; i++) {
const value = protocol.a() as string;
if(ps.includes(value)){
if (ps.includes(value)) {
finded = true;
break;
}
Expand All @@ -676,7 +683,7 @@ describe('test built-in recommend types', () => {
let finded = false;
for (let i = 0; i < 100; i++) {
const value = tld.a() as string;
if(ps.includes(value)){
if (ps.includes(value)) {
finded = true;
break;
}
Expand All @@ -694,7 +701,7 @@ describe('test built-in recommend types', () => {
const suffix = value.slice(label.length);
expect(value.startsWith(label)).toBeTruthy();
expect(suffix.charAt(0) === '.').toBeTruthy();
if(ps.includes(suffix.slice(1))){
if (ps.includes(suffix.slice(1))) {
finded = true;
break;
}
Expand All @@ -713,7 +720,7 @@ describe('test built-in recommend types', () => {
const segs = ip6.split(':');
segs.map((seg) => {
expect(/^[0-9a-f]{0,4}$/.test(seg)).toBeTruthy();
})
});
}
// test ip config options
expect(() => {
Expand All @@ -738,10 +745,15 @@ describe('test built-in recommend types', () => {
expect(eIpFirst >= 240 && eIpFirst <= 247).toBeTruthy();
// IPV6 with compress
const ipv6compress = Such.instance<string>(':ipv6:#[compress=0.5]');
for(let i = 0; i < 100; i++){
expect(ipv6compress.a().split(':').every((seg: string) => {
return seg === '' || seg === '0' || /^[0-9a-f]{1,4}$/.test(seg);
})).toBeTruthy();
for (let i = 0; i < 100; i++) {
expect(
ipv6compress
.a()
.split(':')
.every((seg: string) => {
return seg === '' || seg === '0' || /^[0-9a-f]{1,4}$/.test(seg);
}),
).toBeTruthy();
}
});
// uppsercase
Expand Down Expand Up @@ -820,7 +832,9 @@ describe('test built-in recommend types', () => {
).toBeTruthy();
}
// argb
const argbColor$hex = Such.instance(':color$hex:#[argb=true,lowercase=true]');
const argbColor$hex = Such.instance(
':color$hex:#[argb=true,lowercase=true]',
);
for (let i = 0; i < 100; i++) {
const value = argbColor$hex.a() as string;
expect(
Expand Down Expand Up @@ -894,7 +908,7 @@ describe('test built-in recommend types', () => {
const actualNum = Number(num);
if (index === 0) {
return !isNaN(actualNum) && actualNum >= 0 && actualNum <= 360;
} else if(index === segs.length -1){
} else if (index === segs.length - 1) {
return !isNaN(actualNum) && actualNum >= 0 && actualNum <= 1;
} else {
return /^([1-9][0-9]|[0-9]|100)%$/.test(num);
Expand All @@ -904,11 +918,15 @@ describe('test built-in recommend types', () => {
}
});
// test multiple called
test("multiple called", () => {
for(let i = 0; i < 5; i++){
expect(Object.keys(Such.as<{ip:string}>({
ip: ":ip"
})).includes('ip')).toBeTruthy();
test('multiple called', () => {
for (let i = 0; i < 5; i++) {
expect(
Object.keys(
Such.as<{ ip: string }>({
ip: ':ip',
}),
).includes('ip'),
).toBeTruthy();
}
});
});
});
1 change: 1 addition & 0 deletions dist/such.3.0.1.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.

11 changes: 8 additions & 3 deletions examples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ import PathMap from '../src/helpers/pathmap';
// const txt = await Such.asc('mock.txt');
// console.log(txt);
const instance = Such.instance({
a: ":::`:province`,`:cascader:&//${0}`,`<number>:number`,`:ref:&//${number}`",
b: ":ref:&./a/${0}"
"errno:{1}": [0, 1],
"errmsg": ":string:{10,30}",
"data?": {
"province": ":province",
"city": ":cascader:&./province",
"area": ":cascader:&./city"
}
});
console.log(Such.template('`:province`,`:cascader:&/${0}`,`<number>:number`,`:ref:&//${number}`').a());
// console.log(Such.template('`:province`,`:cascader:&/${0}`,`<number>:number`,`:ref:&//${number}`').a());
for(let i = 0; i < 10; i++){
console.log(instance.a());
}
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": "3.0.0",
"version": "3.0.1",
"description": "Generating fake data, define your own types easily, expandable and powerful.",
"main": "./lib/index.js",
"typings": "./lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/parser/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const parser: IParserFactory = {
variable = prefix.slice(1, -1);
break;
case '.':
depth = prefix.split('/').length - (prefix.charAt(1) === '/' ? 1 : 0);
depth = prefix.split('/').length - (prefix === '.' ? 1 : 0);
break;
case '/':
fix = true;
Expand Down

0 comments on commit 5179a40

Please sign in to comment.