Comparing version 4.6.10 to 4.6.11
@@ -10,2 +10,6 @@ import { EntityFormDataContextDTO, EntityFormTemplate, FormData, FormDataDoc, FormOutputBinding, FormOutputBindings, IModuleLink, MODULES } from 'bf-types'; | ||
}; | ||
export declare type ArrayModulePropertyStrategy = { | ||
stratergy: 'UPDATE' | 'UPSERT' | 'REPLACE'; | ||
postKey: string; | ||
}; | ||
export declare const resolveOutputBindingValue: (binding: FormOutputBinding, fullContext: EntityFormDataContextDTO & FormDataDoc) => any; | ||
@@ -15,2 +19,3 @@ export declare const convertFormTemplateBindingFromDb: (formTemplate: FormOutputBindings) => FormOutputBindings; | ||
export declare function castValueFromContext(contextKeyValue: any, matchValue: any): string | number | boolean; | ||
export declare function resolveBracketStatergy(postKey: string): ArrayModulePropertyStrategy; | ||
export declare function resolveBracketSyntax(target: string | undefined, keys: string[], bindingValues: any[], fullContext: EntityFormDataContextDTO & FormDataDoc): Array<{ | ||
@@ -17,0 +22,0 @@ [x: string]: any; |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.formTemplateToChanges = exports.resolveBracketSyntax = exports.castValueFromContext = exports.convertToTemplateBidningFromDb = exports.convertFormTemplateBindingFromDb = exports.resolveOutputBindingValue = exports.deepResolve = void 0; | ||
exports.formTemplateToChanges = exports.resolveBracketSyntax = exports.resolveBracketStatergy = exports.castValueFromContext = exports.convertToTemplateBidningFromDb = exports.convertFormTemplateBindingFromDb = exports.resolveOutputBindingValue = exports.deepResolve = void 0; | ||
const handlebars_1 = __importDefault(require("@timwoods/handlebars")); | ||
@@ -64,2 +64,3 @@ const deepmerge_1 = __importDefault(require("deepmerge")); | ||
exports.convertToTemplateBidningFromDb = convertToTemplateBidningFromDb; | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
function castValueFromContext(contextKeyValue, matchValue) { | ||
@@ -82,5 +83,31 @@ switch (typeof contextKeyValue) { | ||
exports.castValueFromContext = castValueFromContext; | ||
function resolveBracketStatergy(postKey) { | ||
const regex = /^(.*)?\[(.*)\]/gi; | ||
const [stg, key] = [postKey.replace(regex, '$1'), postKey.replace(regex, '$2')]; | ||
const validStratergySymbols = ['_', '+', '~']; | ||
const stratergyValue = { | ||
stratergy: key.length === 0 ? 'REPLACE' : 'UPDATE', | ||
postKey: key, | ||
}; | ||
if (!postKey.startsWith('[') && validStratergySymbols.includes(stg) && key.length !== 0) { | ||
const stratergySymbol = postKey.charAt(0); | ||
stratergyValue.postKey = postKey.substring(1).replace(/[\[\]]/g, ''); | ||
switch (stratergySymbol) { | ||
case '+': | ||
stratergyValue.stratergy = 'UPSERT'; | ||
break; | ||
case '_': | ||
stratergyValue.stratergy = 'UPDATE'; | ||
break; | ||
case '~': | ||
stratergyValue.stratergy = 'REPLACE'; | ||
break; | ||
} | ||
} | ||
return stratergyValue; | ||
} | ||
exports.resolveBracketStatergy = resolveBracketStatergy; | ||
function resolveBracketSyntax(target, keys, bindingValues, fullContext) { | ||
const preKey = keys.slice(0, -1).join('.'); | ||
const postKey = keys[keys.length - 1].replace(/[\[\]]/g, ''); | ||
const { postKey, stratergy } = resolveBracketStatergy(keys[keys.length - 1]); | ||
const matchers = postKey.split(';').map((subMatcher) => subMatcher.split(':')); | ||
@@ -92,36 +119,56 @@ let contextValues = exports.deepResolve(`${target}.${preKey}`, fullContext) || []; | ||
} | ||
const resolveValue = (changeActionSymbol, value, mergeValue = {}) => { | ||
const key = matchers | ||
.map((matcher) => { | ||
if (matcher.length === 1) { | ||
const resolvedContextValue = exports.deepResolve(matcher[0], value); | ||
return [ | ||
...matcher, | ||
typeof resolvedContextValue === 'object' ? JSON.stringify(resolvedContextValue) : resolvedContextValue, | ||
].join(':'); | ||
} | ||
return matcher.join(':'); | ||
}) | ||
.join(';'); | ||
return { | ||
[`${preKey}.${changeActionSymbol}[${key}]`]: deepmerge_1.default(mergeValue, value, { | ||
arrayMerge: combineMerge, | ||
}), | ||
}; | ||
}; | ||
return bindingValues.flatMap((bindingValue) => { | ||
return contextValues | ||
.filter((contextValue) => matchers.reduce((pv, cv) => { | ||
if (cv.length === 1) { | ||
const key = cv[0]; | ||
const resolvedBindingValue = exports.deepResolve(key, bindingValue); | ||
const resolvedContextValue = exports.deepResolve(key, contextValue); | ||
return pv && lodash_isequal_1.default(resolvedBindingValue, resolvedContextValue); | ||
const changeActions = []; | ||
for (const contextValue of contextValues) { | ||
const isFound = matchers.reduce((pv, cv) => { | ||
if (cv.length === 1) { | ||
const key = cv[0]; | ||
const resolvedBindingValue = exports.deepResolve(key, bindingValue); | ||
const resolvedContextValue = exports.deepResolve(key, contextValue); | ||
return pv && lodash_isequal_1.default(resolvedBindingValue, resolvedContextValue); | ||
} | ||
else if (cv.length === 2) { | ||
const contextKeyValue = exports.deepResolve(cv[0], contextValue); | ||
return pv && lodash_isequal_1.default(contextKeyValue, castValueFromContext(contextKeyValue, cv[1])); | ||
} | ||
return pv && false; | ||
}, true); | ||
if (stratergy === 'UPDATE' && isFound) { | ||
changeActions.push(resolveValue('_', bindingValue, contextValue)); | ||
} | ||
else if (cv.length === 2) { | ||
const contextKeyValue = exports.deepResolve(cv[0], contextValue); | ||
return pv && lodash_isequal_1.default(contextKeyValue, castValueFromContext(contextKeyValue, cv[1])); | ||
else if (stratergy === 'UPSERT') { | ||
if (isFound) { | ||
changeActions.push(resolveValue('_', bindingValue, contextValue)); | ||
} | ||
else { | ||
changeActions.push(resolveValue('+', bindingValue)); | ||
} | ||
} | ||
return pv && false; | ||
}, true)) | ||
.map((filterValue) => { | ||
const key = matchers | ||
.map((matcher) => { | ||
if (matcher.length === 1) { | ||
const resolvedFilterValue = exports.deepResolve(matcher[0], filterValue); | ||
return [ | ||
...matcher, | ||
typeof resolvedFilterValue === 'object' ? JSON.stringify(resolvedFilterValue) : resolvedFilterValue, | ||
].join(':'); | ||
else if (stratergy === 'REPLACE') { | ||
if (isFound) { | ||
changeActions.push(resolveValue('-', contextValue)); | ||
} | ||
return matcher.join(':'); | ||
}) | ||
.join(';'); | ||
return { | ||
[`${preKey}.[${key}]`]: deepmerge_1.default(filterValue, bindingValue, { | ||
arrayMerge: combineMerge, | ||
}), | ||
}; | ||
}); | ||
changeActions.push(resolveValue('+', bindingValue)); | ||
} | ||
} | ||
return changeActions; | ||
}); | ||
@@ -176,4 +223,4 @@ } | ||
const subkey = keys.join('.'); | ||
if (/.*\[.+\]$/.test(option)) { | ||
const bracketSyntaxChanges = resolveBracketSyntax(target, keys, value, fullContext); | ||
if (/.*\[.*\]$/.test(option) || Array.isArray(value)) { | ||
const bracketSyntaxChanges = resolveBracketSyntax(target, [...keys, ...(/.*\[.*\]$/.test(option) ? [] : ['~[]'])], value, fullContext); | ||
for (const change of bracketSyntaxChanges) { | ||
@@ -180,0 +227,0 @@ for (const [key, value] of Object.entries(change)) { |
@@ -37,5 +37,2 @@ "use strict"; | ||
primary: true, | ||
metadata: { | ||
hello: 'world', | ||
}, | ||
roles: [], | ||
@@ -42,0 +39,0 @@ id: 'f67b3d46-65fc-4f8d-956b-c1ce35761664', |
{ | ||
"name": "bf-lib", | ||
"version": "4.6.10", | ||
"version": "4.6.11", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "description": "the standard client library for block-5 software", |
150259
3502