@ts-graphviz/common 0.0.0-pr956-20240225073457

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/common.ts ADDED
@@ -0,0 +1,5 @@
1
+ export * from './functional.js';
2
+ export * from './types.js';
3
+ export * from './attribute.js';
4
+ export * from './models-context.js';
5
+ export * from './models.js';
@@ -0,0 +1,24 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { filter, map, pipe } from './functional.js';
3
+
4
+ describe('pipe', () => {
5
+ const double = (n: number): number => n * 2;
6
+ const addFive = (n: number): number => n + 5;
7
+ const substractOne = (n: number): number => n - 1;
8
+
9
+ it('should pipe functions', () => {
10
+ expect(pipe(double, addFive, substractOne)(2)).toEqual(8);
11
+ });
12
+ });
13
+
14
+ describe('map', () => {
15
+ it('should map a list of values', () => {
16
+ expect(map((n: number) => n * 2)([1, 2, 3])).toEqual([2, 4, 6]);
17
+ });
18
+ });
19
+
20
+ describe('filter', () => {
21
+ it('should filter a list of values', () => {
22
+ expect(filter((n: number) => n % 2 === 0)([1, 2, 3])).toEqual([2]);
23
+ });
24
+ });
@@ -0,0 +1,74 @@
1
+ export type F<A extends any[], O> = (...args: A) => O;
2
+
3
+ export type IO<I, O> = F<[I], O>;
4
+
5
+ function _pipe<T extends any[], R>(
6
+ ...args: [o1: F<T, any>, ...operations: IO<any, any>[]]
7
+ ): F<T, R> {
8
+ const [o1, ...operations] = args;
9
+ return (...t: T) => operations.reduce((acc, f) => f(acc), o1(...t));
10
+ }
11
+
12
+ export function pipe<I extends any[], O>(f0: F<I, O>): F<I, O>;
13
+ export function pipe<I extends any[], T1, O>(
14
+ f0: F<I, T1>,
15
+ f1: IO<T1, O>,
16
+ ): F<I, O>;
17
+ export function pipe<I extends any[], T1, T2, O>(
18
+ f0: F<I, T1>,
19
+ f1: IO<T1, T2>,
20
+ f2: IO<T2, O>,
21
+ ): F<I, O>;
22
+ export function pipe<I extends any[], T1, T2, T3, O>(
23
+ f0: F<I, T1>,
24
+ f1: IO<T1, T2>,
25
+ f2: IO<T2, T3>,
26
+ f3: IO<T3, O>,
27
+ ): F<I, O>;
28
+ export function pipe<I extends any[], T1, T2, T3, T4, O>(
29
+ f0: F<I, T1>,
30
+ f1: IO<T1, T2>,
31
+ f2: IO<T2, T3>,
32
+ f3: IO<T3, T4>,
33
+ f4: IO<T4, O>,
34
+ ): F<I, O>;
35
+ export function pipe<I extends any[], O>(
36
+ o1: F<I, any>,
37
+ ...operations: IO<any, any>[]
38
+ ): F<I, O> {
39
+ return _pipe(o1, ...operations);
40
+ }
41
+
42
+ const defer =
43
+ <P, A extends any[], R>(fn: (src: P, ...args: A) => R) =>
44
+ (...args: A) =>
45
+ (src: P): R =>
46
+ fn(src, ...args);
47
+
48
+ const toIterable = <T, TF extends () => IterableIterator<T>>(f: TF) => ({
49
+ [Symbol.iterator]: f,
50
+ });
51
+
52
+ export const map = defer(
53
+ <T, O>(src: Iterable<T>, selector: (item: T) => O): O[] =>
54
+ Array.from(
55
+ toIterable(function* () {
56
+ for (const v of src) {
57
+ yield selector(v);
58
+ }
59
+ }),
60
+ ),
61
+ );
62
+
63
+ export const filter = defer(
64
+ <T>(src: Iterable<T>, pred: (item: T) => boolean): T[] =>
65
+ Array.from(
66
+ toIterable(function* () {
67
+ for (const x of src) {
68
+ if (pred(x)) {
69
+ yield x;
70
+ }
71
+ }
72
+ }),
73
+ ),
74
+ );
@@ -0,0 +1,41 @@
1
+ import {
2
+ EdgeConstructor,
3
+ NodeConstructor,
4
+ RootGraphConstructor,
5
+ SubgraphConstructor,
6
+ } from './models.js';
7
+
8
+ /**
9
+ * @group Models Context
10
+ */
11
+ export interface ModelsContext {
12
+ Graph: RootGraphConstructor;
13
+ Digraph: RootGraphConstructor;
14
+ Subgraph: SubgraphConstructor;
15
+ Node: NodeConstructor;
16
+ Edge: EdgeConstructor;
17
+ }
18
+
19
+ /**
20
+ * @group Models Context
21
+ */
22
+ export const RootModelsContext: ModelsContext = Object.seal({
23
+ // NOTE: RootModelsContext is also initialized after the model class is declared in the 'core/index.js' module.
24
+ Graph: null,
25
+ Digraph: null,
26
+ Subgraph: null,
27
+ Node: null,
28
+ Edge: null,
29
+ } as unknown as ModelsContext);
30
+
31
+ /**
32
+ * @group Models Context
33
+ */
34
+ export function createModelsContext(
35
+ models: Partial<ModelsContext>,
36
+ ): ModelsContext {
37
+ return Object.assign(
38
+ Object.seal(Object.assign({}, RootModelsContext)),
39
+ models,
40
+ );
41
+ }