web-one 0.0.3 → 0.0.5

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/form.js CHANGED
@@ -8,12 +8,11 @@ function fromFormData(formData, attrs, includeUndefine, includeErrorForNumbers)
8
8
  }
9
9
  exports.fromFormData = fromFormData;
10
10
  function fromFormDataWithAttributes(formData, attrs, includeUndefine, includeErrorForNumbers) {
11
- var obj = {};
12
- var keys = formData.keys();
13
- for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
14
- var key = keys_1[_i];
15
- var attr = attrs[key];
16
- var v = formData.get(key);
11
+ const obj = {};
12
+ const keys = formData.keys();
13
+ for (const key of keys) {
14
+ const attr = attrs[key];
15
+ const v = formData.get(key);
17
16
  if (attr) {
18
17
  obj[key] = v;
19
18
  if (v && typeof v === "string") {
@@ -23,7 +22,7 @@ function fromFormDataWithAttributes(formData, attrs, includeUndefine, includeErr
23
22
  }
24
23
  }
25
24
  else if (attr.type === "datetime" || attr.type === "date") {
26
- var d = new Date(v);
25
+ const d = new Date(v);
27
26
  if (d.toString() !== "Invalid Date") {
28
27
  obj[key] = d;
29
28
  }
@@ -35,11 +34,11 @@ function fromFormDataWithAttributes(formData, attrs, includeUndefine, includeErr
35
34
  obj[key] = v.split(",");
36
35
  }
37
36
  else if (attr.type === "integers" || attr.type === "numbers") {
38
- var s = v.split(",");
39
- var nums = [];
40
- for (var i = 0; i < s.length; i++) {
37
+ const s = v.split(",");
38
+ const nums = [];
39
+ for (let i = 0; i < s.length; i++) {
41
40
  if (!isNaN(s[i])) {
42
- var num = parseFloat(s[i]);
41
+ const num = parseFloat(s[i]);
43
42
  nums.push(num);
44
43
  }
45
44
  else if (includeErrorForNumbers) {
package/lib/index.js CHANGED
@@ -4,38 +4,49 @@ function __export(m) {
4
4
  }
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  __export(require("./form"));
7
- var resources = (function () {
8
- function resources() {
9
- }
10
- resources.limits = [12, 24, 60, 100, 120, 180, 300, 600];
11
- resources.page = "page";
12
- resources.limit = "limit";
13
- resources.defaultLimit = 12;
14
- resources.sort = "sort";
15
- return resources;
16
- }());
7
+ class resources {
8
+ }
17
9
  exports.resources = resources;
10
+ resources.limits = [12, 24, 60, 100, 120, 180, 300, 600];
11
+ resources.page = "page";
12
+ resources.limit = "limit";
13
+ resources.defaultLimit = 12;
14
+ resources.sort = "sort";
18
15
  function removePage(obj) {
19
- var arr = [];
20
- var keys = Object.keys(obj);
21
- for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
22
- var k = keys_1[_i];
16
+ const arr = [];
17
+ const keys = Object.keys(obj);
18
+ for (const k of keys) {
23
19
  if (k !== resources.page) {
24
- var v = obj[k];
25
- arr.push(k + "=" + encodeURI(v));
20
+ const v = obj[k];
21
+ if (typeof v === "string") {
22
+ arr.push(`${k}=${encodeURI(v)}`);
23
+ }
24
+ else if (Array.isArray(v)) {
25
+ const x = v;
26
+ if (x.length > 0) {
27
+ arr.push(`${k}=${encodeURI(x[x.length - 1])}`);
28
+ }
29
+ }
26
30
  }
27
31
  }
28
32
  return arr.length === 0 ? "" : arr.join("&");
29
33
  }
30
34
  exports.removePage = removePage;
31
35
  function removeSort(obj) {
32
- var arr = [];
33
- var keys = Object.keys(obj);
34
- for (var _i = 0, keys_2 = keys; _i < keys_2.length; _i++) {
35
- var k = keys_2[_i];
36
+ const arr = [];
37
+ const keys = Object.keys(obj);
38
+ for (const k of keys) {
36
39
  if (k !== resources.sort) {
37
- var v = obj[k];
38
- arr.push(k + "=" + encodeURI(v));
40
+ const v = obj[k];
41
+ if (typeof v === "string") {
42
+ arr.push(`${k}=${encodeURI(v)}`);
43
+ }
44
+ else if (Array.isArray(v)) {
45
+ const x = v;
46
+ if (x.length > 0) {
47
+ arr.push(`${k}=${encodeURI(x[x.length - 1])}`);
48
+ }
49
+ }
39
50
  }
40
51
  }
41
52
  return arr.length === 0 ? "" : arr.join("&");
@@ -49,7 +60,7 @@ function getSortString(field, sort) {
49
60
  }
50
61
  exports.getSortString = getSortString;
51
62
  function buildFilter(obj, dates, nums, arr) {
52
- var filter = fromParams(obj, arr);
63
+ const filter = fromParams(obj, arr);
53
64
  filter[resources.page] = getPage(filter[resources.page]);
54
65
  filter[resources.limit] = getLimit(filter[resources.limit]);
55
66
  format(filter, dates, nums);
@@ -57,16 +68,31 @@ function buildFilter(obj, dates, nums, arr) {
57
68
  }
58
69
  exports.buildFilter = buildFilter;
59
70
  function fromParams(obj, arr) {
60
- var s = {};
61
- var keys = Object.keys(obj);
62
- for (var _i = 0, keys_3 = keys; _i < keys_3.length; _i++) {
63
- var key = keys_3[_i];
71
+ const s = {};
72
+ const keys = Object.keys(obj);
73
+ for (const key of keys) {
64
74
  if (inArray(key, arr)) {
65
- var x = obj[key].split(",");
66
- setValue(s, key, x);
75
+ const v = obj[key];
76
+ if (typeof v === "string") {
77
+ const x = v.split(",");
78
+ setValue(s, key, x);
79
+ }
80
+ else if (Array.isArray(v)) {
81
+ const x = v;
82
+ setValue(s, key, x);
83
+ }
67
84
  }
68
85
  else {
69
- setValue(s, key, obj[key]);
86
+ const v = obj[key];
87
+ if (typeof v === "string") {
88
+ setValue(s, key, v);
89
+ }
90
+ else if (Array.isArray(v)) {
91
+ const x = v;
92
+ if (x.length > 0) {
93
+ setValue(s, key, x[x.length - 1]);
94
+ }
95
+ }
70
96
  }
71
97
  }
72
98
  return s;
@@ -76,8 +102,7 @@ function inArray(s, arr) {
76
102
  if (!arr || arr.length === 0) {
77
103
  return false;
78
104
  }
79
- for (var _i = 0, arr_1 = arr; _i < arr_1.length; _i++) {
80
- var a = arr_1[_i];
105
+ for (const a of arr) {
81
106
  if (s === a) {
82
107
  return true;
83
108
  }
@@ -86,26 +111,26 @@ function inArray(s, arr) {
86
111
  }
87
112
  exports.inArray = inArray;
88
113
  function setValue(o, key, value) {
89
- var obj = o;
90
- var replaceKey = key.replace(/\[/g, ".[").replace(/\.\./g, ".");
114
+ const obj = o;
115
+ let replaceKey = key.replace(/\[/g, ".[").replace(/\.\./g, ".");
91
116
  if (replaceKey.indexOf(".") === 0) {
92
117
  replaceKey = replaceKey.slice(1, replaceKey.length);
93
118
  }
94
- var keys = replaceKey.split(".");
95
- var firstKey = keys.shift();
119
+ const keys = replaceKey.split(".");
120
+ const firstKey = keys.shift();
96
121
  if (!firstKey) {
97
122
  return;
98
123
  }
99
- var isArrayKey = /\[([0-9]+)\]/.test(firstKey);
124
+ const isArrayKey = /\[([0-9]+)\]/.test(firstKey);
100
125
  if (keys.length > 0) {
101
- var firstKeyValue = obj[firstKey] || {};
102
- var returnValue = setValue(firstKeyValue, keys.join("."), value);
126
+ const firstKeyValue = obj[firstKey] || {};
127
+ const returnValue = setValue(firstKeyValue, keys.join("."), value);
103
128
  return setKey(obj, isArrayKey, firstKey, returnValue);
104
129
  }
105
130
  return setKey(obj, isArrayKey, firstKey, value);
106
131
  }
107
132
  exports.setValue = setValue;
108
- var setKey = function (_object, _isArrayKey, _key, _nextValue) {
133
+ const setKey = (_object, _isArrayKey, _key, _nextValue) => {
109
134
  if (_isArrayKey) {
110
135
  if (_object.length > _key) {
111
136
  _object[_key] = _nextValue;
@@ -119,8 +144,8 @@ var setKey = function (_object, _isArrayKey, _key, _nextValue) {
119
144
  }
120
145
  return _object;
121
146
  };
122
- var _datereg = "/Date(";
123
- var _re = /-?\d+/;
147
+ const _datereg = "/Date(";
148
+ const _re = /-?\d+/;
124
149
  function toDate(v) {
125
150
  if (!v) {
126
151
  return null;
@@ -131,11 +156,11 @@ function toDate(v) {
131
156
  else if (typeof v === "number") {
132
157
  return new Date(v);
133
158
  }
134
- var i = v.indexOf(_datereg);
159
+ const i = v.indexOf(_datereg);
135
160
  if (i >= 0) {
136
- var m = _re.exec(v);
161
+ const m = _re.exec(v);
137
162
  if (m !== null) {
138
- var d = parseInt(m[0], 10);
163
+ const d = parseInt(m[0], 10);
139
164
  return new Date(d);
140
165
  }
141
166
  else {
@@ -147,23 +172,22 @@ function toDate(v) {
147
172
  return new Date(v);
148
173
  }
149
174
  else {
150
- var d = parseInt(v, 10);
175
+ const d = parseInt(v, 10);
151
176
  return new Date(d);
152
177
  }
153
178
  }
154
179
  }
155
180
  function format(obj, dates, nums) {
156
- var o = obj;
181
+ const o = obj;
157
182
  if (dates && dates.length > 0) {
158
- for (var _i = 0, dates_1 = dates; _i < dates_1.length; _i++) {
159
- var s = dates_1[_i];
160
- var v = o[s];
183
+ for (const s of dates) {
184
+ const v = o[s];
161
185
  if (v) {
162
186
  if (v instanceof Date) {
163
187
  continue;
164
188
  }
165
189
  if (typeof v === "string" || typeof v === "number") {
166
- var d = toDate(v);
190
+ const d = toDate(v);
167
191
  if (d) {
168
192
  if (!(d instanceof Date) || d.toString() === "Invalid Date") {
169
193
  delete o[s];
@@ -174,15 +198,14 @@ function format(obj, dates, nums) {
174
198
  }
175
199
  }
176
200
  else if (typeof v === "object") {
177
- var keys = Object.keys(v);
178
- for (var _a = 0, keys_4 = keys; _a < keys_4.length; _a++) {
179
- var key = keys_4[_a];
180
- var v2 = v[key];
201
+ const keys = Object.keys(v);
202
+ for (const key of keys) {
203
+ const v2 = v[key];
181
204
  if (v2 instanceof Date) {
182
205
  continue;
183
206
  }
184
207
  if (typeof v2 === "string" || typeof v2 === "number") {
185
- var d2 = toDate(v2);
208
+ const d2 = toDate(v2);
186
209
  if (d2) {
187
210
  if (!(d2 instanceof Date) || d2.toString() === "Invalid Date") {
188
211
  delete v[key];
@@ -198,9 +221,8 @@ function format(obj, dates, nums) {
198
221
  }
199
222
  }
200
223
  if (nums && nums.length > 0) {
201
- for (var _b = 0, nums_1 = nums; _b < nums_1.length; _b++) {
202
- var s = nums_1[_b];
203
- var v = o[s];
224
+ for (const s of nums) {
225
+ const v = o[s];
204
226
  if (v) {
205
227
  if (v instanceof Date) {
206
228
  delete o[s];
@@ -215,15 +237,14 @@ function format(obj, dates, nums) {
215
237
  continue;
216
238
  }
217
239
  else {
218
- var i = parseFloat(v);
240
+ const i = parseFloat(v);
219
241
  o[s] = i;
220
242
  }
221
243
  }
222
244
  else if (typeof v === "object") {
223
- var keys = Object.keys(v);
224
- for (var _c = 0, keys_5 = keys; _c < keys_5.length; _c++) {
225
- var key = keys_5[_c];
226
- var v2 = v[key];
245
+ const keys = Object.keys(v);
246
+ for (const key of keys) {
247
+ const v2 = v[key];
227
248
  if (v2 instanceof Date) {
228
249
  delete o[key];
229
250
  continue;
@@ -236,7 +257,7 @@ function format(obj, dates, nums) {
236
257
  delete v[key];
237
258
  }
238
259
  else {
239
- var i = parseFloat(v2);
260
+ const i = parseFloat(v2);
240
261
  v[key] = i;
241
262
  }
242
263
  }
@@ -261,8 +282,19 @@ function buildSort(s) {
261
282
  }
262
283
  exports.buildSort = buildSort;
263
284
  function buildSortFromParams(params) {
264
- var s = params[resources.sort];
265
- return buildSort(s);
285
+ const s = params[resources.sort];
286
+ if (s !== undefined) {
287
+ if (typeof s === "string") {
288
+ return buildSort(s);
289
+ }
290
+ else if (Array.isArray(s)) {
291
+ const x = s;
292
+ if (x.length > 0) {
293
+ return buildSort(x[x.length - 1]);
294
+ }
295
+ }
296
+ }
297
+ return buildSort(undefined);
266
298
  }
267
299
  exports.buildSortFromParams = buildSortFromParams;
268
300
  function renderSort(field, sort) {
@@ -273,11 +305,11 @@ function renderSort(field, sort) {
273
305
  }
274
306
  exports.renderSort = renderSort;
275
307
  function buildSortSearch(params, fields, sortStr) {
276
- var search = removeSort(params);
277
- var sort = buildSort(sortStr);
278
- var sorts = {};
279
- var prefix = search.length > 0 ? "?" + search + "&" : "?";
280
- for (var i = 0; i < fields.length; i++) {
308
+ const search = removeSort(params);
309
+ const sort = buildSort(sortStr);
310
+ let sorts = {};
311
+ const prefix = search.length > 0 ? "?" + search + "&" : "?";
312
+ for (let i = 0; i < fields.length; i++) {
281
313
  sorts[fields[i]] = {
282
314
  url: prefix + resources.sort + "=" + getSortString(fields[i], sort),
283
315
  tag: renderSort(fields[i], sort),
@@ -286,20 +318,19 @@ function buildSortSearch(params, fields, sortStr) {
286
318
  return sorts;
287
319
  }
288
320
  exports.buildSortSearch = buildSortSearch;
289
- function formatInteger(v, groupSeparator) {
290
- if (groupSeparator === void 0) { groupSeparator = ","; }
321
+ function formatInteger(v, groupSeparator = ",") {
291
322
  if (v == null || !Number.isFinite(v)) {
292
323
  return "";
293
324
  }
294
- var isNegative = v < 0;
295
- var n = Math.abs(Math.trunc(v));
325
+ const isNegative = v < 0;
326
+ let n = Math.abs(Math.trunc(v));
296
327
  if (n < 1000) {
297
- return isNegative ? "-" + n : "" + n;
328
+ return isNegative ? `-${n}` : `${n}`;
298
329
  }
299
- var result = "";
300
- var count = 0;
330
+ let result = "";
331
+ let count = 0;
301
332
  while (n > 0) {
302
- var digit = n % 10;
333
+ const digit = n % 10;
303
334
  n = (n / 10) | 0;
304
335
  if (count > 0 && count % 3 === 0) {
305
336
  result = groupSeparator + result;
@@ -307,7 +338,7 @@ function formatInteger(v, groupSeparator) {
307
338
  result = digit + result;
308
339
  count++;
309
340
  }
310
- return isNegative ? "-" + result : result;
341
+ return isNegative ? `-${result}` : result;
311
342
  }
312
343
  exports.formatInteger = formatInteger;
313
344
  function formatNumber(v, scale, d, g) {
@@ -321,12 +352,12 @@ function formatNumber(v, scale, d, g) {
321
352
  else if (!g) {
322
353
  g = d === "," ? "." : ",";
323
354
  }
324
- var s = scale === 0 || scale ? v.toFixed(scale) : v.toString();
325
- var x = s.split(".", 2);
326
- var y = x[0];
327
- var arr = [];
328
- var len = y.length - 1;
329
- for (var k = 0; k < len; k++) {
355
+ const s = scale === 0 || scale ? v.toFixed(scale) : v.toString();
356
+ const x = s.split(".", 2);
357
+ const y = x[0];
358
+ const arr = [];
359
+ const len = y.length - 1;
360
+ for (let k = 0; k < len; k++) {
330
361
  arr.push(y[len - k]);
331
362
  if ((k + 1) % 3 === 0) {
332
363
  arr.push(g);
@@ -341,17 +372,13 @@ function formatNumber(v, scale, d, g) {
341
372
  }
342
373
  }
343
374
  exports.formatNumber = formatNumber;
344
- function getDateFormat(profile) {
345
- return "M/D/YYYY";
346
- }
347
- exports.getDateFormat = getDateFormat;
348
375
  function getPage(page) {
349
- var num = getNumber(page);
376
+ const num = getNumber(page);
350
377
  return num === undefined || num < 1 ? 1 : num;
351
378
  }
352
379
  exports.getPage = getPage;
353
380
  function getLimit(limit) {
354
- var num = getNumber(limit);
381
+ const num = getNumber(limit);
355
382
  return num === undefined || num < 1 ? resources.defaultLimit : num;
356
383
  }
357
384
  exports.getLimit = getLimit;
@@ -370,19 +397,17 @@ function clone(obj) {
370
397
  return obj;
371
398
  }
372
399
  if (Array.isArray(obj)) {
373
- var arr = [];
374
- for (var _i = 0, obj_1 = obj; _i < obj_1.length; _i++) {
375
- var sub = obj_1[_i];
376
- var c = clone(sub);
400
+ const arr = [];
401
+ for (const sub of obj) {
402
+ const c = clone(sub);
377
403
  arr.push(c);
378
404
  }
379
405
  return arr;
380
406
  }
381
- var x = {};
382
- var keys = Object.keys(obj);
383
- for (var _a = 0, keys_6 = keys; _a < keys_6.length; _a++) {
384
- var k = keys_6[_a];
385
- var v = obj[k];
407
+ const x = {};
408
+ const keys = Object.keys(obj);
409
+ for (const k of keys) {
410
+ const v = obj[k];
386
411
  if (v instanceof Date) {
387
412
  x[k] = new Date(v.getTime());
388
413
  }
@@ -404,29 +429,29 @@ function datetimeToString(date) {
404
429
  if (!date || date === "") {
405
430
  return undefined;
406
431
  }
407
- var d2 = typeof date !== "string" ? date : new Date(date);
408
- var year = d2.getFullYear();
409
- var month = pad(d2.getMonth() + 1);
410
- var day = pad(d2.getDate());
411
- var hours = pad(d2.getHours());
412
- var minutes = pad(d2.getMinutes());
413
- var seconds = pad(d2.getSeconds());
414
- return year + "-" + month + "-" + day + "T" + hours + ":" + minutes + ":" + seconds;
432
+ const d2 = typeof date !== "string" ? date : new Date(date);
433
+ const year = d2.getFullYear();
434
+ const month = pad(d2.getMonth() + 1);
435
+ const day = pad(d2.getDate());
436
+ const hours = pad(d2.getHours());
437
+ const minutes = pad(d2.getMinutes());
438
+ const seconds = pad(d2.getSeconds());
439
+ return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}`;
415
440
  }
416
441
  exports.datetimeToString = datetimeToString;
417
442
  function formatDate(d, format) {
418
443
  if (!d || !format) {
419
444
  return "";
420
445
  }
421
- var y = d.getFullYear();
422
- var m = d.getMonth() + 1;
423
- var day = d.getDate();
424
- var out = "";
425
- var i = 0;
446
+ const y = d.getFullYear();
447
+ const m = d.getMonth() + 1;
448
+ const day = d.getDate();
449
+ let out = "";
450
+ let i = 0;
426
451
  while (i < format.length) {
427
- var c = format.charCodeAt(i);
452
+ const c = format.charCodeAt(i);
428
453
  if (c === 121) {
429
- var len = count(format, i, 121);
454
+ const len = count(format, i, 121);
430
455
  if (len >= 4) {
431
456
  out += y.toString();
432
457
  i += 4;
@@ -438,13 +463,13 @@ function formatDate(d, format) {
438
463
  continue;
439
464
  }
440
465
  if (c === 77) {
441
- var len = count(format, i, 77);
466
+ const len = count(format, i, 77);
442
467
  out += len >= 2 ? pad(m) : m.toString();
443
468
  i += len >= 2 ? 2 : 1;
444
469
  continue;
445
470
  }
446
471
  if (c === 100) {
447
- var len = count(format, i, 100);
472
+ const len = count(format, i, 100);
448
473
  out += len >= 2 ? pad(day) : day.toString();
449
474
  i += len >= 2 ? 2 : 1;
450
475
  continue;
@@ -461,7 +486,7 @@ function shortYear(y) {
461
486
  : "" + ((y % 100 + 100) % 100);
462
487
  }
463
488
  function count(s, i, ch) {
464
- var n = 0;
489
+ let n = 0;
465
490
  while (i + n < s.length && s.charCodeAt(i + n) === ch) {
466
491
  n++;
467
492
  }
@@ -471,7 +496,7 @@ function formatDateTime(date, dateFormat) {
471
496
  if (!date) {
472
497
  return "";
473
498
  }
474
- var sd = formatDate(date, dateFormat);
499
+ const sd = formatDate(date, dateFormat);
475
500
  if (sd.length === 0) {
476
501
  return sd;
477
502
  }
@@ -482,7 +507,7 @@ function formatLongDateTime(date, dateFormat) {
482
507
  if (!date) {
483
508
  return "";
484
509
  }
485
- var sd = formatDate(date, dateFormat);
510
+ const sd = formatDate(date, dateFormat);
486
511
  if (sd.length === 0) {
487
512
  return sd;
488
513
  }
@@ -493,7 +518,7 @@ function formatFullDateTime(date, dateFormat, s) {
493
518
  if (!date) {
494
519
  return "";
495
520
  }
496
- var sd = formatDate(date, dateFormat);
521
+ const sd = formatDate(date, dateFormat);
497
522
  if (sd.length === 0) {
498
523
  return sd;
499
524
  }
@@ -509,12 +534,12 @@ function formatLongTime(d) {
509
534
  }
510
535
  exports.formatLongTime = formatLongTime;
511
536
  function formatFullTime(d, s) {
512
- var se = s && s.length > 0 ? s : ".";
537
+ const se = s && s.length > 0 ? s : ".";
513
538
  return formatLongTime(d) + se + pad3(d.getMilliseconds());
514
539
  }
515
540
  exports.formatFullTime = formatFullTime;
516
541
  function dateToString(d, milli) {
517
- var s = "" + d.getFullYear() + pad(d.getMonth() + 1) + pad(d.getDate()) + pad(d.getHours()) + pad(d.getMinutes()) + pad(d.getSeconds());
542
+ const s = `${d.getFullYear()}${pad(d.getMonth() + 1)}${pad(d.getDate())}${pad(d.getHours())}${pad(d.getMinutes())}${pad(d.getSeconds())}`;
518
543
  if (milli) {
519
544
  return s + pad3(d.getMilliseconds());
520
545
  }
@@ -530,50 +555,108 @@ function pad3(n) {
530
555
  }
531
556
  return n < 10 ? "00" + n : "0" + n.toString();
532
557
  }
533
- var formatter = (function () {
534
- function formatter() {
535
- }
536
- formatter.removePhoneFormat = function (phone) {
558
+ class formatter {
559
+ static removePhoneFormat(phone) {
537
560
  if (phone) {
538
561
  return phone.replace(formatter.phone, "");
539
562
  }
540
563
  else {
541
564
  return phone;
542
565
  }
543
- };
544
- formatter.formatPhone = function (phone) {
566
+ }
567
+ static formatPhone(phone) {
545
568
  if (!phone) {
546
569
  return "";
547
570
  }
548
- var s = phone;
549
- var x = formatter.removePhoneFormat(phone);
571
+ let s = phone;
572
+ const x = formatter.removePhoneFormat(phone);
550
573
  if (x.length === 10) {
551
- var USNumber = x.match(formatter.usPhone);
574
+ const USNumber = x.match(formatter.usPhone);
552
575
  if (USNumber != null) {
553
- s = USNumber[1] + " " + USNumber[2] + "-" + USNumber[3];
576
+ s = `${USNumber[1]} ${USNumber[2]}-${USNumber[3]}`;
554
577
  }
555
578
  }
556
579
  else if (x.length <= 3 && x.length > 0) {
557
580
  s = x;
558
581
  }
559
582
  else if (x.length > 3 && x.length < 7) {
560
- s = x.substring(0, 3) + " " + x.substring(3, x.length);
583
+ s = `${x.substring(0, 3)} ${x.substring(3, x.length)}`;
561
584
  }
562
585
  else if (x.length >= 7 && x.length < 10) {
563
- s = x.substring(0, 3) + " " + x.substring(3, 6) + "-" + x.substring(6, x.length);
586
+ s = `${x.substring(0, 3)} ${x.substring(3, 6)}-${x.substring(6, x.length)}`;
564
587
  }
565
588
  else if (x.length >= 11) {
566
- var l = x.length;
567
- s = x.substring(0, l - 7) + " " + x.substring(l - 7, l - 4) + "-" + x.substring(l - 4, l);
589
+ const l = x.length;
590
+ s = `${x.substring(0, l - 7)} ${x.substring(l - 7, l - 4)}-${x.substring(l - 4, l)}`;
568
591
  }
569
592
  return s;
570
- };
571
- formatter.phone = / |\-|\.|\(|\)/g;
572
- formatter.usPhone = /(\d{3})(\d{3})(\d{4})/;
573
- return formatter;
574
- }());
593
+ }
594
+ }
575
595
  exports.formatter = formatter;
596
+ formatter.phone = / |\-|\.|\(|\)/g;
597
+ formatter.usPhone = /(\d{3})(\d{3})(\d{4})/;
576
598
  function formatPhone(phone) {
577
599
  return formatter.formatPhone(phone);
578
600
  }
579
601
  exports.formatPhone = formatPhone;
602
+ function rebuildPath(items, lang) {
603
+ for (const item of items) {
604
+ item.path = item.type === "content" ? `/${lang}${item.path}` : `${item.path}?lang=${lang}`;
605
+ const children = item.children;
606
+ if (children && children.length > 0) {
607
+ rebuildPath(children, lang);
608
+ }
609
+ }
610
+ }
611
+ exports.rebuildPath = rebuildPath;
612
+ function sub(n1, n2) {
613
+ if (!n1 && !n2) {
614
+ return 0;
615
+ }
616
+ else if (n1 && n2) {
617
+ return n1 - n2;
618
+ }
619
+ else if (n1) {
620
+ return n1;
621
+ }
622
+ else if (n2) {
623
+ return -n2;
624
+ }
625
+ return 0;
626
+ }
627
+ exports.sub = sub;
628
+ function subMenuItem(p1, p2) {
629
+ return sub(p1.sequence, p2.sequence);
630
+ }
631
+ function toMenuItems(m) {
632
+ const ps = getRoot(m);
633
+ for (const p of ps) {
634
+ getChildren(p, m);
635
+ }
636
+ return ps.sort(subMenuItem);
637
+ }
638
+ exports.toMenuItems = toMenuItems;
639
+ function getRoot(ms) {
640
+ const ps = [];
641
+ for (const m of ms) {
642
+ if (!m.parent || m.parent.length === 0) {
643
+ delete m.parent;
644
+ ps.push(m);
645
+ }
646
+ }
647
+ return ps.sort(subMenuItem);
648
+ }
649
+ function getChildren(m, all) {
650
+ const children = [];
651
+ for (const s of all) {
652
+ if (s.parent === m.id) {
653
+ delete s.parent;
654
+ children.push(s);
655
+ getChildren(s, all);
656
+ }
657
+ }
658
+ if (children.length > 0) {
659
+ children.sort(subMenuItem);
660
+ m.children = children;
661
+ }
662
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "web-one",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "Web utilities",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
package/src/index.ts CHANGED
@@ -7,32 +7,40 @@ export class resources {
7
7
  static defaultLimit = 12
8
8
  static sort = "sort"
9
9
  }
10
- export interface StringMap {
11
- [key: string]: string
12
- }
13
- export interface Params {
14
- params: StringMap
15
- searchParams: StringMap
16
- }
17
- export function removePage(obj: StringMap): string {
10
+ export type StringMap = Record<string, string | string[] | undefined>
11
+ export function removePage(obj: Record<string, string | string[] | undefined>): string {
18
12
  const arr: string[] = []
19
13
  const keys = Object.keys(obj)
20
14
  for (const k of keys) {
21
15
  if (k !== resources.page) {
22
16
  const v = obj[k]
23
- arr.push(`${k}=${encodeURI(v)}`)
17
+ if (typeof v === "string") {
18
+ arr.push(`${k}=${encodeURI(v)}`)
19
+ } else if (Array.isArray(v)) {
20
+ const x = v as string[]
21
+ if (x.length > 0) {
22
+ arr.push(`${k}=${encodeURI(x[x.length - 1])}`)
23
+ }
24
+ }
24
25
  }
25
26
  }
26
27
  return arr.length === 0 ? "" : arr.join("&")
27
28
  }
28
29
 
29
- export function removeSort(obj: StringMap): string {
30
+ export function removeSort(obj: Record<string, string | string[] | undefined>): string {
30
31
  const arr: string[] = []
31
32
  const keys = Object.keys(obj)
32
33
  for (const k of keys) {
33
34
  if (k !== resources.sort) {
34
35
  const v = obj[k]
35
- arr.push(`${k}=${encodeURI(v)}`)
36
+ if (typeof v === "string") {
37
+ arr.push(`${k}=${encodeURI(v)}`)
38
+ } else if (Array.isArray(v)) {
39
+ const x = v as string[]
40
+ if (x.length > 0) {
41
+ arr.push(`${k}=${encodeURI(x[x.length - 1])}`)
42
+ }
43
+ }
36
44
  }
37
45
  }
38
46
  return arr.length === 0 ? "" : arr.join("&")
@@ -60,22 +68,37 @@ export function getSortString(field: string, sort: Sort): string {
60
68
  }
61
69
  return field
62
70
  }
63
- export function buildFilter<T>(obj: StringMap, dates?: string[], nums?: string[], arr?: string[]): T {
71
+ export function buildFilter<T>(obj: Record<string, string | string[] | undefined>, dates?: string[], nums?: string[], arr?: string[]): T {
64
72
  const filter: any = fromParams<T>(obj, arr)
65
73
  filter[resources.page] = getPage(filter[resources.page] as string)
66
74
  filter[resources.limit] = getLimit(filter[resources.limit] as string)
67
75
  format(filter, dates, nums)
68
76
  return filter
69
77
  }
70
- export function fromParams<T>(obj: StringMap, arr?: string[]): T {
78
+ export function fromParams<T>(obj: Record<string, string | string[] | undefined>, arr?: string[]): T {
71
79
  const s: any = {}
72
80
  const keys = Object.keys(obj)
73
81
  for (const key of keys) {
74
82
  if (inArray(key, arr)) {
75
- const x = (obj[key] as string).split(",")
76
- setValue(s, key, x)
83
+ const v = obj[key]
84
+ if (typeof v === "string") {
85
+ const x = v.split(",")
86
+ setValue(s, key, x)
87
+ } else if (Array.isArray(v)) {
88
+ const x: string[] = v as string[]
89
+ setValue(s, key, x)
90
+ }
91
+
77
92
  } else {
78
- setValue(s, key, obj[key] as string)
93
+ const v = obj[key]
94
+ if (typeof v === "string") {
95
+ setValue(s, key, v)
96
+ } else if (Array.isArray(v)) {
97
+ const x: string[] = v as string[]
98
+ if (x.length > 0) {
99
+ setValue(s, key, x[x.length - 1])
100
+ }
101
+ }
79
102
  }
80
103
  }
81
104
  return s
@@ -247,9 +270,19 @@ export function buildSort(s?: string): Sort {
247
270
  return { field: s.startsWith("+") ? s.substring(1) : s, type: "+" }
248
271
  }
249
272
  }
250
- export function buildSortFromParams(params: StringMap): Sort {
251
- const s = params[resources.sort] as string
252
- return buildSort(s)
273
+ export function buildSortFromParams(params: Record<string, string | string[] | undefined>): Sort {
274
+ const s = params[resources.sort]
275
+ if (s !== undefined) {
276
+ if (typeof s === "string") {
277
+ return buildSort(s)
278
+ } else if (Array.isArray(s)) {
279
+ const x: string[] = s as string[]
280
+ if (x.length > 0) {
281
+ return buildSort(x[x.length - 1])
282
+ }
283
+ }
284
+ }
285
+ return buildSort(undefined)
253
286
  }
254
287
  export function renderSort(field: string, sort: Sort): string {
255
288
  if (field === sort.field) {
@@ -257,7 +290,7 @@ export function renderSort(field: string, sort: Sort): string {
257
290
  }
258
291
  return ""
259
292
  }
260
- export function buildSortSearch(params: StringMap, fields: string[], sortStr?: string): SortMap {
293
+ export function buildSortSearch(params: Record<string, string | string[] | undefined>, fields: string[], sortStr?: string): SortMap {
261
294
  const search = removeSort(params)
262
295
  const sort = buildSort(sortStr)
263
296
  let sorts: SortMap = {}
@@ -330,9 +363,6 @@ export function formatNumber(v?: number | null, scale?: number, d?: string | nul
330
363
  }
331
364
  }
332
365
 
333
- export function getDateFormat(profile?: string): string {
334
- return "M/D/YYYY"
335
- }
336
366
  export function getPage(page?: string): number {
337
367
  const num = getNumber(page)
338
368
  return num === undefined || num < 1 ? 1 : num
@@ -554,3 +584,83 @@ export class formatter {
554
584
  export function formatPhone(phone?: string | null): string {
555
585
  return formatter.formatPhone(phone)
556
586
  }
587
+
588
+ export interface MenuItem {
589
+ id: string
590
+ name: string
591
+ path: string
592
+ resource?: string
593
+ icon?: string
594
+ sequence?: number
595
+ prefetch?: boolean
596
+ type?: string
597
+ children?: MenuItem[]
598
+ }
599
+ export interface Category {
600
+ id: string
601
+ name: string
602
+ path: string
603
+ resource?: string
604
+ icon?: string
605
+ sequence?: number
606
+ prefetch?: boolean
607
+ type?: string
608
+ parent?: string
609
+ children?: MenuItem[]
610
+ }
611
+ export function rebuildPath(items: MenuItem[], lang: string) {
612
+ for (const item of items) {
613
+ item.path = item.type === "content" ? `/${lang}${item.path}` : `${item.path}?lang=${lang}`
614
+ const children = item.children
615
+ if (children && children.length > 0) {
616
+ rebuildPath(children, lang)
617
+ }
618
+ }
619
+ }
620
+
621
+ export function sub(n1?: number, n2?: number): number {
622
+ if (!n1 && !n2) {
623
+ return 0
624
+ } else if (n1 && n2) {
625
+ return n1 - n2
626
+ } else if (n1) {
627
+ return n1
628
+ } else if (n2) {
629
+ return -n2
630
+ }
631
+ return 0
632
+ }
633
+ function subMenuItem(p1: MenuItem, p2: MenuItem): number {
634
+ return sub(p1.sequence, p2.sequence)
635
+ }
636
+ export function toMenuItems(m: Category[]): MenuItem[] {
637
+ const ps: Category[] = getRoot(m)
638
+ for (const p of ps) {
639
+ getChildren(p, m)
640
+ }
641
+ return ps.sort(subMenuItem)
642
+ }
643
+ function getRoot(ms: Category[]): Category[] {
644
+ const ps: Category[] = []
645
+ for (const m of ms) {
646
+ if (!m.parent || m.parent.length === 0) {
647
+ delete m.parent
648
+ ps.push(m)
649
+ }
650
+ }
651
+ return ps.sort(subMenuItem)
652
+ }
653
+ function getChildren(m: Category, all: Category[]) {
654
+ const children: MenuItem[] = []
655
+ for (const s of all) {
656
+ if (s.parent === m.id) {
657
+ delete s.parent
658
+ children.push(s)
659
+ getChildren(s, all)
660
+ }
661
+ }
662
+ if (children.length > 0) {
663
+ children.sort(subMenuItem)
664
+ m.children = children
665
+ }
666
+ }
package/tsconfig.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "compileOnSave": true,
3
3
  "compilerOptions": {
4
- "target": "es5",
4
+ "target": "es6",
5
5
  "baseUrl": "./src",
6
6
  "outDir": "./lib",
7
7
  "module": "commonjs",