web-one 0.0.5 → 0.0.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/lib/index.js +66 -14
- package/package.json +1 -1
- package/src/index.ts +62 -15
package/lib/index.js
CHANGED
|
@@ -12,11 +12,22 @@ resources.page = "page";
|
|
|
12
12
|
resources.limit = "limit";
|
|
13
13
|
resources.defaultLimit = 12;
|
|
14
14
|
resources.sort = "sort";
|
|
15
|
-
function
|
|
15
|
+
function getRecordValue(v) {
|
|
16
|
+
if (typeof v === "string") {
|
|
17
|
+
return v;
|
|
18
|
+
}
|
|
19
|
+
else if (Array.isArray(v)) {
|
|
20
|
+
return v.length > 0 ? v[v.length - 1] : undefined;
|
|
21
|
+
}
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
exports.getRecordValue = getRecordValue;
|
|
25
|
+
function removePage(obj, pageKey) {
|
|
16
26
|
const arr = [];
|
|
17
27
|
const keys = Object.keys(obj);
|
|
28
|
+
const page = pageKey ? pageKey : resources.page;
|
|
18
29
|
for (const k of keys) {
|
|
19
|
-
if (k !==
|
|
30
|
+
if (k !== page) {
|
|
20
31
|
const v = obj[k];
|
|
21
32
|
if (typeof v === "string") {
|
|
22
33
|
arr.push(`${k}=${encodeURI(v)}`);
|
|
@@ -32,11 +43,12 @@ function removePage(obj) {
|
|
|
32
43
|
return arr.length === 0 ? "" : arr.join("&");
|
|
33
44
|
}
|
|
34
45
|
exports.removePage = removePage;
|
|
35
|
-
function removeSort(obj) {
|
|
46
|
+
function removeSort(obj, sortKey) {
|
|
36
47
|
const arr = [];
|
|
37
48
|
const keys = Object.keys(obj);
|
|
49
|
+
const sort = sortKey ? sortKey : resources.sort;
|
|
38
50
|
for (const k of keys) {
|
|
39
|
-
if (k !==
|
|
51
|
+
if (k !== sort) {
|
|
40
52
|
const v = obj[k];
|
|
41
53
|
if (typeof v === "string") {
|
|
42
54
|
arr.push(`${k}=${encodeURI(v)}`);
|
|
@@ -52,6 +64,28 @@ function removeSort(obj) {
|
|
|
52
64
|
return arr.length === 0 ? "" : arr.join("&");
|
|
53
65
|
}
|
|
54
66
|
exports.removeSort = removeSort;
|
|
67
|
+
function removeLimit(obj, limitKey, pageKey) {
|
|
68
|
+
const arr = [];
|
|
69
|
+
const keys = Object.keys(obj);
|
|
70
|
+
const page = pageKey ? pageKey : resources.page;
|
|
71
|
+
const limit = limitKey ? limitKey : resources.limit;
|
|
72
|
+
for (const k of keys) {
|
|
73
|
+
if (k !== page && k !== limit) {
|
|
74
|
+
const v = obj[k];
|
|
75
|
+
if (typeof v === "string") {
|
|
76
|
+
arr.push(`${k}=${encodeURI(v)}`);
|
|
77
|
+
}
|
|
78
|
+
else if (Array.isArray(v)) {
|
|
79
|
+
const x = v;
|
|
80
|
+
if (x.length > 0) {
|
|
81
|
+
arr.push(`${k}=${encodeURI(x[x.length - 1])}`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return arr.length === 0 ? "" : arr.join("&");
|
|
87
|
+
}
|
|
88
|
+
exports.removeLimit = removeLimit;
|
|
55
89
|
function getSortString(field, sort) {
|
|
56
90
|
if (field === sort.field) {
|
|
57
91
|
return sort.type === "-" ? field : "-" + field;
|
|
@@ -59,10 +93,12 @@ function getSortString(field, sort) {
|
|
|
59
93
|
return field;
|
|
60
94
|
}
|
|
61
95
|
exports.getSortString = getSortString;
|
|
62
|
-
function buildFilter(obj, dates, nums, arr) {
|
|
96
|
+
function buildFilter(obj, defaultLimit, dates, nums, arr, limitKey, pageKey) {
|
|
63
97
|
const filter = fromParams(obj, arr);
|
|
64
|
-
|
|
65
|
-
|
|
98
|
+
const page = pageKey ? pageKey : resources.page;
|
|
99
|
+
const limit = limitKey ? limitKey : resources.limit;
|
|
100
|
+
filter[page] = getPage(filter[page]);
|
|
101
|
+
filter[limit] = getLimit(filter[limit], defaultLimit);
|
|
66
102
|
format(filter, dates, nums);
|
|
67
103
|
return filter;
|
|
68
104
|
}
|
|
@@ -297,13 +333,13 @@ function buildSortFromParams(params) {
|
|
|
297
333
|
return buildSort(undefined);
|
|
298
334
|
}
|
|
299
335
|
exports.buildSortFromParams = buildSortFromParams;
|
|
300
|
-
function
|
|
336
|
+
function getSortType(field, sort) {
|
|
301
337
|
if (field === sort.field) {
|
|
302
|
-
return sort.type === "-" ? "
|
|
338
|
+
return sort.type === "-" ? "-" : "+";
|
|
303
339
|
}
|
|
304
|
-
return
|
|
340
|
+
return undefined;
|
|
305
341
|
}
|
|
306
|
-
exports.
|
|
342
|
+
exports.getSortType = getSortType;
|
|
307
343
|
function buildSortSearch(params, fields, sortStr) {
|
|
308
344
|
const search = removeSort(params);
|
|
309
345
|
const sort = buildSort(sortStr);
|
|
@@ -312,7 +348,7 @@ function buildSortSearch(params, fields, sortStr) {
|
|
|
312
348
|
for (let i = 0; i < fields.length; i++) {
|
|
313
349
|
sorts[fields[i]] = {
|
|
314
350
|
url: prefix + resources.sort + "=" + getSortString(fields[i], sort),
|
|
315
|
-
|
|
351
|
+
type: getSortType(fields[i], sort),
|
|
316
352
|
};
|
|
317
353
|
}
|
|
318
354
|
return sorts;
|
|
@@ -377,9 +413,9 @@ function getPage(page) {
|
|
|
377
413
|
return num === undefined || num < 1 ? 1 : num;
|
|
378
414
|
}
|
|
379
415
|
exports.getPage = getPage;
|
|
380
|
-
function getLimit(limit) {
|
|
416
|
+
function getLimit(limit, defaultLimit) {
|
|
381
417
|
const num = getNumber(limit);
|
|
382
|
-
return num === undefined || num < 1 ?
|
|
418
|
+
return num === undefined || num < 1 ? defaultLimit : num;
|
|
383
419
|
}
|
|
384
420
|
exports.getLimit = getLimit;
|
|
385
421
|
function getNumber(num, defaultNum) {
|
|
@@ -660,3 +696,19 @@ function getChildren(m, all) {
|
|
|
660
696
|
m.children = children;
|
|
661
697
|
}
|
|
662
698
|
}
|
|
699
|
+
function cloneArray(arr) {
|
|
700
|
+
return arr.map(item => (Object.assign({}, item)));
|
|
701
|
+
}
|
|
702
|
+
exports.cloneArray = cloneArray;
|
|
703
|
+
function getOffset(limit, page, firstLimit) {
|
|
704
|
+
const p = page && page > 0 ? page : 1;
|
|
705
|
+
if (firstLimit && firstLimit > 0) {
|
|
706
|
+
const offset = limit * (p - 2) + firstLimit;
|
|
707
|
+
return offset < 0 ? 0 : offset;
|
|
708
|
+
}
|
|
709
|
+
else {
|
|
710
|
+
const offset = limit * (p - 1);
|
|
711
|
+
return offset < 0 ? 0 : offset;
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
exports.getOffset = getOffset;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -7,12 +7,21 @@ export class resources {
|
|
|
7
7
|
static defaultLimit = 12
|
|
8
8
|
static sort = "sort"
|
|
9
9
|
}
|
|
10
|
+
export function getRecordValue(v: string | string[] | undefined): string | undefined {
|
|
11
|
+
if (typeof v === "string") {
|
|
12
|
+
return v
|
|
13
|
+
} else if (Array.isArray(v)) {
|
|
14
|
+
return v.length > 0 ? v[v.length - 1] : undefined
|
|
15
|
+
}
|
|
16
|
+
return undefined
|
|
17
|
+
}
|
|
10
18
|
export type StringMap = Record<string, string | string[] | undefined>
|
|
11
|
-
export function removePage(obj: Record<string, string | string[] | undefined
|
|
19
|
+
export function removePage(obj: Record<string, string | string[] | undefined>, pageKey?: string): string {
|
|
12
20
|
const arr: string[] = []
|
|
13
21
|
const keys = Object.keys(obj)
|
|
22
|
+
const page = pageKey ? pageKey : resources.page
|
|
14
23
|
for (const k of keys) {
|
|
15
|
-
if (k !==
|
|
24
|
+
if (k !== page) {
|
|
16
25
|
const v = obj[k]
|
|
17
26
|
if (typeof v === "string") {
|
|
18
27
|
arr.push(`${k}=${encodeURI(v)}`)
|
|
@@ -27,11 +36,12 @@ export function removePage(obj: Record<string, string | string[] | undefined>):
|
|
|
27
36
|
return arr.length === 0 ? "" : arr.join("&")
|
|
28
37
|
}
|
|
29
38
|
|
|
30
|
-
export function removeSort(obj: Record<string, string | string[] | undefined
|
|
39
|
+
export function removeSort(obj: Record<string, string | string[] | undefined>, sortKey?: string): string {
|
|
31
40
|
const arr: string[] = []
|
|
32
41
|
const keys = Object.keys(obj)
|
|
42
|
+
const sort = sortKey ? sortKey : resources.sort
|
|
33
43
|
for (const k of keys) {
|
|
34
|
-
if (k !==
|
|
44
|
+
if (k !== sort) {
|
|
35
45
|
const v = obj[k]
|
|
36
46
|
if (typeof v === "string") {
|
|
37
47
|
arr.push(`${k}=${encodeURI(v)}`)
|
|
@@ -45,6 +55,27 @@ export function removeSort(obj: Record<string, string | string[] | undefined>):
|
|
|
45
55
|
}
|
|
46
56
|
return arr.length === 0 ? "" : arr.join("&")
|
|
47
57
|
}
|
|
58
|
+
export function removeLimit(obj: Record<string, string | string[] | undefined>, limitKey?: string, pageKey?: string): string {
|
|
59
|
+
const arr: string[] = []
|
|
60
|
+
const keys = Object.keys(obj)
|
|
61
|
+
const page = pageKey ? pageKey : resources.page
|
|
62
|
+
const limit = limitKey ? limitKey : resources.limit
|
|
63
|
+
for (const k of keys) {
|
|
64
|
+
if (k !== page && k !== limit) {
|
|
65
|
+
const v = obj[k]
|
|
66
|
+
if (typeof v === "string") {
|
|
67
|
+
arr.push(`${k}=${encodeURI(v)}`)
|
|
68
|
+
} else if (Array.isArray(v)) {
|
|
69
|
+
const x = v as string[]
|
|
70
|
+
if (x.length > 0) {
|
|
71
|
+
arr.push(`${k}=${encodeURI(x[x.length - 1])}`)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return arr.length === 0 ? "" : arr.join("&")
|
|
77
|
+
}
|
|
78
|
+
|
|
48
79
|
export type SearchParams = {
|
|
49
80
|
q?: string
|
|
50
81
|
page?: string
|
|
@@ -57,7 +88,7 @@ export interface Sort {
|
|
|
57
88
|
}
|
|
58
89
|
export interface SortType {
|
|
59
90
|
url: string
|
|
60
|
-
|
|
91
|
+
type?: "+" | "-"
|
|
61
92
|
}
|
|
62
93
|
export interface SortMap {
|
|
63
94
|
[key: string]: SortType
|
|
@@ -68,10 +99,12 @@ export function getSortString(field: string, sort: Sort): string {
|
|
|
68
99
|
}
|
|
69
100
|
return field
|
|
70
101
|
}
|
|
71
|
-
export function buildFilter<T>(obj: Record<string, string | string[] | undefined>, dates?: string[], nums?: string[], arr?: string[]): T {
|
|
102
|
+
export function buildFilter<T>(obj: Record<string, string | string[] | undefined>, defaultLimit: number,dates?: string[], nums?: string[], arr?: string[], limitKey?: string, pageKey?: string): T {
|
|
72
103
|
const filter: any = fromParams<T>(obj, arr)
|
|
73
|
-
|
|
74
|
-
|
|
104
|
+
const page = pageKey ? pageKey : resources.page
|
|
105
|
+
const limit = limitKey ? limitKey : resources.limit
|
|
106
|
+
filter[page] = getPage(filter[page] as string)
|
|
107
|
+
filter[limit] = getLimit(filter[limit] as string, defaultLimit)
|
|
75
108
|
format(filter, dates, nums)
|
|
76
109
|
return filter
|
|
77
110
|
}
|
|
@@ -284,11 +317,11 @@ export function buildSortFromParams(params: Record<string, string | string[] | u
|
|
|
284
317
|
}
|
|
285
318
|
return buildSort(undefined)
|
|
286
319
|
}
|
|
287
|
-
export function
|
|
320
|
+
export function getSortType(field: string, sort: Sort): "-" | "+" | undefined {
|
|
288
321
|
if (field === sort.field) {
|
|
289
|
-
return sort.type === "-" ? "
|
|
322
|
+
return sort.type === "-" ? "-" : "+"
|
|
290
323
|
}
|
|
291
|
-
return
|
|
324
|
+
return undefined
|
|
292
325
|
}
|
|
293
326
|
export function buildSortSearch(params: Record<string, string | string[] | undefined>, fields: string[], sortStr?: string): SortMap {
|
|
294
327
|
const search = removeSort(params)
|
|
@@ -298,12 +331,11 @@ export function buildSortSearch(params: Record<string, string | string[] | undef
|
|
|
298
331
|
for (let i = 0; i < fields.length; i++) {
|
|
299
332
|
sorts[fields[i]] = {
|
|
300
333
|
url: prefix + resources.sort + "=" + getSortString(fields[i], sort),
|
|
301
|
-
|
|
334
|
+
type: getSortType(fields[i], sort),
|
|
302
335
|
}
|
|
303
336
|
}
|
|
304
337
|
return sorts
|
|
305
338
|
}
|
|
306
|
-
|
|
307
339
|
export function formatInteger(v?: number | null, groupSeparator: string = ","): string {
|
|
308
340
|
if (v == null || !Number.isFinite(v)) {
|
|
309
341
|
return ""
|
|
@@ -367,9 +399,9 @@ export function getPage(page?: string): number {
|
|
|
367
399
|
const num = getNumber(page)
|
|
368
400
|
return num === undefined || num < 1 ? 1 : num
|
|
369
401
|
}
|
|
370
|
-
export function getLimit(limit
|
|
402
|
+
export function getLimit(limit: string | undefined, defaultLimit: number): number {
|
|
371
403
|
const num = getNumber(limit)
|
|
372
|
-
return num === undefined || num < 1 ?
|
|
404
|
+
return num === undefined || num < 1 ? defaultLimit : num
|
|
373
405
|
}
|
|
374
406
|
export function getNumber(num?: string, defaultNum?: number): number | undefined {
|
|
375
407
|
return !num || num.length === 0 ? defaultNum : isNaN(num as any) ? undefined : parseInt(num, 10)
|
|
@@ -664,3 +696,18 @@ function getChildren(m: Category, all: Category[]) {
|
|
|
664
696
|
m.children = children
|
|
665
697
|
}
|
|
666
698
|
}
|
|
699
|
+
|
|
700
|
+
export function cloneArray<T>(arr: T[]): T[] {
|
|
701
|
+
return arr.map(item => ({ ...item }));
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
export function getOffset(limit: number, page?: number, firstLimit?: number): number {
|
|
705
|
+
const p = page && page > 0 ? page : 1
|
|
706
|
+
if (firstLimit && firstLimit > 0) {
|
|
707
|
+
const offset = limit * (p - 2) + firstLimit
|
|
708
|
+
return offset < 0 ? 0 : offset
|
|
709
|
+
} else {
|
|
710
|
+
const offset = limit * (p - 1)
|
|
711
|
+
return offset < 0 ? 0 : offset
|
|
712
|
+
}
|
|
713
|
+
}
|