Guest User

Untitled

a guest
Oct 6th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. In a recent project I had to validate a form in Javascript, I've done it thousands of times, but during this one instance I had a stroke of ... `oh my god I can really do this` ... moment. Essentially it cleaned up about 40 lines of code down to about five or six, and ontop of that, you can easily set variables inside your operation or outside, its just beautiful:
  2.  
  3. ``` javascript
  4. return (
  5. (!(username += '') || username === '') ? { error: "No Username Given.", field: 'name' }
  6. : (!(username += '') || password === '') ? { error: "No Password Given.", field: 'pass' }
  7. : (username.length < 3) ? { error: "Username is less than 3 Characters.", field: 'name' }
  8. : (password.length < 4) ? { error: "Password is less than 4 Characters.", field: 'pass' }
  9. : (!/^([a-z0-9-_]+)$/i.test(username)) ? { error: "Username contains invalid characters.", field: 'name' }
  10. : false
  11. );
  12. ```
  13.  
  14. It's really it's own language if you think about it. It's just so concise and direct.
  15.  
  16. ### Basic Validation
  17.  
  18.  
  19. Validate existence:
  20.  
  21. ``` javascript
  22. (!username || username === '') ? { error: "No Username Given.", field: 'name' } // Doesn't Exist? Return this
  23. : false // Otherwise, nothing to return; False is good. It means it passed validation.
  24. ```
  25.  
  26. ### Complex Validation
  27.  
  28. Regular Expressions? Requirements? All easy.
  29.  
  30. ``` javascript
  31. (!/^([a-z0-9-_]+)$/i.test(username)) ? { error: "Username contains invalid characters.", field: 'name' } // Test username against RegExp, return this on failure
  32. : false // It passed!
  33. ```
  34.  
  35. ### Nesting?
  36.  
  37. No problemo.
  38.  
  39. ``` javascript
  40. (!username) ?
  41. (
  42. (!password) ? { error: "Missing Username and Password.", field: ['name', 'pass'] }
  43. : { error: "Missing Username", field: 'name' }
  44. )
  45. : false
  46. ```
  47.  
  48. Complex problems are still simple to solve in the end. It's so beautiful it hurts.
  49.  
  50. ### Variable Setting
  51.  
  52. If you wish to setup variables in a normal manner you can do so on the lines before validation, this only works for things that are not null, undefined, false.
  53.  
  54. ``` javascript
  55. (username = username.toString()) &&
  56. (password = password.toString()) &&
  57. ```
  58.  
  59. Another method in one statement:
  60.  
  61. ``` javascript
  62. (
  63. username = username.toString() &&
  64. password = password.toString()
  65. ) &&
  66. ```
  67.  
  68. Optional Variable Storage? No Problem.
  69.  
  70. ``` javascript
  71. (
  72. username = username.toString() ||
  73. password = password.toString() // Password is now set only if username is true.
  74. ) &&
  75. ```
  76.  
  77. A better method for this scenario would be to coerce the variable into a string: `!(username += "")` and check it.
  78.  
  79. Or simply do it before the return ;)
Add Comment
Please, Sign In to add comment