@ram_28/kf-ai-sdk 1.0.14 → 1.0.15

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.
@@ -203,43 +203,33 @@ export function useFilter(options: UseFilterOptionsType = {}): UseFilterReturnTy
203
203
  // ADD OPERATIONS
204
204
  // ============================================================
205
205
 
206
- const add = useCallback((condition: Omit<ConditionType, "id">): string => {
207
- const id = generateId();
208
- const newCondition: ConditionType = { ...condition, id };
209
- setItems((prev) => [...prev, newCondition]);
210
- return id;
211
- }, []);
212
-
213
- const addGroup = useCallback((groupOperator: ConditionGroupOperatorType): string => {
214
- const id = generateId();
215
- const newGroup: ConditionGroupType = {
216
- id,
217
- Operator: groupOperator,
218
- Condition: [],
219
- };
220
- setItems((prev) => [...prev, newGroup]);
221
- return id;
222
- }, []);
223
-
224
- const addTo = useCallback(
225
- (parentId: string, condition: Omit<ConditionType, "id">): string => {
206
+ const addCondition = useCallback(
207
+ (condition: Omit<ConditionType, "id">, parentId?: string): string => {
226
208
  const id = generateId();
227
209
  const newCondition: ConditionType = { ...condition, id };
228
- setItems((prev) => addToParent(prev, parentId, newCondition));
210
+ if (parentId) {
211
+ setItems((prev) => addToParent(prev, parentId, newCondition));
212
+ } else {
213
+ setItems((prev) => [...prev, newCondition]);
214
+ }
229
215
  return id;
230
216
  },
231
217
  []
232
218
  );
233
219
 
234
- const addGroupTo = useCallback(
235
- (parentId: string, groupOperator: ConditionGroupOperatorType): string => {
220
+ const addConditionGroup = useCallback(
221
+ (groupOperator: ConditionGroupOperatorType, parentId?: string): string => {
236
222
  const id = generateId();
237
223
  const newGroup: ConditionGroupType = {
238
224
  id,
239
225
  Operator: groupOperator,
240
226
  Condition: [],
241
227
  };
242
- setItems((prev) => addToParent(prev, parentId, newGroup));
228
+ if (parentId) {
229
+ setItems((prev) => addToParent(prev, parentId, newGroup));
230
+ } else {
231
+ setItems((prev) => [...prev, newGroup]);
232
+ }
243
233
  return id;
244
234
  },
245
235
  []
@@ -249,7 +239,7 @@ export function useFilter(options: UseFilterOptionsType = {}): UseFilterReturnTy
249
239
  // UPDATE OPERATIONS
250
240
  // ============================================================
251
241
 
252
- const update = useCallback(
242
+ const updateCondition = useCallback(
253
243
  (id: string, updates: Partial<Omit<ConditionType, "id">>): void => {
254
244
  setItems((prev) =>
255
245
  updateInTree(prev, id, (item) => {
@@ -263,7 +253,7 @@ export function useFilter(options: UseFilterOptionsType = {}): UseFilterReturnTy
263
253
  []
264
254
  );
265
255
 
266
- const updateOperator = useCallback(
256
+ const updateGroupOperator = useCallback(
267
257
  (id: string, newOperator: ConditionGroupOperatorType): void => {
268
258
  setItems((prev) =>
269
259
  updateInTree(prev, id, (item) => {
@@ -281,11 +271,11 @@ export function useFilter(options: UseFilterOptionsType = {}): UseFilterReturnTy
281
271
  // REMOVE & ACCESS
282
272
  // ============================================================
283
273
 
284
- const remove = useCallback((id: string): void => {
274
+ const removeCondition = useCallback((id: string): void => {
285
275
  setItems((prev) => removeFromTree(prev, id));
286
276
  }, []);
287
277
 
288
- const get = useCallback(
278
+ const getCondition = useCallback(
289
279
  (id: string): ConditionType | ConditionGroupType | undefined => {
290
280
  return findById(items, id);
291
281
  },
@@ -296,11 +286,11 @@ export function useFilter(options: UseFilterOptionsType = {}): UseFilterReturnTy
296
286
  // UTILITY
297
287
  // ============================================================
298
288
 
299
- const clear = useCallback((): void => {
289
+ const clearAllConditions = useCallback((): void => {
300
290
  setItems([]);
301
291
  }, []);
302
292
 
303
- const setOperator = useCallback((op: ConditionGroupOperatorType): void => {
293
+ const setRootOperator = useCallback((op: ConditionGroupOperatorType): void => {
304
294
  setOperatorState(op);
305
295
  }, []);
306
296
 
@@ -316,21 +306,19 @@ export function useFilter(options: UseFilterOptionsType = {}): UseFilterReturnTy
316
306
  hasConditions,
317
307
 
318
308
  // Add operations
319
- add,
320
- addGroup,
321
- addTo,
322
- addGroupTo,
309
+ addCondition,
310
+ addConditionGroup,
323
311
 
324
312
  // Update operations
325
- update,
326
- updateOperator,
313
+ updateCondition,
314
+ updateGroupOperator,
327
315
 
328
316
  // Remove & access
329
- remove,
330
- get,
317
+ removeCondition,
318
+ getCondition,
331
319
 
332
320
  // Utility
333
- clear,
334
- setOperator,
321
+ clearAllConditions,
322
+ setRootOperator,
335
323
  };
336
324
  }
@@ -1,129 +0,0 @@
1
- import { useState as I, useMemo as B, useCallback as u } from "react";
2
- const c = (t) => "Condition" in t, M = (t) => "LHSField" in t;
3
- let D = 0;
4
- const a = () => `filter_${Date.now()}_${++D}`, H = (t) => t.id ? t : { ...t, id: a() }, h = (t) => t.map((o) => {
5
- const n = H(o);
6
- return c(n) ? {
7
- ...n,
8
- Condition: h(n.Condition)
9
- } : n;
10
- }), w = (t) => t.map((o) => {
11
- if (c(o)) {
12
- const { id: O, ...C } = o;
13
- return {
14
- ...C,
15
- Condition: w(o.Condition)
16
- };
17
- }
18
- const { id: n, ...r } = o;
19
- return r;
20
- }), G = (t, o) => {
21
- for (const n of t) {
22
- if (n.id === o)
23
- return n;
24
- if (c(n)) {
25
- const r = G(n.Condition, o);
26
- if (r) return r;
27
- }
28
- }
29
- }, f = (t, o, n) => t.map((r) => r.id === o ? n(r) : c(r) ? {
30
- ...r,
31
- Condition: f(r.Condition, o, n)
32
- } : r), T = (t, o) => t.filter((n) => n.id !== o).map((n) => c(n) ? {
33
- ...n,
34
- Condition: T(n.Condition, o)
35
- } : n), l = (t, o, n) => t.map((r) => r.id === o && c(r) ? {
36
- ...r,
37
- Condition: [...r.Condition, n]
38
- } : c(r) ? {
39
- ...r,
40
- Condition: l(r.Condition, o, n)
41
- } : r);
42
- function P(t = {}) {
43
- const [o, n] = I(
44
- () => h(t.initialConditions || [])
45
- ), [r, O] = I(
46
- t.initialOperator || "And"
47
- ), C = B(() => {
48
- if (o.length !== 0)
49
- return {
50
- Operator: r,
51
- Condition: w(o)
52
- };
53
- }, [o, r]), g = o.length > 0, v = u((i) => {
54
- const e = a(), d = { ...i, id: e };
55
- return n((s) => [...s, d]), e;
56
- }, []), F = u((i) => {
57
- const e = a(), d = {
58
- id: e,
59
- Operator: i,
60
- Condition: []
61
- };
62
- return n((s) => [...s, d]), e;
63
- }, []), S = u(
64
- (i, e) => {
65
- const d = a(), s = { ...e, id: d };
66
- return n((p) => l(p, i, s)), d;
67
- },
68
- []
69
- ), y = u(
70
- (i, e) => {
71
- const d = a(), s = {
72
- id: d,
73
- Operator: e,
74
- Condition: []
75
- };
76
- return n((p) => l(p, i, s)), d;
77
- },
78
- []
79
- ), _ = u(
80
- (i, e) => {
81
- n(
82
- (d) => f(d, i, (s) => c(s) ? s : { ...s, ...e })
83
- );
84
- },
85
- []
86
- ), $ = u(
87
- (i, e) => {
88
- n(
89
- (d) => f(d, i, (s) => c(s) ? { ...s, Operator: e } : s)
90
- );
91
- },
92
- []
93
- ), b = u((i) => {
94
- n((e) => T(e, i));
95
- }, []), k = u(
96
- (i) => G(o, i),
97
- [o]
98
- ), x = u(() => {
99
- n([]);
100
- }, []), A = u((i) => {
101
- O(i);
102
- }, []);
103
- return {
104
- // State
105
- operator: r,
106
- items: o,
107
- payload: C,
108
- hasConditions: g,
109
- // Add operations
110
- add: v,
111
- addGroup: F,
112
- addTo: S,
113
- addGroupTo: y,
114
- // Update operations
115
- update: _,
116
- updateOperator: $,
117
- // Remove & access
118
- remove: b,
119
- get: k,
120
- // Utility
121
- clear: x,
122
- setOperator: A
123
- };
124
- }
125
- export {
126
- c as a,
127
- M as i,
128
- P as u
129
- };
@@ -1 +0,0 @@
1
- "use strict";const d=require("react"),a=r=>"Condition"in r,A=r=>"LHSField"in r;let B=0;const c=()=>`filter_${Date.now()}_${++B}`,D=r=>r.id?r:{...r,id:c()},k=r=>r.map(o=>{const n=D(o);return a(n)?{...n,Condition:k(n.Condition)}:n}),O=r=>r.map(o=>{if(a(o)){const{id:b,...C}=o;return{...C,Condition:O(o.Condition)}}const{id:n,...t}=o;return t}),G=(r,o)=>{for(const n of r){if(n.id===o)return n;if(a(n)){const t=G(n.Condition,o);if(t)return t}}},p=(r,o,n)=>r.map(t=>t.id===o?n(t):a(t)?{...t,Condition:p(t.Condition,o,n)}:t),I=(r,o)=>r.filter(n=>n.id!==o).map(n=>a(n)?{...n,Condition:I(n.Condition,o)}:n),f=(r,o,n)=>r.map(t=>t.id===o&&a(t)?{...t,Condition:[...t.Condition,n]}:a(t)?{...t,Condition:f(t.Condition,o,n)}:t);function H(r={}){const[o,n]=d.useState(()=>k(r.initialConditions||[])),[t,b]=d.useState(r.initialOperator||"And"),C=d.useMemo(()=>{if(o.length!==0)return{Operator:t,Condition:O(o)}},[o,t]),h=o.length>0,w=d.useCallback(e=>{const i=c(),u={...e,id:i};return n(s=>[...s,u]),i},[]),T=d.useCallback(e=>{const i=c(),u={id:i,Operator:e,Condition:[]};return n(s=>[...s,u]),i},[]),g=d.useCallback((e,i)=>{const u=c(),s={...i,id:u};return n(l=>f(l,e,s)),u},[]),v=d.useCallback((e,i)=>{const u=c(),s={id:u,Operator:i,Condition:[]};return n(l=>f(l,e,s)),u},[]),F=d.useCallback((e,i)=>{n(u=>p(u,e,s=>a(s)?s:{...s,...i}))},[]),S=d.useCallback((e,i)=>{n(u=>p(u,e,s=>a(s)?{...s,Operator:i}:s))},[]),y=d.useCallback(e=>{n(i=>I(i,e))},[]),_=d.useCallback(e=>G(o,e),[o]),$=d.useCallback(()=>{n([])},[]),q=d.useCallback(e=>{b(e)},[]);return{operator:t,items:o,payload:C,hasConditions:h,add:w,addGroup:T,addTo:g,addGroupTo:v,update:F,updateOperator:S,remove:y,get:_,clear:$,setOperator:q}}exports.isCondition=A;exports.isConditionGroup=a;exports.useFilter=H;