Last active
July 30, 2023 01:39
-
-
Save riseansmal/8dc039839b19526d573f469eeba79dd7 to your computer and use it in GitHub Desktop.
'Tight Code' JavaScript Helper Function to Validate UUIDv4 (RFC4122) Values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Note: Yes, I know there are npm Packages that can do this, but it's | |
| not always the best idea to drag a whole package including all it's | |
| potential vulnerabilities around just for a simple task. | |
| -- Always Be Kind and Help Others to Learn and Grow :) | |
| */ | |
| // 'Tight Code' JavaScript Helper Function to Validate UUIDv4 (RFC4122) Values | |
| function validateUUIDv4(uuid) { | |
| const uuidv4Regex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/; | |
| return uuidv4Regex.test(uuid); | |
| }; | |
| /* | |
| What is Tight Code? | |
| 'Tight Code' is a term used in software development to describe code that is efficient, both in terms of its conciseness and its performance. | |
| Here are the characteristics of tight code: | |
| 1. Conciseness: Tight code often refers to code that is written in a concise way, meaning that it accomplishes its tasks with a minimal amount of code. It's all about doing more with less. | |
| 2. Performance: Tight code is also performance-optimized, meaning it runs quickly and doesn't consume unnecessary resources like memory or CPU cycles. | |
| 3. Readability: Despite being concise and efficient, tight code should still be easy to read and understand. This is essential for maintainability. | |
| 4. No Redundancies: Tight code should not have unnecessary repetitions or unused code blocks. | |
| Remember, writing tight code doesn't mean making it so terse that it becomes difficult to read and understand. | |
| Code readability and maintainability are crucial in software development. | |
| In some cases, writing a few extra lines of code can be worth it if it makes your code easier for others (and your future self) to understand. | |
| */ | |
| /* | |
| MIT License | |
| Copyright (c) 2023 Ri Sean Smal | |
| Permission is hereby granted, free of charge, to any person obtaining a copy | |
| of this software and associated documentation files (the "Software"), to deal | |
| in the Software without restriction, including without limitation the rights | |
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| copies of the Software, and to permit persons to whom the Software is | |
| furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in all | |
| copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| SOFTWARE. | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment