@etsoo/shared 1.1.38 → 1.1.39

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/README.md CHANGED
@@ -147,7 +147,7 @@ DOM/window related utilities
147
147
  |Name|Description|
148
148
  |---:|---|
149
149
  |clearFormData|Clear form data|
150
- |dataAs|Cast data to target type|
150
+ |dataAs|Cast data as template format|
151
151
  |detectedCountry|Current detected country|
152
152
  |detectedCulture|Current detected culture|
153
153
  |dimensionEqual|Check two rectangles equality|
@@ -20,7 +20,14 @@ export declare namespace DomUtils {
20
20
  * @param keepFields Fields need to be kept
21
21
  */
22
22
  function clearFormData(data: IFormData, source?: {}, keepFields?: string[]): IFormData;
23
- function dataAs<T extends DataTypes.BasicTemplate>(source: IFormData | {}, template: T, keepSource?: boolean): DataTypes.BasicTemplateType<T>;
23
+ /**
24
+ * Cast data as template format
25
+ * @param source Source data
26
+ * @param template Format template
27
+ * @param keepSource Keep other source properties
28
+ * @returns Result
29
+ */
30
+ function dataAs<T extends DataTypes.BasicTemplate>(source: unknown, template: T, keepSource?: boolean): DataTypes.BasicTemplateType<T>;
24
31
  /**
25
32
  * Cast data to target type
26
33
  * @param source Source data
@@ -134,12 +134,21 @@ var DomUtils;
134
134
  Reflect.set(data, property, propertyValue);
135
135
  }
136
136
  }
137
+ /**
138
+ * Cast data as template format
139
+ * @param source Source data
140
+ * @param template Format template
141
+ * @param keepSource Keep other source properties
142
+ * @returns Result
143
+ */
137
144
  function dataAs(source, template, keepSource = false) {
138
145
  // New data
139
146
  // Object.create(...)
140
147
  const data = {};
141
- // Travel all properties
142
- dataAsTraveller(source, data, template, keepSource, false);
148
+ if (source != null && typeof source === 'object') {
149
+ // Travel all properties
150
+ dataAsTraveller(source, data, template, keepSource, false);
151
+ }
143
152
  // Return
144
153
  return data;
145
154
  }
@@ -20,7 +20,14 @@ export declare namespace DomUtils {
20
20
  * @param keepFields Fields need to be kept
21
21
  */
22
22
  function clearFormData(data: IFormData, source?: {}, keepFields?: string[]): IFormData;
23
- function dataAs<T extends DataTypes.BasicTemplate>(source: IFormData | {}, template: T, keepSource?: boolean): DataTypes.BasicTemplateType<T>;
23
+ /**
24
+ * Cast data as template format
25
+ * @param source Source data
26
+ * @param template Format template
27
+ * @param keepSource Keep other source properties
28
+ * @returns Result
29
+ */
30
+ function dataAs<T extends DataTypes.BasicTemplate>(source: unknown, template: T, keepSource?: boolean): DataTypes.BasicTemplateType<T>;
24
31
  /**
25
32
  * Cast data to target type
26
33
  * @param source Source data
@@ -131,12 +131,21 @@ export var DomUtils;
131
131
  Reflect.set(data, property, propertyValue);
132
132
  }
133
133
  }
134
+ /**
135
+ * Cast data as template format
136
+ * @param source Source data
137
+ * @param template Format template
138
+ * @param keepSource Keep other source properties
139
+ * @returns Result
140
+ */
134
141
  function dataAs(source, template, keepSource = false) {
135
142
  // New data
136
143
  // Object.create(...)
137
144
  const data = {};
138
- // Travel all properties
139
- dataAsTraveller(source, data, template, keepSource, false);
145
+ if (source != null && typeof source === 'object') {
146
+ // Travel all properties
147
+ dataAsTraveller(source, data, template, keepSource, false);
148
+ }
140
149
  // Return
141
150
  return data;
142
151
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.38",
3
+ "version": "1.1.39",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
package/src/DomUtils.ts CHANGED
@@ -151,8 +151,15 @@ export namespace DomUtils {
151
151
  }
152
152
  }
153
153
 
154
+ /**
155
+ * Cast data as template format
156
+ * @param source Source data
157
+ * @param template Format template
158
+ * @param keepSource Keep other source properties
159
+ * @returns Result
160
+ */
154
161
  export function dataAs<T extends DataTypes.BasicTemplate>(
155
- source: IFormData | {},
162
+ source: unknown,
156
163
  template: T,
157
164
  keepSource: boolean = false
158
165
  ): DataTypes.BasicTemplateType<T> {
@@ -160,8 +167,10 @@ export namespace DomUtils {
160
167
  // Object.create(...)
161
168
  const data = <DataTypes.BasicTemplateType<T>>{};
162
169
 
163
- // Travel all properties
164
- dataAsTraveller(source, data, template, keepSource, false);
170
+ if (source != null && typeof source === 'object') {
171
+ // Travel all properties
172
+ dataAsTraveller(source, data, template, keepSource, false);
173
+ }
165
174
 
166
175
  // Return
167
176
  return data;