@beisen/common-func
Advanced tools
Comparing version 0.0.14 to 0.0.15
@@ -9,3 +9,3 @@ /* eslint-disable */ | ||
import {clearToolTipDom, uid, judgeBrowser, judgeSys, getHeightLight, deepClone, transferData} from './src/index.js'; | ||
import {clearToolTipDom, uid, judgeBrowser, judgeSys, getHeightLight, deepClone, transferData, transFormat} from './src/index.js'; | ||
@@ -52,2 +52,3 @@ class App extends React.Component { | ||
console.log(transferData([{id:1,pid:0},{id:2,pid:1}])) | ||
console.log(transFormat('I {0} is {1} , a {0}', 1, 'wu')) | ||
} | ||
@@ -54,0 +55,0 @@ |
{ | ||
"name": "@beisen\/common-func", | ||
"version": "0.0.14", | ||
"version": "0.0.15", | ||
"description": "CommonFunc", | ||
@@ -5,0 +5,0 @@ "main": "lib\/index.js", |
@@ -396,2 +396,21 @@ /* eslint-disable */ | ||
.replace(/&/g, '&'); | ||
} | ||
} | ||
/** | ||
* 将字符串模板中的占位符替换为相应的变量 | ||
* @param {String} template 字符串模板, 变量key只能为数字 | ||
* @param {...any} variables 对应的变量 | ||
* 示例:(括号中的序号与翻译参数的顺序有关) | ||
* console.log(transFormat("{0} is {1}", 1, 2)); | ||
* console.log(transFormat("{1} is {0}", 1, 2)); | ||
*/ | ||
export function transFormat(template, ...variables) { | ||
const keys = template.match(/{\s*[0-9]+\s*}/g).map(str => { | ||
return str.slice(1, str.length - 1).trim(); | ||
}); | ||
return keys.reduce( | ||
(template, key) => template.replace(/{\s*[0-9]+\s*}/, variables[key]), | ||
template | ||
); | ||
} | ||
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
1952939
27
47149