# yeung-utils **Repository Path**: cuitzhy/yeung-utils ## Basic Information - **Project Name**: yeung-utils - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-04-08 - **Last Updated**: 2024-06-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 常用工具函数 平时用到的常用工具函数,从1.0.9版本使用ts开发 支持 es 方式引入 ```js // es6的方式 import { convertTothousandths } from "@yeung2017/utils"; ``` ## API #### Table of Contents * [IAddQueryToUrlOptions](#iaddquerytourloptions) * [url](#url) * [query](#query) * [mergeQuery](#mergequery) * [addQueryToUrl](#addquerytourl) * [Parameters](#parameters) * [IConvertTothousandthsOptions](#iconverttothousandthsoptions) * [separator](#separator) * [convertTothousandths](#converttothousandths) * [Parameters](#parameters-1) * [IDateFormatOptions](#idateformatoptions) * [format](#format) * [fallback](#fallback) * [isMillisecond](#ismillisecond) * [autoTenSecond](#autotensecond) * [dateFormat](#dateformat) * [Parameters](#parameters-2) * [Examples](#examples) * [formatNumberDivisor](#formatnumberdivisor) * [Parameters](#parameters-3) * [Examples](#examples-1) * [formatNumberPercent](#formatnumberpercent) * [Parameters](#parameters-4) * [Examples](#examples-2) * [formatQuery](#formatquery) * [Parameters](#parameters-5) * [Examples](#examples-3) * [IGetTimeDurationOptions](#igettimedurationoptions) * [startTimeStamp](#starttimestamp) * [endTimeStamp](#endtimestamp) * [IGetTimeDurationResult](#igettimedurationresult) * [DDMax](#ddmax) * [DD](#dd) * [HHMax](#hhmax) * [HH](#hh) * [mmMax](#mmmax) * [mm](#mm) * [ssMax](#ssmax) * [ss](#ss) * [SSSMax](#sssmax) * [SSS](#sss) * [duration](#duration) * [getTimeDuration](#gettimeduration) * [Parameters](#parameters-6) * [IIsCommonAppBroswerOptions](#iiscommonappbrosweroptions) * [pattern](#pattern) * [isCommonAppBroswer](#iscommonappbroswer) * [Parameters](#parameters-7) * [safaJsonParse](#safajsonparse) * [Parameters](#parameters-8) * [safeJsonStringify](#safejsonstringify) * [Parameters](#parameters-9) * [padEndZero](#padendzero) * [Parameters](#parameters-10) * [Examples](#examples-4) * [ISplitPriceResult](#isplitpriceresult) * [price](#price) * [integer](#integer) * [decimal](#decimal) * [decimalNoPoint](#decimalnopoint) * [priceFixed](#pricefixed) * [splitPrice](#splitprice) * [Parameters](#parameters-11) * [Examples](#examples-5) * [IToFenOptions](#itofenoptions) * [decimal](#decimal-1) * [toFen](#tofen) * [Parameters](#parameters-12) * [Examples](#examples-6) * [IToYuanOptions](#itoyuanoptions) * [decimal](#decimal-2) * [decimalIfNeeded](#decimalifneeded) * [thousandCentimeter](#thousandcentimeter) * [thousandCentimeterSeparator](#thousandcentimeterseparator) * [toYuan](#toyuan) * [Parameters](#parameters-13) * [Examples](#examples-7) ### IAddQueryToUrlOptions addQueryToUrl方法的选项 #### url url Type: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) #### query 查询字符串,也可以是个对象 Type: ([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)) #### mergeQuery 是否覆盖更新query, 比如addQueryToUrl({url:'xxx?a=1',query:{a:2},mergeQuery:true})将会是xxx?a=2,而不是xxx?a=1\&a=2 Type: [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean) ### addQueryToUrl 将query添加到url上。 注意:如果query中项的值是一个对象或者数组, 将会JSON.stringify这个值(这与qs),如果要还原这个值,请使用JSON.parse(decodeURIComponent(decodeURIComponent(value)))。 如果想将query还原成对象,可以使用formatQuery方法。 #### Parameters * `options` **[IAddQueryToUrlOptions](#iaddquerytourloptions)?** (optional, default `{}`) * `options.url` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** url (optional, default `''`) * `options.query` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object))?** 查询字符串,也可以是个对象 (optional, default `''`) * `options.mergeQuery` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** 是否覆盖更新query, (optional, default `true`) Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 拼接好的字符串 **Meta** * **since**: 1.0.2 ### IConvertTothousandthsOptions convertTothousandths的选项 #### separator 分隔符 Type: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) ### convertTothousandths 将数字字符串转化为千分位 #### Parameters * `num` **([number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number) | [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String))** (optional, default `''`) * `options` **[IConvertTothousandthsOptions](#iconverttothousandthsoptions)** (optional, default `{}`) * `options.separator` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 分隔符 (optional, default `','`) Returns **any** {string} **Meta** * **since**: 1.0.0 ### IDateFormatOptions dateFormat方法的选项 #### format 输出格式,默认为YYYY-MM-DD HH:mm:ss Type: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) #### fallback 时间戳不能正确格式化的时候输出fallback,默认为‘--’ Type: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) #### isMillisecond 是否是毫秒时间戳,默认为true Type: [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean) #### autoTenSecond 是否自动认为10位时间戳是秒时间戳,默认为true Type: [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean) ### dateFormat 格式化时间戳,默认输出格式为YYYY-MM-DD HH:mm:ss。不能正确格式化的时候输出fallback,默认为‘--’ #### Parameters * `timeStamp` **([number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number) | [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String))?** 时间戳 * `options` **[IDateFormatOptions](#idateformatoptions)?** 选项 (optional, default `{}`) * `options.format` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** 输出格式,默认为YYYY-MM-DD HH:mm:ss (optional, default `'YYYY-MM-DD HH:mm:ss'`) * `options.fallback` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** 时间戳不能正确格式化的时候输出fallback,默认为‘--’ (optional, default `'--'`) * `options.isMillisecond` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** 是否是毫秒时间戳,默认为true (optional, default `true`) * `options.autoTenSecond` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** 是否自动认为10位时间戳是秒时间戳,默认为true (optional, default `true`) #### Examples ```javascript dateFormat(1602491858395) // 2020-10-12 16:37:38 dateFormat(1602491858) // 2020-10-12 16:37:38 ``` Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 格式化后的时间戳 **Meta** * **since**: 1.0.4 ### formatNumberDivisor 把数字转换成带小数的字符串,默认得到万分之一,并保留指定小数,若小数位不够,用0补齐,如210000转换为21.00(单位是万) #### Parameters * `number` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** (optional, default `0`) * `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** 选项 (optional, default `{}`) * `options.divisor` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** 除数,默认为10000,如果为100,则表示百分之一,如果为1,则表示原数保留两位小数 (optional, default `10000`) * `options.precision` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** 保留几位小数 (optional, default `2`) #### Examples ```javascript formatNumberDivisor(210000) // 输出为 21.00 ``` ```javascript formatNumberDivisor(210000, { precision: 0 }) // 输出为 21 ``` Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** **Meta** * **since**: 1.1.4 ### formatNumberPercent 得到百分比,并默认保留两位小数,若小数位数不足,则用0补齐,如1.10 #### Parameters * `num` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** 分子 (optional, default `0`) * `total` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** 分母 (optional, default `100`) * `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** 选项 (optional, default `{}`) * `options.precision` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** 保留几位小数 (optional, default `0`) #### Examples ```javascript formatNumberPercent(1, 2) // 输出为 50.00 ``` Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** **Meta** * **since**: 1.1.4 ### formatQuery 格式化查询字符串为对象 查询字符串的值为原始值,包括JSON字符串 需要配合addQueryToUrl使用 #### Parameters * `query` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 查询字符串 可以是完整的url (optional, default `''`) #### Examples ```javascript formatQuery('http://xxx?b=2&k=1&a=%5B1%2C2%2C3%5D') // 输出为 { a: [ 1, 2, 3, ], k: 1, b: 2, } ``` Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** 查询字符串转换成的对象 **Meta** * **since**: 1.0.2 ### IGetTimeDurationOptions getTimeDuration方法的选项 #### startTimeStamp 起始时间戳 毫秒 Type: [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number) #### endTimeStamp 结束时间戳 毫秒 Type: [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number) ### IGetTimeDurationResult getTimeDuration方法的返回值 #### DDMax 天部分 Type: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) #### DD 0-30天部分 Type: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) #### HHMax 小时部分 Type: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) #### HH 0-23小时部分 Type: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) #### mmMax 分钟部分 Type: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) #### mm 0-59分钟部分 Type: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) #### ssMax 秒部分 Type: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) #### ss 0-59秒部分 Type: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) #### SSSMax 毫秒部分 Type: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) #### SSS 0-999毫秒部分 Type: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) #### duration Duration实例 Type: duration.Duration ### getTimeDuration 根据起始时间和结束时间得到差值信息 #### Parameters * `options` **[IGetTimeDurationOptions](#igettimedurationoptions)** 选项 (optional, default `{}`) * `options.startTimeStamp` (optional, default `0`) * `options.endTimeStamp` (optional, default `0`) Returns **[IGetTimeDurationResult](#igettimedurationresult)** **Meta** * **since**: 1.1.4 ### IIsCommonAppBroswerOptions isCommonAppBroswer的选项 #### pattern 要匹配的内容,正则表达式,默认为/(micromessenger|qq|weibo)/i Type: [RegExp](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp) ### isCommonAppBroswer 是否是常见的APP的内置浏览器,默认匹配微信、QQ、微博 #### Parameters * `ua` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 浏览器ua (optional, default `navigator.userAgent.toLowerCase()`) * `options` **[IIsCommonAppBroswerOptions](#iiscommonappbrosweroptions)** (optional, default `{}`) * `options.pattern` **[RegExp](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp)** 要匹配的内容,正则表达式,默认为/(micromessenger|qq|weibo)/i (optional, default `/(micromessenger|qq|weibo)/i`) Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** **Meta** * **since**: 1.0.2 ### safaJsonParse 安全地解析JSON字符串。 该函数尝试解析一个给定的字符串为JSON对象。如果解析失败,由于JSON格式不正确或其他原因, 函数将返回`undefined`,而不是抛出错误。这种方式可以更优雅地处理解析错误,避免了程序因捕获异常而中断。 #### Parameters * `data` **any** 要解析的JSON字符串。 Returns **any** 解析后的JSON对象,如果解析失败则返回`undefined`。 ### safeJsonStringify 安全地将数据转换为JSON字符串。 该函数尝试将给定的数据转换为JSON字符串。如果转换过程中发生错误(例如,数据结构不被支持), 则函数会安静地失败,返回`undefined`而不是抛出异常。这使得调用者可以更易于处理不可预计的 数据结构或类型,而不会导致程序因异常而中断。 #### Parameters * `data` **any** 要转换为JSON字符串的数据。 Returns **any** 成功时返回数据的JSON字符串表示;失败时返回`undefined`。 ### padEndZero 根据精度,补齐末位的0 #### Parameters * `num` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** (optional, default `0`) * `precision` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** (optional, default `0`) #### Examples ```javascript padEndZero(1.1, 2) // 输出为 1.10 ``` Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** **Meta** * **since**: 1.1.4 ### ISplitPriceResult splitPrice的返回值 #### price 原始的价格 Type: ([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)) #### integer 整数部分 Type: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) #### decimal 小数部分 Type: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) #### decimalNoPoint 小数部分没有小数点部分 Type: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) #### priceFixed 整数加小数部分 Type: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) ### splitPrice 格式化价格,返回一个对象,包含原始的价格、整数部分、小数部分、小数部分没有小数点部分、整数加小数部分。 如果想要保留合适的小数位数请在调用该方法之前处理好 #### Parameters * `price` **([number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number) | [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String))** 价格 #### Examples ```javascript splitPrice(1.2) // 输出为 { price: 1.2, integer: '1', decimal: '.2', decimalNoPoint: '2', priceFixed: '1.2', } ``` Returns **[ISplitPriceResult](#isplitpriceresult)** 包含原始的价格、整数部分、小数部分、小数部分没有小数点部分、整数加小数部分的对象 **Meta** * **since**: 1.0.9 ### IToFenOptions toFen的选项 #### decimal 保留几位小数,默认为0 Type: [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number) ### toFen 将元转换成分 #### Parameters * `yuan` **([number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number) | [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String))** * `options` **[IToFenOptions](#itofenoptions)** (optional, default `{}`) * `options.decimal` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** 保留几位小数,默认为0 (optional, default `0`) #### Examples ```javascript toFen(1.25 // 125 toFen(19.9) // 1990,不是1989.9999999999998 ``` Returns **any** {number} **Meta** * **since**: 1.0.1 ### IToYuanOptions toYuan的选项 #### decimal 保留几位小数,默认为2 Type: [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number) #### decimalIfNeeded 是否只显示必要的小数,比如5.00将显示为5,5.10将显示为5.1,默认为false Type: [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean) #### thousandCentimeter 是否千分位 Type: [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean) #### thousandCentimeterSeparator 千分位符号 Type: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) ### toYuan 分转化成元,分一般为整数,但该方法也支持小数。 0.105.toFixed(2) 返回的是 0.1 而不是 0.11,所以不用toFixed #### Parameters * `num` **([number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number) | [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String))** 分,不一定是整数 * `options` **[IToYuanOptions](#itoyuanoptions)** 选项 (optional, default `{}`) * `options.decimal` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** 保留几位小数,默认为2 (optional, default `2`) * `options.thousandCentimeter` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** 是否千分位 (optional, default `false`) * `options.thousandCentimeterSeparator` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 千分位符号 (optional, default `','`) * `options.decimalIfNeeded` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** 是否只显示必要的小数,比如5.00将显示为5,5.10将显示为5.1,默认为false (optional, default `false`) #### Examples ```javascript toYuan(100) // 1.00 ``` Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 格式化后的字符串元 **Meta** * **since**: 1.0.1