Last active
September 11, 2020 03:14
-
-
Save leandrovip/e9b246a291455503a228d7073900f0aa to your computer and use it in GitHub Desktop.
Método de extensão para verificar se um código é um GTIN (8,12,13,14) válido
This file contains 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
/// <summary> | |
/// Verifica se um código é um GTIN (8,12,13,14) válido | |
/// </summary> | |
/// <param name="value">Código</param> | |
/// <returns><b>true</b> caso for válido</returns> | |
public static bool IsGtin(this string value) | |
{ | |
var code = value.OnlyNumbers(); | |
if (code.IsNullOrEmpty()) return false; | |
code = code.ZeroFill(14); | |
var digit = code[13] - '0'; | |
code = code.Substring(0, 13); | |
var sum = 0; | |
var factor = 3; | |
foreach (var dig in code) | |
{ | |
sum += (dig - '0') * factor; | |
factor = factor == 3 ? 1 : 3; | |
} | |
var check = (10 - sum % 10) % 10; | |
return check == digit; | |
} | |
/// <summary> | |
/// Retorna apenas os numeros da string. | |
/// </summary> | |
/// <param name="value">String para processar.</param> | |
/// <returns>System.String.</returns> | |
public static string OnlyNumbers(this string value) | |
{ | |
return value.TrimVip().IsNullOrEmpty() | |
? string.Empty | |
: new string(value.Where(char.IsDigit).ToArray()); | |
} | |
/// <summary> | |
/// Verifica se a string é nula ou vazia | |
/// </summary> | |
/// <param name="value">string para verificar</param> | |
public static bool IsNullOrEmpty(this string value) | |
{ | |
return string.IsNullOrWhiteSpace(value) || string.IsNullOrWhiteSpace(value); | |
} | |
/// <summary> | |
/// Preenche uma string com zero a direita ate ficar do tamanho especificado. | |
/// </summary> | |
/// <param name="text">The text.</param> | |
/// <param name="length">Tamanho final desejado</param> | |
/// <returns>String do tamanho especificado e se menor complementada com zero a direita/esquerda</returns> | |
public static string ZeroFill(this string text, int length) | |
{ | |
return text.OnlyNumbers().StringFill(length, '0'); | |
} | |
/// <summary> | |
/// Alinha a string a direita/esquerda e preenche com caractere informado ate ficar do tamanho especificado. | |
/// </summary> | |
/// <param name="text">O texto</param> | |
/// <param name="length">Tamanho final desejado</param> | |
/// <param name="with">Caractere para preencher</param> | |
/// <param name="esquerda">Direção do preenchimento</param> | |
/// <returns>String do tamanho especificado e se menor complementada com o caractere informado a direita/esquerda</returns> | |
public static string StringFill(this string text, int length, char with = ' ', bool esquerda = true) | |
{ | |
if (text.IsNullOrEmpty()) text = string.Empty; | |
if (text.Length > length) | |
{ | |
text = text.Remove(length); | |
} | |
else | |
{ | |
length -= text.Length; | |
if (esquerda) | |
text = new string(with, length) + text; | |
else | |
text += new string(with, length); | |
} | |
return text; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment