Skip to content
This repository was archived by the owner on Jul 13, 2025. It is now read-only.

Commit b0f9d45

Browse files
committed
feat: Use ferrum.doctest to make sure examples are valid js code
1 parent f4eeee4 commit b0f9d45

File tree

10 files changed

+7709
-2511
lines changed

10 files changed

+7709
-2511
lines changed

README.md

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Features from the Rust language in JavaScript: Provides [Traits](https://doc.rus
4444
<a name="usage-features"></a>
4545
## Usage & Features
4646

47-
```bash
47+
```bash,notest
4848
$ npm add ferrum
4949
```
5050

@@ -88,9 +88,10 @@ assertSequenceEquals(b, [88, 90]);
8888

8989
const obj = {foo: 23, bar: 24};
9090
const log = [];
91-
for (const [key, value] in iter(obj)) {
91+
for (const [key, value] of iter(obj)) {
9292
log.push(`${key} | ${value}`);
9393
}
94+
9495
assertEquals(log, [
9596
'foo | 23',
9697
'bar | 24',
@@ -135,6 +136,8 @@ Sometimes it can be useful to create an intermediate function with just a
135136
few arguments instead of calling the function right away:
136137

137138
```js
139+
const { map, plus, list, assertSequenceEquals } = require('ferrum');
140+
138141
const myList = [
139142
[1,2,3],
140143
[4,5,6],
@@ -145,7 +148,7 @@ const myList = [
145148
// This example uses currying twice: in the `plus(2)`
146149
// and in the `map()`
147150
const a = map(myList, map(plus(2)));
148-
assertSequenceEquals(a, [
151+
assertSequenceEquals(map(a, list), [
149152
[3,4,5],
150153
[6,7,8],
151154
[9,10,11]
@@ -154,7 +157,7 @@ assertSequenceEquals(a, [
154157
// This is what the code would look like without currying:
155158
// A lot less convenient and harder to read
156159
const b = map(myList, (sublist) => map(sublist, (b) => plus(b, 2)));
157-
assertSequenceEquals(b, [
160+
assertSequenceEquals(map(b, list), [
158161
[3,4,5],
159162
[6,7,8],
160163
[9,10,11]
@@ -166,7 +169,7 @@ why we call it reverse currying. We have decided to use currying this way, becau
166169
be extra arguments after the function (otherwise you end up with dangling arguments multiple lines below)
167170
while the function is usually also the first parameter you want to supply when currying:
168171

169-
```js
172+
```js,notest
170173
const {each} = require('ferrum');
171174
172175
// This is much more handy
@@ -196,16 +199,18 @@ standard library in the form of the [`|>` operator](https://developer.mozilla.or
196199

197200
```js
198201
const {sqrt} = Math;
199-
const {pipe, filter, uniq, map, mul, mapSort, identity, pipe,
200-
prepend, takeWhile, all, range} = require('ferrum');
202+
const {
203+
pipe, filter, uniq, map, mul, mapSort, identity,
204+
prepend, takeWhile, all, range, assertSequenceEquals,
205+
} = require('ferrum');
201206

202207
const a = pipe(
203208
[5,1,6,7,10,11,1,3,4],
204209
filter(x => x%2 === 1), // Get rid of even number
205210
uniq, // Get rid of duplicates
206211
map(mul(3)), // Multiply each element by three
207212
mapSort(identity)); // Sort all numbers
208-
assertSequenceEquals(q, [3,9,12,15,18,21,30,33]);
213+
assertSequenceEquals(a, [3,9,15,21,33]);
209214

210215
// Very simple primality test
211216
const isPrime = (v) => v > 1 && pipe(
@@ -330,7 +335,7 @@ This means that the values in iterators/sequences are only evaluated once they
330335
are needed:
331336

332337
```js
333-
const {map, plus} = require('ferrum');
338+
const {map, plus, list} = require('ferrum');
334339
const a = map([1,2,3], plus(2)); // At this point, no calculations have been performed
335340
const b = list(a); // This will actually cause the values of the a iterator to be calculated
336341
```
@@ -342,13 +347,23 @@ sequences, like the `primes()` sequence above. It can be more efficient as well,
342347
values that are not needed are not computed.
343348

344349
```js
345-
const {take, list, assertEquals} = require('ferrum');
350+
const {take, list, assertSequenceEquals} = require('ferrum');
351+
352+
function* fibonacci() {
353+
let a=0, b=1;
354+
while (true) {
355+
yield a;
356+
yield b;
357+
a += b;
358+
b += a;
359+
}
360+
}
346361

347-
// Even though primes() is infinite, this just works because only the
348-
// first five primes are actually calculated.
349-
// Note that just list(primes()) would crash the program since that would
362+
// Even though fibonacci() is infinite, this just works because only the
363+
// first five fibonacci numbers are actually generated
364+
// Note that just list(fibonacci()) would crash the program since that would
350365
// require infinite memory and infinite time
351-
assertEquals(list(take(5, primes())), [2, 3, 5, 7, 11]);
366+
assertSequenceEquals(take(fibonacci(), 5), [0, 1, 1, 2, 3]);
352367
```
353368

354369
Underscore and lodash use arrays instead of iterators, so they have no lazy evaluation support.
@@ -446,7 +461,8 @@ Object.prototype[Size] = () => {
446461

447462
// Using symbols on values like null or undefined will just lead to a TypeError
448463
// being thrown
449-
null[Size] = () => 0;
464+
465+
//null[Size] = () => 0; // throws TypeError
450466
```
451467

452468
The oldest pre-ES6 implementation just used method names; this strategy is very
@@ -478,7 +494,7 @@ Using traits we can actually encapsulate this relationship well:
478494

479495
```js
480496
// dbtable.js
481-
const { Trait } = require('ferrum');
497+
const { Trait, Size: SyncSize } = require('ferrum');
482498
const Size = new Trait('Size');
483499

484500
class DbTable {
@@ -487,23 +503,13 @@ class DbTable {
487503
}
488504
};
489505

490-
module.exports = {Size, DbTable};
491-
```
492-
493-
```js
494-
// localtable.js
495-
const { Trait } = require('ferrum');
496-
const {Size: AsyncSize} = require('./dbtable.js')
497-
498-
const Size = new Trait('Size');
499-
500506
class MyLocalTable {
501507
[Size.sym]() {
502508
return this._payload.length;
503509
}
504510
}
505511

506-
AsyncSize.implDerived([Size], ([size], v) => Promise.resolve(size(v)));
512+
Size.implDerived([SyncSize], ([size], v) => Promise.resolve(size(v)));
507513
```
508514

509515
This example above illustrates how – using traits – we can not only deal with
@@ -527,10 +533,10 @@ of traits at once.
527533
const {plus, and, not, is, xor, map, list} = require('ferrum');
528534

529535
list(map([1,2,3], plus(2))); // => [3,4,5]
530-
and(True, False); // => False
531-
not(1); // => False
532-
is(2, 2); // => True
533-
xor(True, False); // => True
536+
and(true, false); // => false
537+
not(1); // => false
538+
is(2, 2); // => true
539+
xor(true, false); // => true
534540
```
535541

536542
<a name="typing-utilities"></a>
@@ -540,7 +546,7 @@ Ferrum provides utilities for working with types that can be safely
540546
used with null and undefined.
541547

542548
```js
543-
class {isdef, type, typename} = require('ferrum');
549+
const {isdef, type, typename} = require('ferrum');
544550

545551
isdef(0); // => true
546552
isdef(null); // => false
@@ -607,20 +613,20 @@ pair(2)(1); // => [1,2]
607613
<a name="build"></a>
608614
### Build
609615

610-
```bash
616+
```bash,notest
611617
$ npm install
612618
```
613619

614620
<a name="test"></a>
615621
### Test
616622

617-
```bash
623+
```bash,notest
618624
$ npm test
619625
```
620626

621627
<a name="lint"></a>
622628
### Lint
623629

624-
```bash
630+
```bash,notest
625631
$ npm run lint
626632
```

0 commit comments

Comments
 (0)