@vpmedia/simplify 1.18.0 → 1.19.0
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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @vpmedia/simplify
|
|
2
2
|
|
|
3
|
-
[](https://badge.fury.io/js/@vpmedia%2Fsimplify)
|
|
4
4
|
[](https://github.com/vpmedia/simplify/actions/workflows/ci.yml)
|
|
5
5
|
|
|
6
6
|
@vpmedia/simplify TBD
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -2,14 +2,22 @@ import { underscoreToCamelCase } from './underscoreToCamelCase.js';
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Maps server data to client data.
|
|
5
|
-
* @param {
|
|
6
|
-
* @returns {
|
|
5
|
+
* @param {{}} data - The server input data.
|
|
6
|
+
* @returns {{}} The output data.
|
|
7
7
|
*/
|
|
8
8
|
export const serverDataToState = (data) => {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const clientKey = underscoreToCamelCase(serverKey);
|
|
12
|
-
result[clientKey] = data[serverKey];
|
|
9
|
+
if (Array.isArray(data)) {
|
|
10
|
+
return data.map((entry) => serverDataToState(entry));
|
|
13
11
|
}
|
|
14
|
-
|
|
12
|
+
|
|
13
|
+
if (data !== null && typeof data === 'object') {
|
|
14
|
+
const result = {};
|
|
15
|
+
for (const [key, value] of Object.entries(data)) {
|
|
16
|
+
const clientKey = underscoreToCamelCase(key);
|
|
17
|
+
result[clientKey] = serverDataToState(value);
|
|
18
|
+
}
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return data; // Return primitives as-is
|
|
15
23
|
};
|
|
@@ -1,8 +1,20 @@
|
|
|
1
1
|
import { serverDataToState } from './serverDataToState.js';
|
|
2
2
|
|
|
3
3
|
test('serverDataToState()', () => {
|
|
4
|
-
const state = serverDataToState({
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
const state = serverDataToState({
|
|
5
|
+
my_array: [{ key_a: 'value1' }],
|
|
6
|
+
my_data: { key_a: 'value1' },
|
|
7
|
+
my_list: [1, 2, 3],
|
|
8
|
+
my_null: null,
|
|
9
|
+
my_number: 1000,
|
|
10
|
+
my_string: 'a',
|
|
11
|
+
my_var: 'test',
|
|
12
|
+
});
|
|
13
|
+
expect(state.myArray[0].keyA).toBe('value1');
|
|
14
|
+
expect(state.myData.keyA).toBe('value1');
|
|
7
15
|
expect(state.myList[0]).toBe(1);
|
|
16
|
+
expect(state.myNull).toBe(null);
|
|
17
|
+
expect(state.myNumber).toBe(1000);
|
|
18
|
+
expect(state.myString).toBe('a');
|
|
19
|
+
expect(state.myVar).toBe('test');
|
|
8
20
|
});
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export function serverDataToState(data:
|
|
1
|
+
export function serverDataToState(data: {}): {};
|
|
2
2
|
//# sourceMappingURL=serverDataToState.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serverDataToState.d.ts","sourceRoot":"","sources":["../../src/util/serverDataToState.js"],"names":[],"mappings":"AAOO,wCAHI,
|
|
1
|
+
{"version":3,"file":"serverDataToState.d.ts","sourceRoot":"","sources":["../../src/util/serverDataToState.js"],"names":[],"mappings":"AAOO,wCAHI,EAAE,GACA,EAAE,CAiBd"}
|