Repository: dubzzz/fast-check
Pull request #1835 (2021-05-06 21:09:53)

🚚 Move `lorem` arbitrary into /arbitrary
<!-- Why is this PR for? -->
<!-- Describe the reason why you opened this PR or give a link towards the associated issue. -->

## In a nutshell

❌ New feature
❌ Fix an issue
❌ Documentation improvement
✔️ Other: *please explain*

(✔️: yes, ❌: no)

## Potential impacts

None

codesandbox[bot]: This pull request is automatically built and testable in [CodeSandbox](https://codesandbox.io).

  To see build info of the built libraries, click [here](https://ci.codesandbox.io/status/dubzzz/fast-check/pr/1835/builds/127508) or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 3671729a546f8ca411b2acbad9988652f3cf9c06:

|Sandbox| Source |
|--|--|
|[Vanilla](https://codesandbox.io/s/vanilla-4ltmw)| Configuration |
|[fast-check-examples](https://codesandbox.io/s/fast-check-examples-8o6jv)| Configuration |

codecov[bot]: # [Codecov](https://codecov.io/gh/dubzzz/fast-check/pull/1835?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nicolas+DUBIEN) Report
> Merging [#1835](https://codecov.io/gh/dubzzz/fast-check/pull/1835?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nicolas+DUBIEN) (97d9d76) into [main](https://codecov.io/gh/dubzzz/fast-check/commit/57f685dd420376885e5fb690f18bcc1a5125d2ad?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nicolas+DUBIEN) (57f685d) will **decrease** coverage by `0.02%`.
> The diff coverage is `94.73%`.

[![Impacted file tree graph](https://codecov.io/gh/dubzzz/fast-check/pull/1835/graphs/tree.svg?width=650&height=150&src=pr&token=fRi2fJN730&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nicolas+DUBIEN)](https://codecov.io/gh/dubzzz/fast-check/pull/1835?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nicolas+DUBIEN)

```diff
@@            Coverage Diff             @@
##             main    #1835      +/-   ##
==========================================
- Coverage   97.53%   97.51%   -0.03%     
==========================================
  Files         143      144       +1     
  Lines        3851     3858       +7     
  Branches      700      701       +1     
==========================================
+ Hits         3756     3762       +6     
- Misses         91       92       +1     
  Partials        4        4              
```

| Flag | Coverage Δ | |
|---|---|---|
| unit-tests | `97.51% <94.73%> (-0.03%)` | :arrow_down: |
| unit-tests-10.x-Linux | `?` | |
| unit-tests-12.x-Linux | `?` | |
| unit-tests-14.x-Linux | `97.51% <94.73%> (-0.03%)` | :arrow_down: |
| unit-tests-16.x-Linux | `?` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nicolas+DUBIEN#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/dubzzz/fast-check/pull/1835?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nicolas+DUBIEN) | Coverage Δ | |
|---|---|---|
| [src/arbitrary/lorem.ts](https://codecov.io/gh/dubzzz/fast-check/pull/1835/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nicolas+DUBIEN#diff-c3JjL2FyYml0cmFyeS9sb3JlbS50cw==) | `94.73% <88.88%> (ø)` | |
| [src/arbitrary/\_internals/mappers/WordsToLorem.ts](https://codecov.io/gh/dubzzz/fast-check/pull/1835/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nicolas+DUBIEN#diff-c3JjL2FyYml0cmFyeS9faW50ZXJuYWxzL21hcHBlcnMvV29yZHNUb0xvcmVtLnRz) | `100.00% <100.00%> (ø)` | |
| [src/fast-check-default.ts](https://codecov.io/gh/dubzzz/fast-check/pull/1835/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nicolas+DUBIEN#diff-c3JjL2Zhc3QtY2hlY2stZGVmYXVsdC50cw==) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/dubzzz/fast-check/pull/1835?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nicolas+DUBIEN).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nicolas+DUBIEN)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/dubzzz/fast-check/pull/1835?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nicolas+DUBIEN). Last update [57f685d...97d9d76](https://codecov.io/gh/dubzzz/fast-check/pull/1835?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nicolas+DUBIEN). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nicolas+DUBIEN).


Patch:
diff --git a/src/arbitrary/_internals/mappers/WordsToLorem.ts b/src/arbitrary/_internals/mappers/WordsToLorem.ts
new file mode 100644
index 00000000..4a867922
--- /dev/null
+++ b/src/arbitrary/_internals/mappers/WordsToLorem.ts
@@ -0,0 +1,21 @@
+/** @internal */
+export function wordsToJoinedStringMapper(words: string[]): string {
+  // Strip any coma
+  return words.map((w) => (w[w.length - 1] === ',' ? w.substr(0, w.length - 1) : w)).join(' ');
+}
+
+/** @internal */
+export function wordsToSentenceMapper(words: string[]): string {
+  // Strip trailing coma (only)
+  let sentence = words.join(' ');
+  if (sentence[sentence.length - 1] === ',') {
+    sentence = sentence.substr(0, sentence.length - 1);
+  }
+  return sentence[0].toUpperCase() + sentence.substring(1) + '.';
+}
+
+/** @internal */
+export function sentencesToParagraphMapper(sentences: string[]): string {
+  // Sentences are supposed to always end by a '.' and not contain any other '.'
+  return sentences.join(' ');
+}
diff --git a/src/check/arbitrary/LoremArbitrary.ts b/src/arbitrary/lorem.ts
similarity index 90%
rename from src/check/arbitrary/LoremArbitrary.ts
rename to src/arbitrary/lorem.ts
index 892f8750..e7b3ff0c 100644
--- a/src/check/arbitrary/LoremArbitrary.ts
+++ b/src/arbitrary/lorem.ts
@@ -1,7 +1,12 @@
-import { array } from '../../arbitrary/array';
-import { constant } from '../../arbitrary/constant';
-import { Arbitrary } from './definition/Arbitrary';
-import { frequency } from '../../arbitrary/frequency';
+import { array } from './array';
+import { constant } from './constant';
+import { Arbitrary } from '../check/arbitrary/definition/Arbitrary';
+import { frequency } from './frequency';
+import {
+  sentencesToParagraphMapper,
+  wordsToJoinedStringMapper,
+  wordsToSentenceMapper,
+} from './_internals/mappers/WordsToLorem';
 
 /**
  * Constraints to be applied on {@link lorem}
@@ -259,17 +264,14 @@ function lorem(...args: [] | [number] | [number, boolean] | [LoremConstraints]):
   const maxWordsCount = typeof args[0] === 'object' ? args[0].maxCount : args[0];
   const sentencesMode = typeof args[0] === 'object' ? args[0].mode === 'sentences' : args[1];
   const maxCount = maxWordsCount || 5;
-  if (maxCount < 1) throw new Error(`lorem has to produce at least one word/sentence`);
+  if (maxCount < 1) {
+    throw new Error(`lorem has to produce at least one word/sentence`);
+  }
   if (sentencesMode) {
-    const sentence = array(loremWord(), { minLength: 1 })
-      .map((words) => words.join(' '))
-      .map((s) => (s[s.length - 1] === ',' ? s.substr(0, s.length - 1) : s))
-      .map((s) => s[0].toUpperCase() + s.substring(1) + '.');
-    return array(sentence, { minLength: 1, maxLength: maxCount }).map((sentences) => sentences.join(' '));
+    const sentence = array(loremWord(), { minLength: 1 }).map(wordsToSentenceMapper);
+    return array(sentence, { minLength: 1, maxLength: maxCount }).map(sentencesToParagraphMapper);
   } else {
-    return array(loremWord(), { minLength: 1, maxLength: maxCount }).map((words) =>
-      words.map((w) => (w[w.length - 1] === ',' ? w.substr(0, w.length - 1) : w)).join(' ')
-    );
+    return array(loremWord(), { minLength: 1, maxLength: maxCount }).map(wordsToJoinedStringMapper);
   }
 }
 
diff --git a/src/fast-check-default.ts b/src/fast-check-default.ts
index 276b02cb..087288ce 100644
--- a/src/fast-check-default.ts
+++ b/src/fast-check-default.ts
@@ -54,7 +54,7 @@ import { maxSafeNat } from './arbitrary/maxSafeNat';
 import { nat, NatConstraints } from './arbitrary/nat';
 import { ipV4, ipV4Extended, ipV6 } from './check/arbitrary/IpArbitrary';
 import { letrec } from './arbitrary/letrec';
-import { lorem, LoremConstraints } from './check/arbitrary/LoremArbitrary';
+import { lorem, LoremConstraints } from './arbitrary/lorem';
 import { mapToConstant } from './arbitrary/mapToConstant';
 import { memo, Memo } from './arbitrary/memo';
 import { mixedCase, MixedCaseConstraints } from './arbitrary/mixedCase';


Test patch:
diff --git a/test/unit/check/arbitrary/LoremArbitrary.spec.ts b/test/unit/arbitrary/LoremArbitrary.spec.ts
similarity index 92%
rename from test/unit/check/arbitrary/LoremArbitrary.spec.ts
rename to test/unit/arbitrary/LoremArbitrary.spec.ts
index f819bd0b..4f59450a 100644
--- a/test/unit/check/arbitrary/LoremArbitrary.spec.ts
+++ b/test/unit/arbitrary/LoremArbitrary.spec.ts
@@ -1,10 +1,10 @@
 import * as prand from 'pure-rand';
-import * as fc from '../../../../lib/fast-check';
+import * as fc from '../../../lib/fast-check';
 
-import { lorem } from '../../../../src/check/arbitrary/LoremArbitrary';
-import { Random } from '../../../../src/random/generator/Random';
+import { lorem } from '../../../src/arbitrary/lorem';
+import { Random } from '../../../src/random/generator/Random';
 
-import { generateOneValue } from './generic/GenerateOneValue';
+import { generateOneValue } from '../check/arbitrary/generic/GenerateOneValue';
 
 describe('LoremArbitrary', () => {
   describe('lorem', () => {