web-one 0.0.7 → 0.0.9

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 CHANGED
@@ -354,6 +354,35 @@ function buildSortSearch(params, fields, sortStr) {
354
354
  return sorts;
355
355
  }
356
356
  exports.buildSortSearch = buildSortSearch;
357
+ function buildSorts(sorts, prefix) {
358
+ const l = sorts.length;
359
+ for (let i = 0; i < l; i++) {
360
+ sorts[i].value = prefix + sorts[i].value;
361
+ }
362
+ }
363
+ exports.buildSorts = buildSorts;
364
+ function getSortText(sorts, sort, defaultText, textOnly) {
365
+ if (!sort) {
366
+ return defaultText;
367
+ }
368
+ const l = sorts.length;
369
+ if (textOnly) {
370
+ for (let i = 0; i < l; i++) {
371
+ if (sorts[i].value === sort) {
372
+ return sorts[i].text;
373
+ }
374
+ }
375
+ }
376
+ else {
377
+ for (let i = 0; i < l; i++) {
378
+ if (sorts[i].value === sort) {
379
+ return sorts[i].fulltext;
380
+ }
381
+ }
382
+ }
383
+ return defaultText;
384
+ }
385
+ exports.getSortText = getSortText;
357
386
  function formatInteger(v, groupSeparator = ",") {
358
387
  if (v == null || !Number.isFinite(v)) {
359
388
  return "";
@@ -645,6 +674,22 @@ function rebuildPath(items, lang) {
645
674
  }
646
675
  }
647
676
  exports.rebuildPath = rebuildPath;
677
+ function localize(items, resource) {
678
+ for (const item of items) {
679
+ if (item.resource) {
680
+ const text = resource[item.resource];
681
+ if (text) {
682
+ item.name = text;
683
+ }
684
+ }
685
+ const children = item.children;
686
+ if (children && children.length > 0) {
687
+ localize(children, resource);
688
+ }
689
+ }
690
+ return items;
691
+ }
692
+ exports.localize = localize;
648
693
  function sub(n1, n2) {
649
694
  if (!n1 && !n2) {
650
695
  return 0;
@@ -696,10 +741,6 @@ function getChildren(m, all) {
696
741
  m.children = children;
697
742
  }
698
743
  }
699
- function cloneArray(arr) {
700
- return arr.map(item => (Object.assign({}, item)));
701
- }
702
- exports.cloneArray = cloneArray;
703
744
  function getOffset(limit, page, firstLimit) {
704
745
  const p = page && page > 0 ? page : 1;
705
746
  if (firstLimit && firstLimit > 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "web-one",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "description": "Web utilities",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
package/src/index.ts CHANGED
@@ -15,7 +15,7 @@ export function getRecordValue(v: string | string[] | undefined): string | undef
15
15
  }
16
16
  return undefined
17
17
  }
18
- export type StringMap = Record<string, string | string[] | undefined>
18
+ // export type StringMap = Record<string, string | string[] | undefined>
19
19
  export function removePage(obj: Record<string, string | string[] | undefined>, pageKey?: string): string {
20
20
  const arr: string[] = []
21
21
  const keys = Object.keys(obj)
@@ -336,6 +336,38 @@ export function buildSortSearch(params: Record<string, string | string[] | undef
336
336
  }
337
337
  return sorts
338
338
  }
339
+ export interface Item {
340
+ id?: string
341
+ value: string
342
+ text?: string
343
+ fulltext?: string
344
+ }
345
+ export function buildSorts(sorts: Item[], prefix: string) {
346
+ const l = sorts.length
347
+ for (let i = 0; i < l; i++) {
348
+ sorts[i].value = prefix + sorts[i].value
349
+ }
350
+ }
351
+ export function getSortText(sorts: Item[], sort: string | undefined, defaultText?: string, textOnly?: boolean): string | undefined {
352
+ if (!sort) {
353
+ return defaultText
354
+ }
355
+ const l = sorts.length
356
+ if (textOnly) {
357
+ for (let i = 0; i < l; i++) {
358
+ if (sorts[i].value === sort) {
359
+ return sorts[i].text
360
+ }
361
+ }
362
+ } else {
363
+ for (let i = 0; i < l; i++) {
364
+ if (sorts[i].value === sort) {
365
+ return sorts[i].fulltext
366
+ }
367
+ }
368
+ }
369
+ return defaultText
370
+ }
339
371
  export function formatInteger(v?: number | null, groupSeparator: string = ","): string {
340
372
  if (v == null || !Number.isFinite(v)) {
341
373
  return ""
@@ -406,12 +438,12 @@ export function getLimit(limit: string | undefined, defaultLimit: number): numbe
406
438
  export function getNumber(num?: string, defaultNum?: number): number | undefined {
407
439
  return !num || num.length === 0 ? defaultNum : isNaN(num as any) ? undefined : parseInt(num, 10)
408
440
  }
409
- export function clone(obj: any): any {
441
+ export function clone<T>(obj: T): T {
410
442
  if (!obj) {
411
443
  return obj
412
444
  }
413
445
  if (obj instanceof Date) {
414
- return new Date(obj.getTime())
446
+ return new Date(obj.getTime()) as any
415
447
  }
416
448
  if (typeof obj !== "object") {
417
449
  return obj
@@ -422,12 +454,12 @@ export function clone(obj: any): any {
422
454
  const c = clone(sub)
423
455
  arr.push(c)
424
456
  }
425
- return arr
457
+ return arr as any
426
458
  }
427
459
  const x: any = {}
428
460
  const keys = Object.keys(obj)
429
461
  for (const k of keys) {
430
- const v = obj[k]
462
+ const v = (obj as any)[k]
431
463
  if (v instanceof Date) {
432
464
  x[k] = new Date(v.getTime())
433
465
  } else {
@@ -618,7 +650,7 @@ export function formatPhone(phone?: string | null): string {
618
650
  }
619
651
 
620
652
  export interface MenuItem {
621
- id: string
653
+ id?: string
622
654
  name: string
623
655
  path: string
624
656
  resource?: string
@@ -649,6 +681,26 @@ export function rebuildPath(items: MenuItem[], lang: string) {
649
681
  }
650
682
  }
651
683
  }
684
+ interface StringMap2 {
685
+ [key: string]: string;
686
+ }
687
+ export function localize(items: MenuItem[], resource: StringMap2): MenuItem[] {
688
+ for (const item of items) {
689
+ if (item.resource) {
690
+ const text = resource[item.resource];
691
+ if (text) {
692
+ item.name = text;
693
+ }
694
+ }
695
+
696
+ const children = item.children;
697
+ if (children && children.length > 0) {
698
+ localize(children, resource);
699
+ }
700
+ }
701
+
702
+ return items;
703
+ }
652
704
 
653
705
  export function sub(n1?: number, n2?: number): number {
654
706
  if (!n1 && !n2) {
@@ -697,10 +749,6 @@ function getChildren(m: Category, all: Category[]) {
697
749
  }
698
750
  }
699
751
 
700
- export function cloneArray<T>(arr: T[]): T[] {
701
- return arr.map(item => ({ ...item }));
702
- }
703
-
704
752
  export function getOffset(limit: number, page?: number, firstLimit?: number): number {
705
753
  const p = page && page > 0 ? page : 1
706
754
  if (firstLimit && firstLimit > 0) {