Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion @coven/compare/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"$schema": "https://raw.githubusercontent.com/denoland/deno/main/cli/schemas/config-file.v1.json",
"exports": "./mod.ts",
"name": "@coven/compare",
"version": "0.9.3"
"version": "0.9.4"
}
2 changes: 1 addition & 1 deletion @coven/constants/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"$schema": "https://raw.githubusercontent.com/denoland/deno/main/cli/schemas/config-file.v1.json",
"exports": "./mod.ts",
"name": "@coven/constants",
"version": "0.9.3"
"version": "0.9.4"
}
2 changes: 1 addition & 1 deletion @coven/cron/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"$schema": "https://raw.githubusercontent.com/denoland/deno/main/cli/schemas/config-file.v1.json",
"exports": "./mod.ts",
"name": "@coven/cron",
"version": "0.9.3"
"version": "0.9.4"
}
8 changes: 4 additions & 4 deletions @coven/cron/parse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { EMPTY_OBJECT } from "@coven/constants";
import { build } from "@coven/expression";
import { build, getGroups } from "@coven/expression";
import { entriesToObject, length, objectToEntries } from "@coven/iterables";
import { memoFunction } from "@coven/memo";
import type { KeyOf, Maybe, ReadonlyRecord } from "@coven/types";
Expand Down Expand Up @@ -33,8 +32,9 @@ export const parse: (expression: CronString) => Maybe<CronObject> =
memoFunction((expression: CronString) => {
const entries = parseFieldTuplesMap(
objectToEntries(
(buildIU(cronRegExp).exec(normalizeAliases(expression))?.groups
?? EMPTY_OBJECT) as ReadonlyRecord<
getGroups<ReadonlyArray<KeyOf<CronObject>>>(
buildIU(cronRegExp),
)(normalizeAliases(expression)) as ReadonlyRecord<
KeyOf<CronObject>,
string
>,
Expand Down
2 changes: 1 addition & 1 deletion @coven/expression/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"$schema": "https://raw.githubusercontent.com/denoland/deno/main/cli/schemas/config-file.v1.json",
"exports": "./mod.ts",
"name": "@coven/expression",
"version": "0.9.3"
"version": "0.9.4"
}
55 changes: 55 additions & 0 deletions @coven/expression/getGroups.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { EMPTY_OBJECT } from "@coven/constants";
import { memo } from "@coven/memo";
import type {
ExpectedStringables,
ReadonlyRecord,
RegularExpressionFlags,
Stringable,
} from "@coven/types";

/**
* @internal Small type to work with {@linkcode ExpectedStringables} to add
* the expected parts of a group name to the given strings.
*/
export type WrapGroupKeys<GroupKeys extends ReadonlyArray<Stringable>> =
GroupKeys extends (
readonly [
infer Head extends Stringable,
...infer Tail extends ReadonlyArray<Stringable>,
]
) ?
readonly [`(?<${Head}>`, ...WrapGroupKeys<Tail>]
: ReadonlyArray<never>;

/**
* Given a regular expression (ideally generated by `@coven/expression`'s `build`
* or `buildUnicode`), extract only the groups on it.
*
* @example Escape given value
* ```typescript
* import { buildUnicode, captureNamed, WILDCARD } from "@coven/expression";
*
* const getExample = getGroups<readonly ["example"]>(
* buildUnicode(captureNamed("example")(WILDCARD)) // /(?<example>.)/
* );
*
* getExample("🔮"); // { example: "🔮" }
* ```
* @param escaped Value to escape.
* @returns Escaped value.
*/
export const getGroups =
<GroupKeys extends ReadonlyArray<Stringable>>({
flags,
source,
}: {
flags: RegularExpressionFlags;
source: ExpectedStringables<WrapGroupKeys<GroupKeys>>;
}): ((
stringable: Stringable,
) => Partial<ReadonlyRecord<`${GroupKeys[number]}`, string>>) =>
(stringable) =>
memo(
new RegExp(source, flags).exec(`${stringable}`)?.groups
?? EMPTY_OBJECT,
) as ReadonlyRecord<`${GroupKeys[number]}`, string>;
1 change: 1 addition & 0 deletions @coven/expression/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export { controlCharacter } from "./controlCharacter.ts";
export { disjunction } from "./disjunction.ts";
export { escape } from "./escape.ts";
export { exists } from "./exists.ts";
export { getGroups } from "./getGroups.ts";
export { group } from "./group.ts";
export { hexadecimal } from "./hexadecimal.ts";
export type { HexadecimalDigit } from "./HexadecimalDigit.ts";
Expand Down
25 changes: 25 additions & 0 deletions @coven/expression/tests/getGroups.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { EMPTY_OBJECT } from "@coven/constants";
import { memo } from "@coven/memo";
import { assertStrictEquals } from "@std/assert";
import { buildUnicode } from "../buildUnicode.ts";
import { captureNamed } from "../captureNamed.ts";
import { getGroups } from "../getGroups.ts";
import { WILDCARD } from "../WILDCARD.ts";

Deno.test("Groups captured correctly", () =>
assertStrictEquals(
getGroups<readonly ["example"]>(
buildUnicode(captureNamed("example")(WILDCARD)),
)("🔮"),
memo({ example: "🔮" }),
),
);

Deno.test("When not found, return empty object", () =>
assertStrictEquals(
getGroups<readonly ["example"]>(
buildUnicode(captureNamed("example")(WILDCARD)),
)(""),
EMPTY_OBJECT,
),
);
2 changes: 1 addition & 1 deletion @coven/iterables/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"./async": "./async/mod.ts"
},
"name": "@coven/iterables",
"version": "0.9.3"
"version": "0.9.4"
}
3 changes: 2 additions & 1 deletion @coven/iterables/repeat.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Numeric } from "@coven/types";
import { iteratorFunctionToIterableIterator } from "./iteratorFunctionToIterableIterator.ts";

/**
Expand All @@ -13,7 +14,7 @@ import { iteratorFunctionToIterableIterator } from "./iteratorFunctionToIterable
* @returns Curried function with `item` in context.
*/
export const repeat =
(times: number): (<const Item>(item: Item) => IterableIterator<Item>) =>
(times: Numeric): (<const Item>(item: Item) => IterableIterator<Item>) =>
<const Item>(item: Item) =>
iteratorFunctionToIterableIterator(function* (): Generator<Item> {
if (times === Infinity) {
Expand Down
11 changes: 11 additions & 0 deletions @coven/iterables/tests/repeat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { iterableToArray } from "../iterableToArray.ts";
import { repeat } from "../repeat.ts";

const repeat3Times = repeat(3);
const repeat3nTimes = repeat(3n);

Deno.test(
'a call to repeat with the string "test" and the number 3 returns an array with 3 strings "test" on it',
Expand All @@ -13,3 +14,13 @@ Deno.test(
"test",
]),
);

Deno.test(
'a call to repeat with the string "test" and the bigint 3n returns an array with 3 strings "test" on it',
() =>
assertEquals(iterableToArray(repeat3nTimes("test")), [
"test",
"test",
"test",
]),
);
13 changes: 10 additions & 3 deletions @coven/math/Calculation.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { Precise } from "./precise.ts";

/**
* Object returned by the `calculate` function, which recursively returns itself
* from all its methods. To get the value the dev has to run `toValue()`.
*/
export type Calculation<Value extends number = number> = Readonly<{
export type Calculation = Readonly<{
/**
* Divide previous `value` in calculation by the given `divisor`.
*
Expand All @@ -27,6 +29,11 @@ export type Calculation<Value extends number = number> = Readonly<{
*/
plus: (addend: number) => Calculation;

/**
* Current {@linkcode Precise} value (can be `NaN` or `Infinity`).
*/
precise: Precise | number;

/**
* Multiplies previous `value` in calculation times the given `multiplier`.
*
Expand All @@ -36,7 +43,7 @@ export type Calculation<Value extends number = number> = Readonly<{
times: (multiplier: number) => Calculation;

/**
* Current value of the calculation.
* Current `number` value.
*/
total: Value;
total: number;
}>;
4 changes: 0 additions & 4 deletions @coven/math/MaybeInfinity.ts

This file was deleted.

10 changes: 10 additions & 0 deletions @coven/math/PreciseFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Multary } from "@coven/types";
import type { PreciseToTypeFunction } from "./PreciseToTypeFunction.ts";
import type { Precise } from "./precise.ts";

/**
* Type to represent the curried functions for operations with
* {@linkcode Precise} values.
*/
export type PreciseFunction<Output extends Precise | number = Precise> =
Multary<Precise, PreciseToTypeFunction<Output>>;
7 changes: 7 additions & 0 deletions @coven/math/PreciseToTypeFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Multary } from "@coven/types";
import type { Precise } from "./precise.ts";

/**
* Type used by functions that turn a {@linkcode Precise} into a different type.
*/
export type PreciseToTypeFunction<Output> = Multary<Precise, Output>;
7 changes: 0 additions & 7 deletions @coven/math/PreciseTuple.ts

This file was deleted.

1 change: 1 addition & 0 deletions @coven/math/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ JavaScript runtimes.
import { calculate } from "@coven/math";

calculate(0.1).plus(0.2).total; // 0.3 🤯
calculate(1e20).plus(0.1).minus(1e20).total; // 0.1 🤯
```

## Other links
Expand Down
9 changes: 4 additions & 5 deletions @coven/math/add.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { pipe } from "./pipe.ts";
import { fallbackAdd } from "./fallbackAdd.ts";
import { numberFunction, type NumberFunction } from "./numberFunction.ts";
import { preciseAdd } from "./preciseAdd.ts";

/**
* Curried add operation using {@linkcode pipe} with {@linkcode preciseAdd}.
* Curried add operation using {@linkcode preciseAdd}.
*
* @example
* ```typescript
Expand All @@ -11,10 +12,8 @@ import { preciseAdd } from "./preciseAdd.ts";
* addDot2(0.1); // 0.3
* ```
* @see {@linkcode preciseAdd}
* @see {@linkcode pipe}
*
* @param augend Augend value to be on the right side.
* @returns Curried function with `augend` in context.
*/
export const add: (augend: number) => (addend: number) => number =
pipe(preciseAdd);
export const add: NumberFunction = numberFunction(preciseAdd, fallbackAdd);
14 changes: 0 additions & 14 deletions @coven/math/bigIntMin.ts

This file was deleted.

Loading