Comparing version 0.0.22 to 0.1.0
let {CNYCurrency} = require('../lib/cnycurrency'); | ||
let {datetimeUtils} = require('../lib/datetimeUtils'); | ||
let { CNYCurrency } = require('../lib/cnycurrency'); | ||
let { datetimeUtils } = require('../lib/datetimeUtils'); | ||
let { utils } = require('../lib/utils'); | ||
let m = []; | ||
@@ -77,3 +77,15 @@ let c = new CNYCurrency(-12345); | ||
let rr = utils.mkdirsSyncEx('./hello/test'); | ||
let rs = utils.mkdirsSyncEx('d:/temp/hello/test'); | ||
console.log('rr:' + JSON.stringify(rr)); | ||
console.log('rs:' + JSON.stringify(rs)); | ||
console.log('aaa:' + utils.formatPercentage(0.123)); | ||
console.log('aaa:' + utils.formatPercentage(null)); | ||
let testaaa = new CNYCurrency(100); | ||
testaaa.selfMul(0.102); | ||
m.push(testaaa); | ||
for(let mm of m) { | ||
@@ -80,0 +92,0 @@ let c2 = mm.Chinese(); |
@@ -510,32 +510,82 @@ const _ = require("lodash"); | ||
* 创建目录 | ||
* @param {string} dirpath 要创建的目录,支持多层级创建 | ||
* @param {number} mode 创建后,目录的权限,默认为0o777 | ||
* @return {boolean} 返回创建结果 | ||
* - 注意,请用绝对路径 | ||
* @param {string} paramPath 要创建的目录,支持多层级创建 | ||
* @param {number} paramMode 创建后,目录的权限,默认为0o777 | ||
* @return {boolean} 返回创建结果,只会返回成功失败 | ||
*/ | ||
static mkdirsSync(dirpath, mode) { | ||
static mkdirsSync(paramPath, paramMode = 0o777) { | ||
return this.mkdirsSyncEx(paramPath, paramMode).ret; | ||
} | ||
/** | ||
* 创建目录 | ||
* - 注意,请用绝对路径, 这里会返回具体的错误信息 | ||
* @param {string} paramPath 要创建的目录,支持多层级创建 | ||
* @param {number} paramMode 创建后,目录的权限,默认为0o777 | ||
* @return {{ret: boolean, msg: string}} 返回创建结果,只会返回成功失败 | ||
* - ret = true 表示创建成功 | ||
* - ret = false 表示创建失败 ,msg为错误信息 | ||
*/ | ||
static mkdirsSyncEx(paramPath, paramMode = 0o777) { | ||
let r = { | ||
ret: false, | ||
msg: '' | ||
}; | ||
try { | ||
if (!fs.existsSync(dirpath)) { | ||
if (!fs.existsSync(paramPath)) { | ||
let pathtmp; | ||
dirpath.split(/[/\\]/).forEach((dirname)=> { //这里指用/ 或\ 都可以分隔目录 如 linux的/usr/local/services 和windows的 d:\temp\aaaa | ||
paramPath.split(/[/\\]/).forEach((dirname)=> { //这里指用/ 或\ 都可以分隔目录 如 linux的/usr/local/services 和windows的 d:\temp\aaaa | ||
if (pathtmp) { | ||
pathtmp = path.join(pathtmp, dirname); | ||
} else { | ||
pathtmp = dirname; | ||
if (dirname === '') { | ||
pathtmp = path.sep; | ||
} else { | ||
pathtmp = dirname; | ||
} | ||
} | ||
if (!fs.existsSync(pathtmp)) { | ||
if (!fs.mkdirSync(pathtmp, mode)) { | ||
return false; | ||
} | ||
fs.mkdirSync(pathtmp, paramMode); | ||
} | ||
}); | ||
} | ||
return true; | ||
} | ||
r.ret = true; | ||
return r; | ||
} catch (e) { | ||
let log = require("./log").getLogger(__filename); | ||
log.error("create director fail! path=" + dirpath + " errorMsg:" + e); | ||
return false; | ||
r.ret = false; | ||
r.msg = "create director fail! path=" + paramPath + " errorMsg:" + e; | ||
return r; | ||
} | ||
} | ||
} | ||
/** | ||
* 将数字转换为百分比的字符串 | ||
* - 精确到0.01% | ||
* @param {number} paramValue 要格式化的值 | ||
* @param {string} paramDefault 无效值后,返回的字符串 | ||
* @return {string} 格式化的百分比字符串 | ||
* - 对于paramValue为null或undefined | ||
*/ | ||
static formatPercentage(paramValue, paramDefault = '0.00%') { | ||
if (paramValue === null || paramValue === undefined) { | ||
return paramDefault; | ||
} | ||
let n = ''; | ||
let v = Math.round(Number.parseFloat(paramValue) * 10000); | ||
if (Number.isNaN(v)) { | ||
return paramDefault; | ||
} | ||
if (v < 0) { | ||
// 如果是小于0的数 | ||
n = '-'; | ||
v = Math.abs(v); | ||
} | ||
let lowNumber = (v % 100).toString().padStart(2, '0'); | ||
let highNumber = ((v - lowNumber) / 100).toString().padStart(1, '0'); | ||
return [n, highNumber, '.', lowNumber, '%'].join(''); | ||
} | ||
/** | ||
@@ -573,4 +623,4 @@ * 解析参数列表 | ||
let ret = { _:[], args:{} }; | ||
let ret = { _:[], args:{} }; | ||
paramArgs = paramArgs || []; | ||
@@ -622,3 +672,3 @@ if(!Array.isArray(paramArgs)) { | ||
return ret; | ||
} | ||
} | ||
} | ||
@@ -625,0 +675,0 @@ exports = module.exports = { |
{ | ||
"name": "xmcommon", | ||
"version": "0.0.22", | ||
"version": "0.1.0", | ||
"description": "javascript common lib for es6", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -34,2 +34,9 @@ # xmcommon | ||
## 0.1.0 | ||
- 2019-07-14 | ||
- \* fixbug 修复utils.mkdirSync在linux下,使用绝对路径会失败的bug | ||
- \+ 增加utils.mkdirSyncEx,用于返回具体的错误信息 | ||
- \+ 增加utils.formatPercentage,用于生成百分比字符串 | ||
## 0.0.22 | ||
@@ -36,0 +43,0 @@ |
@@ -362,12 +362,34 @@ // Type definitions for xmcommon 0.0 | ||
*/ | ||
static JsonParse (paramJsonString:string): any; | ||
static JsonParse (paramJsonString:string): any; | ||
/** | ||
* 创建目录 | ||
* @param dirpath 要创建的目录,支持多层级创建 | ||
* @param mode 创建后,目录的权限,默认为0o777 | ||
* @param paramPath 要创建的目录,支持多层级创建 | ||
* @param paramMode 创建后,目录的权限,默认为0o777 | ||
* @return 返回创建结果 | ||
*/ | ||
static mkdirsSync(dirpath:string, mode:number):boolean; | ||
static mkdirsSync(paramPath:string, paramMode?: number): boolean; | ||
/** | ||
* 创建目录 | ||
* - 这个函数与mkdirsSync区别是,这个函数会返回失败的错误信息 | ||
* @param paramPath 要创建的目录,支持多层级创建 | ||
* @param paramMode 创建后,目录的权限,默认为0o777 | ||
* @return 返回创建结果 | ||
* - ret = true 表示创建成功 | ||
* - ret = false 表示创建失败 ,msg为错误信息 | ||
*/ | ||
static mkdirsSyncEx(paramPath: string, paramMode?: number): { ret: boolean, msg: string}; | ||
/** | ||
* 将数字转换为百分比的字符串 | ||
* - 精确到0.01% | ||
* @param paramValue 要格式化的值 | ||
* @param paramDefault 无效值后,返回的字符串 | ||
* @return 格式化的百分比字符串 | ||
* - 对于paramValue为null或undefined | ||
*/ | ||
static formatPercentage(paramValue: number, paramDefault?: string): string; | ||
/** | ||
* 解析参数列表 | ||
@@ -374,0 +396,0 @@ * @param paramArgs 参数列表 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3957
205
136169
25