Skip to content

Commit

Permalink
feat(FeedTransformation): Add normalize field transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
binh-dam-ibigroup committed Mar 19, 2021
1 parent 7bc421c commit c64125e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
3 changes: 3 additions & 0 deletions i18n/english.yml
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ components:
DeleteRecordsTransformation:
label: Delete records from %tablePlaceholder%
name: Delete records transformation
NormalizeFieldTransformation:
label: Normalize fields
name: Normalize fields transformation
ReplaceFileFromStringTransformation:
label: Replace %tablePlaceholder% from %filePlaceholder%
name: Replace file from string transformation
Expand Down
3 changes: 2 additions & 1 deletion lib/common/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const RETRIEVAL_METHODS = Object.freeze({

export const FEED_TRANSFORMATION_TYPES = Object.freeze({
DELETE_RECORDS: 'DeleteRecordsTransformation',
NORMALIZE_FIELDS: 'NormalizeFieldTransformation',
REPLACE_FILE_FROM_VERSION: 'ReplaceFileFromVersionTransformation',
REPLACE_FILE_FROM_STRING: 'ReplaceFileFromStringTransformation'
REPLACE_FILE_FROM_STRING: 'ReplaceFileFromStringTransformation',
})
3 changes: 2 additions & 1 deletion lib/manager/components/transform/FeedTransformRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ function newFeedTransformation (type: string = 'ReplaceFileFromVersionTransforma

const feedTransformationTypes = [
'ReplaceFileFromVersionTransformation',
'ReplaceFileFromStringTransformation'
'ReplaceFileFromStringTransformation',
'NormalizeFieldTransformation'
]

type TransformRulesProps = {
Expand Down
51 changes: 48 additions & 3 deletions lib/manager/components/transform/FeedTransformation.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type Props = {
* Component that renders fields for a single feed transformation
* (e.g., ReplaceFileFromStringTransformation).
*/
export default class FeedTransformation extends Component<Props, {csvData: ?string}> {
state = { csvData: null }
export default class FeedTransformation extends Component<Props, {csvData: ?string, fieldName: ?string}> {
state = { csvData: null, fieldName: null }

_getFieldsForType = (type: string) => {
const {feedSource, transformation} = this.props
Expand All @@ -55,7 +55,7 @@ export default class FeedTransformation extends Component<Props, {csvData: ?stri
/>
)
break
case 'ReplaceFileFromStringTransformation':
case 'ReplaceFileFromStringTransformation': {
const inputIsUnchanged = this.state.csvData === null
const csvData = inputIsUnchanged
? transformation.csvData
Expand Down Expand Up @@ -96,6 +96,41 @@ export default class FeedTransformation extends Component<Props, {csvData: ?stri
</div>
)
break
}
case 'NormalizeFieldTransformation': {
const inputIsUnchanged = this.state.fieldName === null
const fieldName = inputIsUnchanged
? transformation.fieldName
: this.state.fieldName
fields.push(
<div>
<label htmlFor='fieldName'>
Field to normalize:
<input
id='fieldName'
onChange={this._onChangeFieldToNormalize}
value={fieldName} />
</label>
<div style={{marginBottom: '10px'}}>
<Button
bsSize='xsmall'
disabled={inputIsUnchanged}
style={{marginRight: '5px'}}
onClick={this._onSaveFieldToNormalize}>
Save CSV
</Button>
</div>

This transform will:
<ul>
<li>Capitalize bus stop names (for bus stops in all-caps),</li>
<li>Replace '+' and '&amp;' with 'and', '@' with 'at',</li>
<li>Remove contents within parentheses.</li>
</ul>
</div>
)
break
}
default:
break
}
Expand Down Expand Up @@ -141,6 +176,16 @@ export default class FeedTransformation extends Component<Props, {csvData: ?stri
this.props.onChange({csvData}, this.props.index)
}

_onChangeFieldToNormalize = (evt: SyntheticInputEvent<HTMLInputElement>) => {
this.setState({fieldName: evt.target.value})
}

_onSaveFieldToNormalize = () => {
const fieldName = this.state.fieldName || null
this.setState({fieldName: null})
this.props.onChange({fieldName}, this.props.index)
}

_onRemoveTransformation = () => {
this.props.onRemove(this.props.index)
}
Expand Down
1 change: 1 addition & 0 deletions lib/types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ export type FeedTransformation = {
'@type': $Values<typeof FEED_TRANSFORMATION_TYPES>,
active: boolean,
csvData?: string,
fieldName?: string,
matchField?: string,
matchValues?: Array<string>,
sourceVersionId?: string,
Expand Down

0 comments on commit c64125e

Please sign in to comment.