@4us-dev/utils 中文文档教程

发布于 3年前 浏览 37 项目主页 更新于 3年前

4us-dev utils

质量门状态覆盖范围代码味道漏洞可维护性评级安全评级

构建和测试 npm-packageissues下载大小 license

使用 npm

npm i @4us-dev/utils

Usage

Introduction

@4us-dev/utils 是一个带有实用方法的库,可在软件开发过程中帮助开发人员。 它的开发考虑了最佳实践,并有助于对代码进行单元测试。 因此,它没有任何静态方法,从而有助于在必要时在测试期间进行模拟。

StringUtils

isBlank and isNotBlank

如果值为 null、未定义、空或只有空格的字符串,则 isBlank 返回 true。

示例:

new StringUtils().isBlank(null); // true
new StringUtils().isBlank(undefined); // true
new StringUtils().isBlank(''); // true
new StringUtils().isBlank('  '); // true
new StringUtils().isBlank('a'); // false

new StringUtils().isNotBlank(value) 等于 !new StringUtils().isBlank(value)

isEmpty and isNotEmpty

isEmpty 如果值为 null、未定义或为空,则返回 true .

示例:

new StringUtils().isEmpty(null); // true
new StringUtils().isEmpty(undefined); // true
new StringUtils().isEmpty(''); // true
new StringUtils().isEmpty('  '); // false
new StringUtils().isEmpty('a'); // false

new StringUtils().isNotEmpty(value) 等于 !new StringUtils().isEmpty(value)

hideEmail

隐藏部分电子邮件。 您可以隐藏电子邮件的开头和/或结尾。

示例:

默认配置

new StringUtils().hideEmail(`fulano@gmail.com`); // `f...@gmail.com`

如何使用电子邮件的结尾

new StringUtils().hideEmail(`fulano@gmail.com`, {
  hideStart: true, // default true
  hideEnd: true, // default false
}); // `f...@gma...`

如果通知的 email 不包含 @ 或者是 nullundefined< /代码> 该方法的返回值将为 undefined

new StringUtils().hideEmail(`fulano.com`); // undefined
new StringUtils().hideEmail(``); // undefined
new StringUtils().hideEmail(``); // undefined
new StringUtils().hideEmail(null); // undefined

removeNonNumeric

从字符串中删除所有非数字字符。

示例:

new StringUtils().removeNonNumeric('a1b2c3'); // '123'

isInteger

如果值仅包含数值,则

返回 true示例:

new StringUtils().isInteger('123'); // true
new StringUtils().isInteger('12.3'); // false
new StringUtils().isInteger('12,3'); // false
new StringUtils().isInteger('a1b2c3'); // false
new StringUtils().isInteger(' 123 '); // false

isDouble

如果字符串中的值是双

精度值,则返回 true示例:

new StringUtils().isDouble('123'); // true
new StringUtils().isDouble('12.3'); // true
new StringUtils().isDouble('12,3'); // false
new StringUtils().isDouble('a1b2c3'); // false
new StringUtils().isDouble(' 123 '); // false

RandomUtils

nextInt

生成介于 minmax 之间的随机整数(含)。

示例:

// the return a integer between 0 and 100 inclusive
const value = new RandomUtils().getRandomInteger();

// the return can be one of this values 0, 1, 2 or 3
const value = new RandomUtils().getRandomInteger({ min: 0, max: 3 });

nextString

生成一个随机字符串。

示例:

// returns a random string with length 16
const value = new RandomUtils().nextString();

// returns a random string with length 8
const value = new RandomUtils().nextString({ length: 8 });

// returns a random string with length 8 only with letters upper, letters lower and numbers
const value = new RandomUtils().nextString({
  length: 8, // default 16
  upper: true, // default true ex: ABCDEF...
  lower: true, // default true ex: abcdef...
  numbers: true, // default true ex: 012345...
  specialSimple: false, // default true ex: !#$%&*_+=-^~?;:.,|
  specialAmbiguous: false, // default true ex: "()\'`´{[]}/><
});

nextStringCustom

生成一个随机字符串,其中包含 length 信息,并使用 caracteresAllowed 变量作为参考。

示例:

// returns a random string with length 10 using only `A`, `1` or `c` to generate the result
const value = new RandomUtils().nextStringCustom(10, 'A1c');

FormatUtils

formatCPF

删除所有非数字值并应用 CPF 掩码。

示例:

const value = new FormatUtils().formatCPF('66273306010'); // "662.733.060-10"

这应该像这样部分格式化 CPF

const value = new FormatUtils().formatCPF('6627330'); // "662.733.0"

如果值包含非数字字符,它们将在格式化时被删除

const value = new FormatUtils().formatCPF('66A27B33C060D10'); // "662.733.060-10"

formatCNPJ

删除所有非数字值并应用 CNPJ 掩码。

示例:

const value = new FormatUtils().formatCNPJ('94338204000180'); // "94.338.204/0001-80"

这应该像这样部分格式化 CNPJ

const value = new FormatUtils().formatCNPJ('9433820'); // "94.338.20"

如果值包含非数字字符,它们将在格式化时被删除

const value = new FormatUtils().formatCNPJ('943A382B04000C180'); // "94.338.204/0001-80"

formatPhone

删除所有非数字值并应用电话掩码 (99)9999-9999 或 (99)99999-9999。

示例:

const value = new FormatUtils().formatPhone('85999432345'); // "(85)99943-2345"
const value = new FormatUtils().formatPhone('8599943234'); // "(85)9994-3234"

这应该像这样部分格式化电话

const value = new FormatUtils().formatPhone('8599943'); // "(85)9994-3"

如果值包含非数字字符,它们将在格式化时被删除

const value = new FormatUtils().formatPhone('A8B5C9D9f9g4h3i23s4w5q'); // "(85)99943-2345"

formatCEP

删除所有非数字值并应用 CEP 掩码 99999-999

示例:

const value = new FormatUtils().formatPhone('60601023'); // "60601-023"

这应该像这样部分格式化 cep

const value = new FormatUtils().formatPhone('606010'); // "60601-0"

如果value contains non-numeric characters they will be removed in formatting

const value = new FormatUtils().formatPhone('A60b601c02D3E'); // "60601-023"

formatOnlyIntegers

从字符串中删除所有非数字字符。

示例:

const value = new FormatUtils().formatOnlyIntegers('a1b2c3'); // '123'060-10"

BrazilianUtils

isCPF

当 CPF 有效时

返回真示例:

const brazilianUtils = new BrazilianUtils();
if (brazilianUtils.isCPF('66273306010')) {
  console.log('CPF is valid');
} else {
  console.log('CPF is not valid');
}

if (brazilianUtils.isCPF('662.733.060-10')) {
  console.log('CPF is valid');
} else {
  console.log('CPF is not valid');
}

isCNPJ

当 CNPJ 有效时返回真

示例:

const brazilianUtils = new BrazilianUtils();
if (brazilianUtils.isCNPJ('94338204000180')) {
  console.log('CNPJ is valid');
} else {
  console.log('CNPJ is not valid');
}

if (brazilianUtils.isCNPJ('94.338.204/0001-80')) {
  console.log('CNPJ is valid');
} else {
  console.log('CNPJ is not valid');
}

generateCPF

返回有效的 CPF

示例:

const brazilianUtils = new BrazilianUtils();
const cpf = brazilianUtils.generateCPF();

generateCNPJ

返回有效的 CNPJ

示例:

const brazilianUtils = new BrazilianUtils();
const cpf = brazilianUtils.generateCNPJ();

4us-dev utils

Quality Gate StatusCoverageCode SmellsVulnerabilitiesMaintainability RatingSecurity Rating

Build and Tests npm-packageissuesdownloadssize license

Install with npm

npm i @4us-dev/utils

Usage

Introduction

@4us-dev/utils is a lib with utility methods to help developers during software development. It is developed with best practices in mind and to facilitate unit testing of your code. So, it does not have any static method, thus facilitating the practice of mocks during tests when necessary.

StringUtils

isBlank and isNotBlank

isBlank return true if value is null, undefined, empty or string with only white spaces.

Examples:

new StringUtils().isBlank(null); // true
new StringUtils().isBlank(undefined); // true
new StringUtils().isBlank(''); // true
new StringUtils().isBlank('  '); // true
new StringUtils().isBlank('a'); // false

new StringUtils().isNotBlank(value) is equal to !new StringUtils().isBlank(value)

isEmpty and isNotEmpty

isEmpty return true if value is null, undefined, or empty.

Examples:

new StringUtils().isEmpty(null); // true
new StringUtils().isEmpty(undefined); // true
new StringUtils().isEmpty(''); // true
new StringUtils().isEmpty('  '); // false
new StringUtils().isEmpty('a'); // false

new StringUtils().isNotEmpty(value) is equal to !new StringUtils().isEmpty(value)

hideEmail

Hides part of the email. You could hide the start and/or the end of email.

Examples:

Default configuration

new StringUtils().hideEmail(`fulano@gmail.com`); // `f...@gmail.com`

How ofuscate the ends of the email too

new StringUtils().hideEmail(`fulano@gmail.com`, {
  hideStart: true, // default true
  hideEnd: true, // default false
}); // `f...@gma...`

If the email informed does not contains @ or is null or undefined the return of the method will be undefined

new StringUtils().hideEmail(`fulano.com`); // undefined
new StringUtils().hideEmail(``); // undefined
new StringUtils().hideEmail(``); // undefined
new StringUtils().hideEmail(null); // undefined

removeNonNumeric

Removes all non-numeric caracteres from string.

Examples:

new StringUtils().removeNonNumeric('a1b2c3'); // '123'

isInteger

Returns true if the value contains only numeric values

Examples:

new StringUtils().isInteger('123'); // true
new StringUtils().isInteger('12.3'); // false
new StringUtils().isInteger('12,3'); // false
new StringUtils().isInteger('a1b2c3'); // false
new StringUtils().isInteger(' 123 '); // false

isDouble

Returns true if the value into string is a double

Examples:

new StringUtils().isDouble('123'); // true
new StringUtils().isDouble('12.3'); // true
new StringUtils().isDouble('12,3'); // false
new StringUtils().isDouble('a1b2c3'); // false
new StringUtils().isDouble(' 123 '); // false

RandomUtils

nextInt

Generates a random integer between the min and max inclusive.

Examples:

// the return a integer between 0 and 100 inclusive
const value = new RandomUtils().getRandomInteger();

// the return can be one of this values 0, 1, 2 or 3
const value = new RandomUtils().getRandomInteger({ min: 0, max: 3 });

nextString

Generates a random string.

Examples:

// returns a random string with length 16
const value = new RandomUtils().nextString();

// returns a random string with length 8
const value = new RandomUtils().nextString({ length: 8 });

// returns a random string with length 8 only with letters upper, letters lower and numbers
const value = new RandomUtils().nextString({
  length: 8, // default 16
  upper: true, // default true ex: ABCDEF...
  lower: true, // default true ex: abcdef...
  numbers: true, // default true ex: 012345...
  specialSimple: false, // default true ex: !#$%&*_+=-^~?;:.,|
  specialAmbiguous: false, // default true ex: "()\'`´{[]}/><
});

nextStringCustom

Generates a random string with length informed and using caracteresAllowed variable as reference.

Examples:

// returns a random string with length 10 using only `A`, `1` or `c` to generate the result
const value = new RandomUtils().nextStringCustom(10, 'A1c');

FormatUtils

formatCPF

Removes all non-numeric values and apply the CPF mask.

Examples:

const value = new FormatUtils().formatCPF('66273306010'); // "662.733.060-10"

This should format the CPF partially like this

const value = new FormatUtils().formatCPF('6627330'); // "662.733.0"

If the value contains non-numeric characters they will be removed in formatting

const value = new FormatUtils().formatCPF('66A27B33C060D10'); // "662.733.060-10"

formatCNPJ

Removes all non-numeric values and apply the CNPJ mask.

Examples:

const value = new FormatUtils().formatCNPJ('94338204000180'); // "94.338.204/0001-80"

This should format the CNPJ partially like this

const value = new FormatUtils().formatCNPJ('9433820'); // "94.338.20"

If the value contains non-numeric characters they will be removed in formatting

const value = new FormatUtils().formatCNPJ('943A382B04000C180'); // "94.338.204/0001-80"

formatPhone

Removes all non-numeric values and apply the phone mask (99)9999-9999 or (99)99999-9999.

Examples:

const value = new FormatUtils().formatPhone('85999432345'); // "(85)99943-2345"
const value = new FormatUtils().formatPhone('8599943234'); // "(85)9994-3234"

This should format the phone partially like this

const value = new FormatUtils().formatPhone('8599943'); // "(85)9994-3"

If the value contains non-numeric characters they will be removed in formatting

const value = new FormatUtils().formatPhone('A8B5C9D9f9g4h3i23s4w5q'); // "(85)99943-2345"

formatCEP

Removes all non-numeric values and apply the CEP mask 99999-999

Examples:

const value = new FormatUtils().formatPhone('60601023'); // "60601-023"

This should format the cep partially like this

const value = new FormatUtils().formatPhone('606010'); // "60601-0"

If the value contains non-numeric characters they will be removed in formatting

const value = new FormatUtils().formatPhone('A60b601c02D3E'); // "60601-023"

formatOnlyIntegers

Removes all non-numeric caracteres from string.

Examples:

const value = new FormatUtils().formatOnlyIntegers('a1b2c3'); // '123'060-10"

BrazilianUtils

isCPF

Return true when CPF is valid

Examples:

const brazilianUtils = new BrazilianUtils();
if (brazilianUtils.isCPF('66273306010')) {
  console.log('CPF is valid');
} else {
  console.log('CPF is not valid');
}

or

if (brazilianUtils.isCPF('662.733.060-10')) {
  console.log('CPF is valid');
} else {
  console.log('CPF is not valid');
}

isCNPJ

Return true when CNPJ is valid

Examples:

const brazilianUtils = new BrazilianUtils();
if (brazilianUtils.isCNPJ('94338204000180')) {
  console.log('CNPJ is valid');
} else {
  console.log('CNPJ is not valid');
}

or

if (brazilianUtils.isCNPJ('94.338.204/0001-80')) {
  console.log('CNPJ is valid');
} else {
  console.log('CNPJ is not valid');
}

generateCPF

Return a valid CPF

Examples:

const brazilianUtils = new BrazilianUtils();
const cpf = brazilianUtils.generateCPF();

generateCNPJ

Return a valid CNPJ

Examples:

const brazilianUtils = new BrazilianUtils();
const cpf = brazilianUtils.generateCNPJ();
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文