Skip to content

Commit c64125e

Browse files
feat(FeedTransformation): Add normalize field transformation
1 parent 7bc421c commit c64125e

File tree

5 files changed

+56
-5
lines changed

5 files changed

+56
-5
lines changed

i18n/english.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ components:
244244
DeleteRecordsTransformation:
245245
label: Delete records from %tablePlaceholder%
246246
name: Delete records transformation
247+
NormalizeFieldTransformation:
248+
label: Normalize fields
249+
name: Normalize fields transformation
247250
ReplaceFileFromStringTransformation:
248251
label: Replace %tablePlaceholder% from %filePlaceholder%
249252
name: Replace file from string transformation

lib/common/constants/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const RETRIEVAL_METHODS = Object.freeze({
3434

3535
export const FEED_TRANSFORMATION_TYPES = Object.freeze({
3636
DELETE_RECORDS: 'DeleteRecordsTransformation',
37+
NORMALIZE_FIELDS: 'NormalizeFieldTransformation',
3738
REPLACE_FILE_FROM_VERSION: 'ReplaceFileFromVersionTransformation',
38-
REPLACE_FILE_FROM_STRING: 'ReplaceFileFromStringTransformation'
39+
REPLACE_FILE_FROM_STRING: 'ReplaceFileFromStringTransformation',
3940
})

lib/manager/components/transform/FeedTransformRules.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ function newFeedTransformation (type: string = 'ReplaceFileFromVersionTransforma
3232

3333
const feedTransformationTypes = [
3434
'ReplaceFileFromVersionTransformation',
35-
'ReplaceFileFromStringTransformation'
35+
'ReplaceFileFromStringTransformation',
36+
'NormalizeFieldTransformation'
3637
]
3738

3839
type TransformRulesProps = {

lib/manager/components/transform/FeedTransformation.js

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ type Props = {
2727
* Component that renders fields for a single feed transformation
2828
* (e.g., ReplaceFileFromStringTransformation).
2929
*/
30-
export default class FeedTransformation extends Component<Props, {csvData: ?string}> {
31-
state = { csvData: null }
30+
export default class FeedTransformation extends Component<Props, {csvData: ?string, fieldName: ?string}> {
31+
state = { csvData: null, fieldName: null }
3232

3333
_getFieldsForType = (type: string) => {
3434
const {feedSource, transformation} = this.props
@@ -55,7 +55,7 @@ export default class FeedTransformation extends Component<Props, {csvData: ?stri
5555
/>
5656
)
5757
break
58-
case 'ReplaceFileFromStringTransformation':
58+
case 'ReplaceFileFromStringTransformation': {
5959
const inputIsUnchanged = this.state.csvData === null
6060
const csvData = inputIsUnchanged
6161
? transformation.csvData
@@ -96,6 +96,41 @@ export default class FeedTransformation extends Component<Props, {csvData: ?stri
9696
</div>
9797
)
9898
break
99+
}
100+
case 'NormalizeFieldTransformation': {
101+
const inputIsUnchanged = this.state.fieldName === null
102+
const fieldName = inputIsUnchanged
103+
? transformation.fieldName
104+
: this.state.fieldName
105+
fields.push(
106+
<div>
107+
<label htmlFor='fieldName'>
108+
Field to normalize:
109+
<input
110+
id='fieldName'
111+
onChange={this._onChangeFieldToNormalize}
112+
value={fieldName} />
113+
</label>
114+
<div style={{marginBottom: '10px'}}>
115+
<Button
116+
bsSize='xsmall'
117+
disabled={inputIsUnchanged}
118+
style={{marginRight: '5px'}}
119+
onClick={this._onSaveFieldToNormalize}>
120+
Save CSV
121+
</Button>
122+
</div>
123+
124+
This transform will:
125+
<ul>
126+
<li>Capitalize bus stop names (for bus stops in all-caps),</li>
127+
<li>Replace '+' and '&amp;' with 'and', '@' with 'at',</li>
128+
<li>Remove contents within parentheses.</li>
129+
</ul>
130+
</div>
131+
)
132+
break
133+
}
99134
default:
100135
break
101136
}
@@ -141,6 +176,16 @@ export default class FeedTransformation extends Component<Props, {csvData: ?stri
141176
this.props.onChange({csvData}, this.props.index)
142177
}
143178

179+
_onChangeFieldToNormalize = (evt: SyntheticInputEvent<HTMLInputElement>) => {
180+
this.setState({fieldName: evt.target.value})
181+
}
182+
183+
_onSaveFieldToNormalize = () => {
184+
const fieldName = this.state.fieldName || null
185+
this.setState({fieldName: null})
186+
this.props.onChange({fieldName}, this.props.index)
187+
}
188+
144189
_onRemoveTransformation = () => {
145190
this.props.onRemove(this.props.index)
146191
}

lib/types/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ export type FeedTransformation = {
295295
'@type': $Values<typeof FEED_TRANSFORMATION_TYPES>,
296296
active: boolean,
297297
csvData?: string,
298+
fieldName?: string,
298299
matchField?: string,
299300
matchValues?: Array<string>,
300301
sourceVersionId?: string,

0 commit comments

Comments
 (0)