react-ternary-be-gone 0.1.0 → 0.1.1
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/.babelrc +3 -0
- package/babel.config.js +3 -0
- package/dist/Conditional.js +206 -0
- package/dist/babel.config.js +5 -0
- package/dist/index.js +16 -0
- package/dist/useConditionalHelpers.js +53 -0
- package/package.json +9 -4
package/.babelrc
ADDED
package/babel.config.js
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _react = _interopRequireWildcard(require("react"));
|
|
8
|
+
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
|
|
9
|
+
const Conditional = _ref => {
|
|
10
|
+
let {
|
|
11
|
+
when,
|
|
12
|
+
each,
|
|
13
|
+
children,
|
|
14
|
+
fallback = null,
|
|
15
|
+
empty = null,
|
|
16
|
+
loading = false,
|
|
17
|
+
error = null,
|
|
18
|
+
keyExtractor = (item, index) => index,
|
|
19
|
+
// New features
|
|
20
|
+
filter = null,
|
|
21
|
+
sort = null,
|
|
22
|
+
limit = null,
|
|
23
|
+
reverse = false,
|
|
24
|
+
animate = false,
|
|
25
|
+
wrapper = _react.default.Fragment,
|
|
26
|
+
debug = false,
|
|
27
|
+
onRender = null,
|
|
28
|
+
// Conditional rendering helpers
|
|
29
|
+
gt = null,
|
|
30
|
+
// greater than
|
|
31
|
+
lt = null,
|
|
32
|
+
// less than
|
|
33
|
+
eq = null,
|
|
34
|
+
// equal
|
|
35
|
+
ne = null,
|
|
36
|
+
// not equal
|
|
37
|
+
includes = null,
|
|
38
|
+
startsWith = null,
|
|
39
|
+
endsWith = null,
|
|
40
|
+
match = null // regex match
|
|
41
|
+
} = _ref;
|
|
42
|
+
const hasCondition = when !== undefined;
|
|
43
|
+
const hasIteration = each !== undefined;
|
|
44
|
+
|
|
45
|
+
// Advanced condition evaluation
|
|
46
|
+
const evaluateCondition = (0, _react.useMemo)(() => {
|
|
47
|
+
if (when !== undefined) return Boolean(when);
|
|
48
|
+
|
|
49
|
+
// Numerical comparisons
|
|
50
|
+
if (gt !== null && typeof gt === 'object') {
|
|
51
|
+
const {
|
|
52
|
+
value,
|
|
53
|
+
target
|
|
54
|
+
} = gt;
|
|
55
|
+
return value > target;
|
|
56
|
+
}
|
|
57
|
+
if (lt !== null && typeof lt === 'object') {
|
|
58
|
+
const {
|
|
59
|
+
value,
|
|
60
|
+
target
|
|
61
|
+
} = lt;
|
|
62
|
+
return value < target;
|
|
63
|
+
}
|
|
64
|
+
if (eq !== null && typeof eq === 'object') {
|
|
65
|
+
const {
|
|
66
|
+
value,
|
|
67
|
+
target
|
|
68
|
+
} = eq;
|
|
69
|
+
return value === target;
|
|
70
|
+
}
|
|
71
|
+
if (ne !== null && typeof ne === 'object') {
|
|
72
|
+
const {
|
|
73
|
+
value,
|
|
74
|
+
target
|
|
75
|
+
} = ne;
|
|
76
|
+
return value !== target;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// String operations
|
|
80
|
+
if (includes !== null && typeof includes === 'object') {
|
|
81
|
+
const {
|
|
82
|
+
value,
|
|
83
|
+
target
|
|
84
|
+
} = includes;
|
|
85
|
+
return String(value).includes(target);
|
|
86
|
+
}
|
|
87
|
+
if (startsWith !== null && typeof startsWith === 'object') {
|
|
88
|
+
const {
|
|
89
|
+
value,
|
|
90
|
+
target
|
|
91
|
+
} = startsWith;
|
|
92
|
+
return String(value).startsWith(target);
|
|
93
|
+
}
|
|
94
|
+
if (endsWith !== null && typeof endsWith === 'object') {
|
|
95
|
+
const {
|
|
96
|
+
value,
|
|
97
|
+
target
|
|
98
|
+
} = endsWith;
|
|
99
|
+
return String(value).endsWith(target);
|
|
100
|
+
}
|
|
101
|
+
if (match !== null && typeof match === 'object') {
|
|
102
|
+
const {
|
|
103
|
+
value,
|
|
104
|
+
pattern
|
|
105
|
+
} = match;
|
|
106
|
+
return new RegExp(pattern).test(String(value));
|
|
107
|
+
}
|
|
108
|
+
return true;
|
|
109
|
+
}, [when, gt, lt, eq, ne, includes, startsWith, endsWith, match]);
|
|
110
|
+
|
|
111
|
+
// Array processing
|
|
112
|
+
const processedArray = (0, _react.useMemo)(() => {
|
|
113
|
+
if (!hasIteration || !Array.isArray(each)) return [];
|
|
114
|
+
let result = [...each];
|
|
115
|
+
|
|
116
|
+
// Apply filter
|
|
117
|
+
if (filter && typeof filter === 'function') {
|
|
118
|
+
result = result.filter(filter);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Apply sort
|
|
122
|
+
if (sort && typeof sort === 'function') {
|
|
123
|
+
result = result.sort(sort);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Apply reverse
|
|
127
|
+
if (reverse) {
|
|
128
|
+
result = result.reverse();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Apply limit
|
|
132
|
+
if (limit && typeof limit === 'number') {
|
|
133
|
+
result = result.slice(0, limit);
|
|
134
|
+
}
|
|
135
|
+
if (debug) {
|
|
136
|
+
console.log('Conditional Debug:', {
|
|
137
|
+
original: each,
|
|
138
|
+
processed: result,
|
|
139
|
+
filter: !!filter,
|
|
140
|
+
sort: !!sort,
|
|
141
|
+
reverse,
|
|
142
|
+
limit
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
return result;
|
|
146
|
+
}, [each, filter, sort, reverse, limit, hasIteration, debug]);
|
|
147
|
+
|
|
148
|
+
// Render callback
|
|
149
|
+
(0, _react.useEffect)(() => {
|
|
150
|
+
if (onRender && typeof onRender === 'function') {
|
|
151
|
+
onRender({
|
|
152
|
+
condition: evaluateCondition,
|
|
153
|
+
itemCount: processedArray.length,
|
|
154
|
+
hasCondition,
|
|
155
|
+
hasIteration
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}, [evaluateCondition, processedArray.length, hasCondition, hasIteration, onRender]);
|
|
159
|
+
|
|
160
|
+
// Loading state
|
|
161
|
+
if (loading) {
|
|
162
|
+
return fallback || /*#__PURE__*/_react.default.createElement("div", {
|
|
163
|
+
className: "conditional-loading"
|
|
164
|
+
}, "Loading...");
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Error state
|
|
168
|
+
if (error) {
|
|
169
|
+
return /*#__PURE__*/_react.default.createElement("div", {
|
|
170
|
+
className: "conditional-error"
|
|
171
|
+
}, "Error: ", error);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Wrapper component
|
|
175
|
+
const WrapperComponent = wrapper;
|
|
176
|
+
|
|
177
|
+
// Only condition
|
|
178
|
+
if (hasCondition && !hasIteration) {
|
|
179
|
+
return evaluateCondition ? /*#__PURE__*/_react.default.createElement(WrapperComponent, null, children) : fallback;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Only iteration
|
|
183
|
+
if (!hasCondition && hasIteration) {
|
|
184
|
+
if (processedArray.length === 0) {
|
|
185
|
+
return empty || fallback;
|
|
186
|
+
}
|
|
187
|
+
const elements = processedArray.map((item, index) => /*#__PURE__*/_react.default.createElement(_react.default.Fragment, {
|
|
188
|
+
key: keyExtractor(item, index)
|
|
189
|
+
}, typeof children === 'function' ? children(item, index, processedArray) : children));
|
|
190
|
+
return animate ? /*#__PURE__*/_react.default.createElement(WrapperComponent, {
|
|
191
|
+
className: "conditional-animated"
|
|
192
|
+
}, elements) : /*#__PURE__*/_react.default.createElement(WrapperComponent, null, elements);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Both condition and iteration
|
|
196
|
+
if (hasCondition && hasIteration) {
|
|
197
|
+
if (!evaluateCondition) return fallback;
|
|
198
|
+
if (processedArray.length === 0) return empty || fallback;
|
|
199
|
+
const elements = processedArray.map((item, index) => /*#__PURE__*/_react.default.createElement(_react.default.Fragment, {
|
|
200
|
+
key: keyExtractor(item, index)
|
|
201
|
+
}, children(item, index, processedArray)));
|
|
202
|
+
return /*#__PURE__*/_react.default.createElement(WrapperComponent, null, elements);
|
|
203
|
+
}
|
|
204
|
+
return /*#__PURE__*/_react.default.createElement(WrapperComponent, null, children);
|
|
205
|
+
};
|
|
206
|
+
var _default = exports.default = Conditional;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
Object.defineProperty(exports, "useConditionalHelpers", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function () {
|
|
10
|
+
return _useConditionalHelpers.useConditionalHelpers;
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
var _Conditional = _interopRequireDefault(require("./Conditional"));
|
|
14
|
+
var _useConditionalHelpers = require("./useConditionalHelpers");
|
|
15
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
16
|
+
var _default = exports.default = _Conditional.default;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.useConditionalHelpers = void 0;
|
|
7
|
+
var _react = require("react");
|
|
8
|
+
const useConditionalHelpers = () => {
|
|
9
|
+
return (0, _react.useMemo)(() => ({
|
|
10
|
+
// Quick condition helpers
|
|
11
|
+
isEmpty: array => ({
|
|
12
|
+
when: !array || array.length === 0
|
|
13
|
+
}),
|
|
14
|
+
isNotEmpty: array => ({
|
|
15
|
+
when: array && array.length > 0
|
|
16
|
+
}),
|
|
17
|
+
hasLength: (array, length) => ({
|
|
18
|
+
when: array && array.length === length
|
|
19
|
+
}),
|
|
20
|
+
isEven: num => ({
|
|
21
|
+
when: num % 2 === 0
|
|
22
|
+
}),
|
|
23
|
+
isOdd: num => ({
|
|
24
|
+
when: num % 2 !== 0
|
|
25
|
+
}),
|
|
26
|
+
// Array processing helpers
|
|
27
|
+
sortBy: function (field) {
|
|
28
|
+
let order = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'asc';
|
|
29
|
+
return (a, b) => {
|
|
30
|
+
const aValue = a[field];
|
|
31
|
+
const bValue = b[field];
|
|
32
|
+
if (aValue < bValue) {
|
|
33
|
+
return order === 'asc' ? -1 : 1;
|
|
34
|
+
}
|
|
35
|
+
if (aValue > bValue) {
|
|
36
|
+
return order === 'asc' ? 1 : -1;
|
|
37
|
+
}
|
|
38
|
+
return 0;
|
|
39
|
+
};
|
|
40
|
+
},
|
|
41
|
+
filterBy: (field, value) => item => item[field] === value,
|
|
42
|
+
unique: (array, key) => {
|
|
43
|
+
const seen = new Set();
|
|
44
|
+
return array.filter(item => {
|
|
45
|
+
const keyValue = typeof key === 'function' ? key(item) : item[key];
|
|
46
|
+
const isNew = !seen.has(keyValue);
|
|
47
|
+
seen.add(keyValue);
|
|
48
|
+
return isNew;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}), []);
|
|
52
|
+
};
|
|
53
|
+
exports.useConditionalHelpers = useConditionalHelpers;
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-ternary-be-gone",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "A React component designed to simplify conditional rendering and list iteration, providing a more readable and maintainable alternative to ternary operators and verbose conditional logic.",
|
|
5
|
-
"main": "index.js",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
8
|
+
"build": "babel . --out-dir dist"
|
|
8
9
|
},
|
|
9
10
|
"peerDependencies": {
|
|
10
11
|
"react": ">=16.8.0"
|
|
@@ -23,5 +24,9 @@
|
|
|
23
24
|
"bugs": {
|
|
24
25
|
"url": "https://github.com/sundowatch/react-ternary-be-gone/issues"
|
|
25
26
|
},
|
|
26
|
-
"homepage": "https://github.com/sundowatch/react-ternary-be-gone#readme"
|
|
27
|
+
"homepage": "https://github.com/sundowatch/react-ternary-be-gone#readme",
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@babel/cli": "^7.28.0",
|
|
30
|
+
"@babel/preset-react": "^7.27.1"
|
|
31
|
+
}
|
|
27
32
|
}
|