Skip to content

Instantly share code, notes, and snippets.

@moshmage
Last active November 9, 2017 18:31
Show Gist options
  • Save moshmage/a2949f95f20f2ebeace8fef72ec462d2 to your computer and use it in GitHub Desktop.
Save moshmage/a2949f95f20f2ebeace8fef72ec462d2 to your computer and use it in GitHub Desktop.

Revisions

  1. moshmage revised this gist Nov 9, 2017. 1 changed file with 24 additions and 24 deletions.
    48 changes: 24 additions & 24 deletions rut-validation.ts
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,27 @@
    /**
    * Provide string of type 12.333.333-k|123456789|12345678-9|12.345678-9
    * ???
    * profit.
    * @param {string} _rut
    * @returns {boolean}
    */
    isValidRUT(_rut: string) {
    const isRegexValid = _rut.match(/^\d{1,2}\.?\d{3}\.?\d{3}\-?[0-9kK]$/ig);
    if (!isRegexValid) return false;
    /** return early if the provided string is not worthy */
    /**
    * Provide string of type 12.333.333-k|123456789|12345678-9|12.345678-9
    * ???
    * profit.
    * @param {string} _rut
    * @returns {boolean}
    */
    function isValidRUT(_rut) {
    const isRegexValid = _rut.match(/^\d{1,2}\.?\d{3}\.?\d{3}\-?[0-9kK]$/ig);
    if (!isRegexValid) return false;
    /** return early if the provided string is not worthy */

    const rut = _rut.replace(/\D/ig, '');
    /** replace everything that's not a digit from the pool */
    const rut = _rut.replace(/\D/ig, '');
    /** replace everything that's not a digit from the pool */

    const rest = 11 - ([3, 2, 7, 6, 5, 4, 3, 2]
    .reduce((_total, multiplier, index) => _total + (multiplier * +rut[index])) % 11);
    /** subtract eleven to the sum of the magic multiplication and the module of elven */
    const lastChar = _rut[_rut.length - 1];
    const rest = 11 - ([3, 2, 7, 6, 5, 4, 3, 2]
    .reduce((_total, multiplier, index) => _total + (multiplier * +rut[index]), 0) % 11);
    /** subtract eleven to the sum of the magic multiplication and the module of elven */

    const lastChar = _rut[_rut.length - 1];

    if (rest < 11 && ('' + rest) === lastChar) return true;
    /** if the rest is less than elven and equal to the provided strings last character */
    return rest === 11 && lastChar === '0' || rest === 10 && lastChar.search(/[kK]/ig) > -1;
    /** Otherwise check if rest is eleven, or ten, and equal to 0 or K respectively */
    };
    if (rest < 11 && ('' + rest) === lastChar) return true;
    /** if the rest is less than elven and equal to the provided strings last character */

    return rest === 11 && lastChar === '0' || rest === 10 && lastChar.search(/[kK]/ig) > -1;
    /** Otherwise check if rest is eleven, or ten, and equal to 0 or K respectively */
    };
  2. moshmage created this gist Nov 8, 2017.
    27 changes: 27 additions & 0 deletions rut-validation.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    /**
    * Provide string of type 12.333.333-k|123456789|12345678-9|12.345678-9
    * ???
    * profit.
    * @param {string} _rut
    * @returns {boolean}
    */
    isValidRUT(_rut: string) {
    const isRegexValid = _rut.match(/^\d{1,2}\.?\d{3}\.?\d{3}\-?[0-9kK]$/ig);
    if (!isRegexValid) return false;
    /** return early if the provided string is not worthy */

    const rut = _rut.replace(/\D/ig, '');
    /** replace everything that's not a digit from the pool */

    const rest = 11 - ([3, 2, 7, 6, 5, 4, 3, 2]
    .reduce((_total, multiplier, index) => _total + (multiplier * +rut[index])) % 11);
    /** subtract eleven to the sum of the magic multiplication and the module of elven */

    const lastChar = _rut[_rut.length - 1];

    if (rest < 11 && ('' + rest) === lastChar) return true;
    /** if the rest is less than elven and equal to the provided strings last character */

    return rest === 11 && lastChar === '0' || rest === 10 && lastChar.search(/[kK]/ig) > -1;
    /** Otherwise check if rest is eleven, or ten, and equal to 0 or K respectively */
    };