You can use the following code in order to split a full name into 2 separate columns. The following hook example was built for Portal 2.0.
import FlatfileImporter from "@flatfile/adapter";
import $ from "jquery";
import { format, isDate, isFuture, parseISO } from "date-fns";
import countries from "../countries";
import { schemas } from "./schemas";
const { contactSchema } = schemas;
const importer = new FlatfileImporter(
"5fdae7f9-84ca-43bd-b178-2c401871be38",
contactSchema
);
importer.setCustomer({
userId: "19234",
name: "Foo Bar"
});
importer.registerStepHook("review", ({ headers_matched }) => {
// review stephook happens right before the data goes to the review page
if(
headers_matched.find((v)=>v.matched_key==="firstName")&&
headers_matched.find((v)=>v.matched_key==="lastName")&&
!headers_matched.find((v)=>v.matched_key==="fullName")
){
importer.addVirtualField(
{
label: "Full Name",
key: "fullName"
},
{
order: 2
//hideFields: ["firstName", "lastName"]
}
);
}
if(
!headers_matched.find((v)=>v.matched_key==="firstName")&&
!headers_matched.find((v)=>v.matched_key==="lastName")&&
headers_matched.find((v)=>v.matched_key==="fullName")
){
importer.addVirtualField(
{
label: "First Name",
key: "firstName"
},
{
order: 2
}
);
importer.addVirtualField(
{
label: "Last Name",
key: "lastName"
},
{
order: 3
}
);
}
});
importer.registerRecordHook(async (record, index, mode) => {
letout={};
// if virtual field has been added, insert values into it
if(record.firstName&&record.lastName&&!record.fullName){
if(record.firstName){
out.fullName={
value: record.lastName
?record.firstName+""+record.lastName
:record.firstName
};
}elseif(record.lastName){
out.fullName={ value:record.lastName};
}
}
// name splitting example: splits full names in the first name field
if(record.fullName&&!record.lastName&&!record.firstName){
if(record.fullName.includes("")){
constcomponents=record.fullName.split("");
out.firstName={ value:components.shift()};
out.lastName={ value:components.join("")};
}
}
returnout;
});
$("#launch").click(function () {
importer
.requestDataFromUser()
.then(function(results){
importer.displaySuccess("Thanks for your data.");
$("#raw_output").text(JSON.stringify(results.data,"",2));
})
.catch(function(error){
console.info(error||"window close");
});
});
Comments
0 comments
Please sign in to leave a comment.