Skip to content

Commit

Permalink
fix: πŸ› fix the call some extended mockit type cause an error
Browse files Browse the repository at this point in the history
BREAKING CHANGE: 🧨 N

βœ… Closes: #14
  • Loading branch information
fefit committed Feb 15, 2022
1 parent 78318b0 commit bc82a2a
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 7 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

Suchjs add changelog since `v1.0.0`.


## [v2.1.8] - 2022-02-15

### Fixed

- Fix the `Such.as` call the extended types multiple times may lost the `instance` property because the `instance` was created in `init` method, but the `init` method only call once.

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

### Fixed
Expand Down
8 changes: 8 additions & 0 deletions __tests__/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -732,4 +732,12 @@ describe('test built-in recommend types', () => {
expect(flag).toBeTruthy();
}
});
// 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();
}
});
});
1 change: 1 addition & 0 deletions dist/such.2.1.8.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.

54 changes: 49 additions & 5 deletions examples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,55 @@ import Such, { createNsSuch } from '../src/index';
// });
// console.log(value);
// }
const instance = Such.instance<number>(':increment{3}');
for (let i = 0; i < 5; i++) {
const result = instance.a();
console.log(result);
}
// const instance = Such.instance<number>(':increment{3}');
// for (let i = 0; i < 5; i++) {
// const result = instance.a();
// console.log(result);
// }
console.log(Such.as({
bool: ":bool",
int: ":int",
percent: ":percent",
uppercase: ":uppercase:{2,4}",
lowercase: ":lowercase:{2,4}",
alpha: ":alpha:{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",
}));
console.log(Such.as({
bool: ":bool",
int: ":int",
percent: ":percent",
uppercase: ":uppercase:{2,4}",
lowercase: ":lowercase:{2,4}",
alpha: ":alpha:{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",
}));
// dict
// const dict = Such.instance(':dict:&<dataDir>/dict.txt');
// console.log(dict.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": "2.1.7",
"version": "2.1.8",
"description": "Generating fake data, define your own types easily, expandable and powerful.",
"main": "./lib/index.js",
"typings": "./lib/index.d.ts",
Expand Down
6 changes: 6 additions & 0 deletions src/core/such.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,12 @@ export class Such {
},
});
}
// the init method will only call once
// but the generate function is cached will call by other instance
// so here need a judgement, fix #14
if(!this.instance){
this.init();
}
return this.instance.a();
}
// test
Expand Down

0 comments on commit bc82a2a

Please sign in to comment.