Created
April 9, 2013 08:32
-
-
Save greatghoul/5344011 to your computer and use it in GitHub Desktop.
Format javascript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 格式化字符串 | |
// | |
// 用法: | |
// | |
// var s1 = '%{1} and %{2}!'; | |
// console.log('source: ' + s1); | |
// console.log('target: ' + fmt(s1, 'ask', 'learn')); | |
// | |
// var s2 = "%{name} is %{age} years old, his son's name is %{sons[0].name}"; | |
// console.log('source: ' + s2); | |
// console.log('target: ' + fmt(s2, { name: 'Lao Ming', age: 32, sons: [{ name: 'Xiao Ming' }]})); | |
function fmt() { | |
var args = arguments; | |
return args[0].replace(/%\{(.*?)}/g, function(match, prop) { | |
return function(obj, props) { | |
var prop = /\d+/.test(props[0]) ? parseInt(props[0]) : props[0]; | |
if (props.length > 1) { | |
return arguments.callee(obj[prop], props.slice(1)); | |
} else { | |
return obj[prop]; | |
} | |
}(typeof args[1] === 'object' ? args[1] : args, prop.split(/\.|\[|\]\[|\]\./)); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment