Last active
August 12, 2020 18:24
Revisions
-
micheltlutz revised this gist
Aug 12, 2020 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -7,7 +7,6 @@ extension Collection where Element == Int { let mod = 11 let secondMod = 10 let factors = Array((lowerBound...upperBound).reversed()) let multiplied = self.reversed().enumerated().map { -
micheltlutz revised this gist
Aug 12, 2020 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ import Foundation extension Collection where Element == Int { var digitCNPJ: Int { let upperBound = 9 @@ -17,6 +19,7 @@ extension Collection where Element == Int { return (sum % mod) % secondMod } } extension StringProtocol { var isValidCNPJ: Bool { let numbers = compactMap(\.wholeNumberValue) -
micheltlutz revised this gist
Aug 12, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -28,7 +28,7 @@ extension StringProtocol { return firstDigit == numbers[12] && secondDigit == numbers[13] } var isValidCNPJ: Bool { let numbers = compactMap(\.wholeNumberValue) guard numbers.count == 14 && Set(numbers).count != 1 else { return false } -
micheltlutz created this gist
Aug 12, 2020 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,45 @@ extension Collection where Element == Int { var digitCNPJ: Int { let upperBound = 9 let lowerBound = 2 let mod = 11 let secondMod = 10 guard lowerBound < upperBound else { preconditionFailure("lower bound is greater than upper bound") } let factors = Array((lowerBound...upperBound).reversed()) let multiplied = self.reversed().enumerated().map { return $0.element * factors[$0.offset % factors.count] } let sum = multiplied.reduce(0, +) return (sum % mod) % secondMod } } extension StringProtocol { var isValidCNPJ: Bool { let numbers = compactMap(\.wholeNumberValue) guard numbers.count == 14 && Set(numbers).count != 1 else { return false } let digits = Array(numbers[0..<12]) let firstDigit = checkDigitCNPJ(for: digits, upperBound: 9, lowerBound: 2, mod: 11) let secondDigit = checkDigitCNPJ(for: digits + [firstDigit], upperBound: 9, lowerBound: 2, mod: 11) return firstDigit == numbers[12] && secondDigit == numbers[13] } var isValidCNPJd: Bool { let numbers = compactMap(\.wholeNumberValue) guard numbers.count == 14 && Set(numbers).count != 1 else { return false } return numbers.prefix(12).digitCNPJ == numbers[12] && numbers.prefix(13).digitCNPJ == numbers[13] } } // Usage let cnpj = "29.957.575/0001-89".isValidCNPJ // true //let cnpj = "16.914.610/0001-30".isValidCNPJ // true //let cnpj = "16914610000130".isValidCNPJ // true //let cnpj = "66666666666666".isValidCNPJ // false