Skip to content

Instantly share code, notes, and snippets.

@rkleine
Last active May 23, 2018 12:11

Revisions

  1. rkleine renamed this gist Aug 12, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. rkleine revised this gist Aug 12, 2017. 1 changed file with 9 additions and 2 deletions.
    11 changes: 9 additions & 2 deletions cuit-cuit.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    function generateCUIL(dni, type) {
    if (dni.length < 7) return false;
    if (dni.toString().length < 7) return false;

    let xy = 30;

    @@ -9,7 +9,14 @@ function generateCUIL(dni, type) {
    xy = 20;
    }

    const parts = `${xy}${dni}`.split('');
    const parts = `${dni}`.split('');
    const diff = 8 - parts.length;

    for (let i = 0; i < diff; i++) {
    parts.unshift('0');
    }

    parts.unshift(...`${xy}`.split(''))

    const numbers = '5432765432'.split('');
    let sum = 0;
  3. rkleine revised this gist Aug 12, 2017. 2 changed files with 64 additions and 24 deletions.
    64 changes: 64 additions & 0 deletions cuit-cuit.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    function generateCUIL(dni, type) {
    if (dni.length < 7) return false;

    let xy = 30;

    if (type === 'f') {
    xy = 27;
    } else if(type === 'm') {
    xy = 20;
    }

    const parts = `${xy}${dni}`.split('');

    const numbers = '5432765432'.split('');
    let sum = 0;

    for (let i = 0; i <= 9; i++) {
    sum += +parts[i] * +numbers[i];
    }

    const remainder = sum % 11;
    let z = 11 - remainder;

    if (remainder === 0) {
    z = 0;
    } else if (remainder === 1 && type === 'm') {
    z = 9;
    xy = 23;
    } else if (remainder === 1 && type === 'f') {
    z = 4;
    xy = 23;
    }
    return `${xy}-${dni}-${z}`;
    }

    function validateCUIT(number, type) {
    const parts = `${number}`.split('');

    if (parts.length !== 11) return false;

    const numbers = '5432765432'.split('');
    let sum = 0;

    for (let i = 0; i <= 9; i++) {
    sum += +parts[i] * +numbers[i];
    }

    const remainder = sum % 11;
    let z = 11 - remainder;

    if (remainder === 0) {
    z = 0;
    } else if (remainder === 1 && type === 'm') {
    z = 9;
    xy = 23;
    } else if (remainder === 1 && type === 'f') {
    z = 4;
    xy = 23;
    } else if (remainder === 1) {
    z = 9;
    }

    return z === +parts[10];
    }
    24 changes: 0 additions & 24 deletions cuit-validation.js
    Original file line number Diff line number Diff line change
    @@ -1,24 +0,0 @@
    function validCUIT(number) {
    number = String(number || '').split('');

    if (number.length !== 11) { return false; }

    var numbers = '5432765432'.split(''),
    result = 0;

    for (var i = 0; i <= 9; i++) {
    result += Number(number[i]) * Number(numbers[i]);
    }

    result = result % 11;
    result = 11 - result;

    if (result === 11) { result = 0; }
    if (result === 10) { result = 9; }

    return result === Number(number[10]);
    }

    validCUIT(20311251407) // => true
    validCUIT("20-31125140-7") // => true
    validCUIT(20311251405) // => false
  4. rkleine revised this gist May 29, 2017. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  5. rkleine revised this gist Mar 26, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion valid_cuit.rb
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ def valid_cuit?
    numbers = %w(5 4 3 2 7 6 5 4 3 2)
    result = 0

    9.times.each { |i| result += number[i].to_i * numbers[i].to_i }
    10.times.each { |i| result += number[i].to_i * numbers[i].to_i }

    result = result % 11
    result = 11 - result
  6. rkleine revised this gist Mar 26, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion valid_cuit.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    class Object
    def valid_cuit?
    number = "20-31125140-7".to_s.gsub(/\D/, '').split('')
    number = self.to_s.gsub(/\D/, '').split('')

    return if number.size != 11

  7. rkleine revised this gist Mar 26, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion valid_cuit.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    function isValidCUIT(number) {
    function validCUIT(number) {
    number = String(number || '').split('');

    if (number.length !== 11) { return false; }
  8. rkleine revised this gist Mar 26, 2015. 2 changed files with 27 additions and 2 deletions.
    24 changes: 24 additions & 0 deletions valid_cuit.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    function isValidCUIT(number) {
    number = String(number || '').split('');

    if (number.length !== 11) { return false; }

    var numbers = '5432765432'.split(''),
    result = 0;

    for (var i = 0; i <= 9; i++) {
    result += Number(number[i]) * Number(numbers[i]);
    }

    result = result % 11;
    result = 11 - result;

    if (result === 11) { result = 0; }
    if (result === 10) { result = 9; }

    return result === Number(number[10]);
    }

    validCUIT(20311251407) // => true
    validCUIT("20-31125140-7") // => true
    validCUIT(20311251405) // => false
    5 changes: 3 additions & 2 deletions valid_cuit.rb
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,11 @@
    class Object
    def valid_cuit?
    number = self.to_s.gsub(/\D/, '').split('')
    number = "20-31125140-7".to_s.gsub(/\D/, '').split('')

    return if number.size != 11

    numbers = %w(5 4 3 2 7 6 5 4 3 2)
    result = 0;
    result = 0

    9.times.each { |i| result += number[i].to_i * numbers[i].to_i }

    @@ -20,4 +20,5 @@ def valid_cuit?
    end

    20311251407.valid_cuit? # => true
    "20-31125140-7".valid_cuit? # => true
    20311251405.valid_cuit? # => false
  9. rkleine revised this gist Mar 26, 2015. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion valid_cuit.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    class Object
    def valid_cuit?
    number = self.to_s.split('')
    number = self.to_s.gsub(/\D/, '').split('')

    return if number.size != 11

    @@ -18,3 +18,6 @@ def valid_cuit?
    result == number[10].to_i
    end
    end

    20311251407.valid_cuit? # => true
    20311251405.valid_cuit? # => false
  10. rkleine revised this gist Mar 26, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion valid_cuit.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    class Object
    def valid_cuit?
    number = (self.to_s || '').split('')
    number = self.to_s.split('')

    return if number.size != 11

  11. rkleine renamed this gist Mar 26, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  12. rkleine created this gist Mar 26, 2015.
    20 changes: 20 additions & 0 deletions valid_cuit
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    class Object
    def valid_cuit?
    number = (self.to_s || '').split('')

    return if number.size != 11

    numbers = %w(5 4 3 2 7 6 5 4 3 2)
    result = 0;

    9.times.each { |i| result += number[i].to_i * numbers[i].to_i }

    result = result % 11
    result = 11 - result

    result = 0 if result == 11
    result = 9 if result == 10

    result == number[10].to_i
    end
    end