Skip to content

Commit

Permalink
πŸ—οΈ update memoize to work with several arguments.
Browse files Browse the repository at this point in the history
πŸ—οΈ make `calculate` use `memoize`.
πŸ“ update docs.
  • Loading branch information
loucyx committed Oct 22, 2024
1 parent 7b94fa1 commit e72a10d
Show file tree
Hide file tree
Showing 37 changed files with 367 additions and 79 deletions.
121 changes: 112 additions & 9 deletions @coven/compare/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,131 @@

βš–οΈ Minimalist diffing.

This library takes 2 values (a `left` and a `right`) and returns an iterator
with all the differences between said values. The differences are represented by
This library takes 2 values in a curried fashion, a `left` or "original" value
first and then a `right` or "new" value, finally returning an iterator with all
the differences between said values. The yielded differences are represented by
3 kinds:

- **Create:** Missing `left` and existing `right`.
- **Update:** Different `left` and `right` values.
- **Delete:** Existing `left` and missing `right`.

As all [Coven Engineering](https://coven.engineering) libraries, it has 100%
test coverage and it's built in top of modern tech compatible with all
JavaScript runtimes.
## Plain update

If we compare the string 2 different strings, we do it like this:

```typescript
import { compare } from "@coven/compare";

compare("Coven")("Engineering");
```

And we only get a single yielded "update", that looks like this:

```typescript
({
kind: "UPDATE",
left: "Coven",
path: [], // A generator that will yield nothing because is the root value
right: "Engineering",
});
```

## Object update

If we compare two objects like this:

```typescript
import { compare } from "@coven/compare";

compare({ example: 13 })({ example: 42 });
```

The yielded update will have values yielded from its `path`:

```typescript
({
kind: "UPDATE",
left: 13,
right: 42,
path: ["example"],
});
```

## Object delete

Let's say we update that same object to an empty one:

```typescript
import { compare } from "@coven/compare";

compare({ example: 13 })({});
```

Yields this:

```typescript
({
kind: "DELETE",
left: 13,
path: ["example"],
});
```

## Example
## Object create

Let's say we update that same object from an empty one:

```typescript
import { compare } from "@coven/compare";

compare("Coven")("Engineering"); // Yields { kind: "UPDATE", left: "Coven", right: "Engineering" }
compare({ example: 13 })({ example: 42 }); // Yields { kind: "UPDATE", left: "13", right: "42", path: ["example"] }
compare({ example: 13 })({}); // Yields { kind: "DELETE", left: "13", path: ["example"] }
compare({})({ example: 42 });
```

Yields this:

```typescript
({
kind: "CREATE",
right: 42,
path: ["example"],
});
```

## Multiple differences

Now let's see what's yielded if we compare two objects with the 3 type of
differences:

```typescript
import { compare } from "@coven/compare";

compare({
original: "old",
updated: "old",
})({
created: "new",
updated: "new",
});
```

That yields all 3 kind of differences:

```typescript
({ kind: "DELETE", left: "old", path: ["original"] });
({ kind: "UPDATE", left: "old", right: "new", path: ["updated"] });
({ kind: "CREATE", right: "new", path: ["created"] });
```

> [!IMPORTANT]
> In all examples the `path` property is represented as an array, but remember
> `path` is an `IterableIterator` like the output of `compare` is. The included
> `flat` function can be used to flatten all `IterableIterator`s to arrays.
Like all [Coven Engineering](https://coven.engineering) libraries, it has 100%
test coverage and it's built in top of modern tech compatible with all
JavaScript runtimes.

## Other links

- [Coverage](https://coveralls.io/github/covenengineering/libraries).
2 changes: 1 addition & 1 deletion @coven/compare/deno.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@coven/compare",
"version": "0.3.0",
"version": "0.3.2",
"exports": "./mod.ts"
}
10 changes: 10 additions & 0 deletions @coven/constants/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@

🧱 Common constants.

Is pretty common to start folding actions with an empty object, array or string.
This library simply contains those common values as immutable structures to
avoid mutations on them and make DX a little bit better (is way easier to spot
an `EMPTY_OBJECT` that it is to spot a `{}`).

Like all [Coven Engineering](https://coven.engineering) libraries, it has 100%
test coverage and it's built in top of modern tech compatible with all
JavaScript runtimes. The tests for this library only make sure trying to do
mutations throws.

## Constants

- `EMPTY_ARRAY`: Empty read-only array.
Expand Down
2 changes: 1 addition & 1 deletion @coven/constants/deno.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@coven/constants",
"version": "0.3.0",
"version": "0.3.2",
"exports": "./mod.ts"
}
75 changes: 58 additions & 17 deletions @coven/cron/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,76 @@ It also includes a `nextDate` util that given a `Date` and a valid cron
expression, will return the next matching date. It does validations beforehand
so no "Invalid Date" errors are returned.

As all [Coven Engineering](https://coven.engineering) libraries, it has 100%
Like all [Coven Engineering](https://coven.engineering) libraries, it has 100%
test coverage and it's built in top of modern tech compatible with all
JavaScript runtimes.

Only known limitation is it only accepts valid standard unix cron expressions,
so cron quartz is not supported.

## Example
## Parsing

To parse we use the `parse` util:

```typescript
import { parse, stringify } from "@coven/cron";
import { parse } from "@coven/cron";

const cron = parse("1-2,3,4 * 2 8,9 1");
/*
{
minute: [{ from: 1, to: 2 }, 3, 4],
hour: "*",
dayOfMonth: 2,
month: [8, 9],
dayOfWeek: 1
}
*/

stringify(cron); // "1-2,3,4 * 2 8,9 1"

// Also works with partials:
```

Which returns:

```typescript
({
minute: [{ from: 1, to: 2 }, 3, 4],
hour: "*",
dayOfMonth: 2,
month: [8, 9],
dayOfWeek: 1,
});
```

## Stringifying

To stringify we use the `stringify` util:

```typescript
import { stringify } from "@coven/cron";

const cron = stringify({
minute: [{ from: 1, to: 2 }, 3, 4],
hour: "*",
dayOfMonth: 2,
month: [8, 9],
dayOfWeek: 1,
});
```

Which returns:

```typescript
"1-2,3,4 * 2 8,9 1";
```

We can also pass a partial of that "cron object":

```typescript
import { stringify } from "@coven/cron";

stringify({ hour: 13 }); // "* 13 * * *"
```

Which returns:

```typescript
"* 13 * * *";
```

And when an invalid date is specified, `undefined` is returned:

```typescript
import { parse } from "@coven/cron";

// Only parses with valid dates:
parse("* * 31 2 *"); // undefined because 31 of February is invalid
```

Expand Down
2 changes: 1 addition & 1 deletion @coven/cron/deno.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@coven/cron",
"version": "0.3.0",
"version": "0.3.2",
"exports": "./mod.ts"
}
6 changes: 3 additions & 3 deletions @coven/cron/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const buildIU = build("iu");
/**
* Parses a cron expression into an object representation.
*
* > [!IMPORTANT]
* > `parse` doesn't work with non-standard cron expressions such as cron quartz.
*
* @example
* ```typescript
* parse("* * * * *"); // { minute: "*", hour: "*", dayOfMonth: "*", month: "*", dayOfWeek: "*" }
Expand All @@ -21,9 +24,6 @@ const buildIU = build("iu");
* parse("* * 31 2 *"); // undefined
* parse("nope nope nope nope nope"); // undefined
* ```
* @see {@linkcode parseFieldTuplesMap}
* @see {@linkcode normalizeAliases}
*
* @param expression Cron expression to be parsed.
* @returns Object representing that expression or `undefined` if expression is
* invalid.
Expand Down
6 changes: 3 additions & 3 deletions @coven/expression/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ functions and constants strongly typed, so the type is already a hardcoded
string that reflects the regular expression being built, while giving an API
that's easier to read and maintain.

As all [Coven Engineering](https://coven.engineering) libraries, it has 100%
Like all [Coven Engineering](https://coven.engineering) libraries, it has 100%
test coverage and it's built in top of modern tech compatible with all
JavaScript runtimes.

## Example

```typescript
import { build, group, or } from "@coven/expression";
import { buildUnicode, group, or } from "@coven/expression";

build("gu")(group(or("this", "that"))); // /(?:this|that)/gu
buildUnicode(group(or("this", "that"))); // /(?:this|that)/u
```

## Other links
Expand Down
2 changes: 1 addition & 1 deletion @coven/expression/deno.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@coven/expression",
"version": "0.3.0",
"version": "0.3.2",
"exports": "./mod.ts"
}
4 changes: 2 additions & 2 deletions @coven/iterables/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![JSR](https://jsr.io/badges/@coven/iterables)](https://jsr.io/@coven/iterables)
[![JSR Score](https://jsr.io/badges/@coven/iterables/score)](https://jsr.io/@coven/iterables/score)

♻️ Iterable and AsyncIterable utilities.
♻️ `Iterable` and `AsyncIterable` utilities.

One of the places curried functions shine the most is with iterables. More often
than not, mapping, filtering and so on is applied to multiple different
Expand Down Expand Up @@ -49,7 +49,7 @@ forEach(console.log)(map(next)(map(double)([1, 2, 3, 4])));
// 4. 8 β†’ 9 β†’ Logs 9
```

As all [Coven Engineering](https://coven.engineering) libraries, it has 100%
Like all [Coven Engineering](https://coven.engineering) libraries, it has 100%
test coverage and it's built in top of modern tech compatible with all
JavaScript runtimes.

Expand Down
2 changes: 1 addition & 1 deletion @coven/iterables/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@coven/iterables",
"version": "0.3.1",
"version": "0.3.2",
"exports": {
".": "./mod.ts",
"./async": "./async/mod.ts"
Expand Down
7 changes: 6 additions & 1 deletion @coven/math/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ representation of the base and exponent of a number using `bigint`. We then do
all math operations over base and exponent in those tuples and turn them back to
`number` when we are done.

As all [Coven Engineering](https://coven.engineering) libraries, it has 100%
Like all [Coven Engineering](https://coven.engineering) libraries, it has 100%
test coverage and it's built in top of modern tech compatible with all
JavaScript runtimes.

> [!IMPORTANT]
> Obviously, using this or any library like it is way more expensive than doing
> a simple math operation, so use this only when a precise value is required. If
> speed is more important than precision, avoid this or any library like it.
## Example

```typescript
Expand Down
8 changes: 5 additions & 3 deletions @coven/math/calculate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { always } from "@coven/utils";
import { always, memoize } from "@coven/utils";
import { add } from "./add.ts";
import type { Calculation } from "./Calculation.ts";
import { divide } from "./divide.ts";
Expand All @@ -20,7 +20,9 @@ import { subtract } from "./subtract.ts";
* @param value Value to run operations on.
* @returns An object with `divideBy`, `minus`, `plus` and `times` methods and a `value` property.
*/
export const calculate = (value: number): Calculation => ({
export const calculate: (value: number) => Calculation = memoize((
value: number,
): Calculation => ({
/**
* Divide previous `value` in calculation by the given `divisor`.
*
Expand Down Expand Up @@ -57,4 +59,4 @@ export const calculate = (value: number): Calculation => ({
* Current value of the calculation.
*/
valueOf: always(value),
});
}));
2 changes: 1 addition & 1 deletion @coven/math/deno.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@coven/math",
"version": "0.3.0",
"version": "0.3.2",
"exports": "./mod.ts"
}
Loading

0 comments on commit e72a10d

Please sign in to comment.