web-one 0.0.5 → 0.0.6
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 +49 -9
- package/package.json +1 -1
- package/src/index.ts +46 -9
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
|
}
|
|
@@ -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,7 @@ 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;
|
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,32 @@ 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 {
|
|
40
|
+
const arr: string[] = []
|
|
41
|
+
const keys = Object.keys(obj)
|
|
42
|
+
const sort = sortKey ? sortKey : resources.sort
|
|
43
|
+
for (const k of keys) {
|
|
44
|
+
if (k !== sort) {
|
|
45
|
+
const v = obj[k]
|
|
46
|
+
if (typeof v === "string") {
|
|
47
|
+
arr.push(`${k}=${encodeURI(v)}`)
|
|
48
|
+
} else if (Array.isArray(v)) {
|
|
49
|
+
const x = v as string[]
|
|
50
|
+
if (x.length > 0) {
|
|
51
|
+
arr.push(`${k}=${encodeURI(x[x.length - 1])}`)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return arr.length === 0 ? "" : arr.join("&")
|
|
57
|
+
}
|
|
58
|
+
export function removeLimit(obj: Record<string, string | string[] | undefined>, limitKey?: string, pageKey?: string): string {
|
|
31
59
|
const arr: string[] = []
|
|
32
60
|
const keys = Object.keys(obj)
|
|
61
|
+
const page = pageKey ? pageKey : resources.page
|
|
62
|
+
const limit = limitKey ? limitKey : resources.limit
|
|
33
63
|
for (const k of keys) {
|
|
34
|
-
if (k !==
|
|
64
|
+
if (k !== page && k !== limit) {
|
|
35
65
|
const v = obj[k]
|
|
36
66
|
if (typeof v === "string") {
|
|
37
67
|
arr.push(`${k}=${encodeURI(v)}`)
|
|
@@ -45,6 +75,7 @@ export function removeSort(obj: Record<string, string | string[] | undefined>):
|
|
|
45
75
|
}
|
|
46
76
|
return arr.length === 0 ? "" : arr.join("&")
|
|
47
77
|
}
|
|
78
|
+
|
|
48
79
|
export type SearchParams = {
|
|
49
80
|
q?: string
|
|
50
81
|
page?: string
|
|
@@ -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
|
}
|
|
@@ -367,9 +400,9 @@ export function getPage(page?: string): number {
|
|
|
367
400
|
const num = getNumber(page)
|
|
368
401
|
return num === undefined || num < 1 ? 1 : num
|
|
369
402
|
}
|
|
370
|
-
export function getLimit(limit
|
|
403
|
+
export function getLimit(limit: string | undefined, defaultLimit: number): number {
|
|
371
404
|
const num = getNumber(limit)
|
|
372
|
-
return num === undefined || num < 1 ?
|
|
405
|
+
return num === undefined || num < 1 ? defaultLimit : num
|
|
373
406
|
}
|
|
374
407
|
export function getNumber(num?: string, defaultNum?: number): number | undefined {
|
|
375
408
|
return !num || num.length === 0 ? defaultNum : isNaN(num as any) ? undefined : parseInt(num, 10)
|
|
@@ -664,3 +697,7 @@ function getChildren(m: Category, all: Category[]) {
|
|
|
664
697
|
m.children = children
|
|
665
698
|
}
|
|
666
699
|
}
|
|
700
|
+
|
|
701
|
+
export function cloneArray<T>(arr: T[]): T[] {
|
|
702
|
+
return arr.map(item => ({ ...item }));
|
|
703
|
+
}
|