silentium-components 0.0.13 → 0.0.17

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 (43) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/commitizen.cjs +7 -7
  3. package/dist/silentium-components.cjs +135 -2
  4. package/dist/silentium-components.cjs.map +1 -1
  5. package/dist/silentium-components.d.ts +57 -2
  6. package/dist/silentium-components.js +127 -4
  7. package/dist/silentium-components.js.map +1 -1
  8. package/dist/silentium-components.min.js +1 -1
  9. package/dist/silentium-components.min.mjs +1 -1
  10. package/dist/silentium-components.min.mjs.map +1 -1
  11. package/dist/silentium-components.mjs +127 -4
  12. package/dist/silentium-components.mjs.map +1 -1
  13. package/package.json +1 -1
  14. package/src/behaviors/Branch.branchesDontAffectResult.test.ts +23 -0
  15. package/src/behaviors/Branch.test.ts +20 -0
  16. package/src/behaviors/Branch.ts +35 -0
  17. package/src/behaviors/Deferred.test.ts +28 -0
  18. package/src/behaviors/Deferred.ts +27 -0
  19. package/src/behaviors/Fork.ts +3 -2
  20. package/src/behaviors/Lock.test.ts +26 -0
  21. package/src/behaviors/Lock.ts +36 -0
  22. package/src/behaviors/Memo.test.ts +24 -0
  23. package/src/behaviors/Memo.ts +22 -0
  24. package/src/behaviors/Moment.test.ts +24 -0
  25. package/src/behaviors/Moment.ts +17 -0
  26. package/src/behaviors/Path.ts +7 -4
  27. package/src/behaviors/index.ts +5 -0
  28. package/src/boolean/And.test.ts +22 -0
  29. package/src/boolean/And.ts +16 -0
  30. package/src/boolean/Bool.ts +7 -0
  31. package/src/boolean/Not.test.ts +12 -0
  32. package/src/boolean/Not.ts +15 -0
  33. package/src/boolean/Or.test.ts +22 -0
  34. package/src/boolean/Or.ts +16 -0
  35. package/src/boolean/index.ts +3 -0
  36. package/src/index.ts +1 -0
  37. package/src/structures/Record.concatenated.test.ts +40 -0
  38. package/src/system/RegexpMatch._group.test.ts +11 -0
  39. package/src/system/RegexpMatch.test.ts +11 -0
  40. package/src/system/RegexpMatch.ts +19 -0
  41. package/src/system/Set.test.ts +22 -0
  42. package/src/system/Set.ts +20 -0
  43. package/src/system/index.ts +2 -0
@@ -0,0 +1,35 @@
1
+ import { patron, patronOnce, sourceOf, SourceType, value } from "silentium";
2
+
3
+ /**
4
+ * https://silentium-lab.github.io/silentium-components/#/behaviors/branch
5
+ */
6
+ export const branch = <Then, Else>(
7
+ conditionSrc: SourceType<boolean>,
8
+ thenSrc: SourceType<Then>,
9
+ elseSrc?: SourceType<Else>,
10
+ ): SourceType<Then | Else> => {
11
+ const result = sourceOf<Then | Else>();
12
+
13
+ value(
14
+ conditionSrc,
15
+ patron((v) => {
16
+ if (v === true) {
17
+ value(
18
+ thenSrc,
19
+ patronOnce((v) => {
20
+ result.give(v);
21
+ }),
22
+ );
23
+ } else if (elseSrc !== undefined) {
24
+ value(
25
+ elseSrc,
26
+ patronOnce((v) => {
27
+ result.give(v);
28
+ }),
29
+ );
30
+ }
31
+ }),
32
+ );
33
+
34
+ return result.value;
35
+ };
@@ -0,0 +1,28 @@
1
+ import { sourceOf, sourceSync, value } from "silentium";
2
+ import { deferred } from "../behaviors/Deferred";
3
+ import { expect, test, vi } from "vitest";
4
+
5
+ test("Deferred.test", () => {
6
+ const urlSrc = sourceOf<string>("http://hello.com");
7
+ const layoutSrc = sourceOf();
8
+
9
+ const urlWithLayoutSrc = sourceSync(deferred(urlSrc, layoutSrc));
10
+
11
+ const g1 = vi.fn();
12
+ value(urlWithLayoutSrc, g1);
13
+ expect(g1).not.toHaveBeenCalled();
14
+
15
+ layoutSrc.give("layout here");
16
+
17
+ const g2 = vi.fn();
18
+ value(urlWithLayoutSrc, g2);
19
+ expect(g2).toHaveBeenCalledWith("http://hello.com");
20
+
21
+ urlSrc.give("http://new.com");
22
+
23
+ expect(urlWithLayoutSrc.syncValue()).toBe("http://hello.com");
24
+
25
+ layoutSrc.give("layout here again");
26
+
27
+ expect(urlWithLayoutSrc.syncValue()).toBe("http://new.com");
28
+ });
@@ -0,0 +1,27 @@
1
+ import {
2
+ patron,
3
+ sourceOf,
4
+ sourceResettable,
5
+ SourceType,
6
+ value,
7
+ } from "silentium";
8
+
9
+ /**
10
+ * Defer one source after another, gives values of baseSrc only once when triggerSrc responds
11
+ * https://silentium-lab.github.io/silentium-components/#/behaviors/deferred
12
+ */
13
+ export const deferred = <T>(
14
+ baseSrc: SourceType<T>,
15
+ triggerSrc: SourceType<unknown>,
16
+ ) => {
17
+ const result = sourceResettable<T>(sourceOf(), baseSrc as SourceType);
18
+
19
+ value(
20
+ triggerSrc,
21
+ patron(() => {
22
+ value(baseSrc, result);
23
+ }),
24
+ );
25
+
26
+ return result.value;
27
+ };
@@ -6,6 +6,7 @@ import {
6
6
  patron,
7
7
  removePatronFromPools,
8
8
  GuestObjectType,
9
+ patronOnce,
9
10
  } from "silentium";
10
11
 
11
12
  /**
@@ -34,10 +35,10 @@ export const fork = <T, Then, Else>(
34
35
  removePatronFromPools(elsePatron);
35
36
  }
36
37
  if (predicate(v)) {
37
- thenPatron = patron(result);
38
+ thenPatron = patronOnce(result);
38
39
  value(thenSrc, thenPatron);
39
40
  } else if (elseSrc) {
40
- elsePatron = patron(result);
41
+ elsePatron = patronOnce(result);
41
42
  value(elseSrc, elsePatron);
42
43
  }
43
44
  }),
@@ -0,0 +1,26 @@
1
+ import { sourceOf, sourceSync, value } from "silentium";
2
+ import { expect, test, vi } from "vitest";
3
+ import { lock } from "../behaviors/Lock";
4
+
5
+ test("Lock.test", () => {
6
+ const source = sourceOf<number>(1);
7
+ const lockSrc = sourceOf();
8
+
9
+ const lockedSrc = lock(source, lockSrc);
10
+ const lockedSync = sourceSync(lockedSrc);
11
+
12
+ expect(lockedSync.syncValue()).toBe(1);
13
+
14
+ source.give(2);
15
+
16
+ expect(lockedSync.syncValue()).toBe(2);
17
+
18
+ lockSrc.give(1);
19
+ source.give(3);
20
+ source.give(4);
21
+ source.give(5);
22
+
23
+ const g = vi.fn();
24
+ value(lockedSrc, g);
25
+ expect(g).not.toBeCalled();
26
+ });
@@ -0,0 +1,36 @@
1
+ import {
2
+ destroy,
3
+ guestDisposable,
4
+ patron,
5
+ patronOnce,
6
+ sourceOf,
7
+ sourceResettable,
8
+ SourceType,
9
+ subSource,
10
+ value,
11
+ } from "silentium";
12
+
13
+ /**
14
+ * https://silentium-lab.github.io/silentium-components/#/behaviors/lock
15
+ */
16
+ export const lock = <T>(
17
+ baseSrc: SourceType<T>,
18
+ lockSrc: SourceType<unknown>,
19
+ ) => {
20
+ const result = sourceOf();
21
+ const resultResettable = sourceResettable(result, lockSrc);
22
+ let locked = false;
23
+ subSource(result, baseSrc);
24
+
25
+ value(baseSrc, patron(guestDisposable(result.give, () => locked)));
26
+
27
+ value(
28
+ lockSrc,
29
+ patronOnce(() => {
30
+ locked = true;
31
+ destroy([result]);
32
+ }),
33
+ );
34
+
35
+ return resultResettable.value;
36
+ };
@@ -0,0 +1,24 @@
1
+ import { patron, sourceOf, sourceSync, value } from "silentium";
2
+ import { memo } from "../behaviors/Memo";
3
+ import { expect, test } from "vitest";
4
+
5
+ test("Memo.test", () => {
6
+ const src = sourceOf<number>(1);
7
+ const srcMemo = sourceSync(memo(src));
8
+ let counter = 0;
9
+ value(
10
+ srcMemo,
11
+ patron(() => {
12
+ counter += 1;
13
+ }),
14
+ );
15
+
16
+ src.give(2);
17
+ src.give(2);
18
+ src.give(2);
19
+ src.give(2);
20
+ src.give(2);
21
+
22
+ expect(srcMemo.syncValue()).toBe(2);
23
+ expect(counter).toBe(2);
24
+ });
@@ -0,0 +1,22 @@
1
+ import { patron, sourceOf, SourceType, value } from "silentium";
2
+
3
+ /**
4
+ * Didn't respond if new value of baseSrc equals to old value
5
+ * https://silentium-lab.github.io/silentium-components/#/behaviors/memo
6
+ */
7
+ export const memo = <T>(baseSrc: SourceType<T>) => {
8
+ const result = sourceOf<T>();
9
+ let lastValue: T | null = null;
10
+
11
+ value(
12
+ baseSrc,
13
+ patron((v) => {
14
+ if (v !== lastValue) {
15
+ result.give(v);
16
+ lastValue = v;
17
+ }
18
+ }),
19
+ );
20
+
21
+ return result.value;
22
+ };
@@ -0,0 +1,24 @@
1
+ import { patron, sourceOf, sourceSync, value } from "silentium";
2
+ import { moment } from "../behaviors/Moment";
3
+ import { expect, test } from "vitest";
4
+
5
+ test("Moment.test", () => {
6
+ const src = sourceOf<number>(1);
7
+ const srcMoment = sourceSync(moment(src));
8
+ let counter = 0;
9
+ value(
10
+ srcMoment,
11
+ patron(() => {
12
+ counter += 1;
13
+ }),
14
+ );
15
+
16
+ src.give(2);
17
+ src.give(3);
18
+ src.give(4);
19
+ src.give(5);
20
+ src.give(6);
21
+
22
+ expect(srcMoment.syncValue()).toBe(1);
23
+ expect(counter).toBe(1);
24
+ });
@@ -0,0 +1,17 @@
1
+ import { give, guestSync, GuestType, SourceType, value } from "silentium";
2
+
3
+ /**
4
+ * Get's value from source in moment of component call and than return this value every time
5
+ * https://silentium-lab.github.io/silentium-components/#/behaviors/moment
6
+ */
7
+ export const moment = <T>(
8
+ baseSrc: SourceType<T>,
9
+ defaultValue?: T,
10
+ ): SourceType<T> => {
11
+ const guest = guestSync(defaultValue);
12
+ value(baseSrc, guest);
13
+
14
+ return (g: GuestType<T>) => {
15
+ give(guest.value(), g);
16
+ };
17
+ };
@@ -12,11 +12,14 @@ import {
12
12
  * Return source of record path
13
13
  * https://silentium-lab.github.io/silentium-components/#/behaviors/path
14
14
  */
15
- export const path = <T extends Record<string, unknown>, K extends string>(
15
+ export const path = <
16
+ T extends Record<string, unknown> | Array<unknown>,
17
+ K extends string,
18
+ >(
16
19
  baseSrc: SourceType<T>,
17
20
  keySrc: SourceType<K>,
18
21
  ) => {
19
- const pathSrc = sourceOf<T[K]>();
22
+ const pathSrc = sourceOf<unknown>();
20
23
  subSourceMany(pathSrc, [baseSrc, keySrc]);
21
24
 
22
25
  value(
@@ -25,11 +28,11 @@ export const path = <T extends Record<string, unknown>, K extends string>(
25
28
  const keyChunks = key.split(".");
26
29
  let value: unknown = base;
27
30
  keyChunks.forEach((keyChunk) => {
28
- value = (value as T)[keyChunk];
31
+ value = (value as Record<string, unknown>)[keyChunk];
29
32
  });
30
33
 
31
34
  if (value !== undefined && value !== base) {
32
- give(value as T[K], pathSrc);
35
+ give(value, pathSrc);
33
36
  }
34
37
  }),
35
38
  );
@@ -4,3 +4,8 @@ export * from "./Path";
4
4
  export * from "./Deadline";
5
5
  export * from "./Tick";
6
6
  export * from "./Fork";
7
+ export * from "./Deferred";
8
+ export * from "./Branch";
9
+ export * from "./Memo";
10
+ export * from "./Lock";
11
+ export * from "./Moment";
@@ -0,0 +1,22 @@
1
+ import { sourceOf, sourceSync } from "silentium";
2
+ import { and } from "../boolean/And";
3
+ import { expect, test } from "vitest";
4
+
5
+ test("And.test", () => {
6
+ const one = sourceOf<boolean>(false);
7
+ const two = sourceOf<boolean>(false);
8
+ const result = sourceSync(and(one, two));
9
+ expect(result.syncValue()).toBe(false);
10
+
11
+ one.give(true);
12
+ two.give(false);
13
+ expect(result.syncValue()).toBe(false);
14
+
15
+ one.give(false);
16
+ two.give(true);
17
+ expect(result.syncValue()).toBe(false);
18
+
19
+ one.give(true);
20
+ two.give(true);
21
+ expect(result.syncValue()).toBe(true);
22
+ });
@@ -0,0 +1,16 @@
1
+ import { give, GuestType, sourceCombined, SourceType } from "silentium";
2
+
3
+ /**
4
+ * https://silentium-lab.github.io/silentium-components/#/boolean/and
5
+ */
6
+ export const and = (
7
+ oneSrc: SourceType<boolean>,
8
+ twoSrc: SourceType<boolean>,
9
+ ) => {
10
+ return sourceCombined(
11
+ oneSrc,
12
+ twoSrc,
13
+ )((guest: GuestType<boolean>, one, two) => {
14
+ give(one && two, guest);
15
+ });
16
+ };
@@ -0,0 +1,7 @@
1
+ import { sourceApplied, SourceType } from "silentium";
2
+
3
+ /**
4
+ * Convert any source to boolean source
5
+ * https://silentium-lab.github.io/silentium-components/#/boolean/bool
6
+ */
7
+ export const bool = (baseSrc: SourceType) => sourceApplied(baseSrc, Boolean);
@@ -0,0 +1,12 @@
1
+ import { sourceOf, sourceSync } from "silentium";
2
+ import { expect, test } from "vitest";
3
+ import { not } from "../boolean/Not";
4
+
5
+ test("Not.test", () => {
6
+ const one = sourceOf<boolean>(false);
7
+ const result = sourceSync(not(one));
8
+ expect(result.syncValue()).toBe(true);
9
+
10
+ one.give(true);
11
+ expect(result.syncValue()).toBe(false);
12
+ });
@@ -0,0 +1,15 @@
1
+ import { give, guestCast, GuestType, SourceType, value } from "silentium";
2
+
3
+ /**
4
+ * https://silentium-lab.github.io/silentium-components/#/boolean/not
5
+ */
6
+ export const not = (baseSrc: SourceType<boolean>) => {
7
+ return (g: GuestType<boolean>) => {
8
+ value(
9
+ baseSrc,
10
+ guestCast(g, (base) => {
11
+ give(!base, g);
12
+ }),
13
+ );
14
+ };
15
+ };
@@ -0,0 +1,22 @@
1
+ import { sourceOf, sourceSync } from "silentium";
2
+ import { or } from "../boolean/Or";
3
+ import { expect, test } from "vitest";
4
+
5
+ test("Or.test", () => {
6
+ const one = sourceOf<boolean>(false);
7
+ const two = sourceOf<boolean>(false);
8
+ const result = sourceSync(or(one, two));
9
+ expect(result.syncValue()).toBe(false);
10
+
11
+ one.give(true);
12
+ two.give(false);
13
+ expect(result.syncValue()).toBe(true);
14
+
15
+ one.give(false);
16
+ two.give(true);
17
+ expect(result.syncValue()).toBe(true);
18
+
19
+ one.give(true);
20
+ two.give(true);
21
+ expect(result.syncValue()).toBe(true);
22
+ });
@@ -0,0 +1,16 @@
1
+ import { give, GuestType, sourceCombined, SourceType } from "silentium";
2
+
3
+ /**
4
+ * https://silentium-lab.github.io/silentium-components/#/boolean/or
5
+ */
6
+ export const or = (
7
+ oneSrc: SourceType<boolean>,
8
+ twoSrc: SourceType<boolean>,
9
+ ) => {
10
+ return sourceCombined(
11
+ oneSrc,
12
+ twoSrc,
13
+ )((guest: GuestType<boolean>, one, two) => {
14
+ give(one || two, guest);
15
+ });
16
+ };
@@ -0,0 +1,3 @@
1
+ export * from "./And";
2
+ export * from "./Or";
3
+ export * from "./Not";
package/src/index.ts CHANGED
@@ -4,3 +4,4 @@ export * from "./structures";
4
4
  export * from "./strings";
5
5
  export * from "./navigation";
6
6
  export * from "./system";
7
+ export * from "./boolean";
@@ -0,0 +1,40 @@
1
+ import { patron, source, sourceOf, sourceSync } from "silentium";
2
+ import { expect, test } from "vitest";
3
+ import { record } from "./Record";
4
+ import { concatenated } from "../strings";
5
+
6
+ test("Record.concatenated.test", () => {
7
+ const three = sourceOf<string>("three");
8
+ const concatPart = sourceOf<string>("part");
9
+ const recordSrc = sourceSync(
10
+ record({
11
+ one: "one",
12
+ two: source("two"),
13
+ three,
14
+ nested: concatenated(["one", concatPart]),
15
+ }),
16
+ );
17
+ let counter = 0;
18
+ recordSrc.value(
19
+ patron(() => {
20
+ counter += 1;
21
+ }),
22
+ );
23
+
24
+ expect(recordSrc.syncValue()).toStrictEqual({
25
+ one: "one",
26
+ two: "two",
27
+ three: "three",
28
+ nested: "onepart",
29
+ });
30
+
31
+ concatPart.give("changed");
32
+
33
+ expect(recordSrc.syncValue()).toStrictEqual({
34
+ one: "one",
35
+ two: "two",
36
+ three: "three",
37
+ nested: "onechanged",
38
+ });
39
+ expect(counter).toBe(2);
40
+ });
@@ -0,0 +1,11 @@
1
+ import { sourceOf, sourceSync } from "silentium";
2
+ import { regexpMatch } from "./RegexpMatch";
3
+ import { expect, test } from "vitest";
4
+ import { path } from "../behaviors";
5
+
6
+ test("RegexpMatch._group.test", () => {
7
+ const urlSrc = sourceOf<string>("http://domain.com/some/url/");
8
+ const matchedSrc = sourceSync(path(regexpMatch("/(s\\w+)/", urlSrc), "1"));
9
+
10
+ expect(matchedSrc.syncValue()).toBe("some");
11
+ });
@@ -0,0 +1,11 @@
1
+ import { sourceOf, sourceSync } from "silentium";
2
+ import { regexpMatch } from "../system/RegexpMatch";
3
+ import { expect, test } from "vitest";
4
+ import { path } from "../behaviors";
5
+
6
+ test("RegexpMatch.test", () => {
7
+ const urlSrc = sourceOf<string>("http://domain.com/some/url/");
8
+ const matchedSrc = sourceSync(path(regexpMatch("/(s\\w+)/", urlSrc), "0"));
9
+
10
+ expect(matchedSrc.syncValue()).toBe("/some/");
11
+ });
@@ -0,0 +1,19 @@
1
+ import { give, GuestType, sourceCombined, SourceType } from "silentium";
2
+
3
+ /**
4
+ * First match of regexp
5
+ * https://silentium-lab.github.io/silentium-components/#/system/regexp-matched
6
+ */
7
+ export const regexpMatch = (
8
+ patternSrc: SourceType<string>,
9
+ valueSrc: SourceType<string>,
10
+ flagsSrc: SourceType<string> = "",
11
+ ): SourceType<string[]> =>
12
+ sourceCombined(
13
+ patternSrc,
14
+ valueSrc,
15
+ flagsSrc,
16
+ )((g: GuestType<string[]>, pattern, value, flags) => {
17
+ const result = new RegExp(pattern, flags).exec(value);
18
+ give(result ?? [], g);
19
+ });
@@ -0,0 +1,22 @@
1
+ import { sourceOf, sourceSync } from "silentium";
2
+ import { set } from "../system/Set";
3
+ import { expect, test } from "vitest";
4
+
5
+ test("Set.test", () => {
6
+ const value = sourceOf();
7
+ const object = sourceSync(
8
+ set(
9
+ {
10
+ value: "hello",
11
+ },
12
+ "value",
13
+ value,
14
+ ),
15
+ );
16
+
17
+ expect(object.syncValue().value).toBe("hello");
18
+
19
+ value.give("bue!");
20
+
21
+ expect(object.syncValue().value).toBe("bue!");
22
+ });
@@ -0,0 +1,20 @@
1
+ import { patron, sourceAll, SourceType, value } from "silentium";
2
+
3
+ /**
4
+ * Ability to mutate some object, helpful when integrate to procedure systems
5
+ * https://silentium-lab.github.io/silentium-components/#/system/set
6
+ */
7
+ export const set = <T extends Record<string, unknown>>(
8
+ baseSrc: SourceType<T>,
9
+ keySrc: SourceType<string>,
10
+ valueSrc: SourceType<unknown>,
11
+ ) => {
12
+ value(
13
+ sourceAll([baseSrc, keySrc, valueSrc]),
14
+ patron(([base, key, value]) => {
15
+ (base as Record<string, unknown>)[key] = value;
16
+ }),
17
+ );
18
+
19
+ return baseSrc;
20
+ };
@@ -1,2 +1,4 @@
1
1
  export * from "./RegexpMatched";
2
2
  export * from "./RegexpReplaced";
3
+ export * from "./RegexpMatch";
4
+ export * from "./Set";