1 Star 0 Fork 1

写个程序换个饼 / jeasy

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

jeasy

javascript easy tool

Build Status npm version npm downloads coverage

安装

npm install jeasy --save

方法介绍

Date 日期相关

jeasy.moment() 日期函数

jeasy.moment并不对时区进行处理,如果有需要使用时区的请直接使用moment库。

// number格式的年月日
jeasy.moment(19880102);

// string格式的年月日
jeasy.moment('19880102');

// 使用`/`相隔的字符串
jeasy.moment('1988/01/02 12:12:22');

// 使作`-`相隔的字符串
jeasy.moment('1988-01-02 12:12:22');

// 时间戳
jeasy.moment(1547285063173);

// format
jeasy.moment(1547285063173).format("YYYY-MM-DD"); // 2019-01-12
jeasy.moment(1547285063173).format("YYYY年MM月DD日"); // 2019年01月12日
jeasy.moment(1547285063173).format("YYYY-MM-DD HH:mm:ss"); // 2019-01-12 17:24:23

// 获取年月日
jeasy.moment(1547285063173).fullYear; // 2019
jeasy.moment(1547285063173).year; // 19
jeasy.moment(1547285063173).month; // 1
jeasy.moment(1547285063173).date; // 12

// 获取时分秒
jeasy.moment(1547285063173).hour; // 17
jeasy.moment(1547285063173).minute; // 24
jeasy.moment(1547285063173).second; // 23

// 获取周
jeasy.moment(1547285063173).day; // 6

Object 对象与JSON相关

jeasy.equal(object1, object2) 验证两个Object是否相同

// 验证对象
let o1 = {name: 'baukh', age: 31};
let o2 = {name: 'baukh', age: 31};
let o3 = {name: 'kouzi', age: 28};
jeasy.equal(o1, o2); // true
jeasy.equal(o1, o3); // false

// 验证字符串
jeasy.equal('baukh', 'baukh'); // true

// 验证数组
let a1 = [1, 2, 3];
let a2 = [1, 2, 3];
let a3 = [3, 2, 1];
jeasy.equal(a1, a2); // true
jeasy.equal(a1, a3); // false

jeasy.index(array, object) 获取Array中Object的索引

let o1 = {name: 'cc', age: 31};
let o2 = {name: 'kouzi', age: 31};
let o3 = {name: 'baukh', age: 31};
let arr = [{name: 'baukh', age: 31}, {name: 'cc', age: 31}];
jeasy.index(arr, o1); // 1
jeasy.index(arr, o2); // -1
jeasy.index(arr, o3); // 0

jeasy.find(array, key, value) 通过指定字段筛选Array

const arr = [{name: 'baukh', age: 31}, {name: 'cc', age: 30}, {name: 'kouz', age: 29}, {name: 'rabbit', age: 28}];
jeasy.find(arr, 'name', 'baukh'); // [{name: 'baukh', age: 31}]
jeasy.find(arr, 'age', 29); // [{name: 'kouz', age: 29}]
jeasy.find(arr, 'age', 33); // []

jeasy.clone(obj) clone 对象

对 JSON.stringify 存在丢失的类型(如function)不作处理。

let o1 = {name: 'cc', age: 31}; let o2 = jeasy.clone(o1); o1.name; // cc o1 === o2; // false

jeasy.isEmpty(obj) 检测是否为空对象

let o1 = {name: 'cc', age: 31};
let o2 = {};
jeasy.isEmpty(o1); // false
jeasy.isEmpty(o2); // true

jeasy.type(value) 获取传参的类型

jeasy.type(undefined); //undefined
jeasy.type(null); // null
jeasy.type(true); // boolean
jeasy.type(Boolean()); // boolean
jeasy.type(123); // number
jeasy.type(Number(123)); // number
jeasy.type('123'); // string
jeasy.type(String('123')); // string
jeasy.type(() => {}); // function
jeasy.type([]); // array
jeasy.type(new Array(1)); // array
jeasy.type(new Date()); // date
jeasy.type(Error()); // error
jeasy.type(/test/); // regexp
jeasy.type(document.body); // element
jeasy.type(nodeList); // nodeList
jeasy.type(divEle); // element

jeasy.trim(json) 清除JSON中为[null, undefined]的字段

// 传参为对象
let o = {name: 'kouzi', age: 28, like: null, title: undefined, gender: 0};
jeasy.trim(o); // {name: 'kouzi', age: 28, gender: 0}

// 传参为字符串
let o = '  baukh  ';
jeasy.trim(o); // baukh

String 字符串相关

jeasy.toHump(str) 连字符转驼峰

jeasy.toHump('font-size'); //fontSize

jeasy.toHyphen(str); 驼峰转连字符

jeasy.toHyphen('FontSize'); // -font-size

Text 文本相关

jeasy.copyText(str) 将文本放粘贴板

jeasy.copyText('font-size'); // 粘贴板中的值为font-size

jeasy.getTextWidth(text) 获取文本所占宽度

// 需要注意: 获取的宽度与当前document.body上所设置的`font-size`, `font-weight`, `font-family`有关。
document.body.style.fontSize = '12px';
document.body.style.fontFamily = 'Tahoma';
document.body.style.fontWeight = '400';

jeasy.getTextWidth('aaa'); // 18
jeasy.getTextWidth('测试宽度'); // 48

Number 数字相关

jeasy.toPercentile(value, decimal, fixed) 浮点数转换百分位

// @param value: 需要转换的数值
// @param decimal: 保留小数点位数
// @param fixed: 是否强制保留decimal指定的位数
// @returns {*}: 返回百分位格式的字符串
jeasy.toPercentile(0.1230123, 1); // '12.3%'
jeasy.toPercentile(0.1230123, 2); // '12.30%'
jeasy.toPercentile(0.1230123, 2, false); // '12.3%'

jeasy.toThousands(value, decimal, fixed) 浮点数转换千分位

// @param value: 需要转换的数值
// @param decimal: 保留小数点位数
// @param fixed: 是否强制保留decimal指定的位数
// @returns {*}: 返回千分位格式的字符串
jeasy.toThousands(value, 1); // '123.0‰'
jeasy.toThousands(value, 1, false); // '123‰'
jeasy.toThousands(value, 2); // '123.01‰'
jeasy.toThousands(value, 2, false); // '123.01‰'

File 文件相关

jeasy.download(response, fileName);

// @param response: 通过后端接口返回二进制流(blob)。response允许两种格式: 1.blob; 2.{data: blob};
// @param fileName: 文件名称
jeasy.download(response, fileName); // 文件下载

License

MIT License Copyright (c) 2017 拭目以待 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

javascript轻量工具包,包含开发中的一些常用公共方法。 展开 收起
JavaScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/baukh/jeasy.git
git@gitee.com:baukh/jeasy.git
baukh
jeasy
jeasy
master

搜索帮助