3.1.3 • Published 2 months ago

@koalarx/utils v3.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

Koala Utils

Install

npm i @koalarx/utils

String

maskCpf

import { maskCpf } from "@koalarx/utils/operators/string";

maskCpf("11111111111");

// Result: 111.111.111-11

maskCnpj

import { maskCnpj } from "@koalarx/utils/operators/string";

maskCnpj("11111111000111");

// Result: 11.111.111/0001-11

unmaskCpf

import { unmaskCpf } from "@koalarx/utils/operators/string";

unmaskCpf("111.111.111-11");

// Result: 11111111111

unmaskCnpj

import { unmaskCnpj } from "@koalarx/utils/operators/string";

unmaskCnpj("11.111.111/0001-11");

// Result: 11111111000111

validateCpf

import { validateCpf } from "@koalarx/utils/operators/string";

validateCpf("111.111.111-11");

// Result: false

validateCnpj

import { validateCnpj } from "@koalarx/utils/operators/string";

validateCnpj("11.111.111/0001-11");

// Result: false

randomString

import { randomString } from "@koalarx/utils/operators/string";

randomString(4, true, true, true, true);

// Result: 1Aa$

clear

import { clear } from "@koalarx/utils/operators/string";

clear("Olá Mundo");

// Result: Ola Mundo

unmaskCoin

import { unmaskCoin } from "@koalarx/utils/operators/string";

unmaskCoin("R$ 1.000,00");

// Result: 1000

toCamelCase

import { toCamelCase } from "@koalarx/utils/operators/string";

toCamelCase("Olá Mundo");

// Result: olaMundo

nbl2br

import { nbl2br } from "@koalarx/utils/operators/string";

nbl2br("Line1\nLine2");

// Result: Line1<br/>Line2

toRegex

import { toRegex } from "@koalarx/utils/operators/string";

toRegex("000.000.000-00");

// Result: /\d{3}.\d{3}.\d{3}-\d{2}/

Number

maskCoin

import { maskCoin } from "@koalarx/utils/operators/number";

maskCoin(1000);

// Result: R$ 1.000,00

random

import { klNumber } from "@koalarx/utils/operators/number";

klNumber(0).random(1000, 2000).getValue();

// Result: 3000

Date

format

import { format } from "@koalarx/utils/operators/date";

format("2020-01-01", "DD/MM/YYYY");

// Result: 01/01/2020

isHoliday

import { isHoliday } from "@koalarx/utils/operators/date";

isHoliday("2020-01-01");

// Result: true

add

import { format } from "@koalarx/utils/operators/date";

add("2020-01-01", 2);

// Result: 2020-01-03

sub

import { format } from "@koalarx/utils/operators/date";

sub("2020-01-03", 2);

// Result: 2020-01-01

diff

import { format } from "@koalarx/utils/operators/date";

diff("2020-01-01", "2020-01-03");

// Result: 2

Delay

import { delay } from "@koalarx/utils/operators/delay";

async () => {
  //some code...

  await delay(2000);

  //some code...
};

Array

map

import { klArray } from "@koalarx/utils/operators/array";

klArray([{ name: "test1" }, { name: "test2" }])
  .map((item) => {
    if (item.name === "test2") {
      item.name = "Hello World";
    }
    return item;
  })
  .getValue();

// Result: [{ name: 'test1' }, { name: 'Hello World' }]

mapAsync

import { klArray } from "@koalarx/utils/operators/array";

async () => {
  // some code...
  (
    await klArray([
      { proposal: "123" },
      { proposal: "456" },
      { proposal: "789" },
    ]).mapAsync<number>(async (item) => {
      await klDelay(300);
      return parseInt(item.proposal, 10);
    })
  ).getValue();
  // some code...
};

// Result: [123, 456, 789]

merge

import { klArray } from "@koalarx/utils/operators/array";

klArray([1]).merge([2]).getValue();

// Result: [1, 2]

filter

import { klArray } from "@koalarx/utils/operators/array";

klArray([{ teste: 123 }, { teste2: 543 }])
  .filter("123", "teste")
  .getValue();

// Result: [{ teste: 123 }]

getIndex

import { klArray } from "@koalarx/utils/operators/array";

klArray([{ teste: 123 }, { teste: 123 }]).getIndex("teste", 123);

// Result: 0

split

import { klArray } from "@koalarx/utils/operators/array";

klArray([1, 2, 3, 4]).split(2).getValue();

// Result: [ [1, 2], [3, 4] ]

toString

import { klArray } from "@koalarx/utils/operators/array";

klArray([1, 2, 3, 4]).toString(",").getValue();

// Result: '1,2,3,4'

orderBy asc

import { klArray } from "@koalarx/utils/operators/array";

klArray([
  { date: new Date("2020-06-18") },
  { date: new Date("2020-06-15") },
  { date: new Date("2020-06-17") },
  { date: new Date("2020-06-20") },
])
  .orderBy("date")
  .getValue();

// Result: [
//   { date: new Date('2020-06-15') },
//   { date: new Date('2020-06-17') },
//   { date: new Date('2020-06-18') },
//   { date: new Date('2020-06-20') },
// ]

orderBy desc

import { klArray } from "@koalarx/utils/operators/array";

klArray([
  { date: new Date("2020-06-18") },
  { date: new Date("2020-06-15") },
  { date: new Date("2020-06-17") },
  { date: new Date("2020-06-20") },
])
  .orderBy("date", true)
  .getValue();

// Result: [
//   { date: new Date('2020-06-20') },
//   { date: new Date('2020-06-18') },
//   { date: new Date('2020-06-17') },
//   { date: new Date('2020-06-15') },
// ]

toBase64

import { klArray } from "@koalarx/utils/operators/array";

(
  await klArray([{ nome: "Teste 1" }, { nome: "Teste 2" }]).toBase64()
).getValue();

// Result: 'bm9tZQpUZXN0ZSAxClRlc3RlIDI='

pipe

import { klArray } from '@koalarx/utils/operators/array';

[{ proposal: '123' }, { proposal: '456' }, { proposal: '789' }])
  .pipe((klArray) => {
    return klArray.getValue().map((item) => parseInt(item.proposal, 10))
  })
  .getValue()

// Result: [123, 456, 789]

pipeAsync

import { klArray } from "@koalarx/utils/operators/array";

(
  await klArray([
    { proposal: "123" },
    { proposal: "456" },
    { proposal: "789" },
  ]).pipeAsync(async (klArray) => {
    await klDelay(300);
    return klArray.getValue().map((item) => parseInt(item.proposal, 10));
  })
).getValue();

// Result: [123, 456, 789]

shuffleArray

import { klArray } from "@koalarx/utils/operators/array";

klArray([{ id: 1 }, { id: 2 }, { id: 3 }])
  .shuffle()
  .getValue();

// Result: [{ id: 2 }, { id: 1 }, { id: 3 }]

Object

merge

import { klObject } from "@koalarx/utils/operators/object";

klObject({ teste: 1 }).merge<any>({ teste2: 2 }).getValue();

// Result: { teste: 1, teste2: 2 }

toString

import { klObject } from "@koalarx/utils/operators/object";

klObject({
  param1: "Hello",
  param2: "World",
})
  .toString(["param1", "param2"])
  .getValue();

// Result: 'Hello World'

clone

import { clone } from "@koalarx/utils/operators/object";

clone({ id: 1 });

Cron

import { klCron } from "@koalarx/utils/operators/cron";

const cron = klCron();

cron.start();
await klDelay(2000);
cron.end();

cron.duration(); // Result: 2
3.1.3

2 months ago

3.0.12

8 months ago

3.0.4

9 months ago

3.0.13

8 months ago

3.0.3

9 months ago

3.0.10

8 months ago

3.0.11

8 months ago

3.0.8

8 months ago

3.0.7

8 months ago

3.0.14

8 months ago

3.0.6

8 months ago

3.0.5

8 months ago

3.0.9

8 months ago

3.1.2

6 months ago

3.1.1

7 months ago

3.1.0

7 months ago

2.0.2

1 year ago

2.0.6

1 year ago

2.0.8

1 year ago

2.0.1

1 year ago

1.3.0

1 year ago

1.1.0

1 year ago

1.0.102

1 year ago

1.0.101

2 years ago

1.0.100

2 years ago

1.0.99

2 years ago

1.0.95

2 years ago

1.0.94

2 years ago

1.0.97

2 years ago

1.0.96

2 years ago

1.0.91

2 years ago

1.0.88

2 years ago

1.0.84

2 years ago

1.0.87

2 years ago

1.0.86

2 years ago

1.0.85

2 years ago

1.0.62

3 years ago

1.0.83

3 years ago

1.0.61

3 years ago

1.0.60

3 years ago

1.0.64

3 years ago

1.0.63

3 years ago

1.0.73

3 years ago

1.0.72

3 years ago

1.0.76

3 years ago

1.0.75

3 years ago

1.0.74

3 years ago

1.0.59

3 years ago

1.0.78

3 years ago

1.0.49

3 years ago

1.0.51

3 years ago

1.0.50

3 years ago

1.0.55

3 years ago

1.0.53

3 years ago

1.0.52

3 years ago

1.0.48

3 years ago

1.0.47

3 years ago

1.0.40

3 years ago

1.0.44

3 years ago

1.0.43

3 years ago

1.0.41

3 years ago

1.0.45

3 years ago

1.0.39

3 years ago

1.0.38

3 years ago

1.0.37

3 years ago