react-hook-core 0.4.6 → 0.4.7

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/src/search.ts CHANGED
@@ -1,27 +1,49 @@
1
- import { Filter, Locale, resources, StringMap } from "./core";
2
- import { clone } from "./reflect";
1
+ import { resources, StringMap } from "./core"
2
+ import { clone } from "./reflect"
3
+
4
+ export interface PageChange {
5
+ page: number // currentPage
6
+ size: number // itemsPerPage
7
+ }
8
+ export interface Filter {
9
+ q?: string
10
+ page?: number
11
+ limit: number
12
+ firstLimit?: number
13
+ fields?: string[]
14
+ sort?: string
15
+ }
16
+ export interface SearchResult<T> {
17
+ total?: number
18
+ list: T[]
19
+ next?: string
20
+ last?: boolean
21
+ }
22
+ export interface SearchService<T, S extends Filter> {
23
+ keys?(): string[]
24
+ search(s: S, limit?: number, offset?: number | string, fields?: string[]): Promise<SearchResult<T>>
25
+ }
3
26
 
4
27
  export interface Sortable {
5
- sortField?: string;
6
- sortType?: string;
7
- sortTarget?: HTMLElement;
28
+ sortField?: string
29
+ sortType?: string
30
+ sortTarget?: HTMLElement
8
31
  }
9
32
 
10
33
  export interface Pagination {
11
- initLimit?: number;
12
- limit: number;
34
+ initLimit?: number
35
+ limit: number
13
36
  // limit: number;
14
- page?: number;
15
- total?: number;
16
- pages?: number;
17
- showPaging?: boolean;
18
- append?: boolean;
19
- appendMode?: boolean;
20
- appendable?: boolean;
37
+ page?: number
38
+ total?: number
39
+ pages?: number
40
+ showPaging?: boolean
41
+ append?: boolean
42
+ appendMode?: boolean
43
+ appendable?: boolean
21
44
  }
22
45
 
23
- interface Searchable extends Pagination, Sortable {
24
- }
46
+ interface Searchable extends Pagination, Sortable {}
25
47
 
26
48
  export function getOffset(limit: number, page?: number, firstLimit?: number): number {
27
49
  const p = page && page > 0 ? page : 1
@@ -65,66 +87,66 @@ export function mergeFilter<S extends Filter>(obj: S, b?: S, pageSizes?: number[
65
87
  }
66
88
  return a
67
89
  }
68
- export function isArray(key: string, p: any, arrs: string[]|any): boolean {
90
+ export function isArray(key: string, p: any, arrs: string[] | any): boolean {
69
91
  if (p) {
70
92
  if (Array.isArray(p)) {
71
- return true;
93
+ return true
72
94
  }
73
95
  }
74
96
  if (arrs) {
75
97
  if (Array.isArray(arrs)) {
76
98
  if (arrs.indexOf(key) >= 0) {
77
- return true;
99
+ return true
78
100
  }
79
101
  } else {
80
- const v = arrs[key];
102
+ const v = arrs[key]
81
103
  if (v && Array.isArray(v)) {
82
- return true;
104
+ return true
83
105
  }
84
106
  }
85
107
  }
86
- return false;
108
+ return false
87
109
  }
88
110
 
89
111
  // m is search model or an object which is parsed from url
90
112
  export function initFilter<S extends Filter>(m: S, com: Searchable): S {
91
113
  if (!isNaN(m.page as any)) {
92
- const page = parseInt(m.page as any, 10);
93
- m.page = page;
114
+ const page = parseInt(m.page as any, 10)
115
+ m.page = page
94
116
  if (page >= 1) {
95
- com.page = page;
117
+ com.page = page
96
118
  }
97
119
  }
98
120
  if (!isNaN(m.limit as any)) {
99
- const pageSize = parseInt(m.limit as any, 10);
100
- m.limit = pageSize;
121
+ const pageSize = parseInt(m.limit as any, 10)
122
+ m.limit = pageSize
101
123
  if (pageSize > 0) {
102
- com.limit = pageSize;
124
+ com.limit = pageSize
103
125
  }
104
126
  }
105
127
  if (!m.limit && com.limit) {
106
- m.limit = com.limit;
128
+ m.limit = com.limit
107
129
  }
108
130
  if (!isNaN(m.firstLimit as any)) {
109
- const initPageSize = parseInt(m.firstLimit as any, 10);
131
+ const initPageSize = parseInt(m.firstLimit as any, 10)
110
132
  if (initPageSize > 0) {
111
- m.firstLimit = initPageSize;
112
- com.initLimit = initPageSize;
133
+ m.firstLimit = initPageSize
134
+ com.initLimit = initPageSize
113
135
  } else {
114
- com.initLimit = com.limit;
136
+ com.initLimit = com.limit
115
137
  }
116
138
  } else {
117
- com.initLimit = com.limit;
139
+ com.initLimit = com.limit
118
140
  }
119
- const st = m.sort;
141
+ const st = m.sort
120
142
  if (st && st.length > 0) {
121
- const ch = st.charAt(0);
122
- if (ch === '+' || ch === '-') {
123
- com.sortField = st.substring(1);
124
- com.sortType = ch;
143
+ const ch = st.charAt(0)
144
+ if (ch === "+" || ch === "-") {
145
+ com.sortField = st.substring(1)
146
+ com.sortType = ch
125
147
  } else {
126
- com.sortField = st;
127
- com.sortType = '';
148
+ com.sortField = st
149
+ com.sortType = ""
128
150
  }
129
151
  }
130
152
  /*
@@ -132,125 +154,14 @@ export function initFilter<S extends Filter>(m: S, com: Searchable): S {
132
154
  delete m.limit;
133
155
  delete m.firstLimit;
134
156
  */
135
- return m;
136
- }
137
- export function more(com: Pagination): void {
138
- com.append = true;
139
- if (!com.page) {
140
- com.page = 1;
141
- } else {
142
- com.page = com.page + 1;
143
- }
144
- }
145
-
146
- export function reset(com: Searchable): void {
147
- removeSortStatus(com.sortTarget);
148
- com.sortTarget = undefined;
149
- com.sortField = undefined;
150
- com.append = false;
151
- com.page = 1;
152
- }
153
- export function changePageSize(com: Pagination, size: number): void {
154
- com.initLimit = size;
155
- com.limit = size;
156
- com.page = 1;
157
- }
158
- export function changePage(com: Pagination, pageIndex: number, pageSize: number): void {
159
- com.page = pageIndex;
160
- com.limit = pageSize;
161
- com.append = false;
162
- }
163
- export function optimizeFilter<S extends Filter>(obj: S, searchable: Searchable, fields?: string[]): S {
164
- // const sLimit = searchable.limit;
165
- obj.fields = fields;
166
- if (searchable.page && searchable.page > 1) {
167
- obj.page = searchable.page;
168
- } else {
169
- delete obj.page;
170
- }
171
- obj.limit = searchable.limit;
172
-
173
- if (searchable.appendMode && searchable.initLimit !== searchable.limit) {
174
- obj.firstLimit = searchable.initLimit;
175
- } else {
176
- delete obj.firstLimit;
177
- }
178
- if (searchable.sortField && searchable.sortField.length > 0) {
179
- obj.sort = (searchable.sortType === '-' ? '-' + searchable.sortField : searchable.sortField);
180
- } else {
181
- delete obj.sort;
182
- }
183
- if(searchable) {
184
- mapObjects(obj, searchable as any);
185
- }
186
- return obj;
187
- }
188
-
189
- function mapObjects(dest: any, src: any): void {
190
- for (let key in dest) {
191
- if (src.hasOwnProperty(key) && src[key] !== null && src[key] !== undefined) {
192
- if(Array.isArray(dest[key]) && typeof src[key] === 'string' && src[key].length > 0) {
193
- const arrayObjKeySrc = src[key].length > 0 ? (src[key])?.split(',') : [];
194
- if(arrayObjKeySrc && arrayObjKeySrc.length > 1) {
195
- dest[key] = [...arrayObjKeySrc];
196
- } else {
197
- dest[key] = [];
198
- dest[key].push(src[key])
199
- }
200
- } else {
201
- dest[key] = src[key];
202
- }
203
- }
204
- }
157
+ return m
205
158
  }
206
159
 
207
- export function append<T>(list?: T[], results?: T[]): T[] {
208
- if (list && results) {
209
- for (const obj of results) {
210
- list.push(obj);
211
- }
212
- }
213
- if (!list) {
214
- return [];
215
- }
216
- return list;
217
- }
218
- /*
219
- export function showResults<T>(com: Pagination, s: Filter, list: T[], total?: number, nextPageToken?: string): void {
220
- com.pageIndex = (s.page && s.page >= 1 ? s.page : 1);
221
- if (total) {
222
- com.itemTotal = total;
223
- }
224
- if (com.appendMode) {
225
- let limit = s.limit;
226
- if (s.page <= 1 && s.firstLimit && s.firstLimit > 0) {
227
- limit = s.firstLimit;
228
- }
229
- handleAppend(com, limit, list, nextPageToken);
230
- } else {
231
- showPaging(com, s.limit, list, total);
232
- }
233
- }
234
- */
235
- export function handleAppend<T>(com: Pagination, list: T[], limit?: number, nextPageToken?: string): void {
236
- if (!limit || limit === 0) {
237
- com.appendable = false;
238
- } else {
239
- if (!nextPageToken || nextPageToken.length === 0 || list.length < limit) {
240
- com.appendable = false;
241
- } else {
242
- com.appendable = true;
243
- }
244
- }
245
- if (!list || list.length === 0) {
246
- com.appendable = false;
247
- }
248
- }
249
160
  export function showPaging<T>(com: Pagination, list: T[], pageSize?: number, total?: number): void {
250
- com.total = total;
251
- const pageTotal = getPageTotal(pageSize, total);
252
- com.pages = pageTotal;
253
- com.showPaging = (!total || com.pages <= 1 || (list && list.length >= total) ? false : true);
161
+ com.total = total
162
+ const pageTotal = getPageTotal(pageSize, total)
163
+ com.pages = pageTotal
164
+ com.showPaging = !total || com.pages <= 1 || (list && list.length >= total) ? false : true
254
165
  }
255
166
 
256
167
  export function getFields(form?: HTMLFormElement, arr?: string[]): string[] | undefined {
@@ -291,75 +202,18 @@ export function getFields(form?: HTMLFormElement, arr?: string[]): string[] | un
291
202
  }
292
203
  return fields.length > 0 ? fields : undefined
293
204
  }
294
- interface Component<T> {
295
- page?: number;
296
- limit?: number;
297
- sequenceNo?: string;
298
- format?: (oj: T, lc?: Locale) => T;
299
- }
300
- export function formatResultsByComponent<T>(results: T[], c: Component<T>, lc: Locale) {
301
- formatResults(results, c.page, c.limit, c.limit, c.sequenceNo, c.format, lc);
302
- }
303
- export function formatResults<T>(results: T[], page?: number, limit?: number, initPageSize?: number, sequenceNo?: string, ft?: (oj: T, lc?: Locale) => T, lc?: Locale): void {
304
- if (results && results.length > 0) {
305
- let hasSequencePro = false;
306
- if (ft) {
307
- if (sequenceNo && sequenceNo.length > 0) {
308
- for (const obj of results) {
309
- if ((obj as any)[sequenceNo]) {
310
- hasSequencePro = true;
311
- }
312
- ft(obj, lc);
313
- }
314
- } else {
315
- for (const obj of results) {
316
- ft(obj, lc);
317
- }
318
- }
319
- } else if (sequenceNo && sequenceNo.length > 0) {
320
- for (const obj of results) {
321
- if ((obj as any)[sequenceNo]) {
322
- hasSequencePro = true;
323
- }
324
- }
325
- }
326
- if (sequenceNo && sequenceNo.length > 0 && !hasSequencePro) {
327
- if (!page) {
328
- page = 1;
329
- }
330
- if (limit) {
331
- if (!initPageSize) {
332
- initPageSize = limit;
333
- }
334
- if (page <= 1) {
335
- for (let i = 0; i < results.length; i++) {
336
- (results[i] as any)[sequenceNo] = i - limit + limit * page + 1;
337
- }
338
- } else {
339
- for (let i = 0; i < results.length; i++) {
340
- (results[i] as any)[sequenceNo] = i - limit + limit * page + 1 - (limit - initPageSize);
341
- }
342
- }
343
- } else {
344
- for (let i = 0; i < results.length; i++) {
345
- (results[i] as any)[sequenceNo] = i + 1;
346
- }
347
- }
348
- }
349
- }
350
- }
351
205
 
352
206
  export function getPageTotal(pageSize?: number, total?: number): number {
353
207
  if (!pageSize || pageSize <= 0) {
354
- return 1;
208
+ return 1
355
209
  } else {
356
210
  if (!total) {
357
- total = 0;
211
+ total = 0
358
212
  }
359
- if ((total % pageSize) === 0) {
360
- return Math.floor((total / pageSize));
213
+ if (total % pageSize === 0) {
214
+ return Math.floor(total / pageSize)
361
215
  }
362
- return Math.floor((total / pageSize) + 1);
216
+ return Math.floor(total / pageSize + 1)
363
217
  }
364
218
  }
365
219
 
@@ -403,11 +257,10 @@ export function buildMessage<T>(resource: StringMap, results: T[], limit: number
403
257
  }
404
258
 
405
259
  function removeFormatUrl(url: string): string {
406
- const startParams = url.indexOf('?');
407
- return startParams !== -1 ? url.substring(0, startParams) : url;
260
+ const startParams = url.indexOf("?")
261
+ return startParams !== -1 ? url.substring(0, startParams) : url
408
262
  }
409
263
 
410
-
411
264
  function getPrefix(url: string): string {
412
265
  return url.indexOf("?") >= 0 ? "&" : "?"
413
266
  }
@@ -420,7 +273,7 @@ export function addParametersIntoUrl<S extends Filter>(ft: S, isFirstLoad?: bool
420
273
  limit = resources.limit
421
274
  }
422
275
  if (page) {
423
- (ft as any)[resources.page] = page;
276
+ ;(ft as any)[resources.page] = page
424
277
  }
425
278
  const pageIndex = (ft as any)[resources.page]
426
279
  if (pageIndex && !isNaN(pageIndex) && pageIndex <= 1) {
@@ -470,7 +323,7 @@ export function addParametersIntoUrl<S extends Filter>(ft: S, isFirstLoad?: bool
470
323
  url += getPrefix(url) + `${key}.${key2}=${objValueLvl2.toISOString()}`
471
324
  } else {
472
325
  if (typeof objValueLvl2 === "string") {
473
- url += getPrefix(url) + `${key}.${key2}=${encodeURIComponent(objValueLvl2)}`
326
+ url += getPrefix(url) + `${key}.${key2}=${encodeURIComponent(objValueLvl2)}`
474
327
  } else {
475
328
  url += getPrefix(url) + `${key}.${key2}=${objValueLvl2}`
476
329
  }
@@ -496,8 +349,8 @@ export function addParametersIntoUrl<S extends Filter>(ft: S, isFirstLoad?: bool
496
349
  }
497
350
 
498
351
  export interface Sort {
499
- field?: string;
500
- type?: string;
352
+ field?: string
353
+ type?: string
501
354
  }
502
355
  export function buildSort(sort?: string | null): Sort {
503
356
  const sortObj: Sort = {}
@@ -514,9 +367,9 @@ export function buildSort(sort?: string | null): Sort {
514
367
  return sortObj
515
368
  }
516
369
  export function setSort(sortable: Sortable, sort: string | undefined | null) {
517
- const st = buildSort(sort);
518
- sortable.sortField = st.field;
519
- sortable.sortType = st.type;
370
+ const st = buildSort(sort)
371
+ sortable.sortField = st.field
372
+ sortable.sortType = st.type
520
373
  }
521
374
  export function buildSortFilter<S extends Filter>(obj: S, sortable: Sortable): S {
522
375
  const filter: any = clone(obj)
@@ -532,22 +385,22 @@ export function handleToggle(target?: HTMLElement, on?: boolean): boolean {
532
385
  const off = !on
533
386
  if (target) {
534
387
  if (on) {
535
- if (!target.classList.contains('on')) {
536
- target.classList.add('on');
388
+ if (!target.classList.contains("on")) {
389
+ target.classList.add("on")
537
390
  }
538
391
  } else {
539
- target.classList.remove('on');
392
+ target.classList.remove("on")
540
393
  }
541
394
  }
542
395
  return off
543
396
  }
544
397
  export function handleSortEvent(event: Event, com: Sortable): void {
545
398
  if (event && event.target) {
546
- const target = event.target as HTMLElement;
547
- const s = handleSort(target, com.sortTarget, com.sortField, com.sortType);
548
- com.sortField = s.field;
549
- com.sortType = s.type;
550
- com.sortTarget = target;
399
+ const target = event.target as HTMLElement
400
+ const s = handleSort(target, com.sortTarget, com.sortField, com.sortType)
401
+ com.sortField = s.field
402
+ com.sortType = s.type
403
+ com.sortTarget = target
551
404
  }
552
405
  }
553
406
 
@@ -555,106 +408,69 @@ export function getSortElement(target: HTMLElement): HTMLElement {
555
408
  return target.nodeName === "I" ? (target.parentElement as HTMLElement) : target
556
409
  }
557
410
  export function handleSort(target: HTMLElement, previousTarget?: HTMLElement, sortField?: string, sortType?: string): Sort {
558
- const type = target.getAttribute('sort-type');
559
- const field = toggleSortStyle(target);
560
- const s = sort(sortField, sortType, field, type == null ? undefined : type);
411
+ const type = target.getAttribute("sort-type")
412
+ const field = toggleSortStyle(target)
413
+ const s = sort(sortField, sortType, field, type == null ? undefined : type)
561
414
  if (sortField !== field) {
562
- removeSortStatus(previousTarget);
415
+ removeSortStatus(previousTarget)
563
416
  }
564
- return s;
417
+ return s
565
418
  }
566
419
 
567
420
  export function sort(preField?: string, preSortType?: string, field?: string, sortType?: string): Sort {
568
- if (!preField || preField === '') {
421
+ if (!preField || preField === "") {
569
422
  const s: Sort = {
570
423
  field,
571
- type: '+'
572
- };
573
- return s;
424
+ type: "+",
425
+ }
426
+ return s
574
427
  } else if (preField !== field) {
575
428
  const s: Sort = {
576
429
  field,
577
- type: (!sortType ? '+' : sortType)
578
- };
579
- return s;
430
+ type: !sortType ? "+" : sortType,
431
+ }
432
+ return s
580
433
  } else if (preField === field) {
581
- const type = (preSortType === '+' ? '-' : '+');
582
- const s: Sort = {field, type};
583
- return s;
434
+ const type = preSortType === "+" ? "-" : "+"
435
+ const s: Sort = { field, type }
436
+ return s
584
437
  } else {
585
- return {field, type: sortType};
438
+ return { field, type: sortType }
586
439
  }
587
440
  }
588
441
 
589
442
  export function removeSortStatus(target?: HTMLElement): void {
590
443
  if (target && target.children.length > 0) {
591
- target.removeChild(target.children[0]);
444
+ target.removeChild(target.children[0])
592
445
  }
593
446
  }
594
447
 
595
448
  export function toggleSortStyle(target: HTMLElement): string {
596
- let field = target.getAttribute('data-field');
449
+ let field = target.getAttribute("data-field")
597
450
  if (!field) {
598
- const p = target.parentNode as HTMLElement;
451
+ const p = target.parentNode as HTMLElement
599
452
  if (p) {
600
- field = p.getAttribute('data-field');
453
+ field = p.getAttribute("data-field")
601
454
  }
602
455
  }
603
456
  if (!field || field.length === 0) {
604
- return '';
457
+ return ""
605
458
  }
606
- if (target.nodeName === 'I') {
607
- target = target.parentNode as HTMLElement;
459
+ if (target.nodeName === "I") {
460
+ target = target.parentNode as HTMLElement
608
461
  }
609
- let i = null;
462
+ let i = null
610
463
  if (target.children.length === 0) {
611
- target.innerHTML = target.innerHTML + '<i class="sort-up"></i>';
464
+ target.innerHTML = target.innerHTML + '<i class="sort-up"></i>'
612
465
  } else {
613
- i = target.children[0];
614
- if (i.classList.contains('sort-up')) {
615
- i.classList.remove('sort-up');
616
- i.classList.add('sort-down');
617
- } else if (i.classList.contains('sort-down')) {
618
- i.classList.remove('sort-down');
619
- i.classList.add('sort-up');
620
- }
621
- }
622
- return field;
623
- }
624
- export function getModel<S extends Filter>(state: any, modelName: string, searchable: Searchable, fields?: string[], excluding?: string[]|number[]): S {
625
- let obj2 = getModelFromState(state, modelName);
626
-
627
- const obj: any = obj2 ? obj2 : {};
628
- const obj3 = optimizeFilter(obj, searchable, fields);
629
- obj3.excluding = excluding;
630
- return obj3;
631
- }
632
- function getModelFromState(state: any, modelName: string): any {
633
- if (!modelName || modelName.length === 0) {
634
- return state;
635
- }
636
- if (!state) {
637
- return state;
638
- }
639
- return state[modelName];
640
- }
641
- export function getFieldsFromForm(fields?: string[], initFields?: boolean, form?: HTMLFormElement|null): string[]|undefined {
642
- if (fields && fields.length > 0) {
643
- return fields;
644
- }
645
- if (!initFields) {
646
- if (form) {
647
- return getFields(form);
466
+ i = target.children[0]
467
+ if (i.classList.contains("sort-up")) {
468
+ i.classList.remove("sort-up")
469
+ i.classList.add("sort-down")
470
+ } else if (i.classList.contains("sort-down")) {
471
+ i.classList.remove("sort-down")
472
+ i.classList.add("sort-up")
648
473
  }
649
474
  }
650
- return fields;
651
- }
652
- export function validate<S extends Filter>(se: S, callback: () => void, form?: HTMLFormElement|null, lc?: Locale, vf?: (f: HTMLFormElement, lc2?: Locale, focus?: boolean, scr?: boolean) => boolean): void {
653
- let valid = true;
654
- if (form && vf) {
655
- valid = vf(form, lc);
656
- }
657
- if (valid === true) {
658
- callback();
659
- }
475
+ return field
660
476
  }