A simple tool library: jeasy

A year ago, it was found that there was a large use of money in working projects, but only the most basic format function was used. The volume of money directly causes the volume of the project to increase exponentially, and therefore, jeasy is born.
jeasy implements the most commonly used function of money, and GridManager Some of the tool methods are extracted.

install

npm install jeasy --save

Method introduction

Date date related

jeasy.moment() date function

jeasy.moment does not process the time zone. If you need to use the time zone, please use it directly moment Library.

// Date in number format
jeasy.moment(19880102);

// Date in string format
jeasy.moment('19880102');

// Use '/' separated string
jeasy.moment('1988/01/02 12:12:22');

// Make string separated
jeasy.moment('1988-01-02 12:12:22');

// time stamp
jeasy.moment(1547285063173);

// format
jeasy.moment(1547285063173).format("YYYY-MM-DD"); // 2019-01-12
jeasy.moment(1547285063173).format("YYYY year MM month DD day"); // January 12, 2019
jeasy.moment(1547285063173).format("YYYY-MM-DD HH:mm:ss"); // 2019-01-12 17:24:23

// Date of acquisition
jeasy.moment(1547285063173).fullYear; // 2019
jeasy.moment(1547285063173).year; // 19
jeasy.moment(1547285063173).month; // 1
jeasy.moment(1547285063173).date; // 12

// Get time, minute and second
jeasy.moment(1547285063173).hour; // 17
jeasy.moment(1547285063173).minute; // 24
jeasy.moment(1547285063173).second; // 23

// Acquisition week
jeasy.moment(1547285063173).day; // 6

Object object is JSON related

jeasy.equal(object1, object2) verifies whether two objects are the same

// Validation 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

// Validation string
jeasy.equal('baukh', 'baukh'); // true

// Verify array
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) gets the index of the Object in the Array

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) filters Array by specifying fields

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 object

Do not handle missing types (such as function) of JSON.stringify.

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

jeasy.isEmpty(obj) detects whether the object is empty

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

jeasy.type(value) gets the type of the parameter passed

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) clears fields in JSON that are [null, undefined]

// Pass parameter as object
let o = {name: 'kouzi', age: 28, like: null, title: undefined, gender: 0};
jeasy.trim(o); // {name: 'kouzi', age: 28, gender: 0}

// Pass parameter as string
let o = '  baukh  ';
jeasy.trim(o); // baukh

jeasy.toQueryString(formData) Object to queryString

// Pass parameter as regular json
jeasy.toQueryString({ name:'zhangsan', age: 12 }); // 'name=zhangsan&age=12'

// json with null value in the passed parameter
jeasy.toQueryString({ name:'zhangsan', age: '' }); // 'name=zhangsan&age='

// Pass parameter has null value and json of object
jeasy.toQueryString({ name: 'zhangsan', params: { name: 'zhangsan', age: 12 }, page: { currentPage: 1, pageSize: 10 }, other: null });
// => 'name=zhangsan&params={"name":"zhangsan","age":12}&page={"currentPage":1,"pageSize":10}&other=null'

String string correlation

jeasy.toHump(str) hyphen to hump

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

Jeasy.typin (STR); camel to hyphen

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

The form of jeasy.toFormData(str) queryString is parsed into objects

// Normal value
jeasy.toFormData('name=zhangsan&age=22&gender=male') //{name:'zhangsan', age: '22', gender:'male'}

// The first character is? Type
jeasy.toFormData('?name=zhangsan&age=22&gender=male') //{name:'zhangsan', age: '22', gender:'male'}

// Include null values
jeasy.toFormData('name=zhangsan&birth=') //{name:'zhangsan', birth:''}

// Include class pair value
jeasy.toFormData('name=zhangsan&other={gender: "male"}') //{name:'zhangsan', other: "{gender: \"male\"}"}}

Text text related

jeasy.copyText(str) place text on the pasteboard

jeasy.copyText('font-size'); // The value in pasteboard is font size

jeasy.getTextWidth(text) get the width of the text

// Note: the obtained width is related to the 'font size', 'font weight', 'font family' set on the current document.body.
document.body.style.fontSize = '12px';
document.body.style.fontFamily = 'Tahoma';
document.body.style.fontWeight = '400';

jeasy.getTextWidth('aaa'); // 18
jeasy.getTextWidth('Test width'); // 48

Number number related

Jeasy.to percentage (value, decimal, fixed)

// @param value: the value to be converted
// @param decimal: keep decimal places
// @param fixed: force to keep the number of digits specified by decimal
// @Returns {*}: returns a string in percentile format
jeasy.toPercentile(0.1230123, 1); // '12.3%'
jeasy.toPercentile(0.1230123, 2); // '12.30%'
jeasy.toPercentile(0.1230123, 2, false); // '12.3%'

Jeasy.to thousands (value, decimal, fixed)

// @param value: the value to be converted
// @param decimal: keep decimal places
// @param fixed: force to keep the number of digits specified by decimal
// @Returns {*}: returns a string in thousandths format
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 file related

jeasy.download(response, fileName);

// @param response: returns a binary stream (blob) through the back-end interface. Response allows two formats: 1.blob; 2.{data: blob};
// @param fileName: file name
jeasy.download(response, fileName); // File download

Recommend

At the end, we recommend table components GridManager

Keywords: Javascript JSON npm

Added by bobbythenewb on Mon, 09 Dec 2019 18:03:07 +0200