2.3.0 • Published 1 month ago

trumangao-utils v2.3.0

Weekly downloads
3
License
MIT
Repository
github
Last release
1 month ago

trumangao-utils

常用JavaScript工具函数

A series of commonly used JavaScript utility functions. Here for the API.

Install

npm install trumangao-utils

Usage

import { url2obj } from "trumangao-utils";

const url =
  "https://www.npmjs.com/package/trumangao-utils?name=trumangao-utils&ltypescript=true";
const obj = url2obj(url);

console.log(obj); // { name: "trumangao-utils", typescript: "true" }

API

CryptoManager

管理字符的加密/解密,基于 crypto-js

Manage the encryption/decryption of characters, based on crypto-js.

// encryption
import { CryptoManager } from "trumangao-utils";

const cryptoManager = new CryptoManager({
  key: "test_key",
  iv: "test_iv",
  suffix: "test_suffix", // optional
});

const encrypted = cryptoManager.encryptAes("this is a test string");
// decryption
import { CryptoManager } from "trumangao-utils";

const cryptoManager = new CryptoManager({
  key: "test_key",
  iv: "test_iv",
  suffix: "test_suffix", // optional
});

const decrypted = cryptoManager.decryptAes(encrypted);

console.log(decrypted); // "this is a test string"

getStorage

根据键名读取本地存储

Read local storage based on the key.

function getStorage<K extends string>(
  storageType: "sessionStorage" | "localStorage",
  storageKey: K,
): any;

setStorage

根据键名写入本地存储

Write local storage based on the key.

function setStorage<K extends string>(
  storageType: "sessionStorage" | "localStorage",
  storageKey: K,
  storageData: any,
): void;

validateValue

常见数据格式校验

Common data format validation for phone, email, and numEnCn (numbers and Chinese characters).

function validateValue(option: {
  type: "phone" | "email" | "numEnCn";
  value: any;
  required?: boolean;
}): boolean;

checkDataType

精准判断数据类型,返回类型字符串

Precisely determine the data type and return the type string.

function checkDataType(
  data: unknown,
):
  | "String"
  | "Number"
  | "Boolean"
  | "Undefined"
  | "Null"
  | "Symbol"
  | "Function"
  | "Array"
  | "Object"
  | "Date"
  | "RegExp"
  | "Error"
  | "Map"
  | "Set";

downloadBlob

下载blob

Download a blob.

function downloadBlob(blob: Blob, fileName?: string): void;

prefetchAssets

资源预加载

Resource preloading.

function prefetchAssets(assets: { src: string; type: "img" | "audio" }[]): void;

getCharLength

计算字符串长度

Calculate the length of a string.

function getCharLength(str: string): number;

url2obj

获取URL的参数

Convert a URL to an object with query parameters.

function url2obj(url?: string): { [key: string]: string };

filterEmptyValue

过滤对象中的空值,包括空字符串、null、undefined

Filter out empty values in an object, including empty strings, null, and undefined.

function filterEmptyValue<T extends { [key: string]: any }>(obj: T): Partial<T>;

date2string

Date类型数据 转 时间标准格式字符串

Convert a Date object to a standard format string.

function date2string(option?: {
  date?: Date;
  hasTime?: boolean;
  dateSeparator?: string;
  timeSeparator?: string;
}): string;

caniuse_webp

检测 webp 支持性,惰性函数

Detect webp support, lazy function

function caniuse_webp(): boolean;
2.3.0

1 month ago

2.2.11

2 months ago

2.2.7

7 months ago

2.2.6

7 months ago

2.2.10

7 months ago

2.2.9

7 months ago

2.2.8

7 months ago

2.2.1

8 months ago

2.0.3

9 months ago

2.2.0

8 months ago

2.1.1

9 months ago

2.0.2

9 months ago

2.2.3

8 months ago

2.0.5

9 months ago

2.2.2

8 months ago

2.0.4

9 months ago

2.2.5

8 months ago

2.0.11

9 months ago

2.0.7

9 months ago

2.2.4

8 months ago

2.0.6

9 months ago

2.0.9

9 months ago

2.0.10

9 months ago

2.0.8

9 months ago

2.1.0

9 months ago

2.0.1

9 months ago

2.0.0

9 months ago

1.0.0

3 years ago