@firstaccess/analysis-outputs-mapper
Advanced tools
Comparing version 0.0.1 to 0.0.2
{ | ||
"name": "@firstaccess/analysis-outputs-mapper", | ||
"version": "0.0.1", | ||
"main": "src/index.js", | ||
"version": "0.0.2", | ||
"main": "lib/index.js", | ||
"repository": "git@github.com:firstaccess/analysis-outputs-mapper.git", | ||
"author": "", | ||
"license": "UNLICENSED", | ||
"files": [ | ||
"lib", | ||
"src" | ||
], | ||
"scripts": { | ||
"build": "rimraf lib && yarn babelify", | ||
"babelify": "NODE_ENV=production babel -d lib/ src/ --ignore test.js", | ||
"prepare": "yarn build" | ||
}, | ||
"dependencies": { | ||
"lodash": "4.17.5" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "6.26.0", | ||
"babel-plugin-transform-object-rest-spread": "6.26.0", | ||
"babel-preset-env": "1.6.1", | ||
"rimraf": "2.6.2" | ||
} | ||
} |
110
src/index.js
@@ -1,10 +0,9 @@ | ||
const at = require('lodash/at'); | ||
const map = require('lodash/map'); | ||
const omit = require('lodash/omit'); | ||
const head = require('lodash/head'); | ||
const reduce = require('lodash/reduce'); | ||
const isEmpty = require('lodash/isEmpty'); | ||
const isString = require('lodash/isString'); | ||
const isObject = require('lodash/isObject'); | ||
const flowRight = require('lodash/flowRight'); | ||
import at from 'lodash/at'; | ||
import map from 'lodash/map'; | ||
import head from 'lodash/head'; | ||
import reduce from 'lodash/reduce'; | ||
import isEmpty from 'lodash/isEmpty'; | ||
import isString from 'lodash/isString'; | ||
import isObject from 'lodash/isObject'; | ||
import flowRight from 'lodash/flowRight'; | ||
@@ -17,9 +16,9 @@ /** | ||
* constructOutputs([ | ||
* {field: 'outputs.field_1', value: 1, name: 'field 1'}, | ||
* {field: 'outputs.field_2', value: 2, name: 'field 2'}, | ||
* {field: 'field_1', value: 1, name: 'field 1'}, | ||
* {field: 'field_2', value: 2, name: 'field 2'}, | ||
* ]) | ||
* // returns => | ||
* // { | ||
* // "outputs.field_1": {field: 'outputs.field_1', value: 1, name: 'field 1'}, | ||
* // "outputs.field_2": {field: 'outputs.field_2', value: 2, name: 'field 2'}, | ||
* // "field_1": {field: 'field_1', value: 1, name: 'field 1'}, | ||
* // "field_2": {field: 'field_2', value: 2, name: 'field 2'}, | ||
* // } | ||
@@ -30,3 +29,3 @@ * | ||
*/ | ||
function constructOutputs(outputs) { | ||
export function constructOutputs(outputs) { | ||
return reduce( | ||
@@ -55,3 +54,3 @@ outputs, | ||
*/ | ||
function constructFormData(data) { | ||
export function constructFormData(data) { | ||
return reduce(data.sections, (acc, next) => Object.assign(acc, next), {}); | ||
@@ -67,3 +66,3 @@ } | ||
*/ | ||
function constructFormFields(schema) { | ||
export function constructFormFields(schema) { | ||
return reduce( | ||
@@ -99,3 +98,3 @@ schema.sections, | ||
*/ | ||
function mapSchemaFieldToOptions(schemaField) { | ||
export function mapSchemaFieldToOptions(schemaField) { | ||
const fieldToOptionsMapper = FIELD_TO_OPTIONS_MAPPERS[schemaField.type]; | ||
@@ -126,3 +125,3 @@ | ||
*/ | ||
function getValueByPath({ | ||
export function getValueByPath({ | ||
outputs, | ||
@@ -176,6 +175,7 @@ formFields, | ||
*/ | ||
function createListType(sectionItem, outputs, formFields, formData) { | ||
const { options: columnOptions = {} } = sectionItem; | ||
export function createListType(sectionItem, outputs, formFields, formData) { | ||
const { options: columnOptions = {}, items, ...rest } = sectionItem; | ||
return { | ||
...rest, | ||
columns: [ | ||
@@ -185,4 +185,5 @@ { title: 'Name', key: 'name', options: columnOptions.name }, | ||
], | ||
data: map(sectionItem.items, (item) => { | ||
const options = omit(item, ['name', 'value']); | ||
data: map(items, (item) => { | ||
const { name, value, ...options } = item; | ||
const data = isString(item.value) | ||
@@ -193,3 +194,3 @@ ? getValueByPath({ | ||
formData, | ||
valuePath: item.value, | ||
valuePath: value, | ||
valueOptions: options, | ||
@@ -200,3 +201,3 @@ }) | ||
return { | ||
name: item.name, | ||
name, | ||
value: data, | ||
@@ -219,3 +220,3 @@ }; | ||
*/ | ||
function createDefaultListType(outputs, formFields, formData) { | ||
export function createDefaultListType(outputs, formFields, formData) { | ||
return { | ||
@@ -242,3 +243,3 @@ sections: [ | ||
*/ | ||
function createTableType(sectionItem, outputs, formFields, formData) { | ||
export function createTableType(sectionItem, outputs, formFields, formData) { | ||
// For type table, get all the data from outputs | ||
@@ -255,12 +256,12 @@ const dataObject = getValueByPath({ | ||
return { | ||
...sectionItem, | ||
columns: map(sectionItem.columns, (item, index) => { | ||
const options = omit(item, 'title'); | ||
const { title = index, ...options } = item; | ||
return { | ||
title: item.title || index, | ||
title, | ||
options, | ||
key: index, | ||
options, | ||
}; | ||
}), | ||
options: sectionItem.options, | ||
data, | ||
@@ -278,3 +279,8 @@ }; | ||
*/ | ||
function createTableCustomType(sectionItem, outputs, formFields, formData) { | ||
export function createTableCustomType( | ||
sectionItem, | ||
outputs, | ||
formFields, | ||
formData, | ||
) { | ||
const isPath = (str) => str.split('.').length > 1; | ||
@@ -318,3 +324,3 @@ | ||
return { | ||
columns: sectionItem.columns, | ||
...sectionItem, | ||
data, | ||
@@ -335,3 +341,3 @@ }; | ||
*/ | ||
function getMappedOutput({ | ||
export function mapOutputs({ | ||
analysisSchema, | ||
@@ -361,3 +367,3 @@ data = {}, | ||
'The analysis schema is not properly configured.' + | ||
'The schema should be under the `analysis` key', | ||
'The schema should be an object under the `analysis` key.', | ||
); | ||
@@ -367,3 +373,7 @@ } | ||
if (!Array.isArray(analysisSchema.analysis.sections)) { | ||
throw new Error('Sections are not properly configured'); | ||
throw new Error( | ||
`Expected analysis sections to be an array, got: ${JSON.stringify( | ||
analysisSchema.analysis.sections, | ||
)}.`, | ||
); | ||
} | ||
@@ -374,12 +384,17 @@ | ||
if (!section) { | ||
throw new Error(`Section #${sectionIndex + 1} is empty`); | ||
throw new Error(`Section #${sectionIndex + 1} is empty.`); | ||
} | ||
const sectionName = section.title | ||
? `"${section.title}"` | ||
: `#${sectionIndex + 1}`; | ||
if (!Array.isArray(section.items)) { | ||
throw new Error( | ||
`The items for section ${sectionName} are not properly configured`, | ||
`The items for section ${sectionName} are not properly configured. Expected an array, got: ${JSON.stringify( | ||
section.items, | ||
)}.`, | ||
); | ||
} | ||
return { | ||
@@ -389,13 +404,15 @@ title: section.title, | ||
items: section.items.map((sectionItem, itemIndex) => { | ||
if (!sectionItem.type) { | ||
if (!sectionItem) { | ||
throw new Error( | ||
`Item #${itemIndex + 1} in section ${sectionName} is empty`, | ||
`Item #${itemIndex + 1} in section ${sectionName} is empty.`, | ||
); | ||
} | ||
const itemName = sectionItem.title | ||
? `"${sectionItem.title}"` | ||
: `#${itemIndex + 1}`; | ||
if (!sectionItem.type) { | ||
throw new Error( | ||
`The type of item ${itemName} in section ${sectionName} is not properly configured`, | ||
`The type of item ${itemName} in section ${sectionName} is not properly configured.`, | ||
); | ||
@@ -408,8 +425,7 @@ } | ||
throw new Error( | ||
`The type of item ${itemName} in section ${sectionName} is set to an unknown type`, | ||
`The type of item ${itemName} in section ${sectionName} is set to an unknown type.`, | ||
); | ||
} | ||
const value = mapper(sectionItem, outputs, formFields, formData); | ||
return Object.assign({}, sectionItem, value); | ||
return mapper(sectionItem, outputs, formFields, formData); | ||
}), | ||
@@ -424,9 +440,1 @@ }; | ||
} | ||
module.exports = { | ||
getMappedOutput, | ||
createListType, | ||
createTableType, | ||
createTableCustomType, | ||
getValueByPath, | ||
}; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
44220
5
1315
4
2