@uxf/core 10.0.0-beta.15 → 10.0.0-beta.22

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uxf/core",
3
- "version": "10.0.0-beta.15",
3
+ "version": "10.0.0-beta.22",
4
4
  "description": "UXF Core",
5
5
  "author": "Petr Vejvoda <vejvoda@uxf.cz>",
6
6
  "homepage": "https://gitlab.com/uxf-npm/core#readme",
@@ -1,21 +1,9 @@
1
- export declare function buildArray<T>(): {
2
- add: (data: T) => {
3
- add: any;
4
- when: (condition: boolean, data: T) => {
5
- add: any;
6
- when: any;
7
- get: () => T[];
8
- };
9
- get: () => T[];
10
- };
11
- when: (condition: boolean, data: T) => {
12
- add: (data: T) => {
13
- add: any;
14
- when: any;
15
- get: () => T[];
16
- };
17
- when: any;
18
- get: () => T[];
19
- };
20
- get: () => T[];
21
- };
1
+ type Element<T> = T;
2
+ type WhenFn<T> = (when: boolean, element: Element<T>) => ProxiedArray<T>;
3
+ type AddFn<T> = (element: Element<T>) => ProxiedArray<T>;
4
+ type ProxiedArray<T> = {
5
+ when: WhenFn<T>;
6
+ add: AddFn<T>;
7
+ } & T[];
8
+ export declare function buildArray<T = unknown>(initial?: T[]): ProxiedArray<T>;
9
+ export {};
@@ -1,28 +1,27 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.buildArray = void 0;
4
- function get(array) {
5
- return () => {
6
- return array;
7
- };
8
- }
9
- function add(array) {
10
- return (data) => {
11
- array.push(data);
12
- // eslint-disable-next-line @typescript-eslint/no-use-before-define
13
- return { add: add(array), when: when(array), get: get(array) };
14
- };
15
- }
16
- function when(array) {
17
- return (condition, data) => {
18
- if (condition) {
19
- array.push(data);
20
- }
21
- return { add: add(array), when: when(array), get: get(array) };
22
- };
23
- }
24
- function buildArray() {
25
- const array = [];
26
- return { add: add(array), when: when(array), get: get(array) };
4
+ function buildArray(initial) {
5
+ const initialArray = (initial ? [...initial] : []);
6
+ const proxiedArray = new Proxy(initialArray, {
7
+ get: (target, prop, receiver) => {
8
+ if ("add" === prop) {
9
+ return (element) => {
10
+ target.push(element);
11
+ return proxiedArray;
12
+ };
13
+ }
14
+ if ("when" === prop) {
15
+ return (when, element) => {
16
+ if (when) {
17
+ target.push(element);
18
+ }
19
+ return proxiedArray;
20
+ };
21
+ }
22
+ return Reflect.get(target, prop, receiver);
23
+ },
24
+ });
25
+ return proxiedArray;
27
26
  }
28
27
  exports.buildArray = buildArray;
@@ -2,9 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const buildArray_1 = require("./buildArray");
4
4
  test("buildArray", () => {
5
- expect((0, buildArray_1.buildArray)().get()).toStrictEqual([]);
6
- expect((0, buildArray_1.buildArray)().add("uxf").get()).toStrictEqual(["uxf"]);
7
- expect((0, buildArray_1.buildArray)().add("ux").add("fans").get()).toStrictEqual(["ux", "fans"]);
8
- expect((0, buildArray_1.buildArray)().add("ux").when(true, "fans").get()).toStrictEqual(["ux", "fans"]);
9
- expect((0, buildArray_1.buildArray)().add("ux").when(false, "fans").get()).toStrictEqual(["ux"]);
5
+ expect((0, buildArray_1.buildArray)()).toStrictEqual([]);
6
+ expect((0, buildArray_1.buildArray)().add("uxf")).toStrictEqual(["uxf"]);
7
+ expect((0, buildArray_1.buildArray)().add("ux").add("fans")).toStrictEqual(["ux", "fans"]);
8
+ expect((0, buildArray_1.buildArray)().add("ux").when(true, "fans")).toStrictEqual(["ux", "fans"]);
9
+ expect((0, buildArray_1.buildArray)().add("ux").when(false, "fans")).toStrictEqual(["ux"]);
10
10
  });