@sean_kenny/eu4-text-file-to-json-parser-js
Advanced tools
Comparing version 0.1.4 to 0.1.5
@@ -61,3 +61,3 @@ import { readFile } from "fs/promises"; | ||
outputJSONData, | ||
currentKeyToPushTo: propertyName, | ||
currentKeyToPushTo: `${currentKeyToPushTo}${currentKeyToPushTo.length > 0 ? '.' : ''}${propertyName}`, | ||
valueToPush: value.at(0) === '"' && value.at(-1) === '"' | ||
@@ -64,0 +64,0 @@ ? value.slice(1, -1) |
export type ValueOrNestedValue<T> = string | string[] | { | ||
[key: string]: ValueOrNestedValue<T>; | ||
}; | ||
} | { | ||
[key: string]: ValueOrNestedValue<T>; | ||
}[]; | ||
export declare const valueOrNestedValueIsString: <T>(input: ValueOrNestedValue<T>) => input is string; | ||
export declare const valueOrNestedValueIsStringArray: <T>(input: ValueOrNestedValue<T>) => input is string[]; | ||
export declare const valueOrNestedValueIsNestedValueArray: <T>(input: ValueOrNestedValue<T>) => input is { | ||
[key: string]: ValueOrNestedValue<T>; | ||
}[]; | ||
export declare const valueOrNestedValueIsNestedValue: <T>(input: ValueOrNestedValue<T>) => input is { | ||
[key: string]: ValueOrNestedValue<T>; | ||
}; |
export const valueOrNestedValueIsString = (input) => typeof input === 'string'; | ||
export const valueOrNestedValueIsStringArray = (input) => Array.isArray(input); | ||
export const valueOrNestedValueIsNestedValue = (input) => !valueOrNestedValueIsString(input) && !valueOrNestedValueIsStringArray(input); | ||
export const valueOrNestedValueIsStringArray = (input) => Array.isArray(input) && input.every((element) => typeof element === 'string'); | ||
export const valueOrNestedValueIsNestedValueArray = (input) => Array.isArray(input) && input.every((element) => typeof element !== 'string'); | ||
export const valueOrNestedValueIsNestedValue = (input) => !valueOrNestedValueIsString(input) && | ||
!valueOrNestedValueIsStringArray(input) && | ||
!valueOrNestedValueIsNestedValueArray(input); |
@@ -1,2 +0,2 @@ | ||
import { valueOrNestedValueIsNestedValue, valueOrNestedValueIsStringArray } from "./value-or-nested-value.js"; | ||
import { valueOrNestedValueIsNestedValue, valueOrNestedValueIsNestedValueArray, valueOrNestedValueIsString, valueOrNestedValueIsStringArray } from "./value-or-nested-value.js"; | ||
export const writeValueToOutputJSONData = (input) => { | ||
@@ -8,6 +8,33 @@ const { valueToPush } = input; | ||
const currentArrayValueForKey = input.outputJSONData?.[key]; | ||
if (valueOrNestedValueIsString(valueToPush) && | ||
valueOrNestedValueIsString(currentArrayValueForKey)) { | ||
return { | ||
...input.outputJSONData, | ||
[key]: [ | ||
currentArrayValueForKey, | ||
valueToPush | ||
] | ||
}; | ||
} | ||
if (valueOrNestedValueIsString(valueToPush) && | ||
valueOrNestedValueIsStringArray(currentArrayValueForKey)) { | ||
return { | ||
...input.outputJSONData, | ||
[key]: [ | ||
...currentArrayValueForKey, | ||
valueToPush | ||
] | ||
}; | ||
} | ||
if (valueOrNestedValueIsNestedValue(valueToPush) | ||
&& Object.keys(valueToPush).length === 0 | ||
&& currentArrayValueForKey !== undefined) { | ||
return input.outputJSONData; | ||
&& currentArrayValueForKey !== undefined | ||
&& valueOrNestedValueIsNestedValue(currentArrayValueForKey)) { | ||
return { | ||
...input.outputJSONData, | ||
[key]: [ | ||
currentArrayValueForKey, | ||
valueToPush | ||
] | ||
}; | ||
} | ||
@@ -21,2 +48,7 @@ if (currentArrayValueForKey === undefined || (valueOrNestedValueIsNestedValue(currentArrayValueForKey) | ||
} | ||
if (valueOrNestedValueIsStringArray(currentArrayValueForKey) && | ||
valueOrNestedValueIsNestedValue(valueToPush) && | ||
Object.keys(valueToPush).length === 0) { | ||
return input.outputJSONData; | ||
} | ||
if (valueOrNestedValueIsStringArray(currentArrayValueForKey)) { | ||
@@ -36,2 +68,15 @@ if (!valueOrNestedValueIsStringArray(valueToPush)) { | ||
const valueForLowestValueKey = input.outputJSONData?.[lowestValueKey]; | ||
if (valueOrNestedValueIsNestedValueArray(valueForLowestValueKey)) { | ||
return { | ||
...input.outputJSONData, | ||
[lowestValueKey]: [ | ||
...valueForLowestValueKey.slice(0, 1), | ||
writeValueToOutputJSONData({ | ||
outputJSONData: valueForLowestValueKey.at(-1) ?? {}, | ||
currentKeyToPushTo: otherKeys.join('.'), | ||
valueToPush | ||
}) | ||
] | ||
}; | ||
} | ||
if (!valueOrNestedValueIsNestedValue(valueForLowestValueKey)) { | ||
@@ -38,0 +83,0 @@ throw new Error(`Cannot mix JSON values with string and string array values on the same key. Key: ${input.currentKeyToPushTo}`); |
{ | ||
"name": "@sean_kenny/eu4-text-file-to-json-parser-js", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "A library for reading text files in the format EU4 encodes it's game data in.", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -136,2 +136,3 @@ # eu4-text-file-to-json-parser-js | ||
``` | ||
``` | ||
@@ -150,2 +151,24 @@ add_core = I43 | ||
``` | ||
is_city = yes | ||
add_permanent_province_modifier = { | ||
name = harimari_minority_coexisting_small | ||
duration = -1 | ||
} | ||
add_permanent_province_modifier = { | ||
name = temple_complex | ||
duration = -1 | ||
} | ||
``` | ||
``` | ||
{ | ||
"is_city": "yes", | ||
"add_permanent_province_modifier": [ | ||
{ "name": "harimari_minority_coexisting_small", "duration": "-1" }, | ||
{ "name": "temple_complex", "duration": "-1" } | ||
] | ||
} | ||
``` | ||
More full examples are available by perusing the `/test/sample-inputs` and `/test/expected-outputs` folders in the repository. | ||
@@ -152,0 +175,0 @@ |
14739
214
179