@contentstorage/core 2.2.0 → 2.2.1

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.
@@ -1 +1 @@
1
- export declare function flattenJson(data: any, prefix?: string, result?: Record<string, any>): Record<string, any>;
1
+ export declare function flattenJson(data: any, prefix?: string, result?: Record<string, string>): Record<string, string>;
@@ -1,56 +1,21 @@
1
1
  export function flattenJson(data, prefix = '', result = {}) {
2
- const stopFlatteningIfKeyExists = 'contentstorage_type';
3
- // Check if the current data is an object that should not be flattened further
4
- if (typeof data === 'object' &&
5
- data !== null &&
6
- !Array.isArray(data) && // Must be an object, not an array
7
- Object.prototype.hasOwnProperty.call(data, stopFlatteningIfKeyExists)) {
8
- if (prefix) {
9
- // If there's a prefix, this object is nested. Assign it directly.
10
- result[prefix] = data;
11
- }
12
- else {
13
- // This is the root object itself having the 'stopFlatteningIfKeyExists' key.
14
- // Consistent with how root primitives are handled (result remains empty),
15
- // we don't add it to the result if there's no prefix. The function's
16
- // purpose is to flatten *into* key-value pairs. If the root itself
17
- // is one of these "don't flatten" types, 'result' will remain empty,
18
- // which is consistent with the original function's behavior for root primitives.
19
- }
20
- }
21
- else if (typeof data === 'object' && data !== null) {
22
- // It's an object or array that should be processed further
2
+ if (typeof data === 'object' && data !== null) {
23
3
  if (Array.isArray(data)) {
24
- if (data.length === 0 && prefix) {
25
- // Handle empty arrays if prefix exists
26
- result[prefix] = [];
27
- }
28
4
  data.forEach((item, index) => {
29
- // Recursively call, the check for 'stopFlatteningIfKeyExists' will apply to 'item'
30
5
  flattenJson(item, prefix ? `${prefix}.${index}` : `${index}`, result);
31
6
  });
32
7
  }
33
8
  else {
34
- let isEmptyObject = true;
35
9
  for (const key in data) {
36
10
  if (Object.prototype.hasOwnProperty.call(data, key)) {
37
- isEmptyObject = false;
38
11
  const newPrefix = prefix ? `${prefix}.${key}` : key;
39
- // Recursively call, the check for 'stopFlatteningIfKeyExists' will apply to 'data[key]'
40
12
  flattenJson(data[key], newPrefix, result);
41
13
  }
42
14
  }
43
- if (isEmptyObject && prefix) {
44
- // Handle empty objects (that were not 'special') if prefix exists
45
- result[prefix] = {};
46
- }
47
15
  }
48
16
  }
49
17
  else if (prefix) {
50
- result[prefix] = data;
18
+ result[prefix] = String(data);
51
19
  }
52
- // If the initial data is a primitive and prefix is empty, result remains empty.
53
- // If the initial data is a 'special' object (contains 'stopFlatteningIfKeyExists')
54
- // and prefix is empty, result also remains empty based on the logic above.
55
20
  return result;
56
21
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@contentstorage/core",
3
3
  "author": "Kaido Hussar <kaido@contentstorage.app>",
4
4
  "homepage": "https://contentstorage.app",
5
- "version": "2.2.0",
5
+ "version": "2.2.1",
6
6
  "type": "module",
7
7
  "description": "Contentstorage CLI for managing translations and generating TypeScript types",
8
8
  "license": "MIT",