@contentstorage/core 0.3.26 → 0.3.28

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,36 +1,58 @@
1
1
  export function flattenJson(data, prefix = '', result = {}) {
2
- if (typeof data === 'object' && data !== null) {
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
3
23
  if (Array.isArray(data)) {
4
24
  if (data.length === 0 && prefix) {
5
25
  // Handle empty arrays if prefix exists
6
26
  result[prefix] = [];
7
27
  }
8
28
  data.forEach((item, index) => {
29
+ // Recursively call, the check for 'stopFlatteningIfKeyExists' will apply to 'item'
9
30
  flattenJson(item, prefix ? `${prefix}.${index}` : `${index}`, result);
10
31
  });
11
32
  }
12
33
  else {
13
- // It's an object
34
+ // It's an object (and not a special one that stops flattening, due to the preceding 'if' block)
14
35
  let isEmptyObject = true;
15
36
  for (const key in data) {
16
37
  if (Object.prototype.hasOwnProperty.call(data, key)) {
17
38
  isEmptyObject = false;
18
39
  const newPrefix = prefix ? `${prefix}.${key}` : key;
40
+ // Recursively call, the check for 'stopFlatteningIfKeyExists' will apply to 'data[key]'
19
41
  flattenJson(data[key], newPrefix, result);
20
42
  }
21
43
  }
22
44
  if (isEmptyObject && prefix) {
23
- // Handle empty objects if prefix exists
45
+ // Handle empty objects (that were not 'special') if prefix exists
24
46
  result[prefix] = {};
25
47
  }
26
48
  }
27
49
  }
28
50
  else if (prefix) {
29
- // Primitive value (string, number, boolean, null)
51
+ // Primitive value (string, number, boolean, null) and has a prefix
30
52
  result[prefix] = data;
31
53
  }
32
- // If the initial data itself is a primitive and prefix is empty, it means we can't flatten it into key-value pairs.
33
- // This function is designed to take a root object or array.
34
- // If the root `data` is a primitive, `result` would remain empty, which is fine; `pullContent` should validate the root.
54
+ // If the initial data is a primitive and prefix is empty, result remains empty.
55
+ // If the initial data is a 'special' object (contains 'stopFlatteningIfKeyExists')
56
+ // and prefix is empty, result also remains empty based on the logic above.
35
57
  return result;
36
58
  }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import { AppConfig, LanguageCode, ContentStructure } from './types.js';
2
- import { setContentLanguage, getText, getImage } from './lib/contentManagement.js';
2
+ import { setContentLanguage, getText, getImage, getVariation } from './lib/contentManagement.js';
3
3
  export { AppConfig, LanguageCode, ContentStructure };
4
- export { setContentLanguage, getText, getImage };
4
+ export { setContentLanguage, getText, getImage, getVariation };
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import { setContentLanguage, getText, getImage } from './lib/contentManagement.js';
2
- export { setContentLanguage, getText, getImage };
1
+ import { setContentLanguage, getText, getImage, getVariation } from './lib/contentManagement.js';
2
+ export { setContentLanguage, getText, getImage, getVariation };
@@ -16,8 +16,7 @@ export declare function setContentLanguage(contentJson: object): void;
16
16
  * @returns The text string from the JSON, or the fallbackValue, or undefined.
17
17
  */
18
18
  export declare function getText(pathString: keyof ContentStructure, fallbackValue?: string): string | undefined;
19
- export declare function getImage(pathString: string, // Using string for dynamic paths
20
- fallbackValue?: ImageObject): ImageObject | undefined;
19
+ export declare function getImage(pathString: keyof ContentStructure, fallbackValue?: ImageObject): ImageObject | undefined;
21
20
  export declare function getVariation<Path extends keyof ContentStructure>(pathString: Path, variationKey?: ContentStructure[Path] extends {
22
21
  data: infer D;
23
22
  } ? keyof D : string, fallbackString?: string): string | undefined;
@@ -54,8 +54,7 @@ export function getText(pathString, fallbackValue) {
54
54
  return fallbackValue;
55
55
  }
56
56
  }
57
- export function getImage(pathString, // Using string for dynamic paths
58
- fallbackValue) {
57
+ export function getImage(pathString, fallbackValue) {
59
58
  if (!activeContent) {
60
59
  const msg = `[Contentstorage] getImage: Content not loaded (Key: "${pathString}"). Ensure setContentLanguage() was called and completed successfully.`;
61
60
  console.warn(msg);
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@contentstorage/core",
3
3
  "author": "Kaido Hussar <kaidohus@gmail.com>",
4
4
  "homepage": "https://contentstorage.app",
5
- "version": "0.3.26",
5
+ "version": "0.3.28",
6
6
  "type": "module",
7
7
  "description": "Fetch content from contentstorage and generate TypeScript types",
8
8
  "module": "dist/index.js",