Skip to content

Commit

Permalink
πŸ“ Add JSdoc for exported functions (#17)
Browse files Browse the repository at this point in the history
* ✨ Add all JS doc

* πŸ› Update test builder and fix typos

* v0.6.1
  • Loading branch information
A1c0 authored Feb 21, 2022
1 parent 9c00c5a commit 39aea24
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 230 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ sandbox
node_modules
*.log
/test/
/sanctuary-lourde.test.js
/sanctuary-lourdes.test.js
/index.cjs
7 changes: 7 additions & 0 deletions .script/assets/prefix.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import {promise, reject, resolve} from 'fluture';
import test from 'oletus';
import S from 'sanctuary';
import $ from 'sanctuary-def';

import Sl from './index.js';

const log = l => x => `[${l}]: ${S.show(x)}`;

const isEither = S.is ($.Either ($.Unknown) ($.Unknown));
Expand Down
2 changes: 0 additions & 2 deletions .script/assets/prefix_import.txt

This file was deleted.

4 changes: 2 additions & 2 deletions .script/doctest.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
CHECK_TYPES_SANCTUARY=false node .script/test-builder.js
oletus sanctuary-lourde.test.js
rm sanctuary-lourde.test.js
oletus sanctuary-lourdes.test.js
rm sanctuary-lourdes.test.js
20 changes: 1 addition & 19 deletions .script/test-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,6 @@ import * as path from 'path';
import {getApiDoc} from './common.js';
import {APP_DIR, S, Sl, readFile, writeFile} from './utils.js';

const lib = S.pipe ([
readFile,
Sl.replace (/export const create = \({checkTypes}\) => {/)
('const checkTypes = true;'),
S.lines,
lines =>
S.take (S.fromMaybe (0) (Sl.indexOf (' return exportFn;') (lines)))
(lines),
S.fromMaybe ([]),
S.reject (S.test (/^ *\/\/.*$/)),
S.map (Sl.replace (/^ {2}(.*)$/) ('$1')),
S.joinWith ('\n'),
Sl.replace (/^exportFn.*?(\n.*?)*;$/gm) (''),
Sl.replace (/\n{3,}/g) ('\n\n'),
]) (path.resolve (APP_DIR, 'index.js'));

// isFlutureTest :: Array String -> Boolean
const isFlutureTest = S.pipe ([
S.dropLast (1), // drop the result
Expand Down Expand Up @@ -156,10 +140,8 @@ const tests = S.pipe ([
]) (path.resolve (APP_DIR, 'index.js'));

const testScript = S.joinWith ('\n') ([
readFile (path.resolve (APP_DIR, '.script/assets/prefix_import.txt')),
lib,
readFile (path.resolve (APP_DIR, '.script/assets/prefix.txt')),
tests,
]);

writeFile (path.resolve (APP_DIR, 'sanctuary-lourde.test.js')) (testScript);
writeFile (path.resolve (APP_DIR, 'sanctuary-lourdes.test.js')) (testScript);
124 changes: 62 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,72 +42,72 @@ const Sl = create({checkTypes: CHECK_TYPES_SANCTUARY});

### Array

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L28">`nth :: NonNegativeInteger -> Array a -> Maybe a`</a>
#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L48">`nth :: NonNegativeInteger -> Array a -> Maybe a`</a>

Get the N th elements of array

```js
> nth (0) ([])
> Sl.nth (0) ([])
Nothing

> nth (1) ([1, 2, 3])
> Sl.nth (1) ([1, 2, 3])
Just (2)

> nth (7) ([1, 2, 3])
> Sl.nth (7) ([1, 2, 3])
Nothing
```

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L48">`indexOf :: a -> Array a -> Maybe NonNegativeInteger`</a>
#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L69">`indexOf :: a -> Array a -> Maybe NonNegativeInteger`</a>

Get the first index of an array which corresponding to an item

```js
> indexOf ('red') (['red', 'green', 'blue'])
> Sl.indexOf ('red') (['red', 'green', 'blue'])
Just (0)

> indexOf ('yellow') (['red', 'green', 'blue'])
> Sl.indexOf ('yellow') (['red', 'green', 'blue'])
Nothing

> indexOf ({name: "white", hex: "#fff"})
. ([{name: "white", hex: "#fff"}, {name: "black", hex: "#000"}])
> Sl.indexOf ({name: "white", hex: "#fff"})
. ([{name: "white", hex: "#fff"}, {name: "black", hex: "#000"}])
Just (0)
```

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L82">`splitEach :: PositiveInteger -> Array a -> Array Array a`</a>
#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L103">`splitEach :: PositiveInteger -> Array a -> Array Array a`</a>

Split an array on sub-array of size N

```js
> splitEach (3) ([1, 2, 3, 4, 5, 6, 7, 8, 9])
> Sl.splitEach (3) ([1, 2, 3, 4, 5, 6, 7, 8, 9])
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

> splitEach (2) ([1, 2, 3, 4, 5, 6, 7])
> Sl.splitEach (2) ([1, 2, 3, 4, 5, 6, 7])
[[1, 2], [3, 4], [5, 6], [7]]
```

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L103">`intersperse :: a -> Array a -> Array a`</a>
#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L125">`intersperse :: a -> Array a -> Array a`</a>

Separate each item by an element.
Separate each item by an item.

```js
> intersperse ("b") (["a", "c"])
> Sl.intersperse ("b") (["a", "c"])
["a", "b", "c"]

> intersperse ("b") (["a"])
> Sl.intersperse ("b") (["a"])
["a"]

> intersperse ("b") ([])
> Sl.intersperse ("b") ([])
[]
```

### Regex

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L133">`extractString :: Regex -> String -> Maybe String`</a>
#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L155">`extractString :: Regex -> String -> Maybe String`</a>

Get the first group match in a string

```js
> const extractStringExample = extractString (/hello ([a-z]*)!/);
> const extractStringExample = Sl.extractString (/hello ([a-z]*)!/);

> extractStringExample ('hello john!')
Just ("john")
Expand All @@ -122,28 +122,28 @@ Nothing
Nothing
```

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L163">`replace :: Regex -> String -> String -> String`</a>
#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L186">`replace :: Regex -> String -> String -> String`</a>

Replace a substring with a RegExp

```js
> replace (/bob/) ('john') ('hello bob')
> Sl.replace (/bob/) ('john') ('hello bob')
"hello john"

> replace (/a/gi) ('o') ('Aaaaahhhh')
> Sl.replace (/a/gi) ('o') ('Aaaaahhhh')
"ooooohhhh"
```

### Logic

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L185">`allPass :: Array (a -> Boolean) -> a -> Boolean`</a>
#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L206">`allPass :: Array (a -> Boolean) -> a -> Boolean`</a>

Return `true` if all predicates return true, else return `false`

```js
> const isEvenNumber = x => x%2 === 0;
> const isPositiveNumber = x => x > 0;
> const isPositiveEvenNumber = allPass ([isEvenNumber, isPositiveNumber]);
> const isPositiveEvenNumber = Sl.allPass ([isEvenNumber, isPositiveNumber]);

> isPositiveEvenNumber (0)
false
Expand All @@ -158,14 +158,14 @@ false
true
```

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L218">`anyPass :: Array (a -> Boolean) -> a -> Boolean`</a>
#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L240">`anyPass :: Array (a -> Boolean) -> a -> Boolean`</a>

Return `true` if one of predicates return true, else return `false`

```js
> const isSix = x => x === 6;
> const isNegative = x => x < 0;
> const isNegativeOrSix = anyPass ([isNegative, isSix]);
> const isNegativeOrSix = Sl.anyPass ([isNegative, isSix]);

> isNegativeOrSix (0)
false
Expand All @@ -180,13 +180,13 @@ true
true
```

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L251">`cond :: Array Pair (a -> Boolean) (a -> b) -> a -> Either a b`</a>
#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L273">`cond :: Array Pair (a -> Boolean) (a -> b) -> a -> Either a b`</a>

Apply transformer predicate return true anc return a Right value
If any predicate return `true`, it will return initial value in Left Value

```js
> const condExample = cond ([
> const condExample = Sl.cond ([
. S.Pair (S.test (/^[a-zA-Z]+$/)) (S.toUpper),
. S.Pair (S.test (/[a-zA-Z]+/)) (S.toLower),
. ]);
Expand All @@ -205,87 +205,87 @@ Left ("123!")

Use [implementation created by David Chambers](https://gist.github.com/davidchambers/45aa0187a32fbac6912d4b3b4e8530c5)

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L295">`view :: Lens s a -> s -> a`</a>
#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L317">`view :: Lens s a -> s -> a`</a>

Allow to get a value by a Lens

```js
> const email = lens (user => user.email) (email => user => ({...user, email}));
> const email = Sl.lens (user => user.email) (email => user => ({...user, email}));
> const user = {id: 1, email: 'dc@davidchambers.me'};

> view (email) (user)
> Sl.view (email) (user)
dc@davidchambers.me
```

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L307">`over :: Lens s a -> (a -> a) -> s -> s`</a>
#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L329">`over :: Lens s a -> (a -> a) -> s -> s`</a>

Allow to set a value by a Lens

```js
> const email = lens (user => user.email) (email => user => ({...user, email}));
> const email = Sl.lens (user => user.email) (email => user => ({...user, email}));
> const user = {id: 1, email: 'dc@davidchambers.me'};

> over (email) (S.toUpper) (user)
> Sl.over (email) (S.toUpper) (user)
{id: 1, email: 'DC@DAVIDCHAMBERS.ME'}
```

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L320">`lensProp :: String -> Lens s a`</a>
#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L342">`lensProp :: String -> Lens s a`</a>

Create a Lens for an object property

```js
> const user = {id: 1, email: 'dc@davidchambers.me'};

> view (lensProp('email')) (user)
> Sl.view (Sl.lensProp('email')) (user)
'dc@davidchambers.me'

> over (lensProp('email')) (S.toUpper) (user)
> Sl.over (Sl.lensProp('email')) (S.toUpper) (user)
{id: 1, email: 'DC@DAVIDCHAMBERS.ME'}
```

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L348">`lensProps :: Array String -> Lens s a`</a>
#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L370">`lensProps :: Array String -> Lens s a`</a>

Create a Lens for an object property path

```js
> const example = {a: {b: {c: 1}}};

> view (lensProps (['a', 'b', 'c']))
. (example)
> Sl.view (Sl.lensProps (['a', 'b', 'c']))
. (example)
1

> over (lensProps (['a', 'b', 'c']))
. (S.add (1))
. (example)
> Sl.over (Sl.lensProps (['a', 'b', 'c']))
. (S.add (1))
. (example)
{a: {b: {c: 2}}}
```

### Maybe

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L371">`toMaybe :: (a -> Boolean) -> a -> Maybe a`</a>
#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L393">`toMaybe :: (a -> Boolean) -> a -> Maybe a`</a>

Wrapping value in Maybe depending on predicate

```js
> toMaybe (x => !!x) (null)
> Sl.toMaybe (x => !!x) (null)
Nothing

> toMaybe (x => !!x) (undefined)
> Sl.toMaybe (x => !!x) (undefined)
Nothing

> toMaybe (x => !!x) (1)
> Sl.toMaybe (x => !!x) (1)
Just (1)
```

### Either

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L396">`toEither :: (a -> Boolean) -> (a -> b) -> a -> Either b a`</a>
#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L418">`toEither :: (a -> Boolean) -> (a -> b) -> a -> Either b a`</a>

Convert to Either depending on predicate

```js
> const toEven = toEither (x => x % 2 === 0)
. (x => `${x} is not a even number`);
> const toEven = Sl.toEither (x => x % 2 === 0)
. (x => `${x} is not a even number`);

> toEven (1)
Left ("1 is not a even number")
Expand All @@ -296,14 +296,14 @@ Right (2)

### Fluture

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L421">`flMap :: PositiveNumber -> (a -> Fluture b c) -> Array a -> Fluture b Array c`</a>
#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L442">`flMap :: PositiveInteger -> (a -> Fluture b c) -> Array a -> Fluture b Array c`</a>

Apply a function that return a Fluture on each item of an array and return a Fluture

```js
> const array = [1, 2, 3, 4, 5];
> const f1 = flMap (1) (x => resolve (1 + x)) (array);
> const f2 = flMap (1) (x => reject ("error: " + x)) (array);
> const f1 = Sl.flMap (1) (x => resolve (1 + x)) (array);
> const f2 = Sl.flMap (1) (x => reject ("error: " + x)) (array);

> fork (log ('rejection')) (log ('resolution')) (f1)
[resolution]: [2, 3, 4, 5, 6]
Expand All @@ -312,13 +312,13 @@ Apply a function that return a Fluture on each item of an array and return a Flu
[rejection]: "error: 1"
```

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L445">`toFluture :: (a -> Boolean) -> (a -> b) -> a -> Fluture b a`</a>
#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L466">`toFluture :: (a -> Boolean) -> (a -> b) -> a -> Fluture b a`</a>

Convert to a Fluture depending on predicate

```js
> const toOdd = toFluture (x => x % 2 !== 0)
. (x => `${x} is not a odd number`);
> const toOdd = Sl.toFluture (x => x % 2 !== 0)
. (x => `${x} is not a odd number`);

> fork (log ('rejection')) (log ('resolution')) (toOdd (2))
[rejection]: "2 is not a odd number"
Expand All @@ -327,13 +327,13 @@ Convert to a Fluture depending on predicate
[resolution]: 1
```

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L466">`maybeToFluture :: b -> Maybe a -> Fluture b a`</a>
#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L486">`maybeToFluture :: b -> Maybe a -> Fluture b a`</a>

Convert a Maybe to a Fluture

```js
> const f1 = maybeToFluture ("not a number") (S.Just (1));
> const f2 = maybeToFluture ("not a number") (S.Nothing);
> const f1 = Sl.maybeToFluture ("not a number") (S.Just (1));
> const f2 = Sl.maybeToFluture ("not a number") (S.Nothing);

> fork (log ('rejection')) (log ('resolution')) (f1)
[resolution]: 1
Expand All @@ -342,13 +342,13 @@ Convert a Maybe to a Fluture
[rejection]: "not a number"
```

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L489">`eitherToFluture :: Either a b -> Fluture a b`</a>
#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.js#L509">`eitherToFluture :: Either a b -> Fluture a b`</a>

Convert an Either to a Fluture

```js
> const f1 = eitherToFluture (S.Right (1));
> const f2 = eitherToFluture (S.Left ("error"));
> const f1 = Sl.eitherToFluture (S.Right (1));
> const f2 = Sl.eitherToFluture (S.Left ("error"));

> fork (log ('rejection')) (log ('resolution')) (f1)
[resolution]: 1
Expand Down
Loading

0 comments on commit 39aea24

Please sign in to comment.