react-hook-core 0.4.6 → 0.4.8

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,48 @@
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;
13
- // limit: number;
14
- page?: number;
15
- total?: number;
16
- pages?: number;
17
- showPaging?: boolean;
18
- append?: boolean;
19
- appendMode?: boolean;
20
- appendable?: boolean;
34
+ initLimit?: number
35
+ limit: number
36
+ page?: number
37
+ total?: number
38
+ pages?: number
39
+ showPaging?: boolean
40
+ append?: boolean
41
+ appendMode?: boolean
42
+ appendable?: boolean
21
43
  }
22
44
 
23
- interface Searchable extends Pagination, Sortable {
24
- }
45
+ export interface Searchable extends Pagination, Sortable {}
25
46
 
26
47
  export function getOffset(limit: number, page?: number, firstLimit?: number): number {
27
48
  const p = page && page > 0 ? page : 1
@@ -65,66 +86,66 @@ export function mergeFilter<S extends Filter>(obj: S, b?: S, pageSizes?: number[
65
86
  }
66
87
  return a
67
88
  }
68
- export function isArray(key: string, p: any, arrs: string[]|any): boolean {
89
+ export function isArray(key: string, p: any, arrs: string[] | any): boolean {
69
90
  if (p) {
70
91
  if (Array.isArray(p)) {
71
- return true;
92
+ return true
72
93
  }
73
94
  }
74
95
  if (arrs) {
75
96
  if (Array.isArray(arrs)) {
76
97
  if (arrs.indexOf(key) >= 0) {
77
- return true;
98
+ return true
78
99
  }
79
100
  } else {
80
- const v = arrs[key];
101
+ const v = arrs[key]
81
102
  if (v && Array.isArray(v)) {
82
- return true;
103
+ return true
83
104
  }
84
105
  }
85
106
  }
86
- return false;
107
+ return false
87
108
  }
88
109
 
89
110
  // m is search model or an object which is parsed from url
90
111
  export function initFilter<S extends Filter>(m: S, com: Searchable): S {
91
112
  if (!isNaN(m.page as any)) {
92
- const page = parseInt(m.page as any, 10);
93
- m.page = page;
113
+ const page = parseInt(m.page as any, 10)
114
+ m.page = page
94
115
  if (page >= 1) {
95
- com.page = page;
116
+ com.page = page
96
117
  }
97
118
  }
98
119
  if (!isNaN(m.limit as any)) {
99
- const pageSize = parseInt(m.limit as any, 10);
100
- m.limit = pageSize;
120
+ const pageSize = parseInt(m.limit as any, 10)
121
+ m.limit = pageSize
101
122
  if (pageSize > 0) {
102
- com.limit = pageSize;
123
+ com.limit = pageSize
103
124
  }
104
125
  }
105
126
  if (!m.limit && com.limit) {
106
- m.limit = com.limit;
127
+ m.limit = com.limit
107
128
  }
108
129
  if (!isNaN(m.firstLimit as any)) {
109
- const initPageSize = parseInt(m.firstLimit as any, 10);
130
+ const initPageSize = parseInt(m.firstLimit as any, 10)
110
131
  if (initPageSize > 0) {
111
- m.firstLimit = initPageSize;
112
- com.initLimit = initPageSize;
132
+ m.firstLimit = initPageSize
133
+ com.initLimit = initPageSize
113
134
  } else {
114
- com.initLimit = com.limit;
135
+ com.initLimit = com.limit
115
136
  }
116
137
  } else {
117
- com.initLimit = com.limit;
138
+ com.initLimit = com.limit
118
139
  }
119
- const st = m.sort;
140
+ const st = m.sort
120
141
  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;
142
+ const ch = st.charAt(0)
143
+ if (ch === "+" || ch === "-") {
144
+ com.sortField = st.substring(1)
145
+ com.sortType = ch
125
146
  } else {
126
- com.sortField = st;
127
- com.sortType = '';
147
+ com.sortField = st
148
+ com.sortType = ""
128
149
  }
129
150
  }
130
151
  /*
@@ -132,125 +153,14 @@ export function initFilter<S extends Filter>(m: S, com: Searchable): S {
132
153
  delete m.limit;
133
154
  delete m.firstLimit;
134
155
  */
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
- }
156
+ return m
205
157
  }
206
158
 
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
159
  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);
160
+ com.total = total
161
+ const pageTotal = getPageTotal(pageSize, total)
162
+ com.pages = pageTotal
163
+ com.showPaging = !total || com.pages <= 1 || (list && list.length >= total) ? false : true
254
164
  }
255
165
 
256
166
  export function getFields(form?: HTMLFormElement, arr?: string[]): string[] | undefined {
@@ -291,75 +201,18 @@ export function getFields(form?: HTMLFormElement, arr?: string[]): string[] | un
291
201
  }
292
202
  return fields.length > 0 ? fields : undefined
293
203
  }
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
204
 
352
205
  export function getPageTotal(pageSize?: number, total?: number): number {
353
206
  if (!pageSize || pageSize <= 0) {
354
- return 1;
207
+ return 1
355
208
  } else {
356
209
  if (!total) {
357
- total = 0;
210
+ total = 0
358
211
  }
359
- if ((total % pageSize) === 0) {
360
- return Math.floor((total / pageSize));
212
+ if (total % pageSize === 0) {
213
+ return Math.floor(total / pageSize)
361
214
  }
362
- return Math.floor((total / pageSize) + 1);
215
+ return Math.floor(total / pageSize + 1)
363
216
  }
364
217
  }
365
218
 
@@ -403,11 +256,10 @@ export function buildMessage<T>(resource: StringMap, results: T[], limit: number
403
256
  }
404
257
 
405
258
  function removeFormatUrl(url: string): string {
406
- const startParams = url.indexOf('?');
407
- return startParams !== -1 ? url.substring(0, startParams) : url;
259
+ const startParams = url.indexOf("?")
260
+ return startParams !== -1 ? url.substring(0, startParams) : url
408
261
  }
409
262
 
410
-
411
263
  function getPrefix(url: string): string {
412
264
  return url.indexOf("?") >= 0 ? "&" : "?"
413
265
  }
@@ -420,7 +272,7 @@ export function addParametersIntoUrl<S extends Filter>(ft: S, isFirstLoad?: bool
420
272
  limit = resources.limit
421
273
  }
422
274
  if (page) {
423
- (ft as any)[resources.page] = page;
275
+ ;(ft as any)[resources.page] = page
424
276
  }
425
277
  const pageIndex = (ft as any)[resources.page]
426
278
  if (pageIndex && !isNaN(pageIndex) && pageIndex <= 1) {
@@ -470,7 +322,7 @@ export function addParametersIntoUrl<S extends Filter>(ft: S, isFirstLoad?: bool
470
322
  url += getPrefix(url) + `${key}.${key2}=${objValueLvl2.toISOString()}`
471
323
  } else {
472
324
  if (typeof objValueLvl2 === "string") {
473
- url += getPrefix(url) + `${key}.${key2}=${encodeURIComponent(objValueLvl2)}`
325
+ url += getPrefix(url) + `${key}.${key2}=${encodeURIComponent(objValueLvl2)}`
474
326
  } else {
475
327
  url += getPrefix(url) + `${key}.${key2}=${objValueLvl2}`
476
328
  }
@@ -496,8 +348,8 @@ export function addParametersIntoUrl<S extends Filter>(ft: S, isFirstLoad?: bool
496
348
  }
497
349
 
498
350
  export interface Sort {
499
- field?: string;
500
- type?: string;
351
+ field?: string
352
+ type?: string
501
353
  }
502
354
  export function buildSort(sort?: string | null): Sort {
503
355
  const sortObj: Sort = {}
@@ -514,9 +366,9 @@ export function buildSort(sort?: string | null): Sort {
514
366
  return sortObj
515
367
  }
516
368
  export function setSort(sortable: Sortable, sort: string | undefined | null) {
517
- const st = buildSort(sort);
518
- sortable.sortField = st.field;
519
- sortable.sortType = st.type;
369
+ const st = buildSort(sort)
370
+ sortable.sortField = st.field
371
+ sortable.sortType = st.type
520
372
  }
521
373
  export function buildSortFilter<S extends Filter>(obj: S, sortable: Sortable): S {
522
374
  const filter: any = clone(obj)
@@ -532,22 +384,22 @@ export function handleToggle(target?: HTMLElement, on?: boolean): boolean {
532
384
  const off = !on
533
385
  if (target) {
534
386
  if (on) {
535
- if (!target.classList.contains('on')) {
536
- target.classList.add('on');
387
+ if (!target.classList.contains("on")) {
388
+ target.classList.add("on")
537
389
  }
538
390
  } else {
539
- target.classList.remove('on');
391
+ target.classList.remove("on")
540
392
  }
541
393
  }
542
394
  return off
543
395
  }
544
396
  export function handleSortEvent(event: Event, com: Sortable): void {
545
397
  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;
398
+ const target = event.target as HTMLElement
399
+ const s = handleSort(target, com.sortTarget, com.sortField, com.sortType)
400
+ com.sortField = s.field
401
+ com.sortType = s.type
402
+ com.sortTarget = target
551
403
  }
552
404
  }
553
405
 
@@ -555,106 +407,69 @@ export function getSortElement(target: HTMLElement): HTMLElement {
555
407
  return target.nodeName === "I" ? (target.parentElement as HTMLElement) : target
556
408
  }
557
409
  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);
410
+ const type = target.getAttribute("sort-type")
411
+ const field = toggleSortStyle(target)
412
+ const s = sort(sortField, sortType, field, type == null ? undefined : type)
561
413
  if (sortField !== field) {
562
- removeSortStatus(previousTarget);
414
+ removeSortStatus(previousTarget)
563
415
  }
564
- return s;
416
+ return s
565
417
  }
566
418
 
567
419
  export function sort(preField?: string, preSortType?: string, field?: string, sortType?: string): Sort {
568
- if (!preField || preField === '') {
420
+ if (!preField || preField === "") {
569
421
  const s: Sort = {
570
422
  field,
571
- type: '+'
572
- };
573
- return s;
423
+ type: "+",
424
+ }
425
+ return s
574
426
  } else if (preField !== field) {
575
427
  const s: Sort = {
576
428
  field,
577
- type: (!sortType ? '+' : sortType)
578
- };
579
- return s;
429
+ type: !sortType ? "+" : sortType,
430
+ }
431
+ return s
580
432
  } else if (preField === field) {
581
- const type = (preSortType === '+' ? '-' : '+');
582
- const s: Sort = {field, type};
583
- return s;
433
+ const type = preSortType === "+" ? "-" : "+"
434
+ const s: Sort = { field, type }
435
+ return s
584
436
  } else {
585
- return {field, type: sortType};
437
+ return { field, type: sortType }
586
438
  }
587
439
  }
588
440
 
589
441
  export function removeSortStatus(target?: HTMLElement): void {
590
442
  if (target && target.children.length > 0) {
591
- target.removeChild(target.children[0]);
443
+ target.removeChild(target.children[0])
592
444
  }
593
445
  }
594
446
 
595
447
  export function toggleSortStyle(target: HTMLElement): string {
596
- let field = target.getAttribute('data-field');
448
+ let field = target.getAttribute("data-field")
597
449
  if (!field) {
598
- const p = target.parentNode as HTMLElement;
450
+ const p = target.parentNode as HTMLElement
599
451
  if (p) {
600
- field = p.getAttribute('data-field');
452
+ field = p.getAttribute("data-field")
601
453
  }
602
454
  }
603
455
  if (!field || field.length === 0) {
604
- return '';
456
+ return ""
605
457
  }
606
- if (target.nodeName === 'I') {
607
- target = target.parentNode as HTMLElement;
458
+ if (target.nodeName === "I") {
459
+ target = target.parentNode as HTMLElement
608
460
  }
609
- let i = null;
461
+ let i = null
610
462
  if (target.children.length === 0) {
611
- target.innerHTML = target.innerHTML + '<i class="sort-up"></i>';
463
+ target.innerHTML = target.innerHTML + '<i class="sort-up"></i>'
612
464
  } 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);
465
+ i = target.children[0]
466
+ if (i.classList.contains("sort-up")) {
467
+ i.classList.remove("sort-up")
468
+ i.classList.add("sort-down")
469
+ } else if (i.classList.contains("sort-down")) {
470
+ i.classList.remove("sort-down")
471
+ i.classList.add("sort-up")
648
472
  }
649
473
  }
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
- }
474
+ return field
660
475
  }