1
\$\begingroup\$

On Louis Vuitton bags they contain a "date code" on a tag inside the bag. This code is sometimes used to validate the authenticity of the bag.

In reference to this site: http://authenticationfiles.com/louis-vuitton-date-and-country-code.html and the rules displayed there, I created a "Louis Vuitton Date Code Validator".

The validation and year(circa) setting seems to be working fine within my react app and validates the examples that are shown within the tables. What ways could I improve my code to better parse and validate the date codes?

I tried my best to think of all the possibilities of how the code could be fake and checked for them. I think I can remove all the times I'm setting this.item.validDateCode = false as it's already set to false.

validateDateCode() {
    const countryCodes = ["A0", "A1", "A2", "AA", "AAS", "AH", "AN", "AR", "AS", "BA", "BJ", "BU", "DR", "DU", "DR", "DT", "CO", "CT", "CX", "ET", "FL", "LW", "MB", "MI", "NO", "RA", "RI", "SD", "SF", "SL", "SN", "SP", "SR", "TJ", "TH", "TN", "TR", "TS", "VI", "VX"];
    const dateCode = this.item.dateCode;
    const dateCodeLength = dateCode.length;

    if (dateCodeLength === 3 || dateCodeLength === 4) {
        /**
        * 3 & 4 digit date codes are valid if they only contain
        * numbers so we check if dateCode is a number.
        */
        if (!isNaN(dateCode)) {
            /**
            * Grab first 2 digits in year, because 3 digit
            * date codes are in format YYM and 4 digit date
            * codes are in format YYMM.
            */
            let year = dateCode.substring(0, 2);
            // Anything before 1980 contains no dateCode
            if (parseInt(year) >= 80) {
                this.item.validDateCode = true;
                this.item.circa = `19${year}`;
            } else {
                this.item.validDateCode = false;
            }
        } else {
            this.item.validDateCode = false;
        }
    } else if (dateCodeLength === 5 || dateCodeLength === 6) {
        /**
        * 5 & 6 digit date codes contain numbers and letters so
        * we will check if it is not a number.
        */
        if (isNaN) {
            let firstStringOfDateCode = dateCode.substring(0, 1);
            // Check if first string in dateCode is number or letter
            if (isNaN(firstStringOfDateCode)) {
                if (dateCodeLength === 5) {
                    let countryCode = dateCode.substring(0, 2);
                    // Check if valid country code
                    if (_.includes(countryCodes, countryCode)) {
                        let dateCodeExcludeCC = dateCode.substring(2, 5);
                        // Check remaining if number
                        if (!isNaN(dateCodeExcludeCC)) {
                            let year = dateCode.substring(2, 4);
                            // Check if year is netweem 1980 and 1989
                            if (parseInt(year) >= 80 && parseInt(year) <= 89) {
                                this.item.validDateCode = true;
                                this.item.circa = `19${year}`;
                            } else {
                                this.item.validDateCode = false;
                            }
                        } else {
                            this.item.validDateCode = false;
                        }
                    } else {
                        this.item.validDateCode = false;
                    }
                } else {
                    let countryCode = dateCode.substring(0, 2);
                    // Check if valid country code
                    if (_.includes(countryCodes, countryCode)) {
                        // Check remaining if number
                        let dateCodeExcludeCC = dateCode.substring(2, 6);
                        if (!isNaN(dateCodeExcludeCC)) {
                            /**
                            * From here we need to figure out if the 6 digit
                            * date code, that starts with a country code, is
                            * from the 80s, 90s or 00s.
                            */
                            let thirdPosition = dateCode.substring(2, 3),
                                fourthPosition = dateCode.substring(3, 4);

                            if (fourthPosition === '0' || fourthPosition === '1') {
                                let y1 = fourthPosition,
                                    y2 = dateCode.substring(5, 6),
                                    currentYear = new Date().getFullYear(),
                                    currentTenthsYear = currentYear.toString().substring(2, 3);
                                // Check if tenths place is greater than current years
                                if (parseInt(y1) <= parseInt(currentTenthsYear)) {
                                    this.item.validDateCode = true;
                                    this.item.circa = `20${y1}${y2}`;
                                } else {
                                    this.item.validDateCode = false;
                                }
                            } else if (fourthPosition === '9') {
                                let year = dateCode.substring(5, 6);
                                this.item.validDateCode = true;
                                this.item.circa = `199${year}`;
                            } else if (thirdPosition === '8') {
                                this.item.validDateCode = true;
                                this.item.circa = `198${fourthPosition}`;
                            } else {
                                this.item.validDateCode = false;
                            }
                        } else {
                            this.item.validDateCode = false;
                        }
                    } else {
                        this.item.validDateCode = false;
                    }
                }
            } else {
                let countryCode = (dateCodeLength === 5) ? dateCode.substring(3, 5) | dateCode.substring(4, 6);
                // Check if valid country code
                if (_.includes(countryCodes, countryCode)) {
                    let year = dateCode.substring(0, 2);
                    // Check if year is past 1980
                    if (parseInt(year) > 80) {
                        this.item.validDateCode = true;
                        this.item.circa = `19${year}`;
                    } else {
                        this.item.validDateCode = false;
                    }
                } else {
                    this.item.validDateCode = false;
                }
            }
        } else {
            this.item.validDateCode = false;
        }
    } else {
        this.item.validDateCode = false;
    }

    this.emit("change");
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$
  1. You're correct. You definitely don't need this.item.validDateCode = false that many times. You can set it to false in the before your conditional checks because it is only set to true if the item matches your criteria.

  2. You can use regex to do most of your matching. It would cut down on a lot of logic. For example:

    var matches = /([0-9]{2})([0-9]{1})/.exec(dateCode);
    // logic here
    

    This would handle 3 digit date codes, but you could probably come up with one that fits your situation, and it would be trivial to handle letter + number codes. Using regex would make your logic a lot more readable and self-documenting. Your comments are good, but it is still very difficult to follow along.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.