data-faker-plus 0.0.8 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +53 -1
- package/dist/index.cjs +1 -0
- package/dist/index.esm.js +1 -1
- package/dist/index.iife.js +1 -0
- package/dist/types/core/DataFieldDecoratorFactory.d.ts +8 -4
- package/dist/types/core/IteratorFactory.d.ts +25 -0
- package/dist/types/core/ModelManager.d.ts +1 -0
- package/dist/types/index.d.ts +1 -0
- package/package.json +3 -3
- package/dist/index.cjs.js +0 -1
- package/dist/index.umd.js +0 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Getting Started
|
|
2
2
|
|
|
3
|
-
[Official Website](https://df-docs-
|
|
3
|
+
[Official Website](https://df-docs-seven.vercel.app/)
|
|
4
4
|
[中文官网](https://datafaker-9j23z0sk.maozi.io/)
|
|
5
5
|
|
|
6
6
|
## Introduction
|
|
@@ -580,3 +580,55 @@ const userDatas = fakeData(userModel, {
|
|
|
580
580
|
},
|
|
581
581
|
});
|
|
582
582
|
```
|
|
583
|
+
|
|
584
|
+
## Data Iterator
|
|
585
|
+
|
|
586
|
+
Data Iterators are designed to help you sequentially use data from a collection when generating data, simplifying your code writing. For example, consider the following scenario:
|
|
587
|
+
|
|
588
|
+
> Need to generate 3 user data entries, each with a different hobby.
|
|
589
|
+
|
|
590
|
+
If written using traditional `faker.js`, the code would look like this:
|
|
591
|
+
|
|
592
|
+
```javascript{2,6}
|
|
593
|
+
let hobbyArr = ['basketball', 'soccer', 'table tennis'];
|
|
594
|
+
let index = 0;
|
|
595
|
+
const userModel = defineModel('user', {
|
|
596
|
+
id: 'string.uuid',
|
|
597
|
+
hobby: () => {
|
|
598
|
+
return hobbyArr[index++];
|
|
599
|
+
},
|
|
600
|
+
});
|
|
601
|
+
console.log(fakeData(userModel, 4));
|
|
602
|
+
```
|
|
603
|
+
|
|
604
|
+
The generated data would be as follows:
|
|
605
|
+
|
|
606
|
+
```json
|
|
607
|
+
[
|
|
608
|
+
{ "id": "d16e7a49-5e7a-40a0-97e7-68693ffa7268", "hobby": "basketball" },
|
|
609
|
+
{ "id": "268a6a63-5eee-4668-a166-d1b9f8bcf510", "hobby": "soccer" },
|
|
610
|
+
{ "id": "2ed907c6-0cdf-40bd-95cf-6aaf3ebe5d1c", "hobby": "table tennis" },
|
|
611
|
+
{ "id": "7d9b0df7-7fd3-401d-a7a9-59759a0948b4", "hobby": undefined }
|
|
612
|
+
]
|
|
613
|
+
```
|
|
614
|
+
|
|
615
|
+
As you can see, you need to manually maintain the `index` variable, which is inconvenient and prone to confusion with other `index` variables. Therefore, `DataFaker` takes this into account. You only need to obtain an iterator to sequentially use data from the collection, as shown below:
|
|
616
|
+
|
|
617
|
+
```javascript {2,6}
|
|
618
|
+
let hobbyArr = ['basketball', 'soccer', 'table tennis'];
|
|
619
|
+
const iterator = IteratorFactory.getIterator(hobbyArr);
|
|
620
|
+
const userModel = defineModel('user', {
|
|
621
|
+
id: 'string.uuid',
|
|
622
|
+
hobby: () => {
|
|
623
|
+
return iterator.next().value;
|
|
624
|
+
},
|
|
625
|
+
});
|
|
626
|
+
console.log(fakeData(userModel, 4));
|
|
627
|
+
```
|
|
628
|
+
|
|
629
|
+
`DataFaker` provides four types of data iterators, which can be obtained from the iterator factory `IteratorFactory`:
|
|
630
|
+
|
|
631
|
+
- Forward Iterator: `IteratorFactory.getIterator()`
|
|
632
|
+
- Reverse Iterator: `IteratorFactory.getReverseIterator()`
|
|
633
|
+
- Loop Forward Iterator: `IteratorFactory.getLoopIterator()`
|
|
634
|
+
- Loop Reverse Iterator: `IteratorFactory.getLoopReverseIterator()`
|