web-one 0.0.8 → 0.0.10

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
@@ -674,6 +674,22 @@ function rebuildPath(items, lang) {
674
674
  }
675
675
  }
676
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;
677
693
  function sub(n1, n2) {
678
694
  if (!n1 && !n2) {
679
695
  return 0;
@@ -725,10 +741,6 @@ function getChildren(m, all) {
725
741
  m.children = children;
726
742
  }
727
743
  }
728
- function cloneArray(arr) {
729
- return arr.map(item => (Object.assign({}, item)));
730
- }
731
- exports.cloneArray = cloneArray;
732
744
  function getOffset(limit, page, firstLimit) {
733
745
  const p = page && page > 0 ? page : 1;
734
746
  if (firstLimit && firstLimit > 0) {
@@ -741,3 +753,43 @@ function getOffset(limit, page, firstLimit) {
741
753
  }
742
754
  }
743
755
  exports.getOffset = getOffset;
756
+ function isValidPath(path) {
757
+ const len = path.length;
758
+ if (len === 0) {
759
+ return false;
760
+ }
761
+ for (let i = 0; i < len; i++) {
762
+ const c = path.charCodeAt(i);
763
+ if (c >= 97 && c <= 122)
764
+ continue;
765
+ if (c >= 65 && c <= 90)
766
+ continue;
767
+ if (c >= 48 && c <= 57)
768
+ continue;
769
+ if (c === 95 || c === 45 || c === 47)
770
+ continue;
771
+ return false;
772
+ }
773
+ return true;
774
+ }
775
+ exports.isValidPath = isValidPath;
776
+ function isValidSlug(path) {
777
+ const len = path.length;
778
+ if (len === 0) {
779
+ return false;
780
+ }
781
+ for (let i = 0; i < len; i++) {
782
+ const c = path.charCodeAt(i);
783
+ if (c >= 97 && c <= 122)
784
+ continue;
785
+ if (c >= 65 && c <= 90)
786
+ continue;
787
+ if (c >= 48 && c <= 57)
788
+ continue;
789
+ if (c === 95 || c === 45)
790
+ continue;
791
+ return false;
792
+ }
793
+ return true;
794
+ }
795
+ exports.isValidSlug = isValidSlug;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "web-one",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
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)
@@ -438,12 +438,12 @@ export function getLimit(limit: string | undefined, defaultLimit: number): numbe
438
438
  export function getNumber(num?: string, defaultNum?: number): number | undefined {
439
439
  return !num || num.length === 0 ? defaultNum : isNaN(num as any) ? undefined : parseInt(num, 10)
440
440
  }
441
- export function clone(obj: any): any {
441
+ export function clone<T>(obj: T): T {
442
442
  if (!obj) {
443
443
  return obj
444
444
  }
445
445
  if (obj instanceof Date) {
446
- return new Date(obj.getTime())
446
+ return new Date(obj.getTime()) as any
447
447
  }
448
448
  if (typeof obj !== "object") {
449
449
  return obj
@@ -454,12 +454,12 @@ export function clone(obj: any): any {
454
454
  const c = clone(sub)
455
455
  arr.push(c)
456
456
  }
457
- return arr
457
+ return arr as any
458
458
  }
459
459
  const x: any = {}
460
460
  const keys = Object.keys(obj)
461
461
  for (const k of keys) {
462
- const v = obj[k]
462
+ const v = (obj as any)[k]
463
463
  if (v instanceof Date) {
464
464
  x[k] = new Date(v.getTime())
465
465
  } else {
@@ -650,7 +650,7 @@ export function formatPhone(phone?: string | null): string {
650
650
  }
651
651
 
652
652
  export interface MenuItem {
653
- id: string
653
+ id?: string
654
654
  name: string
655
655
  path: string
656
656
  resource?: string
@@ -681,6 +681,26 @@ export function rebuildPath(items: MenuItem[], lang: string) {
681
681
  }
682
682
  }
683
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
+ }
684
704
 
685
705
  export function sub(n1?: number, n2?: number): number {
686
706
  if (!n1 && !n2) {
@@ -729,10 +749,6 @@ function getChildren(m: Category, all: Category[]) {
729
749
  }
730
750
  }
731
751
 
732
- export function cloneArray<T>(arr: T[]): T[] {
733
- return arr.map(item => ({ ...item }));
734
- }
735
-
736
752
  export function getOffset(limit: number, page?: number, firstLimit?: number): number {
737
753
  const p = page && page > 0 ? page : 1
738
754
  if (firstLimit && firstLimit > 0) {
@@ -743,3 +759,42 @@ export function getOffset(limit: number, page?: number, firstLimit?: number): nu
743
759
  return offset < 0 ? 0 : offset
744
760
  }
745
761
  }
762
+
763
+ export function isValidPath(path: string): boolean {
764
+ const len = path.length;
765
+ if (len === 0) {
766
+ return false;
767
+ }
768
+ for (let i = 0; i < len; i++) {
769
+ const c = path.charCodeAt(i);
770
+ // a-z
771
+ if (c >= 97 && c <= 122) continue;
772
+ // A-Z
773
+ if (c >= 65 && c <= 90) continue;
774
+ // 0-9
775
+ if (c >= 48 && c <= 57) continue;
776
+ // _, -, /
777
+ if (c === 95 || c === 45 || c === 47) continue;
778
+ return false;
779
+ }
780
+ return true;
781
+ }
782
+ export function isValidSlug(path: string): boolean {
783
+ const len = path.length;
784
+ if (len === 0) {
785
+ return false;
786
+ }
787
+ for (let i = 0; i < len; i++) {
788
+ const c = path.charCodeAt(i);
789
+ // a-z
790
+ if (c >= 97 && c <= 122) continue;
791
+ // A-Z
792
+ if (c >= 65 && c <= 90) continue;
793
+ // 0-9
794
+ if (c >= 48 && c <= 57) continue;
795
+ // _, -, /
796
+ if (c === 95 || c === 45) continue;
797
+ return false;
798
+ }
799
+ return true;
800
+ }