koala-utils v1.0.34
Koala Utils
Install
npm i koala-utils
Usage
console.log(arraySample);// 1,2
### map
```bash
const result = koala([{name: 'test1'}, {name: 'test2'}])
.array<any>()
.map(item => {
if (item.name === 'test2') {
item.name = 'Hello World';
}
return item;
}).getValue();
console.log(result);// [{name: 'test1'}, {name: 'Hello World'}]
filter
let result = koala([
{teste: 123},
{teste2: 543}
]).array().filter("123", "teste").getValue();
console.log(result);// [{teste: 123}]
getIndex
let index = koala([
{teste: 123},
{teste: "123"}
]).array().getIndex("teste",123);
console.log(index);// 0
split
let result = koala([1,2,3,4]).array()
.split(2)
.getValue();
console.log(result);// [[1,2],[3,4]]
toString
let result = koala([1,2,3,4]).array()
.toString(',')
.getValue();
console.log(result);// "1,2,3,4"
orderBy
let result = koala([
{date: new Date('2020-06-18')},
{date: new Date('2020-06-15')},
{date: new Date('2020-06-17')},
{date: new Date('2020-06-20')}
]).array().orderBy('date').getValue();
// [
// {date: new Date('2020-06-15')},
// {date: new Date('2020-06-17')},
// {date: new Date('2020-06-18')},
// {date: new Date('2020-06-20')}
// ]
console.log(result);
//inverse
let result = koala([
{date: new Date('2020-06-18')},
{date: new Date('2020-06-15')},
{date: new Date('2020-06-17')},
{date: new Date('2020-06-20')}
]).array().orderBy('date',true);
// [
// {date: new Date('2020-06-20')},
// {date: new Date('2020-06-18')},
// {date: new Date('2020-06-17')},
// {date: new Date('2020-06-15')}
// ]
console.log(result);
toBase64
let result = koala([
{nome: 'Teste 1'},
{nome: 'Teste 2'}
]).array()
.toBase64()
.getValue();
console.log(result);// "bm9tZQpUZXN0ZSAxClRlc3RlIDI="
pipe
let result = koala([
{proposal: '123'},
{proposal: '456'},
{proposal: '789'}
]).array<{proposal: string}>()
.pipe<number>(objProposta => {
return klArray.getValue().map((item) => parseInt(item.proposal));
})
.getValue();
console.log(result);// [123,456,789]
pipeAsync
let result = (await koala([
{proposal: '123'},
{proposal: '456'},
{proposal: '789'}
]).array<{proposal: string}>()
.pipeAsync<number>(async objProposta => {
await KlDelay.waitfor(300);
return klArray.getValue().map((item) => parseInt(item.proposal));
}))
.getValue();
console.log(result);// [123,456,789]
console.log(result);// "Ola Mundo"
let result = koala('Olá Mundo').string() .clear('-') .getValue();
console.log(result);// "Ola-Mundo"
### nbl2br
```bash
let result = koala('Olá\nMundo').string()
.nbl2br()
.getValue();
console.log(result);// "Olá<br/>Mundo"
maskCpf
let result = koala('47695329037').string()
.maskCpf()
.getValue();
console.log(result);// "476.953.290-37"
maskCnpj
let result = koala('5581451000183').string()
.maskCnpj()
.getValue();
console.log(result);// "05.581.451/0001-83"
toCamelCase
let result = koala('Olá Mundo').string()
.toCamelCase()
.getValue();
console.log(result);// "olaMundo"
split
let result = koala('1,2').string()
.split()
.getValue();
console.log(result);// ['1', '2']
unmaskCoin
let result = koala('1.000,00').string()
.unmaskCoin()
.getValue();
console.log(result);// 1000
concat
let result = koala('teste').string()
.concat('1')
.getValue();
console.log(result);// "teste1"
let result = koala('1').string()
.concat('teste', true)
.getValue();
console.log(result);// "teste1"
random
let result = koala('').string()
.random(4, true, true, true, true)
.getValue();
console.log(result);// "4Oa@"
toBase64
let result = koala('teste').string()
.toBase64()
.getValue();
console.log(result);// "dGVzdGU="
replace
let result = koala('Hellow World').string()
.replace('Hellow', 'Hello')
.getValue();
console.log(result);// 1000
</details><br>
<details>
<summary><strong>Number Utils usage</strong></summary>
### random
```bash
let result = koala(0).number()
.random(1000, 2000)
.getValue();
console.log(result);// 1389
maskCoin
let result = koala(1000).number()
.maskCoin()
.getValue();
console.log(result);// "R$ 1.000,00"
console.log(result);// '20/06/2020'
let result = koala('2020-06-20').date() .format('HH:mm:ss') .getValue();
console.log(result);// '00:00:00'
let result = koala('2020-06-20').date() .format() .getValue();
console.log(result);// '20/06/2020 00:00:00'
let result = koala('2020-06-20T13:51:00').date('+0300') .format() .getValue();
console.log(result);// '20/06/2020 07:51:00'
### add
```bash
let result = koala('2020-01-01').date()
.add({qtd: 1, type: 'days'})
.getValue();
console.log(result);// Date('2020-01-02')
let result = koala('2020-10-30').date()
.add({qtd: 1, type: 'days', ignoreDays: [
KlDateDay.saturday,
KlDateDay.sunday
])
.getValue();
console.log(result);// Date('2020-11-02')
sub
let result = koala('2020-01-02').date()
.sub({qtd: 1, type: 'days'})
.getValue();
console.log(result);// Date('2020-01-01')
let result = koala('2020-10-30').date()
.sub({qtd: 1, type: 'days', ignoreDays: [
KlDateDay.saturday,
KlDateDay.sunday
])
.getValue();
console.log(result);// Date('2020-11-23')
isHoliday
let result = koala('2020-11-02').date().isHoliday();
console.log(result);// true
console.log(result); // {teste: 1,teste2: 2}
### toString
```bash
let result = koala({
param1: "Hello",
param2: "World"
}).object().toString(['param1','param2']).getValue();
console.log(result); // "Hello World"
console.log(result); // {statusCode: number, data: Users[]}
</details><br>
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago