Skip to content

Commit fa09e80

Browse files
committed
feat: 🎸 rewrite the path search in template
BREAKING CHANGE: 🧨 Y ✅ Closes: Y
1 parent b0be01f commit fa09e80

File tree

16 files changed

+242
-213
lines changed

16 files changed

+242
-213
lines changed

‎__tests__/types.test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -362,14 +362,14 @@ describe('test built-in types', () => {
362362
// in a template
363363
return Such.as({
364364
a: 1,
365-
b: ':::`:string``:ref:&/${2}`',
365+
b: ':::`:string``:ref:&//${2}`',
366366
});
367367
}).toThrow();
368368
expect(() => {
369369
// in a template
370370
const instance = Such.instance({
371371
a: 1,
372-
b: ':::`:string``:ref:&/${2}``:number`',
372+
b: ':::`:string``:ref:&//${2}``:number`',
373373
});
374374
for (let i = 0; i < 10; i++) {
375375
instance.a();
@@ -379,7 +379,7 @@ describe('test built-in types', () => {
379379
// in a template with named
380380
const instance = Such.instance({
381381
a: 1,
382-
b: ':::`:string``:ref:&/${mystr}``:number`',
382+
b: ':::`:string``:ref:&//${mystr}``:number`',
383383
});
384384
for (let i = 0; i < 10; i++) {
385385
instance.a();
@@ -415,15 +415,15 @@ describe('test built-in types', () => {
415415
const tmplRef = {
416416
a: 'hello',
417417
b: 'world',
418-
c: ':::`:ref:&./a`:`:regexp:/abc/`;`:ref:&./b`:`:ref:&/${1}`;',
418+
c: ':::`:ref:&./a`:`:regexp:/abc/`;`:ref:&./b`:`:ref:&//${1}`;',
419419
};
420420
const tmplRefData = Such.as(tmplRef) as typeof tmplRef;
421421
expect(tmplRefData.c === `${tmplRef.a}:abc;${tmplRef.b}:abc;`).toBeTruthy();
422422
// make a template ref with named
423423
const namedTmplRef = {
424424
a: 'hello',
425425
b: 'world',
426-
c: ':::`<say>:ref:&./a`,`<say>:ref:&./b`!`<helloworld>:ref:&/${say}:@join(",")`!`:ref:&/${helloworld}`!',
426+
c: ':::`<say>:ref:&./a`,`<say>:ref:&./b`!`<helloworld>:ref:&//${say}:@join(",")`!`:ref:&//${helloworld}`!',
427427
};
428428
const namedTmplRefData = Such.as(namedTmplRef) as typeof namedTmplRef;
429429
expect(
@@ -624,7 +624,7 @@ describe('test built-in types', () => {
624624
const refTmplData = Such.as<typeof refTmpl>(refTmpl);
625625
expect(refTmplData.tmpl === [refTmpl.a, refTmpl.b].join(" ")).toBeTruthy();
626626
// reference
627-
expect(['truetrue', 'falsefalse'].includes(Such.template('`:bool``:ref:&/${0}`').a())).toBeTruthy();
627+
expect(['truetrue', 'falsefalse'].includes(Such.template('`:bool``:ref:&//${0}`').a())).toBeTruthy();
628628
// wrong reference
629629
expect(() => {
630630
Such.as(':::`:ref:&./a,&./b`');

‎examples/index.ts

+18-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,24 @@ import Such, { createNsSuch } from '../src/index';
33
import PathMap from '../src/helpers/pathmap';
44
(async () => {
55
await Such.loadData();
6-
console.log(Such.as(':dict:&<dataDir>/dict.txt'));
7-
const txt = await Such.asc('mock.txt');
8-
console.log(txt);
9-
const map = new PathMap(false);
10-
map.set([0], 1);
11-
map.set([1], 2);
12-
console.log(map.get([]));
6+
// console.log(Such.as(':dict:&<dataDir>/dict.txt'));
7+
// const txt = await Such.asc('mock.txt');
8+
// console.log(txt);
9+
const instance = Such.instance({
10+
a: ":::`:province`,`:cascader:&//${0}`,`<number>:number`,`:ref:&//${number}`",
11+
b: ":ref:&./a/${0}"
12+
});
13+
for(let i = 0; i < 10; i++){
14+
console.log(instance.a());
15+
}
16+
// console.log(Such.template('`:bool``:ref:&//${0}`').a());
17+
// const tmplRef = {
18+
// a: 'hello',
19+
// b: 'world',
20+
// c: ':::`:ref:&./a`:`:regexp:/abc/`;`:ref:&./b`:`:ref:&//${1}`;',
21+
// };
22+
// const tmplRefData = Such.as(tmplRef);
23+
// console.log(tmplRefData);
1324
// const globalConfig = {
1425
// suffix: 'ok'
1526
// };

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "suchjs",
3-
"version": "2.2.3",
3+
"version": "3.0.0",
44
"description": "Generating fake data, define your own types easily, expandable and powerful.",
55
"main": "./lib/index.js",
66
"typings": "./lib/index.d.ts",

‎src/core/mockit.ts

+26-29
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ type TPartStaticMockit = Partial<TStaticMockit>;
225225
export default abstract class Mockit<T = unknown> {
226226
// chain names
227227
public static chainNames: string[] = [];
228+
// inherit base mockit type
229+
public static baseType: typeof BaseExtendMockit;
228230
// constructor name, for cache
229231
public static readonly constrName: string;
230232
// namespace
@@ -308,28 +310,25 @@ export default abstract class Mockit<T = unknown> {
308310
*
309311
* @param {string} name
310312
* @param {TMModifierFn<T>} fn
311-
* @param {string} [pos]
312313
* @returns
313314
* @memberof Mockit
314315
*/
315316
public addModifier(
316317
name: string,
317-
fn: TMModifierFn<T>,
318-
pos?: string,
318+
fn: TMModifierFn<T>
319319
): void | never {
320-
return this.add('modifier', name, fn, pos);
320+
return this.add('modifier', name, fn);
321321
}
322322
/**
323323
*
324324
*
325325
* @param {string} name
326326
* @param {TMRuleFn} fn
327-
* @param {string} [pos]
328327
* @returns
329328
* @memberof Mockit
330329
*/
331-
public addRule(name: string, fn: TMRuleFn, pos?: string): void | never {
332-
return this.add('rule', name, fn, pos);
330+
public addRule(name: string, fn: TMRuleFn): void | never {
331+
return this.add('rule', name, fn);
333332
}
334333
/**
335334
*
@@ -457,8 +456,7 @@ export default abstract class Mockit<T = unknown> {
457456
private add(
458457
type: 'rule' | 'modifier',
459458
name: string,
460-
fn: TMRuleFn | TMModifierFn<T>,
461-
pos?: string,
459+
fn: TMRuleFn | TMModifierFn<T>
462460
): never | void {
463461
const { namespace, constrName } = this.getStaticProps();
464462
const curName = constrName;
@@ -489,26 +487,7 @@ export default abstract class Mockit<T = unknown> {
489487
if (isRuleType) {
490488
this.setAllowAttrs(name);
491489
}
492-
// add by position
493-
if (pos === undefined || pos.trim() === '') {
494-
target.push(name);
495-
} else {
496-
let prepend = false;
497-
if (pos.charAt(0) === '^') {
498-
prepend = true;
499-
pos = pos.slice(1);
500-
}
501-
if (pos === '') {
502-
target.unshift(name);
503-
} else {
504-
const findIndex = target.indexOf(pos);
505-
if (findIndex < 0) {
506-
throw new Error(`no exists ${type} name of ${pos}`);
507-
} else {
508-
target.splice(findIndex + (prepend ? 0 : 1), 0, name);
509-
}
510-
}
511-
}
490+
target.push(name);
512491
fns[name] = fn;
513492
}
514493
/**
@@ -525,6 +504,7 @@ export default abstract class Mockit<T = unknown> {
525504
configOptions,
526505
selfConfigOptions,
527506
specialType,
507+
baseType
528508
} = staticMockit;
529509
return {
530510
constrName,
@@ -534,6 +514,7 @@ export default abstract class Mockit<T = unknown> {
534514
configOptions,
535515
selfConfigOptions,
536516
specialType,
517+
baseType
537518
};
538519
}
539520
/**
@@ -700,3 +681,19 @@ export default abstract class Mockit<T = unknown> {
700681
return this.runFuncs(result, options);
701682
}
702683
}
684+
685+
/**
686+
* BaseExtendMockit
687+
* Just for types
688+
*/
689+
export class BaseExtendMockit extends Mockit {
690+
init(): void {
691+
// nothing to do
692+
}
693+
test(): boolean {
694+
return false;
695+
}
696+
generate(): void {
697+
// nothing to do
698+
}
699+
}

‎src/core/parser.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { IParserConfig } from '../types/parser';
2-
import { encodeSplitor, splitor as confSplitor } from '../data/config';
3-
import { encodeRegexpChars, hasOwn, isArray } from '../helpers/utils';
2+
import { encodeSplitor, encodeRegexpChars, splitor as confSplitor } from '../data/config';
3+
import { hasOwn, isArray } from '../helpers/utils';
44
import { TMatchResult, TObj, TStrList } from '../types/common';
55
import Mockit from './mockit';
66
export interface Tags {
@@ -341,8 +341,9 @@ export class Dispatcher {
341341
);
342342
} else {
343343
if (mockit) {
344-
const { constrName, allowAttrs } = mockit.getStaticProps();
345-
if (!allowAttrs.includes(type)) {
344+
const { constrName, allowAttrs, baseType } = mockit.getStaticProps();
345+
// when no base type, check the allow attrs
346+
if (!baseType && !allowAttrs.includes(type)) {
346347
switch (type) {
347348
case '$config':
348349
throw new Error(

0 commit comments

Comments
 (0)