express-ext 0.3.9 → 0.4.1

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