@imagineapps/utils
v0.0.3
Published
`@imagineapps-team/utils` é o pacote que contém as funções utilitárias que podem ser usadas em qualquer projeto.
Downloads
28
Readme
utils
@imagineapps-team/utils
é o pacote que contém as funções utilitárias que podem ser usadas em qualquer projeto.
Instalação
yarn add @imagineapps-team/utils
# ou
npm install @imagineapps-team/utils
String
Utilitários para manipulação de strings.
StringComposer
Classe para facilitar a manipulação de strings.
import { StringComposer } from '@imagineapps-team/utils';
const stringComposer = new StringComposer('Hello World');
stringComposer
.replace('Hello', 'Hi')
.replace('World', 'Everyone')
.append('!')
.prepend('Hey, ');
console.log(stringComposer.toString()); // Hey, Hi Everyone!
console.log(
StringComposer.create('Isso pode ser um texto muito grande')
.truncate(10)
.toString()
);
// -> Isso pode
console.log(
StringComposer.create('Arquivo de ação de usuário 2020')
.sanitize()
.truncate(30)
.toString()
);
// -> arquivo-de-acao-de-usuario-2020
StringSanitizer
Higienização e tratamento de strings.
import { StringSanitizer } from '@imagineapps-team/utils';
const stringSanitizer = new StringSanitizer();
sanitizeSpecialLetters
: Substitui todos os caracteres especiais por seu caractere "normal" (ASCII) correspondente. Também trata caracteres ISO-8859-1.
console.log(stringSanitizer.sanitizeSpecialLetters('Não é possível remover acentos'));
// -> Nao e possivel remover acentos
console.log(stringSanitizer.sanitizeSpecialLetters('ação de bem estar'));
// -> acao de bem estar
clearSpecialCharacters
: Remove todos os caracteres que não sejam letras, números ou espaços.
console.log(stringSanitizer.clearSpecialCharacters('#$ Não é possível remover acentos'));
// -> Não é possível remover acentos
StringTransformer
Transformação de strings comuns
import { StringTransformer } from '@imagineapps-team/utils';
const stringTransformer = new StringTransformer();
toKebabCase
: Transforma uma string em kebab-case.
console.log(stringTransformer.toKebabCase('Não é possível remover acentos'));
// -> nao-e-possivel-remover-acentos
truncate
: Trunca uma string para um tamanho máximo, adicionando um sufixo opcional no final.
console.log(stringTransformer.truncate('Não é possível remover acentos', 10));
// -> Não é pos
console.log(stringTransformer.truncate('Não é possível remover acentos', 10, '...'));
// -> Não é pos...
chunk
: Divide uma string em um array de strings de tamanho máximo.
console.log(stringTransformer.chunk('Não é possível remover acentos', 10));
// -> ['Não é pos', 'sível remo', 'ver acento', 's']
toSlug
: Transforma uma string em slug, podendo opcionalmente escolher o separador e tamanho máximo
console.log(stringTransformer.toSlug('Não é possível remover acentos'));
// -> nao-e-possivel-remover-acentos
console.log(stringTransformer.toSlug('Não é possível remover acentos', { separator: '_', maxLength: 10 }));
// -> nao_e_pos
StringEncoder
Codificação e decodificação de strings.
import { StringEncoder } from '@imagineapps-team/utils';
const stringEncoder = new StringEncoder();
fromISO88591
: Decodifica uma string ISO-8859-1 para UTF-8.
console.log(stringEncoder.fromISO88591('ação de bem estar'));
// -> ação de bem estar
StringChecker
Verificação de strings.
import { StringChecker } from '@imagineapps-team/utils';
const stringChecker = new StringChecker();
hasUnicode
: Verifica se uma string contém caracteres unicode.
console.log(stringChecker.hasUnicode('Não é possível remover acentos'));
// -> false
console.log(stringChecker.hasUnicode('Não é possível remover acentos 😁'));
// -> true
hasHtmlEntities
: Verifica se uma string contém entidades HTML.
console.log(stringChecker.hasHtmlEntities('Não é possível remover acentos'));
// -> false
console.log(stringChecker.hasHtmlEntities('Não é possível remover acentos '));
// -> true
Object
Utilitários para manipulação de objetos.
ObjectSet
Facilitar a manipulação de objetos únicos
import { ObjectSet } from '@imagineapps-team/utils';
const objectSet = new ObjectSet({ compareBy: 'id' });
objectSet.add({ id: 1, name: 'John' });
objectSet.add({ id: 2, name: 'Mary' });
objectSet.add({ id: 3, name: 'Peter' });
console.log(objectSet.toArray());
// -> [{ id: 1, name: 'John' }, { id: 2, name: 'Mary' }, { id: 3, name: 'Peter' }]
objectSet.add({ id: 2, name: 'Mary', other: 'other' });
console.log(objectSet.toArray());
// -> [{ id: 1, name: 'John' }, { id: 2, name: 'Mary' }, { id: 3, name: 'Peter' }] (não adiciona o objeto pois já existe um com o mesmo id)
console.log(objectSet.getDuplicates());
// -> [{ key: 2, identifier: 'id', values: [{ id: 2, name: 'Mary' }, { id: 2, name: 'Mary', other: 'other' } ] }]
objectSet.remove({ id: 2, name: 'Mary' });
console.log(objectSet.toArray());
// -> [{ id: 1, name: 'John' }, { id: 3, name: 'Peter' }]
objectSet.has({ id: 2, name: 'Mary' });
// -> false
Building
Run nx build utils
to build the library.
Running unit tests
Run nx test utils
to execute the unit tests via Jest.