silentium-components 0.0.26 → 0.0.28

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.
Files changed (82) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/dist/silentium-components.cjs +570 -342
  3. package/dist/silentium-components.cjs.map +1 -1
  4. package/dist/silentium-components.d.ts +169 -30
  5. package/dist/silentium-components.js +546 -319
  6. package/dist/silentium-components.js.map +1 -1
  7. package/dist/silentium-components.min.js +1 -1
  8. package/dist/silentium-components.min.mjs +1 -1
  9. package/dist/silentium-components.min.mjs.map +1 -1
  10. package/dist/silentium-components.mjs +546 -319
  11. package/dist/silentium-components.mjs.map +1 -1
  12. package/package.json +2 -2
  13. package/src/behaviors/Branch._main.test.ts +22 -0
  14. package/src/behaviors/Branch.branchesDontAffectResult.test.ts +11 -11
  15. package/src/behaviors/Branch.dontRespondAfterRespond.test.ts +15 -17
  16. package/src/behaviors/Branch.ts +28 -21
  17. package/src/behaviors/{Deadline.test.ts → Deadline._main.test.ts} +5 -5
  18. package/src/behaviors/Deadline._value.test.ts +6 -6
  19. package/src/behaviors/Deadline.ts +45 -41
  20. package/src/behaviors/Deferred.test.ts +14 -14
  21. package/src/behaviors/Deferred.ts +21 -15
  22. package/src/behaviors/Dirty.test.ts +8 -8
  23. package/src/behaviors/Dirty.ts +43 -35
  24. package/src/behaviors/Loading.test.ts +8 -8
  25. package/src/behaviors/Loading.ts +24 -14
  26. package/src/behaviors/Lock.test.ts +14 -14
  27. package/src/behaviors/Lock.ts +21 -14
  28. package/src/behaviors/Memo.test.ts +16 -14
  29. package/src/behaviors/Memo.ts +20 -12
  30. package/src/behaviors/OnlyChanged.test.ts +7 -7
  31. package/src/behaviors/OnlyChanged.ts +22 -15
  32. package/src/behaviors/Path._main.test.ts +14 -0
  33. package/src/behaviors/Path.index.test.ts +4 -4
  34. package/src/behaviors/Path.nested.test.ts +4 -4
  35. package/src/behaviors/Path.ts +27 -22
  36. package/src/behaviors/Shot._main.test.ts +29 -0
  37. package/src/behaviors/Shot._onlyChanged.test.ts +15 -13
  38. package/src/behaviors/Shot.ts +24 -15
  39. package/src/behaviors/Sync.test.ts +10 -0
  40. package/src/behaviors/Sync.ts +38 -15
  41. package/src/behaviors/Tick.test.ts +8 -8
  42. package/src/behaviors/Tick.ts +20 -12
  43. package/src/boolean/And.test.ts +12 -12
  44. package/src/boolean/And.ts +18 -14
  45. package/src/boolean/Bool.ts +11 -2
  46. package/src/boolean/Not.test.ts +6 -6
  47. package/src/boolean/Not.ts +15 -10
  48. package/src/boolean/Or.test.ts +12 -12
  49. package/src/boolean/Or.ts +18 -14
  50. package/src/boolean/index.ts +1 -0
  51. package/src/formats/FromJson.test.ts +11 -0
  52. package/src/formats/FromJson.ts +22 -15
  53. package/src/formats/ToJson.test.ts +11 -0
  54. package/src/formats/ToJson.ts +23 -16
  55. package/src/lists/First.test.ts +4 -4
  56. package/src/lists/First.ts +11 -6
  57. package/src/navigation/Router.test.ts +13 -13
  58. package/src/navigation/Router.ts +36 -29
  59. package/src/strings/Concatenated.test.ts +6 -6
  60. package/src/strings/Concatenated.ts +18 -14
  61. package/src/structures/HashTable.test.ts +7 -7
  62. package/src/structures/HashTable.ts +17 -11
  63. package/src/structures/Record._main.test.ts +28 -0
  64. package/src/structures/Record.concatenated.test.ts +17 -15
  65. package/src/structures/Record.nested.test.ts +10 -10
  66. package/src/structures/RecordOf.ts +29 -0
  67. package/src/structures/index.ts +1 -1
  68. package/src/system/RegexpMatch._group.test.ts +9 -6
  69. package/src/system/RegexpMatch._main.test.ts +16 -0
  70. package/src/system/RegexpMatch.ts +20 -17
  71. package/src/system/RegexpMatched.test.ts +6 -6
  72. package/src/system/RegexpMatched.ts +19 -16
  73. package/src/system/RegexpReplaced.test.ts +10 -6
  74. package/src/system/RegexpReplaced.ts +25 -18
  75. package/src/system/Set.test.ts +6 -6
  76. package/src/system/Set.ts +20 -17
  77. package/src/behaviors/Branch.test.ts +0 -22
  78. package/src/behaviors/Path.test.ts +0 -14
  79. package/src/behaviors/Shot.test.ts +0 -29
  80. package/src/structures/Record.test.ts +0 -28
  81. package/src/structures/Record.ts +0 -22
  82. package/src/system/RegexpMatch.test.ts +0 -13
@@ -1,19 +1,42 @@
1
- import { InformationType } from "silentium";
1
+ import { From, isFilled, TheInformation, TheOwner } from "silentium";
2
2
 
3
- export const sync = <T>(base: InformationType<T>) => {
4
- let value: T | undefined;
3
+ export class Sync<T> extends TheInformation<T> {
4
+ private theValue: T | undefined;
5
+ private isInit = false;
5
6
 
6
- base((v) => {
7
- value = v;
8
- });
7
+ public constructor(private baseSrc: TheInformation<T>) {
8
+ super(baseSrc);
9
+ }
9
10
 
10
- return {
11
- value() {
12
- if (value === undefined) {
13
- throw new Error("no value in sync");
14
- }
11
+ public value(o: TheOwner<T>): this {
12
+ this.baseSrc.value(o);
13
+ return this;
14
+ }
15
15
 
16
- return value;
17
- },
18
- };
19
- };
16
+ public valueExisted() {
17
+ this.initOwner();
18
+ return isFilled(this.theValue);
19
+ }
20
+
21
+ public valueSync(): T {
22
+ this.initOwner();
23
+
24
+ if (!isFilled(this.theValue)) {
25
+ throw new Error("no value in sync");
26
+ }
27
+
28
+ return this.theValue;
29
+ }
30
+
31
+ public initOwner() {
32
+ if (!this.isInit) {
33
+ this.isInit = true;
34
+ this.value(
35
+ new From((v) => {
36
+ this.theValue = v;
37
+ }),
38
+ );
39
+ }
40
+ return this;
41
+ }
42
+ }
@@ -1,6 +1,6 @@
1
- import { any, of, sharedStateless } from "silentium";
1
+ import { Any, From, Late, Shared } from "silentium";
2
2
  import { afterEach, beforeEach, expect, test, vi } from "vitest";
3
- import { tick } from "../behaviors/Tick";
3
+ import { Tick } from "../behaviors/Tick";
4
4
 
5
5
  beforeEach(() => {
6
6
  vi.useFakeTimers({ shouldAdvanceTime: true });
@@ -12,15 +12,15 @@ afterEach(() => {
12
12
  });
13
13
 
14
14
  test("Tick.test", async () => {
15
- const [s1, s1o] = of<number>(1);
16
- const [s2, s2o] = of<number>(2);
17
- const [tickSrc] = sharedStateless(tick(any(s1, s2)));
15
+ const s1 = new Late<number>(1);
16
+ const s2 = new Late<number>(2);
17
+ const tickSrc = new Shared(new Tick(new Any(s1, s2)), true);
18
18
 
19
19
  const g = vi.fn();
20
- tickSrc(g);
20
+ tickSrc.value(new From(g));
21
21
 
22
- s1o(3);
23
- s2o(4);
22
+ s1.owner().give(3);
23
+ s2.owner().give(4);
24
24
 
25
25
  await vi.advanceTimersByTimeAsync(10);
26
26
  vi.runAllTicks();
@@ -1,11 +1,15 @@
1
- import { InformationType } from "silentium";
1
+ import { From, TheInformation, TheOwner } from "silentium";
2
2
 
3
3
  /**
4
4
  * Accumulates the last value of the source and returns one result once per tick
5
5
  * https://silentium-lab.github.io/silentium-components/#/behaviors/tick
6
6
  */
7
- export const tick = <T>(baseSrc: InformationType<T>): InformationType<T> => {
8
- return (o) => {
7
+ export class Tick<T> extends TheInformation<T> {
8
+ public constructor(private baseSrc: TheInformation<T>) {
9
+ super(baseSrc);
10
+ }
11
+
12
+ public value(o: TheOwner<T>): this {
9
13
  let microtaskScheduled = false;
10
14
  let lastValue: T | null = null;
11
15
 
@@ -14,17 +18,21 @@ export const tick = <T>(baseSrc: InformationType<T>): InformationType<T> => {
14
18
  queueMicrotask(() => {
15
19
  microtaskScheduled = false;
16
20
  if (lastValue !== null) {
17
- o(lastValue);
21
+ o.give(lastValue);
18
22
  lastValue = null;
19
23
  }
20
24
  });
21
25
  };
22
26
 
23
- baseSrc((v) => {
24
- lastValue = v;
25
- if (!microtaskScheduled) {
26
- scheduleMicrotask();
27
- }
28
- });
29
- };
30
- };
27
+ this.baseSrc.value(
28
+ new From((v) => {
29
+ lastValue = v;
30
+ if (!microtaskScheduled) {
31
+ scheduleMicrotask();
32
+ }
33
+ }),
34
+ );
35
+
36
+ return this;
37
+ }
38
+ }
@@ -1,24 +1,24 @@
1
- import { of } from "silentium";
1
+ import { From, Late } from "silentium";
2
2
  import { expect, test, vi } from "vitest";
3
- import { and } from "../boolean/And";
3
+ import { And } from "../boolean/And";
4
4
 
5
5
  test("And.test", () => {
6
- const [one, oo] = of<boolean>(false);
7
- const [two, to] = of<boolean>(false);
8
- const result = and(one, two);
6
+ const one = new Late<boolean>(false);
7
+ const two = new Late<boolean>(false);
8
+ const result = new And(one, two);
9
9
  const g = vi.fn();
10
- result(g);
10
+ result.value(new From(g));
11
11
  expect(g).toHaveBeenLastCalledWith(false);
12
12
 
13
- oo(true);
14
- to(false);
13
+ one.owner().give(true);
14
+ two.owner().give(false);
15
15
  expect(g).toHaveBeenLastCalledWith(false);
16
16
 
17
- oo(false);
18
- to(true);
17
+ one.owner().give(false);
18
+ two.owner().give(true);
19
19
  expect(g).toHaveBeenLastCalledWith(false);
20
20
 
21
- oo(true);
22
- to(true);
21
+ one.owner().give(true);
22
+ two.owner().give(true);
23
23
  expect(g).toHaveBeenLastCalledWith(true);
24
24
  });
@@ -1,18 +1,22 @@
1
- import { all, InformationType } from "silentium";
1
+ import { All, From, TheInformation, TheOwner } from "silentium";
2
2
 
3
3
  /**
4
4
  * https://silentium-lab.github.io/silentium-components/#/boolean/and
5
5
  */
6
- export const and = (
7
- oneSrc: InformationType<boolean>,
8
- twoSrc: InformationType<boolean>,
9
- ): InformationType<boolean> => {
10
- return (o) => {
11
- all(
12
- oneSrc,
13
- twoSrc,
14
- )(([one, two]) => {
15
- o(one && two);
16
- });
17
- };
18
- };
6
+ export class And extends TheInformation<boolean> {
7
+ public constructor(
8
+ private oneSrc: TheInformation<boolean>,
9
+ private twoSrc: TheInformation<boolean>,
10
+ ) {
11
+ super(oneSrc, twoSrc);
12
+ }
13
+
14
+ public value(o: TheOwner<boolean>): this {
15
+ new All(this.oneSrc, this.twoSrc).value(
16
+ new From(([one, two]) => {
17
+ o.give(one && two);
18
+ }),
19
+ );
20
+ return this;
21
+ }
22
+ }
@@ -1,7 +1,16 @@
1
- import { applied, InformationType } from "silentium";
1
+ import { Applied, TheInformation, TheOwner } from "silentium";
2
2
 
3
3
  /**
4
4
  * Convert any source to boolean source
5
5
  * https://silentium-lab.github.io/silentium-components/#/boolean/bool
6
6
  */
7
- export const bool = (baseSrc: InformationType) => applied(baseSrc, Boolean);
7
+ export class Bool extends TheInformation<boolean> {
8
+ public constructor(private baseSrc: TheInformation) {
9
+ super(baseSrc);
10
+ }
11
+
12
+ public value(o: TheOwner<boolean>): this {
13
+ new Applied(this.baseSrc, Boolean).value(o);
14
+ return this;
15
+ }
16
+ }
@@ -1,14 +1,14 @@
1
- import { of } from "silentium";
1
+ import { From, Late } from "silentium";
2
2
  import { expect, test, vi } from "vitest";
3
- import { not } from "../boolean/Not";
3
+ import { Not } from "../boolean/Not";
4
4
 
5
5
  test("Not.test", () => {
6
- const [one, oo] = of<boolean>(false);
7
- const result = not(one);
6
+ const one = new Late<boolean>(false);
7
+ const result = new Not(one);
8
8
  const g = vi.fn();
9
- result(g);
9
+ result.value(new From(g));
10
10
  expect(g).toHaveBeenLastCalledWith(true);
11
11
 
12
- oo(true);
12
+ one.owner().give(true);
13
13
  expect(g).toHaveBeenLastCalledWith(false);
14
14
  });
@@ -1,14 +1,19 @@
1
- import { InformationType } from "silentium";
1
+ import { From, TheInformation, TheOwner } from "silentium";
2
2
 
3
3
  /**
4
4
  * https://silentium-lab.github.io/silentium-components/#/boolean/not
5
5
  */
6
- export const not = (
7
- baseSrc: InformationType<boolean>,
8
- ): InformationType<boolean> => {
9
- return (o) => {
10
- baseSrc((v) => {
11
- o(!v);
12
- });
13
- };
14
- };
6
+ export class Not extends TheInformation<boolean> {
7
+ public constructor(private baseSrc: TheInformation<boolean>) {
8
+ super(baseSrc);
9
+ }
10
+
11
+ public value(o: TheOwner<boolean>): this {
12
+ this.baseSrc.value(
13
+ new From((v) => {
14
+ o.give(!v);
15
+ }),
16
+ );
17
+ return this;
18
+ }
19
+ }
@@ -1,24 +1,24 @@
1
- import { of } from "silentium";
1
+ import { From, Late } from "silentium";
2
2
  import { expect, test, vi } from "vitest";
3
- import { or } from "../boolean/Or";
3
+ import { Or } from "../boolean/Or";
4
4
 
5
5
  test("Or.test", () => {
6
- const [one, oo] = of<boolean>(false);
7
- const [two, to] = of<boolean>(false);
8
- const result = or(one, two);
6
+ const one = new Late<boolean>(false);
7
+ const two = new Late<boolean>(false);
8
+ const result = new Or(one, two);
9
9
  const g = vi.fn();
10
- result(g);
10
+ result.value(new From(g));
11
11
  expect(g).toHaveBeenLastCalledWith(false);
12
12
 
13
- oo(true);
14
- to(false);
13
+ one.owner().give(true);
14
+ two.owner().give(false);
15
15
  expect(g).toHaveBeenLastCalledWith(true);
16
16
 
17
- oo(false);
18
- to(true);
17
+ one.owner().give(false);
18
+ two.owner().give(true);
19
19
  expect(g).toHaveBeenLastCalledWith(true);
20
20
 
21
- oo(true);
22
- to(true);
21
+ one.owner().give(true);
22
+ two.owner().give(true);
23
23
  expect(g).toHaveBeenLastCalledWith(true);
24
24
  });
package/src/boolean/Or.ts CHANGED
@@ -1,18 +1,22 @@
1
- import { all, InformationType } from "silentium";
1
+ import { All, From, TheInformation, TheOwner } from "silentium";
2
2
 
3
3
  /**
4
4
  * https://silentium-lab.github.io/silentium-components/#/boolean/or
5
5
  */
6
- export const or = (
7
- oneSrc: InformationType<boolean>,
8
- twoSrc: InformationType<boolean>,
9
- ): InformationType<boolean> => {
10
- return (o) => {
11
- all(
12
- oneSrc,
13
- twoSrc,
14
- )(([one, two]) => {
15
- o(one || two);
16
- });
17
- };
18
- };
6
+ export class Or extends TheInformation<boolean> {
7
+ public constructor(
8
+ private oneSrc: TheInformation<boolean>,
9
+ private twoSrc: TheInformation<boolean>,
10
+ ) {
11
+ super(oneSrc, twoSrc);
12
+ }
13
+
14
+ public value(o: TheOwner<boolean>): this {
15
+ new All(this.oneSrc, this.twoSrc).value(
16
+ new From(([one, two]) => {
17
+ o.give(one || two);
18
+ }),
19
+ );
20
+ return this;
21
+ }
22
+ }
@@ -1,3 +1,4 @@
1
1
  export * from "./And";
2
2
  export * from "./Or";
3
3
  export * from "./Not";
4
+ export * from "./Bool";
@@ -0,0 +1,11 @@
1
+ import { Late } from "silentium";
2
+ import { expect, test } from "vitest";
3
+ import { Sync } from "../behaviors/Sync";
4
+ import { FromJson } from "../formats/FromJson";
5
+
6
+ test("FromJson.test", () => {
7
+ const one = new Late('{"hello": "world"}');
8
+ const objectSync = new Sync(new FromJson<{ hello: string }>(one));
9
+
10
+ expect(objectSync.valueSync().hello).toBe("world");
11
+ });
@@ -1,19 +1,26 @@
1
- import { InformationType, OwnerType } from "silentium";
1
+ import { From, TheInformation, TheOwner } from "silentium";
2
2
 
3
3
  /**
4
4
  * Represents object from json
5
5
  */
6
- export const fromJson = <T>(
7
- jsonSrc: InformationType<string>,
8
- errorOwner?: OwnerType,
9
- ): InformationType<T> => {
10
- return (o) => {
11
- jsonSrc((json) => {
12
- try {
13
- o(JSON.parse(json));
14
- } catch (error) {
15
- errorOwner?.(new Error(`Failed to parse JSON: ${error}`));
16
- }
17
- });
18
- };
19
- };
6
+ export class FromJson<T> extends TheInformation<T> {
7
+ public constructor(
8
+ private jsonSrc: TheInformation<string>,
9
+ private errorOwner?: TheOwner,
10
+ ) {
11
+ super(jsonSrc);
12
+ }
13
+
14
+ public value(o: TheOwner<T>): this {
15
+ this.jsonSrc.value(
16
+ new From((json) => {
17
+ try {
18
+ o.give(JSON.parse(json));
19
+ } catch (error) {
20
+ this.errorOwner?.give(new Error(`Failed to parse JSON: ${error}`));
21
+ }
22
+ }),
23
+ );
24
+ return this;
25
+ }
26
+ }
@@ -0,0 +1,11 @@
1
+ import { Late } from "silentium";
2
+ import { expect, test } from "vitest";
3
+ import { Sync } from "../behaviors/Sync";
4
+ import { ToJson } from "../formats/ToJson";
5
+
6
+ test("FromJson.test", () => {
7
+ const one = new Late({ hello: "world" });
8
+ const objectSync = new Sync(new ToJson(one));
9
+
10
+ expect(objectSync.valueSync()).toBe('{"hello":"world"}');
11
+ });
@@ -1,19 +1,26 @@
1
- import { InformationType, OwnerType } from "silentium";
1
+ import { From, TheInformation, TheOwner } from "silentium";
2
2
 
3
3
  /**
4
- * Repreresents json from object
4
+ * Represents json from object
5
5
  */
6
- export const toJson = (
7
- dataSrc: InformationType,
8
- errorOwner?: OwnerType,
9
- ): InformationType<string> => {
10
- return (o) => {
11
- dataSrc((data) => {
12
- try {
13
- o(JSON.stringify(data));
14
- } catch {
15
- errorOwner?.(new Error("Failed to convert to JSON"));
16
- }
17
- });
18
- };
19
- };
6
+ export class ToJson extends TheInformation<string> {
7
+ public constructor(
8
+ private dataSrc: TheInformation,
9
+ private errorOwner?: TheOwner,
10
+ ) {
11
+ super(dataSrc);
12
+ }
13
+
14
+ public value(o: TheOwner<string>): this {
15
+ this.dataSrc.value(
16
+ new From((data: unknown) => {
17
+ try {
18
+ o.give(JSON.stringify(data));
19
+ } catch {
20
+ this.errorOwner?.give(new Error("Failed to convert to JSON"));
21
+ }
22
+ }),
23
+ );
24
+ return this;
25
+ }
26
+ }
@@ -1,10 +1,10 @@
1
- import { i } from "silentium";
1
+ import { From, Of } from "silentium";
2
2
  import { expect, test, vi } from "vitest";
3
- import { first } from "./First";
3
+ import { First } from "./First";
4
4
 
5
5
  test("first", () => {
6
- const f = first(i([1, 2, 3]));
6
+ const f = new First(new Of([1, 2, 3]));
7
7
  const g = vi.fn();
8
- f(g);
8
+ f.value(new From(g));
9
9
  expect(g).toHaveBeenCalledWith(1);
10
10
  });
@@ -1,10 +1,15 @@
1
- import { applied, InformationType } from "silentium";
1
+ import { Applied, TheInformation, TheOwner } from "silentium";
2
2
 
3
3
  /**
4
4
  * Represents the first element of an array.
5
5
  */
6
- export const first = <T extends Array<unknown>>(
7
- baseSrc: InformationType<T>,
8
- ): InformationType<T[0]> => {
9
- return applied(baseSrc, (a) => a[0]);
10
- };
6
+ export class First<T extends Array<unknown>> extends TheInformation<T[0]> {
7
+ public constructor(private baseSrc: TheInformation<T>) {
8
+ super(baseSrc);
9
+ }
10
+
11
+ public value(o: TheOwner<T[0]>): this {
12
+ new Applied(this.baseSrc, (a) => a[0]).value(o);
13
+ return this;
14
+ }
15
+ }
@@ -1,46 +1,46 @@
1
- import { applied, i, of, shared } from "silentium";
1
+ import { Applied, From, Late, Of, Shared } from "silentium";
2
2
  import { expect, test, vi } from "vitest";
3
- import { router } from "../navigation/Router";
3
+ import { Router } from "../navigation/Router";
4
4
 
5
5
  const drop = (dropPart: string) => (value: string) => {
6
6
  return value.replace(dropPart, "");
7
7
  };
8
8
 
9
9
  test("Router.test", () => {
10
- const [urlSrc, uo] = of<string>("http://domain.com/");
11
- const [urlPathSrc] = shared(applied(urlSrc, drop("http://domain.com")));
10
+ const urlSrc = new Late<string>("http://domain.com/");
11
+ const urlPathSrc = new Shared(new Applied(urlSrc, drop("http://domain.com")));
12
12
  const g = vi.fn();
13
- urlPathSrc(g);
13
+ urlPathSrc.value(new From(g));
14
14
 
15
- const routerSrc = router(
15
+ const routerSrc = new Router(
16
16
  urlPathSrc,
17
- i([
17
+ new Of([
18
18
  {
19
19
  pattern: "^/$",
20
- template: i("page/home.html"),
20
+ template: new Of("page/home.html"),
21
21
  },
22
22
  {
23
23
  pattern: "/some/contacts",
24
24
  template: "page/contacts.html",
25
25
  },
26
26
  ]),
27
- i("page/404.html"),
27
+ new Of<string>("page/404.html"),
28
28
  );
29
29
  const g2 = vi.fn();
30
- routerSrc(g2);
30
+ routerSrc.value(new From(g2));
31
31
 
32
32
  expect(g2).toHaveBeenLastCalledWith("page/home.html");
33
33
 
34
- uo("http://domain.com/some/contacts");
34
+ urlSrc.owner().give("http://domain.com/some/contacts");
35
35
 
36
36
  expect(g).toHaveBeenLastCalledWith("/some/contacts");
37
37
  expect(g2).toHaveBeenLastCalledWith("page/contacts.html");
38
38
 
39
- uo("http://domain.com/some/unknown/");
39
+ urlSrc.owner().give("http://domain.com/some/unknown/");
40
40
 
41
41
  expect(g2).toHaveBeenLastCalledWith("page/404.html");
42
42
 
43
- uo("http://domain.com/");
43
+ urlSrc.owner().give("http://domain.com/");
44
44
 
45
45
  expect(g2).toHaveBeenLastCalledWith("page/home.html");
46
46
  });
@@ -1,39 +1,46 @@
1
- import { any, chain, i, InformationType, OwnerType } from "silentium";
2
- import { branch } from "../behaviors";
3
- import { regexpMatched } from "../system/RegexpMatched";
1
+ import { Any, Chain, From, Of, TheInformation, TheOwner } from "silentium";
2
+ import { Branch } from "../behaviors";
3
+ import { RegexpMatched } from "../system";
4
4
 
5
5
  export interface Route<T> {
6
6
  pattern: string;
7
7
  patternFlags?: string;
8
- template: T | InformationType<T>;
8
+ template: T | TheInformation<T>;
9
9
  }
10
10
 
11
11
  /**
12
12
  * Router component what will return template if url matches pattern
13
13
  * https://silentium-lab.github.io/silentium-components/#/navigation/router
14
14
  */
15
- export const router = <T = "string">(
16
- urlSrc: InformationType<string>,
17
- routesSrc: InformationType<Route<T>[]>,
18
- defaultSrc: InformationType<T>,
19
- ): InformationType<T> => {
20
- return (o) => {
21
- routesSrc((routes) => {
22
- any(
23
- chain(urlSrc, defaultSrc),
24
- ...routes.map((r) => {
25
- return branch(
26
- regexpMatched(
27
- i(r.pattern),
28
- urlSrc,
29
- r.patternFlags ? i(r.patternFlags) : undefined,
30
- ),
31
- (typeof r.template === "function"
32
- ? r.template
33
- : i(r.template)) as InformationType,
34
- );
35
- }),
36
- )(o as OwnerType<unknown>);
37
- });
38
- };
39
- };
15
+ export class Router<T = "string"> extends TheInformation<T> {
16
+ public constructor(
17
+ private urlSrc: TheInformation<string>,
18
+ private routesSrc: TheInformation<Route<T>[]>,
19
+ private defaultSrc: TheInformation<T>,
20
+ ) {
21
+ super(urlSrc, routesSrc, defaultSrc);
22
+ }
23
+
24
+ public value(o: TheOwner<T>): this {
25
+ this.routesSrc.value(
26
+ new From((routes) => {
27
+ new Any(
28
+ new Chain(this.urlSrc, this.defaultSrc),
29
+ ...routes.map((r) => {
30
+ return new Branch(
31
+ new RegexpMatched(
32
+ new Of(r.pattern),
33
+ this.urlSrc,
34
+ r.patternFlags ? new Of(r.patternFlags) : undefined,
35
+ ),
36
+ (r.template instanceof TheInformation
37
+ ? r.template
38
+ : new Of(r.template)) as TheInformation,
39
+ );
40
+ }),
41
+ ).value(o as TheOwner);
42
+ }),
43
+ );
44
+ return this;
45
+ }
46
+ }