@twobirds/microcomponents 0.1.16 → 0.1.19

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/dist/helpers.ts CHANGED
@@ -1,138 +1,138 @@
1
1
  'use strict';
2
2
 
3
3
  export interface LooseObject {
4
- [key: string | symbol]: any;
4
+ [key: string | symbol]: any;
5
5
  }
6
6
 
7
7
  export function isNativeObject(value: any): boolean {
8
- return value && value['constructor'] && value.constructor === Object;
8
+ return value && value['constructor'] && value.constructor === Object;
9
9
  }
10
10
 
11
11
  export function sortObject(o: LooseObject): LooseObject {
12
- let result: LooseObject = {},
13
- keys = [...Object.keys(o)].sort();
14
-
15
- keys.forEach((key) => {
16
- if (isNativeObject(o[key])) {
17
- result[key] = sortObject(o[key]);
18
- } else {
19
- result[key] = o[key];
20
- }
21
- });
22
-
23
- return result;
12
+ let result: LooseObject = {},
13
+ keys = [...Object.keys(o)].sort();
14
+
15
+ keys.forEach((key) => {
16
+ if (isNativeObject(o[key])) {
17
+ result[key] = sortObject(o[key]);
18
+ } else {
19
+ result[key] = o[key];
20
+ }
21
+ });
22
+
23
+ return result;
24
24
  }
25
25
 
26
26
  export function flattenObject(o: LooseObject, doSort: boolean = true): LooseObject {
27
- let result: LooseObject = {};
28
-
29
- [...Object.getOwnPropertyNames(o)].forEach((propName) => {
30
- if (o[propName] instanceof Object) {
31
- const temp = flattenObject(o[propName], false);
32
- result[propName] = {};
33
- for (const j in temp) {
34
- result[propName + '.' + j] = temp[j];
35
- }
36
- } else {
37
- result[propName] = o[propName];
38
- }
39
- });
40
-
41
- return doSort ? sortObject(result) : result;
27
+ let result: LooseObject = {};
28
+
29
+ [...Object.getOwnPropertyNames(o)].forEach((propName) => {
30
+ if (o[propName] instanceof Object) {
31
+ const temp = flattenObject(o[propName], false);
32
+ result[propName] = {};
33
+ for (const j in temp) {
34
+ result[propName + '.' + j] = temp[j];
35
+ }
36
+ } else {
37
+ result[propName] = o[propName];
38
+ }
39
+ });
40
+
41
+ return doSort ? sortObject(result) : result;
42
42
  }
43
43
 
44
44
  export function deepEqual(o1: LooseObject, o2: LooseObject): boolean {
45
- return JSON.stringify(flattenObject(o1)) === JSON.stringify(flattenObject(o2));
45
+ return JSON.stringify(flattenObject(o1)) === JSON.stringify(flattenObject(o2));
46
46
  }
47
47
 
48
48
  export function nameSpace(ns: string | string[], obj: LooseObject = {}): any {
49
- let nsa = ns.constructor === String ? ns.split('.') : ns;
50
-
51
- if (!obj || !nsa[0] || !obj[nsa[0]]) {
52
- console.warn('ns', nsa[0], 'not in', obj);
53
- return;
54
- }
55
- if (obj[nsa[0]].constructor === Object && nsa.length > 1) {
56
- return nameSpace((nsa as string[]).splice(1), obj[nsa[0]]);
57
- }
58
- return obj[nsa[0]];
49
+ let nsa = ns.constructor === String ? ns.split('.') : ns;
50
+
51
+ if (!obj || !nsa[0] || !obj[nsa[0]]) {
52
+ console.warn('ns', nsa[0], 'not in', obj);
53
+ return;
54
+ }
55
+ if (obj[nsa[0]].constructor === Object && nsa.length > 1) {
56
+ return nameSpace((nsa as string[]).splice(1), obj[nsa[0]]);
57
+ }
58
+ return obj[nsa[0]];
59
59
  }
60
60
 
61
61
  export function parse(what: string | object | any[], parseThis: object) {
62
- var args = Array.from(arguments);
63
-
64
- if (args.length > 2) {
65
- while (args.length > 1) {
66
- args[0] = parse(args[0], args[1]);
67
- args.splice(1, 1);
68
- }
69
- return args[0];
70
- }
71
-
72
- if (typeof what === 'string') {
73
- what.match(/\{[^\{\}]*\}/g)?.forEach(function (pPropname) {
74
- var propname = pPropname.substring(1, pPropname.length - 1),
75
- value = nameSpace(propname, parseThis);
76
- //console.log(propname, value);
77
- if (typeof value !== 'undefined') {
78
- what = (what as string).replace(pPropname, value);
79
- }
80
- });
81
- } else if (isNativeObject(what)) {
82
- switch (what.constructor) {
83
- case Object:
84
- Object.keys(what).forEach(function (pKey) {
85
- if (what.hasOwnProperty(pKey)) {
86
- (what as LooseObject)[pKey] = parse((what as LooseObject)[pKey], parseThis);
87
- }
88
- });
89
- break;
90
- case Array:
91
- (what as Array<any>).forEach(function (pValue, pKey, original) {
92
- original[pKey] = parse((what as any)[pKey], parseThis);
93
- });
94
- break;
95
- }
96
- }
97
-
98
- return what;
62
+ var args = Array.from(arguments);
63
+
64
+ if (args.length > 2) {
65
+ while (args.length > 1) {
66
+ args[0] = parse(args[0], args[1]);
67
+ args.splice(1, 1);
68
+ }
69
+ return args[0];
70
+ }
71
+
72
+ if (typeof what === 'string') {
73
+ what.match(/\{[^\{\}]*\}/g)?.forEach(function (pPropname) {
74
+ var propname = pPropname.substring(1, pPropname.length - 1),
75
+ value = nameSpace(propname, parseThis);
76
+ //console.log(propname, value);
77
+ if (typeof value !== 'undefined') {
78
+ what = (what as string).replace(pPropname, value);
79
+ }
80
+ });
81
+ } else if (isNativeObject(what)) {
82
+ switch (what.constructor) {
83
+ case Object:
84
+ Object.keys(what).forEach(function (pKey) {
85
+ if (what.hasOwnProperty(pKey)) {
86
+ (what as LooseObject)[pKey] = parse((what as LooseObject)[pKey], parseThis);
87
+ }
88
+ });
89
+ break;
90
+ case Array:
91
+ (what as Array<any>).forEach(function (pValue, pKey, original) {
92
+ original[pKey] = parse((what as any)[pKey], parseThis);
93
+ });
94
+ break;
95
+ }
96
+ }
97
+
98
+ return what;
99
99
  }
100
100
 
101
101
  export function kebabToPascal(str: string) {
102
- return str
103
- .split('-') // Split by hyphens
104
- .map((word) => word.charAt(0).toUpperCase() + word.slice(1)) // Capitalize each part
105
- .join(''); // Join without spaces
102
+ return str
103
+ .split('-') // Split by hyphens
104
+ .map((word) => word.charAt(0).toUpperCase() + word.slice(1)) // Capitalize each part
105
+ .join(''); // Join without spaces
106
106
  }
107
107
 
108
108
  export function debounce(func: Function, milliseconds: number): Function {
109
- let timeout: any;
110
- return () => {
111
- clearTimeout(timeout);
112
-
113
- timeout = setTimeout(function () {
114
- func(...arguments);
115
- }, milliseconds);
116
- };
109
+ let timeout: any;
110
+ return () => {
111
+ clearTimeout(timeout);
112
+
113
+ timeout = setTimeout(function () {
114
+ func(...arguments);
115
+ }, milliseconds);
116
+ };
117
117
  }
118
118
 
119
119
  export function htmlToElements(html: string): HTMLElement[] {
120
- let template = document.createElement('template');
120
+ let template = document.createElement('template');
121
121
 
122
- html = html.replace(/<([A-Za-z0-9\-]*)([^>\/]*)(\/>)/gi, '<$1$2></$1>'); // replace XHTML closing tags by full closing tags
122
+ html = html.replace(/<([A-Za-z0-9\-]*)([^>\/]*)(\/>)/gi, '<$1$2></$1>'); // replace XHTML closing tags by full closing tags
123
123
 
124
- template.innerHTML = html;
124
+ template.innerHTML = html;
125
125
 
126
- template.content.normalize();
126
+ template.content.normalize();
127
127
 
128
- return Array.from(template.content.childNodes) as HTMLElement[];
128
+ return Array.from(template.content.childNodes) as HTMLElement[];
129
129
  }
130
130
 
131
131
  export function copyGettersSetters(source: LooseObject, target: LooseObject) {
132
- const descriptors = Object.getOwnPropertyDescriptors(source);
133
- for (const [key, descriptor] of Object.entries(descriptors)) {
134
- if ('get' in descriptor || 'set' in descriptor) {
135
- Object.defineProperty(target, key, descriptor);
136
- }
137
- }
132
+ const descriptors = Object.getOwnPropertyDescriptors(source);
133
+ for (const [key, descriptor] of Object.entries(descriptors)) {
134
+ if ('get' in descriptor || 'set' in descriptor) {
135
+ Object.defineProperty(target, key, descriptor);
136
+ }
137
+ }
138
138
  }