@modular-component/core 0.1.7 → 0.2.0
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/CHANGELOG.md +6 -0
- package/dist/index.d.ts +31 -354
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +53 -157
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +155 -240
- package/dist/types/arguments.d.ts +0 -33
- package/dist/types/arguments.d.ts.map +0 -1
- package/dist/types/arguments.js +0 -6
- package/dist/types/arguments.js.map +0 -1
- package/dist/types/methods/add.d.ts +0 -23
- package/dist/types/methods/add.d.ts.map +0 -1
- package/dist/types/methods/add.js +0 -8
- package/dist/types/methods/add.js.map +0 -1
- package/dist/types/methods/at.d.ts +0 -22
- package/dist/types/methods/at.d.ts.map +0 -1
- package/dist/types/methods/at.js +0 -7
- package/dist/types/methods/at.js.map +0 -1
- package/dist/types/methods/before.d.ts +0 -22
- package/dist/types/methods/before.d.ts.map +0 -1
- package/dist/types/methods/before.js +0 -7
- package/dist/types/methods/before.js.map +0 -1
- package/dist/types/methods/hook.d.ts +0 -14
- package/dist/types/methods/hook.d.ts.map +0 -1
- package/dist/types/methods/hook.js +0 -7
- package/dist/types/methods/hook.js.map +0 -1
- package/dist/types/methods/mock.d.ts +0 -26
- package/dist/types/methods/mock.d.ts.map +0 -1
- package/dist/types/methods/mock.js +0 -6
- package/dist/types/methods/mock.js.map +0 -1
- package/dist/types/methods/with.d.ts +0 -52
- package/dist/types/methods/with.d.ts.map +0 -1
- package/dist/types/methods/with.js +0 -8
- package/dist/types/methods/with.js.map +0 -1
- package/dist/types/methods.d.ts +0 -10
- package/dist/types/methods.d.ts.map +0 -1
- package/dist/types/methods.js +0 -5
- package/dist/types/methods.js.map +0 -1
- package/dist/types/modular-component.d.ts +0 -16
- package/dist/types/modular-component.d.ts.map +0 -1
- package/dist/types/modular-component.js +0 -5
- package/dist/types/modular-component.js.map +0 -1
- package/dist/types/stage.d.ts +0 -41
- package/dist/types/stage.d.ts.map +0 -1
- package/dist/types/stage.js +0 -5
- package/dist/types/stage.js.map +0 -1
- package/dist/types/utils.d.ts +0 -24
- package/dist/types/utils.d.ts.map +0 -1
- package/dist/types/utils.js +0 -5
- package/dist/types/utils.js.map +0 -1
- package/dist/types/validation.d.ts +0 -4
- package/dist/types/validation.d.ts.map +0 -1
- package/dist/types/validation.js +0 -2
- package/dist/types/validation.js.map +0 -1
- package/src/types/arguments.ts +0 -57
- package/src/types/methods/add.ts +0 -52
- package/src/types/methods/at.ts +0 -64
- package/src/types/methods/before.ts +0 -64
- package/src/types/methods/hook.ts +0 -27
- package/src/types/methods/mock.ts +0 -90
- package/src/types/methods/with.ts +0 -173
- package/src/types/methods.ts +0 -11
- package/src/types/modular-component.ts +0 -27
- package/src/types/stage.ts +0 -91
- package/src/types/utils.ts +0 -63
- package/src/types/validation.ts +0 -13
package/dist/index.js
CHANGED
|
@@ -1,162 +1,58 @@
|
|
|
1
|
-
|
|
2
|
-
function
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
Component.asHook = generateAsHook();
|
|
45
|
-
// Add each configured stage methods to the component
|
|
46
|
-
Object.keys(methods).forEach((method) => {
|
|
47
|
-
// Check if a stage of the same key already exists
|
|
48
|
-
const stageIndices = stages
|
|
49
|
-
// Map all stages to an [index, stage] tuple
|
|
50
|
-
.map((record, index) => [index, record])
|
|
51
|
-
// Remove all tuples not matching our stage
|
|
52
|
-
.filter(([, record]) => record.stage === methods[method].symbol)
|
|
53
|
-
// Get the index
|
|
54
|
-
.map(([index]) => index);
|
|
55
|
-
const lastIndex = [...stageIndices].pop();
|
|
56
|
-
// @ts-ignore
|
|
57
|
-
Component[`with${method}`] = (value, forceIndex) => {
|
|
58
|
-
// Prepare the new stage
|
|
59
|
-
const stage = { stage: methods[method].symbol, value };
|
|
60
|
-
// Check if a stage of the same key already exists
|
|
61
|
-
const stageIndex = (forceIndex !== undefined
|
|
62
|
-
? stageIndices[forceIndex]
|
|
63
|
-
: lastIndex) ?? -1;
|
|
64
|
-
// If so, copy the stages and replace the previous record
|
|
65
|
-
if (stageIndex > -1) {
|
|
66
|
-
const nextStages = [...stages];
|
|
67
|
-
nextStages[stageIndex] = stage;
|
|
68
|
-
return ModularFactory(methods).build(nextStages)(displayName);
|
|
69
|
-
}
|
|
70
|
-
// Otherwise, append the stage
|
|
71
|
-
return ModularFactory(methods).build([...stages, stage])(displayName);
|
|
72
|
-
};
|
|
73
|
-
// @ts-ignore
|
|
74
|
-
Component[`add${method}`] =
|
|
75
|
-
stageIndices.length < 1
|
|
76
|
-
? undefined
|
|
77
|
-
: (value) => {
|
|
78
|
-
// Prepare the new stage
|
|
79
|
-
const stage = {
|
|
80
|
-
stage: methods[method].symbol,
|
|
81
|
-
value,
|
|
82
|
-
};
|
|
83
|
-
// Append the stage as in multiple mode
|
|
84
|
-
return ModularFactory(methods).build([
|
|
85
|
-
...stages,
|
|
86
|
-
stage,
|
|
87
|
-
])(displayName);
|
|
88
|
-
};
|
|
89
|
-
// @ts-ignore
|
|
90
|
-
Component[`at${method}`] =
|
|
91
|
-
stageIndices.length < 1
|
|
92
|
-
? undefined
|
|
93
|
-
: (forceIndex) => {
|
|
94
|
-
// Find the needed stage
|
|
95
|
-
const stageIndex = (forceIndex !== undefined
|
|
96
|
-
? stageIndices[forceIndex]
|
|
97
|
-
: lastIndex) ?? lastIndex;
|
|
98
|
-
// Otherwise, keep all stages up to and including the found stage
|
|
99
|
-
return ModularFactory(methods).build(stages.slice(0, stageIndex + 1))(displayName);
|
|
100
|
-
};
|
|
101
|
-
// @ts-ignore
|
|
102
|
-
Component[`before${method}`] =
|
|
103
|
-
stageIndices.length < 1
|
|
104
|
-
? undefined
|
|
105
|
-
: (forceIndex) => {
|
|
106
|
-
// Find the needed stage
|
|
107
|
-
const stageIndex = (forceIndex !== undefined
|
|
108
|
-
? stageIndices[forceIndex]
|
|
109
|
-
: lastIndex) ?? lastIndex;
|
|
110
|
-
// Otherwise, keep all stages up to but excluding the found stage
|
|
111
|
-
return ModularFactory(methods).build(stages.slice(0, stageIndex))(displayName);
|
|
112
|
-
};
|
|
113
|
-
// @ts-ignore
|
|
114
|
-
Component[`mock${method}`] =
|
|
115
|
-
stageIndices.length < 1
|
|
116
|
-
? () => Component
|
|
117
|
-
: (value, forceIndex) => {
|
|
118
|
-
// Prepare the mocked stage
|
|
119
|
-
const stage = {
|
|
120
|
-
stage: methods[method].symbol,
|
|
121
|
-
value,
|
|
122
|
-
mocked: true,
|
|
123
|
-
};
|
|
124
|
-
// Find the needed stage
|
|
125
|
-
const stageIndex = (forceIndex !== undefined
|
|
126
|
-
? stageIndices[forceIndex]
|
|
127
|
-
: lastIndex) ?? lastIndex;
|
|
128
|
-
// Replace the stage with its mock
|
|
129
|
-
const nextStages = [...stages];
|
|
130
|
-
nextStages[stageIndex] = stage;
|
|
131
|
-
return ModularFactory(methods).build(nextStages)(displayName);
|
|
132
|
-
};
|
|
133
|
-
const capitalize = (str) => str[0].toUpperCase() + str.slice(1);
|
|
134
|
-
// @ts-ignore
|
|
135
|
-
Component[`asUse${capitalize(methods[method].field)}`] =
|
|
136
|
-
generateAsHook(undefined, methods[method].field);
|
|
137
|
-
});
|
|
138
|
-
return (options?.memo ? memo(Component) : Component);
|
|
139
|
-
};
|
|
140
|
-
factory.memo = (displayName) => factory(displayName, { memo: true });
|
|
141
|
-
return factory;
|
|
142
|
-
},
|
|
143
|
-
extend: (_methods) => {
|
|
144
|
-
return ModularFactory({
|
|
145
|
-
...methods,
|
|
146
|
-
..._methods,
|
|
147
|
-
});
|
|
148
|
-
},
|
|
1
|
+
function InternalFactory(stages) {
|
|
2
|
+
const useComponent = function (props, ref) {
|
|
3
|
+
if (!stages.some((stage) => stage.field === 'render')) {
|
|
4
|
+
stages = [...stages, render(() => null)];
|
|
5
|
+
}
|
|
6
|
+
return useComponent.use('render')(props, ref);
|
|
7
|
+
};
|
|
8
|
+
useComponent.with = (stage) => {
|
|
9
|
+
const index = stages.findIndex((s) => s.field === stage.field);
|
|
10
|
+
if (index !== -1) {
|
|
11
|
+
const next = [...stages];
|
|
12
|
+
next[index] = stage;
|
|
13
|
+
return InternalFactory(next);
|
|
14
|
+
}
|
|
15
|
+
return InternalFactory([...stages, stage]);
|
|
16
|
+
};
|
|
17
|
+
useComponent.force = useComponent.with;
|
|
18
|
+
useComponent.use =
|
|
19
|
+
(field) => (props, ref) => {
|
|
20
|
+
const args = { props };
|
|
21
|
+
const index = field
|
|
22
|
+
? stages.findIndex((stage) => stage.field === field)
|
|
23
|
+
: false;
|
|
24
|
+
if (index === false) {
|
|
25
|
+
for (let stage of stages) {
|
|
26
|
+
args[stage.field] = stage.useStage(args, ref);
|
|
27
|
+
}
|
|
28
|
+
return args;
|
|
29
|
+
}
|
|
30
|
+
const argStages = index === -1 ? stages.slice(0) : stages.slice(0, index + 1);
|
|
31
|
+
const returnStage = argStages.pop();
|
|
32
|
+
for (let stage of argStages) {
|
|
33
|
+
args[stage.field] = stage.useStage(args, ref);
|
|
34
|
+
}
|
|
35
|
+
return returnStage?.useStage(args, ref) ?? null;
|
|
36
|
+
};
|
|
37
|
+
useComponent.stage = (field) => {
|
|
38
|
+
const stage = stages.find((stage) => stage.field === field);
|
|
39
|
+
return stage?.useStage ?? (() => null);
|
|
40
|
+
};
|
|
41
|
+
useComponent.setDisplayName = (displayName) => {
|
|
42
|
+
;
|
|
43
|
+
useComponent.displayName = displayName;
|
|
149
44
|
};
|
|
45
|
+
return useComponent;
|
|
150
46
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
47
|
+
export function ModularComponent(displayName) {
|
|
48
|
+
const useComponent = InternalFactory([]);
|
|
49
|
+
useComponent.displayName = displayName;
|
|
50
|
+
return useComponent;
|
|
51
|
+
}
|
|
52
|
+
export function render(render) {
|
|
53
|
+
return {
|
|
155
54
|
field: 'render',
|
|
156
|
-
|
|
157
|
-
}
|
|
158
|
-
});
|
|
159
|
-
export function createMethodRecord(record) {
|
|
160
|
-
return record;
|
|
55
|
+
useStage: render,
|
|
56
|
+
};
|
|
161
57
|
}
|
|
162
58
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAyEA,SAAS,eAAe,CAKtB,MAGG;IAEH,MAAM,YAAY,GAAG,UAAU,KAAY,EAAE,GAA4B;QACvE,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,KAAK,KAAK,QAAQ,CAAC,EAAE;YACrD,MAAM,GAAG,CAAC,GAAG,MAAM,EAAE,MAAM,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,CAAC,CAAA;SACzC;QACD,OAAO,YAAY,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,KAAK,EAAE,GAAG,CAAC,CAAA;IAC/C,CAAC,CAAA;IAED,YAAY,CAAC,IAAI,GAAG,CAAC,KAAgD,EAAE,EAAE;QACvE,MAAM,KAAK,GAAG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29
29
|
"devDependencies": {
|
|
30
30
|
"@types/react": "^18.0.17",
|
|
31
|
-
"typescript": "^
|
|
31
|
+
"typescript": "^5.2.2"
|
|
32
32
|
},
|
|
33
33
|
"main": "dist/index.js",
|
|
34
34
|
"types": "dist/index.d.ts"
|