daily pastebin goal
38%
SHARE
TWEET

Untitled

a guest Mar 13th, 2018 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //input --> Hamza Khan
  2.     ProcessFullName(full_name): Object {
  3.         var Obj = {
  4.             FirstName: "",
  5.             LastName: "",
  6.             MiddleName: ""
  7.         };
  8.         var splitName = full_name.split(" ");
  9.         if (splitName.length>0) {
  10.             switch (splitName.length) {
  11.                 case 1:
  12.                     Obj.FirstName = splitName[0];
  13.                     break;
  14.                 case 2:
  15.                     Obj.FirstName = splitName[0];
  16.                     Obj.LastName = splitName[1];
  17.                     break;
  18.                 case 3:
  19.                     Obj.FirstName = splitName[0];
  20.                     Obj.MiddleName = splitName[1];
  21.                     Obj.LastName = splitName[2];
  22.                     break;
  23.             }
  24.         }
  25.         return Obj;
  26.     } //Output --> { FirstName: "Hamza", LastName: "Khan", MiddleName: "" }
  27.    
  28. interface SplitNameData {  // Does anybody have a better interface name?
  29.   FirstName: string;
  30.   MiddleName: string;
  31.   LastName: string;
  32. }
  33.  
  34. ProcessFullName(full_name: string): SplitNameData {
  35.   const Obj: SplitNameData = {
  36.     firstName: '',
  37.     middleName: '',
  38.     lastName: ''
  39.   };
  40.    
  41. case 2:
  42.   [Obj.FirstName, Obj.LastName] = splitName;
  43.   break;
  44. case 3:
  45.   [Obj.FirstName, Obj.MiddleName, Obj.LastName] = splitName;
  46.   break;
  47.    
  48. const NAME_PATTERN = /^(?<firstName>[^ ]*) ((?<middleName>[^ ]*) )?(?<lastName>[^ ]*)$/;
  49.  
  50. const emptyNameData = {
  51.   firstName: '', middleName: '', lastName: ''
  52. };
  53.  
  54. const {
  55.   groups: {
  56.     firstName,
  57.     middleName = '',
  58.     lastName
  59.   } = emptyNameData
  60. } = NAME_PATTERN.exec(fullName) || {};
  61. alert([firstName, middleName, lastName]);
  62.    
  63. ProcessFullName(fullName): Object {
  64.     const names = fullName.split(" ");
  65.     const vet = name => name ? name : "";
  66.     return {
  67.         first:  vet(names.shift()),
  68.         last:   vet(names.pop()),
  69.         middle: vet(names.join(" "))
  70.     };
  71. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top