Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. angular.module('handstack').service("InterpreterService", function(){
  2. this.getContactsFromRawCSV = function(file, callback){
  3. /**
  4. * Take CSV in a format similar to:
  5. *
  6. * '
  7. * firstName,lastName,phone,email
  8. * ben,jones,1234,b@b.com
  9. * jessica,lee,2345,j@j.com
  10. * '
  11. *
  12. * and output an array similar to
  13. *
  14. * '
  15. * [
  16. * {
  17. * name:"ben jones",
  18. * phone: "1234",
  19. * email: "b@b.com"
  20. * },
  21. * {
  22. * name:"ben jones",
  23. * phone: "1234",
  24. * email: "b@b.com"
  25. * }
  26. * ]
  27. * '
  28. *
  29. * The order of the values of the CSV should not matter. Also the legend object defines acceptable key names,
  30. * first example the key "phoneNumber" will translate to the phone property of the outputted object.
  31. */
  32.  
  33. let reader = new FileReader();
  34.  
  35. let legend = {
  36. "firstName": ["first name", "first_name", "firstName", "First Name", "first"],
  37. "lastName": ["last name", "last_name", "lastName", "Last Name", "last"],
  38. "phone": ["phone number", "phone_number", "phoneNumber", "Phone Number", "Number", "number", "phone", "Phone"],
  39. "email": ["email", "email_address", "emailAddress", "Email Address", "Email"]
  40. };
  41.  
  42. let key = {};
  43.  
  44. reader.onload = (event) => {
  45. let csvString = event.currentTarget.result;
  46. let lines = csvString.split(/\r\n|\n/);
  47.  
  48. // build our legend-key based on the labels provided in the first line of the csv
  49. let labels = lines.shift().split(','); // INEFFICIENT FOR LARGE ARRAYS
  50.  
  51. let index = 0;
  52. labels.forEach((label) => {
  53. _.each(legend, (legendValue, legendKey) => {
  54. console.log(label, legendKey, legendValue, _.contains(legendValue, label));
  55. if(_.contains(legendValue, label)){
  56. console.log(legendKey + " found!");
  57. key[index] = legendKey;
  58. }
  59. });
  60.  
  61. console.log(key);
  62. index++;
  63. });
  64.  
  65. // based on our legend key, map each remaining line of the csv into a contact object
  66. let contacts = lines
  67. .map((line) => line.split(','))
  68. .map((arrayOfValues) => {
  69. let contact = {};
  70.  
  71. _.each(key, (keyValue, keyKey) => {
  72. contact[keyValue] = arrayOfValues[keyKey];
  73. });
  74.  
  75. return contact;
  76. })
  77. .map((contactObject) => {
  78. contactObject.name = contactObject.firstName + " " + contactObject.lastName;
  79. delete contactObject.firstName;
  80. delete contactObject.lastName;
  81.  
  82. if (contactObject.phone) return contactObject;
  83. })
  84. .filter((item) => (item !== undefined));
  85.  
  86. console.log(contacts);
  87. callback(contacts);
  88. };
  89.  
  90. reader.readAsText(file);
  91. }
  92. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement