常用js方法封装使用
// 冒泡排序
export function bubbleSort(arr) {
let i = arr.length - 1;
while (i > 0) {
let maxIndex = 0;
for (let j = 0; j < i; j++) {
if (arr[j] > arr[j + 1]) {
let temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
maxIndex = j;
}
}
i = maxIndex;
}
return arr;
}
// 通过属性值冒泡排序
export function bubbleSortByProp(arr, prop) {
let i = arr.length - 1;
while (i > 0) {
let maxIndex = 0;
for (let j = 0; j < i; j++) {
if (arr[j][prop] > arr[j + 1][prop]) {
let temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
maxIndex = j;
}
}
i = maxIndex;
}
return arr;
}
// 获取指定月份天数
export function getDateByMon(year, month){
let d = new Date(year, month, 0);
return d.getDate();
}
// 判断时间是不是今天
export function isTodayDate(time) {
if (time.toDateString() === new Date().toDateString()) {
return true;
} else {
return false;
}
}
// 对象判空
export function isObjEmpty(obj) {
if (obj && obj.constructor === Object) {
return Object.keys(obj).length;
}
if (obj && obj.constructor === Array) {
return obj.length;
}
}
// 首字母大写
export const capitalizeFirstLetter = (string) => {
return string.charAt(0).toUpperCase() + string.slice(1);
};
// 过滤值为空字符串或者为null的参数
export function getQuery(query) {
let res = {};
for (let [key, val] of Object.entries(query)) {
if (typeof val === 'string') {
if (!val.trim() || val === 'null') continue;
} else {
if (val === null || val === undefined) continue;
}
res[key] = val;
};
return res;
}
// 判断文件类型
export function isImage(fileName) {
if (typeof fileName !== 'string') return;
let name = fileName.toLowerCase();
return name.endsWith('.png') || name.endsWith('.jpeg') || name.endsWith('.jpg') || name.endsWith('.png') || name.endsWith('.bmp');
} export function isH5Video(fileName) {
if (typeof fileName !== 'string') return;
let name = fileName.toLowerCase();
return name.endsWith('.mp4') || name.endsWith('.webm') || name.endsWith('.ogg');
}
export function isPdf(fileName) {
if (typeof fileName !== 'string') return;
let name = fileName.toLowerCase();
return name.endsWith('.pdf');
} export function isWord(fileName) {
if (typeof fileName !== 'string') return;
let name = fileName.toLowerCase();
return name.endsWith('.doc') || name.endsWith('.docx');
} export function isExcel(fileName) {
if (typeof fileName !== 'string') return;
let name = fileName.toLowerCase();
return name.endsWith('.xlsx') || name.endsWith('.xls');
}
// 数字2位转换
export function toDouble(num) {
if (num < 10) {
return '0' + num;
} else {
return num;
}
}
// 动态加载js
export function loadApi(src) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.type = '';
script.src = src;
script.async = 'async';
document.head.appendChild(script); script.onload = () => { resolve(); };
script.onerror = () => { reject(); };
});
} // 动态加载css
export function loadCss(src) {
return new Promise((resolve, reject) => {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = src;
document.head.appendChild(link);
});
}
常用js方法封装使用的更多相关文章
- 常用js方法封装
常用js方法封装 var myJs = { /* * 格式化日期 * @param dt 日期对象 * @returns {string} 返回值是格式化的字符串日期 */ getDates: fun ...
- 项目常用JS方法封装--奋斗的IT青年(微信公众号)
...
- 常用js方法
function dateGetter(name, size, offset, trim) { offset = offset || 0; return function (date) { var v ...
- 常用js方法整理common.js
项目中常用js方法整理成了common.js var h = {}; h.get = function (url, data, ok, error) { $.ajax({ url: url, data ...
- 项目中常用js方法整理common.js
抽空把项目中常用js方法整理成了common.js,都是网上搜集而来的,大家一起分享吧. var h = {}; h.get = function (url, data, ok, error) { $ ...
- appium安卓自动化的 常用driver方法封装
appium安卓自动化的 常用driver方法封装 做安卓自动化的时候,很多方法写起来会造成代码冗余,把这部分封装起来 ,添加到androidUI工具类里,随时可调用 都放在这个类下面: @Compo ...
- JS常用公共方法封装
_ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\||| : |||/ ...
- 常用js方法整理(个人)
开头总要有点废话 今天想了下,还是分享下自己平时积累的一些实用性较高的js方法,供大家指点和评价.本想分篇介绍,发现有点画蛇添足.整理了下也没多少拿得出手的方法,自然有一些是网上看到的个人觉得很有实用 ...
- 在项目中常用的JS方法封装
使用方法简单,只需要放在你的 utils.js 工具文件中,直接export const 加上下面封装方法,在别的文件中使用 {方法1,方法2,方法3...}引用后直接使用即可. 01.输入一个值.返 ...
随机推荐
- ARM Cortex-M底层技术(1)—程序在Flash和SRAM的空间分配
1. keil编译介绍 当使用keil进行单片机的开发时,运行一段程序后,在output输出框会看到如下图的结果. 图1 keil 的output框 其中,Compiler编译器,使用的版本是 V5. ...
- thinkphp5 select对象怎么转数组?
DB操作返回是数组.模型直接操作返回是对象 对象类型转换数组打开 database.php 增加或修改参数'resultset_type' => '\think\Collection',即可连贯 ...
- Windows 开启 winrm
# Windows 开启 winrm ``` Enable-PSRemoting winrm enumerate winrm/config/listener winrm quickconf ...
- ssh修改超时自动登出时间的方法
echo $TMOUT 查看当前服务器登出时间,如果没有输出表示不会登出 1.修改:vim ~/.bash_profile 2.设置TMOUT值 TMOUT=600 #表示10分钟之后自动登出 TMO ...
- linux运维、架构之路-jumpserver
一.jumpserver介绍 是一款由python编写开源的跳板机(堡垒机)系统,实现了跳板机应有的功能.基于ssh协议来管理,客户端无需安装agent. 特点: 完全开源,GPL ...
- HTML中的表单<form>标签
一.HTML表单 HTML 表单用于搜集不同类型的用户输入. HTML 表单包含表单元素,表单元素指的是不同类型的 input 元素.复选框.单选按钮.提交按钮等等. 关于表单的更多内容可以参考htt ...
- [CF1177B]Digits Sequence (Hard Edition)题解
一个简单的模拟,首先先计算当前是几位数,然后根据几位数推断当前的数是什么,然后求出该位即可 #include <cstdio> int main(){ long long k; scanf ...
- kohana学习经验
1.sql查询文件缓存使用 $tokens = DB::select('id', 'token') ->from('member') ->where('id', 'in', $users) ...
- 使用eclipse导入新项目时中文出现乱码问题
有时候在github上看到别人不错的项目想要拉下来学习学习的时候,总会出现这样的情况,实在蛋疼. 一般出现这种问题,会有三个地方需要改动: 在项目上右键选择 properties 将 text fil ...
- 取得所有网卡的MAC地址,包括禁用的
先在nuget包中添加System.Management.Automation引用. 然后下面就是代码了. using System;using System.Collections.ObjectMo ...