react-hook-core 0.4.5 → 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,28 +257,25 @@ 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
  }
414
267
  export function addParametersIntoUrl<S extends Filter>(ft: S, isFirstLoad?: boolean, page?: number, fields?: string, limit?: string): void {
415
268
  if (!isFirstLoad) {
416
269
  if (!fields || fields.length === 0) {
417
- fields = "fields"
270
+ fields = resources.fields
418
271
  }
419
272
  if (!limit || limit.length === 0) {
420
- limit = "limit"
273
+ limit = resources.limit
421
274
  }
422
- if (page && page > 1) {
423
- if (!ft.page || ft.page <= 1) {
424
- ft.page = page
425
- }
275
+ if (page) {
276
+ ;(ft as any)[resources.page] = page
426
277
  }
427
- const pageIndex = ft.page
278
+ const pageIndex = (ft as any)[resources.page]
428
279
  if (pageIndex && !isNaN(pageIndex) && pageIndex <= 1) {
429
280
  delete ft.page
430
281
  }
@@ -437,12 +288,12 @@ export function addParametersIntoUrl<S extends Filter>(ft: S, isFirstLoad?: bool
437
288
  if (key !== fields) {
438
289
  if (typeof objValue === "string" || typeof objValue === "number") {
439
290
  if (key === limit) {
440
- if (objValue !== resources.limit) {
291
+ if (objValue !== resources.defaultLimit) {
441
292
  url += getPrefix(url) + `${key}=${objValue}`
442
293
  }
443
294
  } else {
444
295
  if (typeof objValue === "string") {
445
- url += getPrefix(url) + `${key}=${encodeURI(objValue)}`
296
+ url += getPrefix(url) + `${key}=${encodeURIComponent(objValue)}`
446
297
  } else {
447
298
  url += getPrefix(url) + `${key}=${objValue}`
448
299
  }
@@ -456,7 +307,7 @@ export function addParametersIntoUrl<S extends Filter>(ft: S, isFirstLoad?: bool
456
307
  const strs: string[] = []
457
308
  for (const subValue of objValue) {
458
309
  if (typeof subValue === "string") {
459
- strs.push(encodeURI(subValue))
310
+ strs.push(encodeURIComponent(subValue))
460
311
  } else if (typeof subValue === "number") {
461
312
  strs.push(subValue.toString())
462
313
  }
@@ -472,7 +323,7 @@ export function addParametersIntoUrl<S extends Filter>(ft: S, isFirstLoad?: bool
472
323
  url += getPrefix(url) + `${key}.${key2}=${objValueLvl2.toISOString()}`
473
324
  } else {
474
325
  if (typeof objValueLvl2 === "string") {
475
- url += getPrefix(url) + `${key}.${key2}=${encodeURI(objValueLvl2)}`
326
+ url += getPrefix(url) + `${key}.${key2}=${encodeURIComponent(objValueLvl2)}`
476
327
  } else {
477
328
  url += getPrefix(url) + `${key}.${key2}=${objValueLvl2}`
478
329
  }
@@ -498,8 +349,8 @@ export function addParametersIntoUrl<S extends Filter>(ft: S, isFirstLoad?: bool
498
349
  }
499
350
 
500
351
  export interface Sort {
501
- field?: string;
502
- type?: string;
352
+ field?: string
353
+ type?: string
503
354
  }
504
355
  export function buildSort(sort?: string | null): Sort {
505
356
  const sortObj: Sort = {}
@@ -516,9 +367,9 @@ export function buildSort(sort?: string | null): Sort {
516
367
  return sortObj
517
368
  }
518
369
  export function setSort(sortable: Sortable, sort: string | undefined | null) {
519
- const st = buildSort(sort);
520
- sortable.sortField = st.field;
521
- sortable.sortType = st.type;
370
+ const st = buildSort(sort)
371
+ sortable.sortField = st.field
372
+ sortable.sortType = st.type
522
373
  }
523
374
  export function buildSortFilter<S extends Filter>(obj: S, sortable: Sortable): S {
524
375
  const filter: any = clone(obj)
@@ -534,22 +385,22 @@ export function handleToggle(target?: HTMLElement, on?: boolean): boolean {
534
385
  const off = !on
535
386
  if (target) {
536
387
  if (on) {
537
- if (!target.classList.contains('on')) {
538
- target.classList.add('on');
388
+ if (!target.classList.contains("on")) {
389
+ target.classList.add("on")
539
390
  }
540
391
  } else {
541
- target.classList.remove('on');
392
+ target.classList.remove("on")
542
393
  }
543
394
  }
544
395
  return off
545
396
  }
546
397
  export function handleSortEvent(event: Event, com: Sortable): void {
547
398
  if (event && event.target) {
548
- const target = event.target as HTMLElement;
549
- const s = handleSort(target, com.sortTarget, com.sortField, com.sortType);
550
- com.sortField = s.field;
551
- com.sortType = s.type;
552
- 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
553
404
  }
554
405
  }
555
406
 
@@ -557,106 +408,69 @@ export function getSortElement(target: HTMLElement): HTMLElement {
557
408
  return target.nodeName === "I" ? (target.parentElement as HTMLElement) : target
558
409
  }
559
410
  export function handleSort(target: HTMLElement, previousTarget?: HTMLElement, sortField?: string, sortType?: string): Sort {
560
- const type = target.getAttribute('sort-type');
561
- const field = toggleSortStyle(target);
562
- 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)
563
414
  if (sortField !== field) {
564
- removeSortStatus(previousTarget);
415
+ removeSortStatus(previousTarget)
565
416
  }
566
- return s;
417
+ return s
567
418
  }
568
419
 
569
420
  export function sort(preField?: string, preSortType?: string, field?: string, sortType?: string): Sort {
570
- if (!preField || preField === '') {
421
+ if (!preField || preField === "") {
571
422
  const s: Sort = {
572
423
  field,
573
- type: '+'
574
- };
575
- return s;
424
+ type: "+",
425
+ }
426
+ return s
576
427
  } else if (preField !== field) {
577
428
  const s: Sort = {
578
429
  field,
579
- type: (!sortType ? '+' : sortType)
580
- };
581
- return s;
430
+ type: !sortType ? "+" : sortType,
431
+ }
432
+ return s
582
433
  } else if (preField === field) {
583
- const type = (preSortType === '+' ? '-' : '+');
584
- const s: Sort = {field, type};
585
- return s;
434
+ const type = preSortType === "+" ? "-" : "+"
435
+ const s: Sort = { field, type }
436
+ return s
586
437
  } else {
587
- return {field, type: sortType};
438
+ return { field, type: sortType }
588
439
  }
589
440
  }
590
441
 
591
442
  export function removeSortStatus(target?: HTMLElement): void {
592
443
  if (target && target.children.length > 0) {
593
- target.removeChild(target.children[0]);
444
+ target.removeChild(target.children[0])
594
445
  }
595
446
  }
596
447
 
597
448
  export function toggleSortStyle(target: HTMLElement): string {
598
- let field = target.getAttribute('data-field');
449
+ let field = target.getAttribute("data-field")
599
450
  if (!field) {
600
- const p = target.parentNode as HTMLElement;
451
+ const p = target.parentNode as HTMLElement
601
452
  if (p) {
602
- field = p.getAttribute('data-field');
453
+ field = p.getAttribute("data-field")
603
454
  }
604
455
  }
605
456
  if (!field || field.length === 0) {
606
- return '';
457
+ return ""
607
458
  }
608
- if (target.nodeName === 'I') {
609
- target = target.parentNode as HTMLElement;
459
+ if (target.nodeName === "I") {
460
+ target = target.parentNode as HTMLElement
610
461
  }
611
- let i = null;
462
+ let i = null
612
463
  if (target.children.length === 0) {
613
- target.innerHTML = target.innerHTML + '<i class="sort-up"></i>';
464
+ target.innerHTML = target.innerHTML + '<i class="sort-up"></i>'
614
465
  } else {
615
- i = target.children[0];
616
- if (i.classList.contains('sort-up')) {
617
- i.classList.remove('sort-up');
618
- i.classList.add('sort-down');
619
- } else if (i.classList.contains('sort-down')) {
620
- i.classList.remove('sort-down');
621
- i.classList.add('sort-up');
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")
622
473
  }
623
474
  }
624
- return field;
625
- }
626
- export function getModel<S extends Filter>(state: any, modelName: string, searchable: Searchable, fields?: string[], excluding?: string[]|number[]): S {
627
- let obj2 = getModelFromState(state, modelName);
628
-
629
- const obj: any = obj2 ? obj2 : {};
630
- const obj3 = optimizeFilter(obj, searchable, fields);
631
- obj3.excluding = excluding;
632
- return obj3;
633
- }
634
- function getModelFromState(state: any, modelName: string): any {
635
- if (!modelName || modelName.length === 0) {
636
- return state;
637
- }
638
- if (!state) {
639
- return state;
640
- }
641
- return state[modelName];
642
- }
643
- export function getFieldsFromForm(fields?: string[], initFields?: boolean, form?: HTMLFormElement|null): string[]|undefined {
644
- if (fields && fields.length > 0) {
645
- return fields;
646
- }
647
- if (!initFields) {
648
- if (form) {
649
- return getFields(form);
650
- }
651
- }
652
- return fields;
653
- }
654
- 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 {
655
- let valid = true;
656
- if (form && vf) {
657
- valid = vf(form, lc);
658
- }
659
- if (valid === true) {
660
- callback();
661
- }
475
+ return field
662
476
  }