Skip to content

Commit

Permalink
Merge pull request #114 from genox/fix-autokey-extract
Browse files Browse the repository at this point in the history
fix isExistingKey
  • Loading branch information
robisim74 authored Feb 2, 2024
2 parents 5427ac7 + 25eba4f commit d691960
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
47 changes: 32 additions & 15 deletions packages/qwik-speak/tools/core/autokeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,45 +36,62 @@ export function generateAutoKey(input: string | number | Record<string, any>) {
/**
* Evaluate if the key could be an existing object path
*/
export function isObjectPath(key: string): boolean {
const regex = /^(?!\.)(?!.*\.\s)(?!.*\s\.)(?!.*\.$)(?=.*\..*)[^\s]+$/;
return regex.test(key);
export function isObjectPath(key: string, separator: string): boolean {
const regex = new RegExp(`^(?!\\${separator})(?!.*\\${separator}\\s)(?!.*\\s\\${separator})(?!.*\\${separator}$)(?=.*\\${separator}.*[^\\s]+)$`);
if (!regex.test(key)) {
return false;
}

const segments = key.split(separator);
const variableNameRegex = /^[a-zA-Z_$][0-9a-zA-Z_$]*$/;
for (const segment of segments) {
if (!variableNameRegex.test(segment)) {
return false;
}
}

return true;
}

/**
* Evaluate if the key could be an existing object path. If the path does no exist in an asset, it will return false.
* Evaluate if the key could be an existing object path. If the path does not exist in an asset, it will return false.
* It will return true if the path exists in all assets
*/
export function isExistingKey(data: Map<string, Translation> | Translation, path: string, keySeparator: string) {
const keys = path.split(keySeparator);
let keyFound = false;

// Iterate if data is a Map
if (data instanceof Map) {
for (const translation of data.values()) {
let value = translation;
for (const key of keys) {
if (value[key] === undefined) {
return false;
if (value[key] !== undefined) {
keyFound = true;
} else {
keyFound = false;
break;
}
value = value[key];
}
if (!keyFound) {
break;
}
}
return true;
}

// Iterate if data is an object
if (typeof data === 'object') {
} else if (typeof data === 'object') {
let value = data;
for (const key of keys) {
if (value[key] === undefined) {
return false;
if (value[key] !== undefined) {
keyFound = true;
} else {
keyFound = false;
break;
}
value = value[key];
}
return true;
}

return true;
return keyFound;
}

/**
Expand Down
9 changes: 6 additions & 3 deletions packages/qwik-speak/tools/extract/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,12 @@ export async function qwikSpeakExtract(options: QwikSpeakExtractOptions) {

if (resolvedOptions.autoKeys) {
// Auto keys should be backward compatible with existing keys. We don't want to override them.
// Let's be conservative and only generate auto keys for keys that have no default value, don't resemble
// an object path and don't already exist in the assets.
if (key && !defaultValue && !isObjectPath(key) && !isExistingKey(assetsData, key, resolvedOptions.keySeparator)) {
if (
key
&& !defaultValue
&& !isExistingKey(assetsData, key, resolvedOptions.keySeparator)
&& !isObjectPath(key, resolvedOptions.keySeparator)
) {
defaultValue = `${key}`;
key = generateAutoKey(key);
}
Expand Down

0 comments on commit d691960

Please sign in to comment.