@44north/utilities 中文文档教程
@44north/utilities
包含许多小实用程序的模块可在您的应用程序中使用,
Install
npm install @44north/utilities
或者
yarn add @44north/utilities
capitalize
import { capitalize } from "@44north/utilities";
const value = "foo bar";
console.log(capitalize(value)); // -> Foo bar
uppercase
import { uppercase } from "@44north/utilities";
const value = "foobar";
console.log(uppercase(value)); // -> FOOBAR
lowercase
import { lowercase } from "@44north/utilities";
const value = "FOOBAR";
console.log(lowercase(value)); // -> foobar
camelCase
import { camelCase } from "@44north/utilities";
const value = "foo-bar";
console.log(camelCase(value)); // -> fooBar
pascalCase
import { pascalCase } from "@44north/utilities";
const value = "foo_bar";
console.log(pascalCase(value)); // -> FooBart
formatPrice
import { formatPrice } from "@44north/utilities";
const value = 1000;
console.log(formatPrice(value)); // -> "$1,000.00"
可以选择将某些选项传递给格式化程序,然后
import { formatPrice } from "@44north/utilities";
const value = 1000.99;
console.log(
formatPrice(value, {
minimumFractionDigits: 0,
maximumFractionDigits: 0
})
); // -> "$1,001"
doesKeyExists
import { doesKeyExists } from "@44north/utilities";
const obj = { foo: "bar" };
console.log(doesKeyExists(obj, "foo")); // -> true
slugify
import { slugify } from "@44north/utilities";
const value = "Foo Bar Baz";
console.log(slugify(value)); // -> foo-bar-baz
isValidEmailAddress
import { isValidEmailAddress } from "@44north/utilities";
const value = "Foo Bar Baz";
console.log(isValidEmailAddress(value)); // -> false
console.log(isValidEmailAddress("test@example.com")); // -> true
asBool
import { asBool } from "@44north/utilities";
const value = "True";
console.log(asBool(value)); // -> true: boolean
sortObjectArrayByKey
import { sortObjectArrayByKey } from "@44north/utilities";
const startingData = [
{
label: "1/2",
value: "50"
},
{
label: "1",
value: "100"
},
{
label: "1/4",
value: "25"
}
];
// SORT BY LABEL KEY ASC
const sortedDataDesc = sortObjectArrayByKey(startingData, "label");
console.log(sortedDataDesc.map((o) => o.value).join(",")); // -> 100,50,25
// SORT BY LABEL KEY DESC
const sortedDataDesc = sortObjectArrayByKey(startingData, "label", "desc");
console.log(sortedDataDesc.map((o) => o.value).join(",")); // -> 25,50,100
// SORT BY VALUE WITH A FUNCTION
const sortedDataCustom = sortObjectArrayByKey(startingData, "value", (a, b) => {
if (Number(a) < Number(b)) {
return -1;
}
if (Number(a) > Number(b)) {
return 1;
}
return 0;
});
console.log(sortedDataCustom.map((o) => o.value).join(",")); // -> 25,50,100
startsWith
如果提供的值包含提供的条件字符串,则
import { startsWith } from "@44north/utilities";
const value = "foobar";
if (startsWith(value, "foo")) {
// it's true!
}
返回一个布尔值。 您可以选择将 RegExp
标志作为第三个参数传递。 不区分大小写是我的默认设置。
endsWith
如果提供的值包含提供的条件字符串,则返回一个布尔值。
import { endsWith } from "@44north/utilities";
const value = "foobar";
if (endsWith(value, "bar")) {
// it's true!
}
您可以选择将 RegExp
标志作为第三个参数传递。 不区分大小写是我的默认设置。
contains
如果提供的值包含提供的条件字符串,则返回一个布尔值。
import { contains } from "@44north/utilities";
const value = "foobar";
if (contains(value, "oba")) {
// it's true!
}
您可以选择将 RegExp
标志作为第三个参数传递。 不区分大小写是我的默认设置。
validatePageSlug
如果提供的 URI 的所有段都是 slug,则返回。
import { validatePageSlug } from "@44north/utilities";
console.log(validatePageSlug("/foo/bar/baz")); // -> true
console.log(validatePageSlug("/foo//bar/baz")); // -> false
console.log(validatePageSlug("/foo/bar baz")); // -> false
@44north/utilities
A Module containing a number of small utilities to use in your applications
Install
npm install @44north/utilities
or
yarn add @44north/utilities
capitalize
import { capitalize } from "@44north/utilities";
const value = "foo bar";
console.log(capitalize(value)); // -> Foo bar
uppercase
import { uppercase } from "@44north/utilities";
const value = "foobar";
console.log(uppercase(value)); // -> FOOBAR
lowercase
import { lowercase } from "@44north/utilities";
const value = "FOOBAR";
console.log(lowercase(value)); // -> foobar
camelCase
import { camelCase } from "@44north/utilities";
const value = "foo-bar";
console.log(camelCase(value)); // -> fooBar
pascalCase
import { pascalCase } from "@44north/utilities";
const value = "foo_bar";
console.log(pascalCase(value)); // -> FooBart
formatPrice
import { formatPrice } from "@44north/utilities";
const value = 1000;
console.log(formatPrice(value)); // -> "$1,000.00"
optionally Pass in some options to the formatter
import { formatPrice } from "@44north/utilities";
const value = 1000.99;
console.log(
formatPrice(value, {
minimumFractionDigits: 0,
maximumFractionDigits: 0
})
); // -> "$1,001"
doesKeyExists
import { doesKeyExists } from "@44north/utilities";
const obj = { foo: "bar" };
console.log(doesKeyExists(obj, "foo")); // -> true
slugify
import { slugify } from "@44north/utilities";
const value = "Foo Bar Baz";
console.log(slugify(value)); // -> foo-bar-baz
isValidEmailAddress
import { isValidEmailAddress } from "@44north/utilities";
const value = "Foo Bar Baz";
console.log(isValidEmailAddress(value)); // -> false
console.log(isValidEmailAddress("test@example.com")); // -> true
asBool
import { asBool } from "@44north/utilities";
const value = "True";
console.log(asBool(value)); // -> true: boolean
sortObjectArrayByKey
import { sortObjectArrayByKey } from "@44north/utilities";
const startingData = [
{
label: "1/2",
value: "50"
},
{
label: "1",
value: "100"
},
{
label: "1/4",
value: "25"
}
];
// SORT BY LABEL KEY ASC
const sortedDataDesc = sortObjectArrayByKey(startingData, "label");
console.log(sortedDataDesc.map((o) => o.value).join(",")); // -> 100,50,25
// SORT BY LABEL KEY DESC
const sortedDataDesc = sortObjectArrayByKey(startingData, "label", "desc");
console.log(sortedDataDesc.map((o) => o.value).join(",")); // -> 25,50,100
// SORT BY VALUE WITH A FUNCTION
const sortedDataCustom = sortObjectArrayByKey(startingData, "value", (a, b) => {
if (Number(a) < Number(b)) {
return -1;
}
if (Number(a) > Number(b)) {
return 1;
}
return 0;
});
console.log(sortedDataCustom.map((o) => o.value).join(",")); // -> 25,50,100
startsWith
returns a boolean if the provided value contains the provided condition string.
import { startsWith } from "@44north/utilities";
const value = "foobar";
if (startsWith(value, "foo")) {
// it's true!
}
optionally, you can pass RegExp
flags as a third parameter. case insenitive is on my default.
endsWith
returns a boolean if the provided value contains the provided condition string.
import { endsWith } from "@44north/utilities";
const value = "foobar";
if (endsWith(value, "bar")) {
// it's true!
}
optionally, you can pass RegExp
flags as a third parameter. case insenitive is on my default.
contains
returns a boolean if the provided value contains the provided condition string.
import { contains } from "@44north/utilities";
const value = "foobar";
if (contains(value, "oba")) {
// it's true!
}
optionally, you can pass RegExp
flags as a third parameter. case insenitive is on my default.
validatePageSlug
returns if all segments of the provided URI are a slug.
import { validatePageSlug } from "@44north/utilities";
console.log(validatePageSlug("/foo/bar/baz")); // -> true
console.log(validatePageSlug("/foo//bar/baz")); // -> false
console.log(validatePageSlug("/foo/bar baz")); // -> false