-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HCK-7920: implemented variant subtype resolve logic on REing from ins…
…tance
- Loading branch information
1 parent
fd02aae
commit 1d74b8f
Showing
4 changed files
with
142 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
reverse_engineering/helpers/variantPropertiesSubTypeResolveHelper.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
const { getMostFrequentValueInList } = require('./utils'); | ||
|
||
/** | ||
* @typedef { (string | number | boolean) } Primitive | ||
*/ | ||
|
||
/** | ||
* | ||
* @param {{ | ||
* propertiesSchema: object, | ||
* documents: object[] | ||
* }} param | ||
* @returns {object} | ||
*/ | ||
const getVariantColumnsWithResolvedSubType = ({ propertiesSchema, documents = [] }) => { | ||
const propertiesEntriesWithUpdatedSubtypes = Object.entries(propertiesSchema).map(([propertyName, propertyData]) => | ||
getVariantColumnWithResolvedSubType({ propertyName, propertyData, documents }), | ||
); | ||
|
||
return Object.fromEntries(propertiesEntriesWithUpdatedSubtypes); | ||
}; | ||
|
||
/** | ||
* | ||
* @param {{ | ||
* propertyName: string, | ||
* propertyValue: object, | ||
* documents: object[] | ||
* }} param | ||
* @returns {[string, object]} | ||
*/ | ||
const getVariantColumnWithResolvedSubType = ({ propertyName, propertyData, documents }) => { | ||
if (propertyData?.mode !== 'var') { | ||
return [propertyName, propertyData]; | ||
} | ||
|
||
const propertyDocumentsRecords = documents.map(document => document[propertyName]); | ||
const parsedDocumentsRecords = propertyDocumentsRecords.map(getParsedVariantRecord); | ||
const parsedDocumentsRecordsTypes = parsedDocumentsRecords.map(getDocumentRecordType); | ||
const mostFrequentType = getMostFrequentValueInList(parsedDocumentsRecordsTypes); | ||
|
||
const updatedPropertyValue = { | ||
...propertyData, | ||
subtype: mostFrequentType, | ||
}; | ||
|
||
return [propertyName, updatedPropertyValue]; | ||
}; | ||
|
||
/** | ||
* | ||
* @param {string} record | ||
* @returns {Primitive | object | Array<Primitive>} | ||
*/ | ||
const getParsedVariantRecord = record => { | ||
try { | ||
const parsedRecord = JSON.parse(record); | ||
return parsedRecord; | ||
} catch { | ||
return {}; | ||
} | ||
}; | ||
|
||
/** | ||
* | ||
* @param {Primitive | object | Array<Primitive | object>} parsedRecord | ||
* @returns {string} | ||
*/ | ||
const getDocumentRecordType = parsedRecord => { | ||
if (Array.isArray(parsedRecord)) { | ||
return 'array'; | ||
} | ||
|
||
if (typeof parsedRecord === 'object') { | ||
return parsedRecord ? 'object' : null; | ||
} | ||
|
||
return typeof parsedRecord; | ||
}; | ||
|
||
module.exports = { | ||
getVariantColumnsWithResolvedSubType, | ||
}; |