🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

js-util-libs

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-util-libs - npm Package Compare versions

Comparing version
1.2.0
to
1.2.1
+7
src/formatSize/__tests__/index.spec.js
// 文件大小格式化
describe("文件大小格式化", () => {
it("测试formatSize方法", () => {
const { formatSize } = require("../index");
expect(formatSize(152225454152)).toEqual('141.77GB');
});
});
// 文件大小格式化
exports.formatSize = (size) => {
const unitsHash = 'B,KB,MB,GB'.split(',')
let index = 0
while (size > 1024 && index < unitsHash.length) {
size /= 1024
index++
}
return Math.round(size * 100) / 100 + unitsHash[index]
}
// 将阿拉伯数字翻译成中文的大写数字
describe("将阿拉伯数字翻译成中文的大写数字", () => {
it("测试changeToChinese方法", () => {
const { changeToChinese } = require("../index");
expect(changeToChinese(1524124545222)).toEqual('一萬五仟二百四十一億二仟四百五十四萬五仟二百二十二');
});
});
// 将阿拉伯数字翻译成中文的大写数字
exports.changeToChinese = (num) =>{
let AA = new Array("零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十");
let BB = new Array("", "十", "百", "仟", "萬", "億", "点", "");
let a = ("" + num).replace(/(^0*)/g, "").split("."),
k = 0,
re = "";
for (let i = a[0].length - 1; i >= 0; i--) {
switch (k) {
case 0:
re = BB[7] + re;
break;
case 4:
if (!new RegExp("0{4}//d{" + (a[0].length - i - 1) + "}$").test(a[0])) re = BB[4] + re;
break;
case 8:
re = BB[5] + re;
BB[7] = BB[5];
k = 0;
break;
}
if (k % 4 == 2 && a[0].charAt(i + 2) != 0 && a[0].charAt(i + 1) == 0) re = AA[0] + re;
if (a[0].charAt(i) != 0) re = AA[a[0].charAt(i)] + BB[k % 4] + re;
k++;
}
if (a.length > 1) {
re += BB[6];
for (let i = 0; i < a[1].length; i++) re += AA[a[1].charAt(i)];
}
if (re == '一十') re = "十";
if (re.match(/^一/) && re.length == 3) re = re.replace("一", "");
return re;
}
// 数字千分位分割
describe("数字千分位分割", () => {
it("测试thousandth方法", () => {
const { thousandth } = require("../index");
expect(thousandth(10000000)).toEqual('10,000,000');
});
});
// 数字千分位分割
exports.thousandth = (num) => {
return num.toString().indexOf(".") !== -1
? num.toLocaleString()
: num.toString().replace(/(\d)(?=(?:\d{3})+$)/g, "$1,");
}
/**
* @param { string } str 待处理字符串
* @param { number } type 去除空格类型 1-所有空格 2-前后空格 3-前空格 4-后空格 默认为1
*/
describe("去除空格", () => {
it("测试trim方法", () => {
const { trim } = require("../index");
const str = " asasa asasa asasa "
expect(trim(str)).toEqual('asasaasasaasasa');
expect(trim(str,2)).toEqual('asasa asasa asasa');
expect(trim(str,3)).toEqual('asasa asasa asasa ');
expect(trim(str,4)).toEqual(' asasa asasa asasa');
});
});
/**
* @param { string } str 待处理字符串
* @param { number } type 去除空格类型 1-所有空格 2-前后空格 3-前空格 4-后空格 默认为1
*/
exports.trim = (str, type = 1) => {
if (type && type !== 1 && type !== 2 && type !== 3 && type !== 4) return;
switch (type) {
case 1:
return str.replace(/\s/g, "");
case 2:
return str.replace(/(^\s)|(\s*$)/g, "");
case 3:
return str.replace(/(^\s)/g, "");
case 4:
return str.replace(/(\s$)/g, "");
default:
return str;
}
}
+18
-2

@@ -73,3 +73,3 @@ // 防抖节流

// 邮箱 手机号 URL 微信号 qq号 车牌号 密码强度校验 是否含中文 邮编号 16进制颜色 身份证号 ipv4 数字 对象 空对象 数组
// 邮箱 手机号 URL 微信号 qq号 车牌号 密码强度校验 是否含中文 邮编号 16进制颜色 身份证号 ipv4 数字 对象 空对象 数组 基本数据类型
export {

@@ -122,2 +122,18 @@ isEmail,

export { utf8Decode ,utf8Encode }
from "./base64/index.js";
from "./base64/index.js";
//去除空格
export { trim }
from "./trim/index.js";
// 数字千分位分割
export { thousandth }
from "./thousandth/index.js";
// 将数字转换为大写金额
export { changeToChinese }
from "./changeToChinese/index.js";
// 文件大小格式化
export { formatSize }
from "./formatSize/index.js";
+7
-7
{
"name": "js-util-libs",
"description": "js常见的函数工具库",
"version": "1.2.0",
"version": "1.2.1",
"author": "fuzhaoyang <932647051@qq.com>",

@@ -68,9 +68,9 @@ "license": "MIT",

"isArray",
"isType"
"isType",
"trim",
"thousandth",
"changeToChinese",
"formatSize"
],
"homepage": "https://github.com/fuzhaoyang/js-util-libs.git",
"repository": {
"type": "git",
"url": "https://github.com/fuzhaoyang/js-util-libs.git"
}
"homepage": "https://github.com/fuzhaoyang/js-util-libs.git"
}

@@ -44,3 +44,3 @@ # js-util-libs(函数库)

5:快速排序
6:选择排序

@@ -56,6 +56,14 @@

16、获取HTML中的纯文本信息
16、获取HTML中的纯文本信息
17、Base64
17、去除空格
18、数字千分位分割(10,000,000)
19、将阿拉伯数字翻译成中文的大写数字(五仟二百二十二)
20、文件大小格式化 (B,KB,MB,GB)
21、Base64
1.加密

@@ -65,3 +73,3 @@

18、UTF-8
22、UTF-8

@@ -72,3 +80,3 @@ 1.加密

19、cookie
23、cookie

@@ -82,3 +90,3 @@ 1.设置

20、常用校验
24、常用校验

@@ -85,0 +93,0 @@ 1.邮箱校验