express-ext 0.4.0 → 0.4.2

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,384 +1,360 @@
1
- import { Request, Response } from 'express';
2
- import { minimizeArray, query, queryNumber } from './http';
3
- import { ViewService } from './LoadController';
4
- import { Attribute, Attributes } from './metadata';
5
- import { resources, StringMap } from './resources';
1
+ import { Request, Response } from "express"
2
+ import { minimizeArray, query, queryNumber } from "./http"
3
+ import { ViewService } from "./LoadController"
4
+ import { Attribute, Attributes } from "./metadata"
5
+ import { resources, StringMap } from "./resources"
6
6
 
7
- const et = '';
7
+ const et = ""
8
8
 
9
9
  export interface Filter {
10
- page?: number;
11
- limit?: number;
10
+ page?: number
11
+ limit?: number
12
12
 
13
- fields?: string[];
14
- sort?: string;
13
+ fields?: string[]
14
+ sort?: string
15
15
 
16
- q?: string;
16
+ q?: string
17
17
  }
18
18
  export interface SearchConfig {
19
- excluding?: string;
20
- fields?: string;
21
- list?: string;
22
- total?: string;
23
- token?: string;
24
- last?: string;
25
- csv?: boolean;
26
- page?: string;
27
- limit?: string;
28
- skip?: string;
29
- refId?: string;
30
- firstLimit?: string;
19
+ excluding?: string
20
+ // fields?: string
21
+ list?: string
22
+ total?: string
23
+ token?: string
24
+ last?: string
25
+ csv?: boolean
26
+ // page?: string
27
+ // limit?: string
28
+ // skip?: string
29
+ // refId?: string;
30
+ // firstLimit?: string
31
31
  }
32
32
  export interface SearchResult<T> {
33
- list: T[];
34
- total?: number;
35
- nextPageToken?: string;
36
- last?: boolean;
33
+ list: T[]
34
+ total?: number
35
+ nextPageToken?: string
36
+ last?: boolean
37
37
  }
38
38
 
39
39
  export function queryLimit(req: Request): number {
40
- return queryNumber(req, resources.limit, resources.defaultLimit);
40
+ return queryNumber(req, resources.limit, resources.defaultLimit)
41
41
  }
42
42
  export function queryPage<F extends Filter>(req: Request, filter?: F): number {
43
- const field = req.query[resources.page];
44
- const v = field ? field.toString() : undefined;
43
+ const field = req.query[resources.page]
44
+ const v = field ? field.toString() : undefined
45
45
  if (!v || v.length === 0) {
46
- (filter as any)[resources.page] = 1;
47
- return 1;
46
+ ;(filter as any)[resources.page] = 1
47
+ return 1
48
48
  }
49
49
  if (isNaN(v as any)) {
50
- (filter as any)[resources.page] = 1;
51
- return 1;
50
+ ;(filter as any)[resources.page] = 1
51
+ return 1
52
52
  }
53
- const n = parseFloat(v);
54
- (filter as any)[resources.page] = n;
55
- return n;
53
+ const n = parseFloat(v)
54
+ ;(filter as any)[resources.page] = n
55
+ return n
56
56
  }
57
57
  export function getOffset(limit: number, page: number): number {
58
- const offset = limit * (page - 1);
59
- return offset < 0 ? 0 : offset;
58
+ const offset = limit * (page - 1)
59
+ return offset < 0 ? 0 : offset
60
60
  }
61
61
 
62
62
  export function getPageTotal(pageSize?: number, total?: number): number {
63
63
  if (!pageSize || pageSize <= 0) {
64
- return 1;
64
+ return 1
65
65
  } else {
66
66
  if (!total) {
67
- total = 0;
67
+ total = 0
68
68
  }
69
69
  if (total % pageSize === 0) {
70
- return Math.floor(total / pageSize);
70
+ return Math.floor(total / pageSize)
71
71
  }
72
- return Math.floor(total / pageSize + 1);
72
+ return Math.floor(total / pageSize + 1)
73
73
  }
74
74
  }
75
75
  export function formatText(...args: any[]): string {
76
- let formatted = args[0];
77
- if (!formatted || formatted === '') {
78
- return '';
76
+ let formatted = args[0]
77
+ if (!formatted || formatted === "") {
78
+ return ""
79
79
  }
80
80
  if (args.length > 1 && Array.isArray(args[1])) {
81
- const params = args[1];
81
+ const params = args[1]
82
82
  for (let i = 0; i < params.length; i++) {
83
- const regexp = new RegExp('\\{' + i + '\\}', 'gi');
84
- formatted = formatted.replace(regexp, params[i]);
83
+ const regexp = new RegExp("\\{" + i + "\\}", "gi")
84
+ formatted = formatted.replace(regexp, params[i])
85
85
  }
86
86
  } else {
87
87
  for (let i = 1; i < args.length; i++) {
88
- const regexp = new RegExp('\\{' + (i - 1) + '\\}', 'gi');
89
- formatted = formatted.replace(regexp, args[i]);
88
+ const regexp = new RegExp("\\{" + (i - 1) + "\\}", "gi")
89
+ formatted = formatted.replace(regexp, args[i])
90
90
  }
91
91
  }
92
- return formatted;
92
+ return formatted
93
93
  }
94
94
  export function buildMessage<T>(resource: StringMap, results: T[], limit: number, page: number | undefined, total?: number): string {
95
95
  if (!results || results.length === 0) {
96
- return resource.msg_no_data_found;
96
+ return resource.msg_no_data_found
97
97
  } else {
98
98
  if (!page) {
99
- page = 1;
99
+ page = 1
100
100
  }
101
- const fromIndex = (page - 1) * limit + 1;
102
- const toIndex = fromIndex + results.length - 1;
103
- const pageTotal = getPageTotal(limit, total);
101
+ const fromIndex = (page - 1) * limit + 1
102
+ const toIndex = fromIndex + results.length - 1
103
+ const pageTotal = getPageTotal(limit, total)
104
104
  if (pageTotal > 1) {
105
- const msg2 = formatText(resource.msg_search_result_page_sequence, fromIndex, toIndex, total, page, pageTotal);
106
- return msg2;
105
+ const msg2 = formatText(resource.msg_search_result_page_sequence, fromIndex, toIndex, total, page, pageTotal)
106
+ return msg2
107
107
  } else {
108
- const msg3 = formatText(resource.msg_search_result_sequence, fromIndex, toIndex);
109
- return msg3;
108
+ const msg3 = formatText(resource.msg_search_result_sequence, fromIndex, toIndex)
109
+ return msg3
110
110
  }
111
111
  }
112
112
  }
113
113
  export function buildPages(pageSize?: number, total?: number): number[] {
114
- const pageTotal = getPageTotal(pageSize, total);
114
+ const pageTotal = getPageTotal(pageSize, total)
115
115
  if (pageTotal <= 1) {
116
- return [1];
116
+ return [1]
117
117
  }
118
- const arr: number[] = [];
118
+ const arr: number[] = []
119
119
  for (let i = 1; i <= pageTotal; i++) {
120
- arr.push(i);
120
+ arr.push(i)
121
121
  }
122
- return arr;
122
+ return arr
123
123
  }
124
124
 
125
125
  export function hasSearch(req: Request): boolean {
126
- return req.url.indexOf('?') >= 0;
126
+ return req.url.indexOf("?") >= 0
127
127
  }
128
128
  export function getSearch(url: string): string {
129
- const i = url.indexOf('?');
130
- return i < 0 ? et : url.substring(i + 1);
129
+ const i = url.indexOf("?")
130
+ return i < 0 ? et : url.substring(i + 1)
131
131
  }
132
132
  export function getField(search: string, fieldName: string): string {
133
- let i = search.indexOf(fieldName + '=');
133
+ let i = search.indexOf(fieldName + "=")
134
134
  if (i < 0) {
135
- return '';
135
+ return ""
136
136
  }
137
137
  if (i > 0) {
138
- if (search.substring(i - 1, 1) != '&') {
139
- i = search.indexOf('&' + fieldName + '=');
138
+ if (search.substring(i - 1, 1) != "&") {
139
+ i = search.indexOf("&" + fieldName + "=")
140
140
  if (i < 0) {
141
- return search;
141
+ return search
142
142
  }
143
- i = i + 1;
143
+ i = i + 1
144
144
  }
145
145
  }
146
- const j = search.indexOf('&', i + fieldName.length);
147
- return j >= 0 ? search.substring(i, j) : search.substring(i);
146
+ const j = search.indexOf("&", i + fieldName.length)
147
+ return j >= 0 ? search.substring(i, j) : search.substring(i)
148
148
  }
149
149
  export function removeField(search: string, fieldName: string): string {
150
- let i = search.indexOf(fieldName + '=');
150
+ let i = search.indexOf(fieldName + "=")
151
151
  if (i < 0) {
152
- return search;
152
+ return search
153
153
  }
154
154
  if (i > 0) {
155
- if (search.substring(i - 1, 1) != '&') {
156
- i = search.indexOf('&' + fieldName + '=');
155
+ if (search.substring(i - 1, 1) != "&") {
156
+ i = search.indexOf("&" + fieldName + "=")
157
157
  if (i < 0) {
158
- return search;
158
+ return search
159
159
  }
160
- i = i + 1;
160
+ i = i + 1
161
161
  }
162
162
  }
163
- const j = search.indexOf('&', i + fieldName.length);
164
- return j >= 0 ? search.substring(0, i) + search.substring(j + 1) : search.substring(0, i - 1);
163
+ const j = search.indexOf("&", i + fieldName.length)
164
+ return j >= 0 ? search.substring(0, i) + search.substring(j + 1) : search.substring(0, i - 1)
165
165
  }
166
166
  export function removePage(search: string): string {
167
- search = removeField(search, resources.page);
168
- search = removeField(search, resources.partial);
169
- return search;
167
+ search = removeField(search, resources.page)
168
+ search = removeField(search, resources.partial)
169
+ return search
170
170
  }
171
171
  export function buildPageSearch(search: string): string {
172
- const sr = removePage(search);
173
- return sr.length == 0 ? sr : '&' + sr;
172
+ const sr = removePage(search)
173
+ return sr.length == 0 ? sr : "&" + sr
174
174
  }
175
175
  export function buildPageSearchFromUrl(url: string): string {
176
- const search = getSearch(url);
177
- return buildPageSearch(search);
176
+ const search = getSearch(url)
177
+ return buildPageSearch(search)
178
178
  }
179
179
  export function removeSort(search: string): string {
180
- search = removeField(search, resources.sort);
181
- search = removeField(search, resources.partial);
182
- return search;
180
+ search = removeField(search, resources.sort)
181
+ search = removeField(search, resources.partial)
182
+ return search
183
183
  }
184
184
  export interface Sort {
185
- field?: string;
186
- type?: string;
185
+ field?: string
186
+ type?: string
187
187
  }
188
188
  export interface SortType {
189
- url: string;
190
- tag: string;
189
+ url: string
190
+ tag: string
191
191
  }
192
192
  export interface SortMap {
193
- [key: string]: SortType;
193
+ [key: string]: SortType
194
194
  }
195
195
  export function getSortString(field: string, sort: Sort): string {
196
196
  if (field === sort.field) {
197
- return sort.type === '-' ? field : '-' + field;
197
+ return sort.type === "-" ? field : "-" + field
198
198
  }
199
- return field;
199
+ return field
200
200
  }
201
201
  export function buildSort(s?: string): Sort {
202
- if (!s || s.indexOf(',') >= 0) {
203
- return {} as Sort;
202
+ if (!s || s.indexOf(",") >= 0) {
203
+ return {} as Sort
204
204
  }
205
- if (s.startsWith('-')) {
206
- return { field: s.substring(1), type: '-' };
205
+ if (s.startsWith("-")) {
206
+ return { field: s.substring(1), type: "-" }
207
207
  } else {
208
- return { field: s.startsWith('+') ? s.substring(1) : s, type: '+' };
208
+ return { field: s.startsWith("+") ? s.substring(1) : s, type: "+" }
209
209
  }
210
210
  }
211
211
  export function buildSortFromRequest(req: Request): Sort {
212
- const s = query(req, resources.sort);
213
- return buildSort(s);
212
+ const s = query(req, resources.sort)
213
+ return buildSort(s)
214
214
  }
215
215
  export function renderSort(field: string, sort: Sort): string {
216
216
  if (field === sort.field) {
217
- return sort.type === '-' ? "<i class='sort-down'></i>" : "<i class='sort-up'></i>";
217
+ return sort.type === "-" ? "<i class='sort-down'></i>" : "<i class='sort-up'></i>"
218
218
  }
219
- return et;
219
+ return et
220
220
  }
221
221
  export function buildSortSearch(search: string, fields: string[], sortStr?: string): SortMap {
222
- const sort = buildSort(sortStr);
223
- search = removeSort(search);
224
- let sorts: SortMap = {};
225
- const prefix = search.length > 0 ? '?' + search + '&' : '?';
222
+ const sort = buildSort(sortStr)
223
+ search = removeSort(search)
224
+ let sorts: SortMap = {}
225
+ const prefix = search.length > 0 ? "?" + search + "&" : "?"
226
226
  for (let i = 0; i < fields.length; i++) {
227
227
  sorts[fields[i]] = {
228
- url: prefix + resources.sort + '=' + getSortString(fields[i], sort),
228
+ url: prefix + resources.sort + "=" + getSortString(fields[i], sort),
229
229
  tag: renderSort(fields[i], sort),
230
- };
230
+ }
231
231
  }
232
- return sorts;
232
+ return sorts
233
233
  }
234
234
  export function clone(obj: any): any {
235
235
  if (!obj) {
236
- return obj;
236
+ return obj
237
237
  }
238
238
  if (obj instanceof Date) {
239
- return new Date(obj.getTime());
239
+ return new Date(obj.getTime())
240
240
  }
241
- if (typeof obj !== 'object') {
242
- return obj;
241
+ if (typeof obj !== "object") {
242
+ return obj
243
243
  }
244
244
  if (Array.isArray(obj)) {
245
- const arr = [];
245
+ const arr = []
246
246
  for (const sub of obj) {
247
- const c = clone(sub);
248
- arr.push(c);
247
+ const c = clone(sub)
248
+ arr.push(c)
249
249
  }
250
- return arr;
250
+ return arr
251
251
  }
252
- const x: any = {};
253
- const keys = Object.keys(obj);
252
+ const x: any = {}
253
+ const keys = Object.keys(obj)
254
254
  for (const k of keys) {
255
- const v = obj[k];
255
+ const v = obj[k]
256
256
  if (v instanceof Date) {
257
- x[k] = new Date(v.getTime());
257
+ x[k] = new Date(v.getTime())
258
258
  } else {
259
259
  switch (typeof v) {
260
- case 'object':
261
- x[k] = clone(v);
262
- break;
260
+ case "object":
261
+ x[k] = clone(v)
262
+ break
263
263
  default:
264
- x[k] = v;
265
- break;
264
+ x[k] = v
265
+ break
266
266
  }
267
267
  }
268
268
  }
269
- return x;
269
+ return x
270
270
  }
271
271
  export function cloneFilter<F extends Filter>(obj: F, limit: number, page: number): F {
272
- const f = clone(obj);
272
+ const f = clone(obj)
273
273
  if (!obj.hasOwnProperty(resources.page)) {
274
- (obj as any)[resources.page] = page;
274
+ ;(obj as any)[resources.page] = page
275
275
  }
276
276
  if (!obj.hasOwnProperty(resources.limit)) {
277
- (obj as any)[resources.limit] = limit;
277
+ ;(obj as any)[resources.limit] = limit
278
278
  }
279
- return f;
279
+ return f
280
280
  }
281
281
 
282
282
  export function jsonResult<T>(res: Response, result: SearchResult<T>, quick?: boolean, fields?: string[], config?: SearchConfig): void {
283
283
  if (quick && fields && fields.length > 0) {
284
- res.status(200).json(toCsv(fields, result)).end();
284
+ res.status(200).json(toCsv(fields, result)).end()
285
285
  } else {
286
- res.status(200).json(buildResult(result, config)).end();
286
+ res.status(200).json(buildResult(result, config)).end()
287
287
  }
288
288
  }
289
289
  export function buildResult<T>(r: SearchResult<T>, conf?: SearchConfig): any {
290
290
  if (!conf) {
291
- return r;
291
+ return r
292
292
  }
293
- const x: any = {};
294
- const li = conf.list ? conf.list : 'list';
295
- x[li] = minimizeArray(r.list);
296
- const to = conf.total ? conf.total : 'total';
297
- x[to] = r.total;
293
+ const x: any = {}
294
+ const li = conf.list ? conf.list : "list"
295
+ x[li] = minimizeArray(r.list)
296
+ const to = conf.total ? conf.total : "total"
297
+ x[to] = r.total
298
298
  if (r.nextPageToken && r.nextPageToken.length > 0) {
299
- const t = conf.token ? conf.token : 'token';
300
- x[t] = r.nextPageToken;
299
+ const t = conf.token ? conf.token : "token"
300
+ x[t] = r.nextPageToken
301
301
  }
302
302
  if (r.last) {
303
- const l = conf.last ? conf.last : 'last';
304
- x[l] = r.last;
303
+ const l = conf.last ? conf.last : "last"
304
+ x[l] = r.last
305
305
  }
306
- return x;
306
+ return x
307
307
  }
308
308
  export function initializeConfig(conf?: SearchConfig): SearchConfig | undefined {
309
309
  if (!conf) {
310
- return undefined;
310
+ return undefined
311
311
  }
312
312
  const c: SearchConfig = {
313
313
  excluding: conf.excluding,
314
- fields: conf.fields,
315
314
  list: conf.list,
316
315
  total: conf.total,
317
316
  token: conf.token,
318
317
  last: conf.last,
319
318
  csv: conf.csv,
320
- page: conf.page,
321
- limit: conf.limit,
322
- skip: conf.skip,
323
- refId: conf.refId,
324
- firstLimit: conf.firstLimit,
325
- };
326
- if (!c.excluding || c.excluding.length === 0) {
327
- c.excluding = 'excluding';
328
319
  }
329
- if (!c.fields || c.fields.length === 0) {
330
- c.fields = 'fields';
320
+ if (!c.excluding || c.excluding.length === 0) {
321
+ c.excluding = "excluding"
331
322
  }
332
323
  if (!c.list || c.list.length === 0) {
333
- c.list = 'list';
324
+ c.list = "list"
334
325
  }
335
326
  if (!c.total || c.total.length === 0) {
336
- c.total = 'total';
327
+ c.total = "total"
337
328
  }
338
329
  if (!c.last || c.last.length === 0) {
339
- c.last = 'last';
330
+ c.last = "last"
340
331
  }
341
332
  if (!c.token || c.token.length === 0) {
342
- c.token = 'nextPageToken';
343
- }
344
- if (!c.page || c.page.length === 0) {
345
- c.page = 'page';
346
- }
347
- if (!c.limit || c.limit.length === 0) {
348
- c.limit = 'limit';
349
- }
350
- if (!c.skip || c.skip.length === 0) {
351
- c.skip = 'skip';
333
+ c.token = "nextPageToken"
352
334
  }
353
- if (!c.refId || c.refId.length === 0) {
354
- c.refId = 'refId';
355
- }
356
- if (!c.firstLimit || c.firstLimit.length === 0) {
357
- c.firstLimit = 'firstLimit';
358
- }
359
- return c;
335
+ return c
360
336
  }
361
337
  export function fromRequest<S>(req: Request, arr?: string[]): S {
362
- const s: any = req.method === 'GET' ? fromUrl(req, arr) : req.body;
363
- return s;
338
+ const s: any = req.method === "GET" ? fromUrl(req, arr) : req.body
339
+ return s
364
340
  }
365
341
  export function buildArray(arr?: string[], s0?: string, s1?: string, s2?: string): string[] {
366
- const r: string[] = [];
342
+ const r: string[] = []
367
343
  if (arr && arr.length > 0) {
368
344
  for (const a of arr) {
369
- r.push(a);
345
+ r.push(a)
370
346
  }
371
347
  }
372
348
  if (s0 && s0.length > 0) {
373
- r.push(s0);
349
+ r.push(s0)
374
350
  }
375
351
  if (s1 && s1.length > 0) {
376
- r.push(s1);
352
+ r.push(s1)
377
353
  }
378
354
  if (s2 && s2.length > 0) {
379
- r.push(s2);
355
+ r.push(s2)
380
356
  }
381
- return r;
357
+ return r
382
358
  }
383
359
  export function fromUrl<S>(req: Request, arr?: string[]): S {
384
360
  /*
@@ -386,29 +362,29 @@ export function fromUrl<S>(req: Request, arr?: string[]): S {
386
362
  fields = 'fields';
387
363
  }
388
364
  */
389
- const s: any = {};
390
- const obj = req.query;
391
- const keys = Object.keys(obj);
365
+ const s: any = {}
366
+ const obj = req.query
367
+ const keys = Object.keys(obj)
392
368
  for (const key of keys) {
393
369
  if (inArray(key, arr)) {
394
- const x = (obj[key] as string).split(',');
395
- setValue(s, key, x);
370
+ const x = (obj[key] as string).split(",")
371
+ setValue(s, key, x)
396
372
  } else {
397
- setValue(s, key, obj[key] as string);
373
+ setValue(s, key, obj[key] as string)
398
374
  }
399
375
  }
400
- return s;
376
+ return s
401
377
  }
402
378
  export function inArray(s: string, arr?: string[]): boolean {
403
379
  if (!arr || arr.length === 0) {
404
- return false;
380
+ return false
405
381
  }
406
382
  for (const a of arr) {
407
383
  if (s === a) {
408
- return true;
384
+ return true
409
385
  }
410
386
  }
411
- return false;
387
+ return false
412
388
  }
413
389
  /*
414
390
  export function setValue<T>(obj: T, path: string, value: string): void {
@@ -430,376 +406,240 @@ export function setValue<T>(obj: T, path: string, value: string): void {
430
406
  }
431
407
  */
432
408
  export function setValue<T, V>(o: T, key: string, value: V): any {
433
- const obj: any = o;
434
- let replaceKey = key.replace(/\[/g, '.[').replace(/\.\./g, '.');
435
- if (replaceKey.indexOf('.') === 0) {
436
- replaceKey = replaceKey.slice(1, replaceKey.length);
409
+ const obj: any = o
410
+ let replaceKey = key.replace(/\[/g, ".[").replace(/\.\./g, ".")
411
+ if (replaceKey.indexOf(".") === 0) {
412
+ replaceKey = replaceKey.slice(1, replaceKey.length)
437
413
  }
438
- const keys = replaceKey.split('.');
439
- const firstKey = keys.shift();
414
+ const keys = replaceKey.split(".")
415
+ const firstKey = keys.shift()
440
416
  if (!firstKey) {
441
- return;
417
+ return
442
418
  }
443
- const isArrayKey = /\[([0-9]+)\]/.test(firstKey);
419
+ const isArrayKey = /\[([0-9]+)\]/.test(firstKey)
444
420
  if (keys.length > 0) {
445
- const firstKeyValue = obj[firstKey] || {};
446
- const returnValue = setValue(firstKeyValue, keys.join('.'), value);
447
- return setKey(obj, isArrayKey, firstKey, returnValue);
421
+ const firstKeyValue = obj[firstKey] || {}
422
+ const returnValue = setValue(firstKeyValue, keys.join("."), value)
423
+ return setKey(obj, isArrayKey, firstKey, returnValue)
448
424
  }
449
- return setKey(obj, isArrayKey, firstKey, value);
425
+ return setKey(obj, isArrayKey, firstKey, value)
450
426
  }
451
427
  const setKey = (_object: any, _isArrayKey: boolean, _key: string, _nextValue: any) => {
452
428
  if (_isArrayKey) {
453
429
  if (_object.length > _key) {
454
- _object[_key] = _nextValue;
430
+ _object[_key] = _nextValue
455
431
  } else {
456
- _object.push(_nextValue);
432
+ _object.push(_nextValue)
457
433
  }
458
434
  } else {
459
- _object[_key] = _nextValue;
435
+ _object[_key] = _nextValue
460
436
  }
461
- return _object;
462
- };
437
+ return _object
438
+ }
463
439
  export interface Limit {
464
- limit?: number;
465
- page?: number;
466
- nextPageToken?: string;
467
- fields?: string[];
468
- pageOrNextPageToken?: string | number;
440
+ limit: number
441
+ page?: number
442
+ nextPageToken?: string
443
+ fields?: string[]
444
+ pageOrNextPageToken?: string | number
469
445
  }
470
446
  export function getParameters<T>(obj: T, config?: SearchConfig): Limit {
471
- const o: any = obj;
472
- if (!config) {
473
- const sfield = 'fields';
474
- let fields;
475
- const fs = o[sfield];
476
- if (fs && Array.isArray(fs)) {
477
- fields = fs;
478
- delete o[sfield];
479
- }
480
- let refId = o['refId'];
481
- if (!refId) {
482
- refId = o['nextPageToken'];
483
- }
484
- const r: Limit = { fields, nextPageToken: refId };
485
- let pageSize = o['limit'];
486
- if (!pageSize) {
487
- pageSize = o['pageSize'];
488
- }
489
- if (pageSize && !isNaN(pageSize)) {
490
- const ipageSize = Math.floor(parseFloat(pageSize));
491
- if (ipageSize > 0) {
492
- r.limit = ipageSize;
493
- /*
494
- const skip = o['skip'];
495
- if (skip && !isNaN(skip)) {
496
- const iskip = Math.floor(parseFloat(skip));
497
- if (iskip >= 0) {
498
- r.page = iskip;
499
- r.pageOrNextPageToken = r.page;
500
- deletePageInfo(o);
501
- return r;
502
- }
503
- }
504
- */
505
- let pageIndex = o['page'];
506
- if (!pageIndex) {
507
- pageIndex = o['pageIndex'];
508
- if (!pageIndex) {
509
- pageIndex = o['pageNo'];
510
- }
511
- }
512
- if (pageIndex && !isNaN(pageIndex)) {
513
- let ipageIndex = Math.floor(parseFloat(pageIndex));
514
- if (ipageIndex < 1) {
515
- ipageIndex = 1;
516
- }
517
- /*
518
- let firstPageSize = o['firstLimit'];
519
- if (!firstPageSize) {
520
- firstPageSize = o['firstPageSize'];
521
- }
522
- if (!firstPageSize) {
523
- firstPageSize = o['initPageSize'];
524
- }
525
- if (firstPageSize && !isNaN(firstPageSize)) {
526
- const ifirstPageSize = Math.floor(parseFloat(firstPageSize));
527
- if (ifirstPageSize > 0) {
528
- r.page = ipageIndex;
529
- r.pageOrNextPageToken = r.page;
530
- deletePageInfo(o);
531
- return r;
532
- }
533
- }
534
- */
535
- r.page = ipageIndex;
536
- r.pageOrNextPageToken = r.page;
537
- deletePageInfo(o);
538
- return r;
539
- }
540
- r.page = 1;
541
- if (r.nextPageToken && r.nextPageToken.length > 0) {
542
- r.pageOrNextPageToken = r.nextPageToken;
543
- }
544
- deletePageInfo(o);
545
- return r;
447
+ const o: any = obj
448
+ let fields: string[] | undefined
449
+ const fs = o[resources.fields]
450
+ if (fs && Array.isArray(fs)) {
451
+ fields = fs
452
+ }
453
+ let nextPageToken: string | undefined = o[resources.nextPageToken]
454
+ let page = 1
455
+ let spage = o[resources.page]
456
+ if (spage && typeof spage === "string") {
457
+ if (!isNaN(spage as any)) {
458
+ const ipage = Math.floor(parseFloat(spage))
459
+ if (ipage > 1) {
460
+ page = ipage
546
461
  }
547
462
  }
548
- if (r.nextPageToken && r.nextPageToken.length > 0) {
549
- r.pageOrNextPageToken = r.nextPageToken;
550
- }
551
- deletePageInfo(o);
552
- return r;
553
- } else {
554
- let sfield = config.fields;
555
- if (!sfield || sfield.length === 0) {
556
- sfield = 'fields';
557
- }
558
- let fields;
559
- const fs = o[sfield];
560
- if (fs && Array.isArray(fs)) {
561
- fields = fs;
562
- delete o[sfield];
563
- }
564
- let strRefId = config.refId;
565
- if (!strRefId || strRefId.length === 0) {
566
- strRefId = 'refId';
567
- }
568
- const refId = o[strRefId];
569
- const r: Limit = { fields, nextPageToken: refId };
570
-
571
- let strLimit = config.limit;
572
- if (!strLimit || strLimit.length === 0) {
573
- strLimit = 'limit';
574
- }
575
- const pageSize = o[strLimit];
576
- const arr = [config.page, config.limit, config.skip, config.refId, config.firstLimit];
577
- if (pageSize && !isNaN(pageSize)) {
578
- const ipageSize = Math.floor(parseFloat(pageSize));
463
+ }
464
+ let pageSize = resources.defaultLimit
465
+ let spageSize = o[resources.limit]
466
+ if (spageSize && typeof spageSize === "string") {
467
+ if (!isNaN(spageSize as any)) {
468
+ const ipageSize = Math.floor(parseFloat(spageSize))
579
469
  if (ipageSize > 0) {
580
- r.limit = ipageSize;
581
- let strSkip = config.skip;
582
- if (!strSkip || strSkip.length === 0) {
583
- strSkip = 'skip';
584
- }
585
- const skip = o[strSkip];
586
- if (skip && !isNaN(skip)) {
587
- const iskip = Math.floor(parseFloat(skip));
588
- if (iskip >= 0) {
589
- r.page = iskip;
590
- r.pageOrNextPageToken = r.page;
591
- deletePageInfo(o, arr);
592
- return r;
593
- }
594
- }
595
- let strPage = config.page;
596
- if (!strPage || strPage.length === 0) {
597
- strPage = 'page';
598
- }
599
- const pageIndex = o[strPage];
600
- if (pageIndex && !isNaN(pageIndex)) {
601
- let ipageIndex = Math.floor(parseFloat(pageIndex));
602
- if (ipageIndex < 1) {
603
- ipageIndex = 1;
604
- }
605
- let strFirstLimit = config.firstLimit;
606
- if (!strFirstLimit || strFirstLimit.length === 0) {
607
- strFirstLimit = 'firstLimit';
608
- }
609
- const firstPageSize = o[strFirstLimit];
610
- if (firstPageSize && !isNaN(firstPageSize)) {
611
- const ifirstPageSize = Math.floor(parseFloat(firstPageSize));
612
- if (ifirstPageSize > 0) {
613
- r.page = ipageSize * (ipageIndex - 2) + ifirstPageSize;
614
- r.pageOrNextPageToken = r.page;
615
- deletePageInfo(o, arr);
616
- return r;
617
- }
618
- }
619
- r.page = ipageSize * (ipageIndex - 1);
620
- r.pageOrNextPageToken = r.page;
621
- deletePageInfo(o, arr);
622
- return r;
623
- }
624
- r.page = 0;
625
- if (r.nextPageToken && r.nextPageToken.length > 0) {
626
- r.pageOrNextPageToken = r.nextPageToken;
627
- }
628
- deletePageInfo(o, arr);
629
- return r;
470
+ pageSize = ipageSize
630
471
  }
631
472
  }
632
- if (r.nextPageToken && r.nextPageToken.length > 0) {
633
- r.pageOrNextPageToken = r.nextPageToken;
634
- }
635
- deletePageInfo(o, arr);
636
- return r;
637
473
  }
474
+ const r: Limit = { limit: pageSize, fields, page, nextPageToken, pageOrNextPageToken: page }
475
+ if (r.nextPageToken && r.nextPageToken.length > 0) {
476
+ r.pageOrNextPageToken = r.nextPageToken
477
+ }
478
+ deletePageInfo(o)
479
+ return r
638
480
  }
639
481
  // tslint:disable-next-line:array-type
640
482
  export function deletePageInfo(obj: any, arr?: Array<string | undefined>): void {
641
483
  if (!arr || arr.length === 0) {
642
- delete obj['limit'];
643
- delete obj['firstLimit'];
644
- delete obj['skip'];
645
- delete obj['page'];
646
- delete obj['pageNo'];
647
- delete obj['pageIndex'];
648
- delete obj['pageSize'];
649
- delete obj['initPageSize'];
650
- delete obj['firstPageSize'];
651
- delete obj['refId'];
652
- delete obj['nextPageToken'];
484
+ delete obj[resources.fields]
485
+ delete obj[resources.limit]
486
+ delete obj[resources.page]
487
+ if (resources.nextPageToken && resources.nextPageToken.length > 0) {
488
+ delete obj[resources.nextPageToken]
489
+ }
490
+ if (resources.partial && resources.partial.length > 0) {
491
+ delete obj[resources.partial]
492
+ }
653
493
  } else {
654
494
  for (const o of arr) {
655
495
  if (o && o.length > 0) {
656
- delete obj[o];
496
+ delete obj[o]
657
497
  }
658
498
  }
659
499
  }
660
500
  }
661
- const re = /"/g;
501
+ const re = /"/g
662
502
  export function toCsv<T>(fields: string[], r: SearchResult<T>): string {
663
503
  if (!r || r.list.length === 0) {
664
- return '0';
504
+ return "0"
665
505
  } else {
666
- const e = '';
667
- const s = 'string';
668
- const n = 'number';
669
- const b = '""';
670
- const rows: string[] = [];
671
- rows.push('' + (r.total ? r.total : '') + ',' + (r.nextPageToken ? r.nextPageToken : '') + ',' + (r.last ? '1' : ''));
506
+ const e = ""
507
+ const s = "string"
508
+ const n = "number"
509
+ const b = '""'
510
+ const rows: string[] = []
511
+ rows.push("" + (r.total ? r.total : "") + "," + (r.nextPageToken ? r.nextPageToken : "") + "," + (r.last ? "1" : ""))
672
512
  for (const item of r.list) {
673
- const cols: string[] = [];
513
+ const cols: string[] = []
674
514
  for (const name of fields) {
675
- const v = (item as any)[name];
515
+ const v = (item as any)[name]
676
516
  if (!v) {
677
- cols.push(e);
517
+ cols.push(e)
678
518
  } else {
679
519
  if (typeof v === s) {
680
- if (s.indexOf(',') >= 0) {
681
- cols.push('"' + v.replace(re, b) + '"');
520
+ if (s.indexOf(",") >= 0) {
521
+ cols.push('"' + v.replace(re, b) + '"')
682
522
  } else {
683
- cols.push(v);
523
+ cols.push(v)
684
524
  }
685
525
  } else if (v instanceof Date) {
686
- cols.push(v.toISOString());
526
+ cols.push(v.toISOString())
687
527
  } else if (typeof v === n) {
688
- cols.push(v.toString());
528
+ cols.push(v.toString())
689
529
  } else {
690
- cols.push('');
530
+ cols.push("")
691
531
  }
692
532
  }
693
533
  }
694
- rows.push(cols.join(','));
534
+ rows.push(cols.join(","))
695
535
  }
696
- return rows.join('\n');
536
+ return rows.join("\n")
697
537
  }
698
538
  }
699
539
 
700
540
  export interface DateRange {
701
- startDate?: Date;
702
- endDate?: Date;
703
- startTime?: Date;
704
- endTime?: Date;
705
- min?: Date;
706
- max?: Date;
707
- upper?: Date;
541
+ startDate?: Date
542
+ endDate?: Date
543
+ startTime?: Date
544
+ endTime?: Date
545
+ min?: Date
546
+ max?: Date
547
+ upper?: Date
708
548
  }
709
549
  export interface NumberRange {
710
- min?: number;
711
- max?: number;
712
- lower?: number;
713
- upper?: number;
550
+ min?: number
551
+ max?: number
552
+ lower?: number
553
+ upper?: number
714
554
  }
715
555
  export interface Metadata {
716
- dates?: string[];
717
- numbers?: string[];
556
+ dates?: string[]
557
+ numbers?: string[]
718
558
  }
719
559
  export function buildMetadata(attributes: Attributes, includeDate?: boolean): Metadata {
720
- const keys: string[] = Object.keys(attributes);
721
- const dates: string[] = [];
722
- const numbers: string[] = [];
560
+ const keys: string[] = Object.keys(attributes)
561
+ const dates: string[] = []
562
+ const numbers: string[] = []
723
563
  for (const key of keys) {
724
- const attr: Attribute = attributes[key];
725
- if (attr.type === 'number' || attr.type === 'integer') {
726
- numbers.push(key);
727
- } else if (attr.type === 'datetime' || (includeDate === true && attr.type === 'date')) {
728
- dates.push(key);
564
+ const attr: Attribute = attributes[key]
565
+ if (attr.type === "number" || attr.type === "integer") {
566
+ numbers.push(key)
567
+ } else if (attr.type === "datetime" || (includeDate === true && attr.type === "date")) {
568
+ dates.push(key)
729
569
  }
730
570
  }
731
- const m: Metadata = {};
571
+ const m: Metadata = {}
732
572
  if (dates.length > 0) {
733
- m.dates = dates;
573
+ m.dates = dates
734
574
  }
735
575
  if (numbers.length > 0) {
736
- m.numbers = numbers;
576
+ m.numbers = numbers
737
577
  }
738
- return m;
578
+ return m
739
579
  }
740
580
 
741
- const _datereg = '/Date(';
742
- const _re = /-?\d+/;
581
+ const _datereg = "/Date("
582
+ const _re = /-?\d+/
743
583
  function toDate(v: any): Date | null | undefined {
744
584
  if (!v) {
745
- return null;
585
+ return null
746
586
  }
747
587
  if (v instanceof Date) {
748
- return v;
749
- } else if (typeof v === 'number') {
750
- return new Date(v);
588
+ return v
589
+ } else if (typeof v === "number") {
590
+ return new Date(v)
751
591
  }
752
- const i = v.indexOf(_datereg);
592
+ const i = v.indexOf(_datereg)
753
593
  if (i >= 0) {
754
- const m = _re.exec(v);
594
+ const m = _re.exec(v)
755
595
  if (m !== null) {
756
- const d = parseInt(m[0], 10);
757
- return new Date(d);
596
+ const d = parseInt(m[0], 10)
597
+ return new Date(d)
758
598
  } else {
759
- return null;
599
+ return null
760
600
  }
761
601
  } else {
762
602
  if (isNaN(v)) {
763
- return new Date(v);
603
+ return new Date(v)
764
604
  } else {
765
- const d = parseInt(v, 10);
766
- return new Date(d);
605
+ const d = parseInt(v, 10)
606
+ return new Date(d)
767
607
  }
768
608
  }
769
609
  }
770
610
 
771
611
  export function format<T>(obj: T, dates?: string[], nums?: string[]): T {
772
- const o: any = obj;
612
+ const o: any = obj
773
613
  if (dates && dates.length > 0) {
774
614
  for (const s of dates) {
775
- const v = o[s];
615
+ const v = o[s]
776
616
  if (v) {
777
617
  if (v instanceof Date) {
778
- continue;
618
+ continue
779
619
  }
780
- if (typeof v === 'string' || typeof v === 'number') {
781
- const d = toDate(v);
620
+ if (typeof v === "string" || typeof v === "number") {
621
+ const d = toDate(v)
782
622
  if (d) {
783
- if (!(d instanceof Date) || d.toString() === 'Invalid Date') {
784
- delete o[s];
623
+ if (!(d instanceof Date) || d.toString() === "Invalid Date") {
624
+ delete o[s]
785
625
  } else {
786
- o[s] = d;
626
+ o[s] = d
787
627
  }
788
628
  }
789
- } else if (typeof v === 'object') {
790
- const keys = Object.keys(v);
629
+ } else if (typeof v === "object") {
630
+ const keys = Object.keys(v)
791
631
  for (const key of keys) {
792
- const v2 = v[key];
632
+ const v2 = v[key]
793
633
  if (v2 instanceof Date) {
794
- continue;
634
+ continue
795
635
  }
796
- if (typeof v2 === 'string' || typeof v2 === 'number') {
797
- const d2 = toDate(v2);
636
+ if (typeof v2 === "string" || typeof v2 === "number") {
637
+ const d2 = toDate(v2)
798
638
  if (d2) {
799
- if (!(d2 instanceof Date) || d2.toString() === 'Invalid Date') {
800
- delete v[key];
639
+ if (!(d2 instanceof Date) || d2.toString() === "Invalid Date") {
640
+ delete v[key]
801
641
  } else {
802
- v[key] = d2;
642
+ v[key] = d2
803
643
  }
804
644
  }
805
645
  }
@@ -810,40 +650,40 @@ export function format<T>(obj: T, dates?: string[], nums?: string[]): T {
810
650
  }
811
651
  if (nums && nums.length > 0) {
812
652
  for (const s of nums) {
813
- const v = o[s];
653
+ const v = o[s]
814
654
  if (v) {
815
655
  if (v instanceof Date) {
816
- delete o[s];
817
- continue;
656
+ delete o[s]
657
+ continue
818
658
  }
819
- if (typeof v === 'number') {
820
- continue;
659
+ if (typeof v === "number") {
660
+ continue
821
661
  }
822
- if (typeof v === 'string') {
662
+ if (typeof v === "string") {
823
663
  if (!isNaN(v as any)) {
824
- delete o[s];
825
- continue;
664
+ delete o[s]
665
+ continue
826
666
  } else {
827
- const i = parseFloat(v);
828
- o[s] = i;
667
+ const i = parseFloat(v)
668
+ o[s] = i
829
669
  }
830
- } else if (typeof v === 'object') {
831
- const keys = Object.keys(v);
670
+ } else if (typeof v === "object") {
671
+ const keys = Object.keys(v)
832
672
  for (const key of keys) {
833
- const v2 = v[key];
673
+ const v2 = v[key]
834
674
  if (v2 instanceof Date) {
835
- delete o[key];
836
- continue;
675
+ delete o[key]
676
+ continue
837
677
  }
838
- if (typeof v2 === 'number') {
839
- continue;
678
+ if (typeof v2 === "number") {
679
+ continue
840
680
  }
841
- if (typeof v2 === 'string') {
681
+ if (typeof v2 === "string") {
842
682
  if (!isNaN(v2 as any)) {
843
- delete v[key];
683
+ delete v[key]
844
684
  } else {
845
- const i = parseFloat(v2);
846
- v[key] = i;
685
+ const i = parseFloat(v2)
686
+ v[key] = i
847
687
  }
848
688
  }
849
689
  }
@@ -851,7 +691,7 @@ export function format<T>(obj: T, dates?: string[], nums?: string[]): T {
851
691
  }
852
692
  }
853
693
  }
854
- return o;
694
+ return o
855
695
  }
856
696
  export function getMetadataFunc<T, ID>(
857
697
  viewService: ViewService<T, ID> | ((id: ID, ctx?: any) => Promise<T>),
@@ -859,20 +699,20 @@ export function getMetadataFunc<T, ID>(
859
699
  numbers?: string[],
860
700
  keys?: Attributes | Attribute[] | string[],
861
701
  ): Metadata | undefined {
862
- const m: Metadata = { dates, numbers };
702
+ const m: Metadata = { dates, numbers }
863
703
  if ((m.dates && m.dates.length > 0) || (m.numbers && m.numbers.length > 0)) {
864
- return m;
704
+ return m
865
705
  }
866
706
  if (keys) {
867
707
  if (!Array.isArray(keys)) {
868
- return buildMetadata(keys);
708
+ return buildMetadata(keys)
869
709
  }
870
710
  }
871
- if (typeof viewService !== 'function' && viewService.metadata) {
872
- const metadata = viewService.metadata();
711
+ if (typeof viewService !== "function" && viewService.metadata) {
712
+ const metadata = viewService.metadata()
873
713
  if (metadata) {
874
- return buildMetadata(metadata);
714
+ return buildMetadata(metadata)
875
715
  }
876
716
  }
877
- return undefined;
717
+ return undefined
878
718
  }