Skip to content

Instantly share code, notes, and snippets.

@Santosl2
Last active October 8, 2024 23:55
Show Gist options
  • Select an option

  • Save Santosl2/2e9a3eea95c6b2ee7a2499ed5317418f to your computer and use it in GitHub Desktop.

Select an option

Save Santosl2/2e9a3eea95c6b2ee7a2499ed5317418f to your computer and use it in GitHub Desktop.
gtm-webhook
___INFO___
{
"type": "MACRO",
"id": "cvt_temp_public_id",
"version": 1,
"securityGroups": [],
"displayName": "Conversor para Webhook | Matheus Filype",
"description": "",
"containerContexts": [
"SERVER"
]
}
___TEMPLATE_PARAMETERS___
[
{
"type": "SELECT",
"name": "opcao",
"displayName": "Tipo de conversão",
"macrosInSelect": true,
"selectItems": [
{
"value": "number",
"displayValue": "Converte para número"
},
{
"value": "string",
"displayValue": "Converter para String"
},
{
"value": "last_name",
"displayValue": "Capturar Sobrenome"
},
{
"value": "float",
"displayValue": "Converter para Dinheiro"
},
{
"value": "city",
"displayValue": "Remover acentos e espaços (padrão cidade fb)"
}
],
"simpleValueType": true
},
{
"type": "TEXT",
"name": "valor",
"displayName": "",
"simpleValueType": true
}
]
___SANDBOXED_JS_FOR_SERVER___
const getType = require('getType');
const JSON = require('JSON');
const makeString = require('makeString');
const makeNumber = require('makeNumber');
const logToConsole = require('logToConsole');
const createRegex = require('createRegex');
const numberRegex = createRegex('[^0-9]', 'g');
let entity = data.valor;
const opcao = data.opcao;
const entityType = getType(entity);
let entityStringified;
if (entityType === 'function') {
return;
}
if (entityType === 'number' && opcao === 'string') {
entityStringified = makeString(entity);
}
if(entityType === 'string' && opcao === 'number'){
entityStringified = makeNumber(entity.replace(numberRegex, ''));
}
if(entityType === 'string' && opcao === 'last_name'){
const nomeRegex = createRegex('( .+)', 'g');
entityStringified = entity.match(nomeRegex)[0].trim();
}
if(entityType === 'number' && opcao === 'float'){
entityStringified = entity / 100;
}
if(entityType === 'string' && opcao === 'float'){
entityStringified = makeNumber(entity) / 100;
}
if(opcao === 'city'){
if(entity){
const accentMap = {
"á": "a", "à": "a", "ã": "a", "â": "a",
"é": "e", "è": "e", "ê": "e",
"í": "i", "ì": "i", "î": "i",
"ó": "o", "ò": "o", "õ": "o", "ô": "o",
"ú": "u", "ù": "u", "û": "u",
"ç": "c",
"Á": "A", "À": "A", "Ã": "A", "Â": "A",
"É": "E", "È": "E", "Ê": "E",
"Í": "I", "Ì": "I", "Î": "I",
"Ó": "O", "Ò": "O", "Õ": "O", "Ô": "O",
"Ú": "U", "Ù": "U", "Û": "U",
"Ç": "C"
};
let result = "";
for (let i = 0; i < entity.length; i++) {
const currentChar = entity.charAt(i);
if(currentChar !== ' '){
result += accentMap[currentChar] || currentChar; // Substitui o caractere se for um acento, caso contrário, mantém
}
}
logToConsole("Processed cityName: " + result);
entity = result.toLowerCase(); // Converte para minúsculas
return entity;
}
// if (entity) {
// let result = '';
// for (let i = 0; i < entity.length; i++) {
// const current = entity.charAt(i).toUpperCase();
// const next = i + 1 < entity.length ? entity.charAt(i + 1).toUpperCase() : '';
// const pair = current + next;
// if (pair === 'ã' || pair === 'á' || pair === 'à ' || pair === 'â') {
// result += 'a';
// i++; // Skip next character
// } else if (pair === 'é' || pair === 'ê') {
// result += 'e';
// i++; // Skip next character
// } else if (pair === 'í') {
// result += 'i';
// i++; // Skip next character
// } else if (pair === 'ó' || pair === 'ô' || pair === 'õ') {
// result += 'o';
// i++; // Skip next character
// } else if (pair === 'ú' || pair === 'ü') {
// result += 'u';
// i++; // Skip next character
// } else if (pair === 'ç') {
// result += 'c';
// i++; // Skip next character
// } else if (current !== ' ') { // Check if the current character is not a space
// result += current;
// }
// // If it is a space, do nothing (effectively removing it)
// }
// entity = result.toLowerCase(); // Lowercase result
// logToConsole("Processed cityName: " + entity); // Log after replacement
// return entity;
// }
}
logToConsole(data);
return entityStringified;
___SERVER_PERMISSIONS___
[
{
"instance": {
"key": {
"publicId": "logging",
"versionId": "1"
},
"param": [
{
"key": "environments",
"value": {
"type": 1,
"string": "debug"
}
}
]
},
"clientAnnotations": {
"isEditedByUser": true
},
"isRequired": true
}
]
___TESTS___
scenarios:
- name: Converter String para numero - retornar numero
code: |
const mockData = {
"valor": "122fdfs",
"opcao": "number"
};
let variableResult = runCode(mockData);
assertThat(variableResult).isEqualTo(122);
- name: Converter String para número - retornar vazio
code: |
const mockData = {
"valor": "fds",
"opcao": "number"
};
let variableResult = runCode(mockData);
assertThat(variableResult).isFalsy('');
- name: Converter Número para String
code: |-
const mockData = {
"valor": 2024,
"opcao": "string"
};
let variableResult = runCode(mockData);
assertThat(variableResult).isEqualTo('2024');
- name: Capturar sobrenome
code: |-
const mockData = {
"valor": "Matheus Filype",
"opcao": "last_name"
};
let variableResult = runCode(mockData);
assertThat(variableResult).isEqualTo('Filype');
- name: Formatar Telefone
code: |-
const mockData = {
"valor": "+55 3198570-2232",
"opcao": "number"
};
let variableResult = runCode(mockData);
assertThat(variableResult).isEqualTo(5531985702232);
- name: Formatar para float
code: |-
const mockData = {
"valor": 1850,
"opcao": "float"
};
let variableResult = runCode(mockData);
assertThat(variableResult).isEqualTo(18.5);
- name: Remover acentos e espaço
code: |-
const mockData = {
"valor": 'são josé dos campos',
"opcao": "city"
};
let variableResult = runCode(mockData);
assertThat(variableResult).isEqualTo('saojosedoscampos');
___NOTES___
Created on 24/06/2024, 19:11:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment