1.0.0 • Published 5 months ago

yunjing-utils v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

parseTime 时间格式化

parseTime(date,fmt)

参数类型说明
datestring/Date/number日期字符串、new Date()类型或时间戳
fmtstring默认值:yyyy-MM-dd hh:mm:ss

返回值说明: 返回 fmt 格式化时间

const time = parseTime("2023-01-01 08:00:00", "yyyy-MM-dd hh:mm:ss");
const time2 = parseTime(new Date().getTime());

formatTime 时间格式化为文字

formatTime(time,fmt)

参数类型说明
timenumber时间戳
fmtstring默认值:yyyy 年 MM 月 dd 日

返回值说明: 距当前时间<30s 返回"刚刚" 距当前时间<1h 返回"xx 分钟前" 距当前时间<1d 返回"xx 小时前" 距当前时间<2d 返回"1 天前" 其他时间返回 fmt 格式化时间

numberTime 返回几天前、几天后的时间

numberTime(date,day,fmt) | 参数 | 类型 | 说明 | | ---- | ------------------ | ------------------------- | | date | string/Date/number | 日期字符串、new Date()类型或时间戳 | | day | number | 默认值:0 | | fmt | string | 默认值:yyyy-MM-dd hh:mm:ss |

返回值说明: 返回 fmt 格式化时间

numberTime(new Date("2023-10-11 00:00:00"), -7); //返回7天前 2023-10-04 00:00:00

getUrlQuery 获取 url 参数

返回值说明: 返回对象结构

//示例url:http://xxx.xx.com?a=123&b=456
console.log(getUrlQuery()); //{a:'123',b:'456'}

devicePlatform 获取设备平台

devicePlatform()

返回值说明:

字段名说明
tridenttrue/false是否为 ie 内核
webKittrue/false是否为谷歌内核
prestotrue/false是否为 opera 内核
geckotrue/false是否为火狐内核
mobiletrue/false是否为移动终端
iostrue/false是否为 ios 系统
androidtrue/false是否为安卓系统
iphonetrue/false是否为 iphone
iPadtrue/false是否为 ipad
wechattrue/false是否为微信
//示例在pc端运行
console.log(utils.devicePlatform().ios); //false
console.log(utils.devicePlatform().webKit); //true

removeNullObj 删除对象中的空字段

removeNullObj(obj)

参数说明: | 参数 | 类型 | 说明 | | --- | --- | --- | | obj | object | 传入一个对象 |

返回说明: 返回处理之后的对象

const obj = {
  a: null,
  b: "",
  c: undefined,
  d: "123",
};
console.log(removeNullObj(obj)); //{d: '123'}

throttle 节流

参数说明: | 参数 | 类型 | 说明 | | --- | --- | --- | | fn | function | 执行的方法 | | delay | number | 延迟的时长 默认 1000ms |

const func = throttle(function () {
  console.log("throttle click");
}, 5000);

debounce 防抖

参数说明: | 参数 | 类型 | 说明 | | --- | --- | --- | | fn | function | 执行的方法 | | delay | number | 延迟的时长 默认 1000ms |

const func = debounce(function () {
  console.log("debounce click");
}, 1000);

flattenArray 数组扁平化

flattenArray(arr: any[], key?: string)

参数说明: | 参数 | 类型 | 说明 | | --- | --- | --- | | arr | any[] | 任意的数组类型 | | key | string | 可选,对象数组的 key |

返回说明: 返回扁平化之后的数组

const flattenNumberArray = [1, [2, [3, 4], 5], 6];
const flattenedNumberArray = flattenArray(flattenNumberArray);
console.log(flattenedNumberArray); //[1,2,3,4,5,6]

const flattenObjArray = [{ a: 1 }, [{ b: 1 }, [{ c: 1 }]]];
const flattenedObjArray = flattenArray(flattenObjArray);
console.log(flattenedObjArray); //{a:1,b:1,c:1}

const nestChildObj = [
  {
    name: "11",
    children: [
      {
        name: "111",
      },
      {
        name: "112",
      },
    ],
  },
  {
    name: "12",
    children: [
      {
        name: "121",
      },
      {
        name: "122",
      },
    ],
  },
];
const flattenedChildObj = flattenArray(nestChildObj, "children");
console.log(flattenedChildObj); //[{name:'11'},{name:'111'},{name:'112'},{name:'12'},{name:'121'},{name:'122'}]