string-format
Advanced tools
Comparing version
36
index.js
@@ -12,7 +12,2 @@ void function(global) { | ||
// defaultTo :: a,a? -> a | ||
function defaultTo(x, y) { | ||
return y == null ? x : y; | ||
} | ||
// create :: Object -> String,*... -> String | ||
@@ -47,4 +42,20 @@ function create(transformers) { | ||
} | ||
var value = defaultTo('', lookup(args, key.split('.'))); | ||
// 1. Split the key into a lookup path. | ||
// 2. If the first path component is not an index, prepend '0'. | ||
// 3. Reduce the lookup path to a single result. If the lookup | ||
// succeeds the result is a singleton array containing the | ||
// value at the lookup path; otherwise the result is []. | ||
// 4. Unwrap the result by reducing with '' as the default value. | ||
var path = key.split('.'); | ||
var value = (/^\d+$/.test(path[0]) ? path : ['0'].concat(path)) | ||
.reduce(function(maybe, key) { | ||
return maybe.reduce(function(_, x) { | ||
return x != null && key in Object(x) ? | ||
[typeof x[key] === 'function' ? x[key]() : x[key]] : | ||
[]; | ||
}, []); | ||
}, [args]) | ||
.reduce(function(_, x) { return x; }, ''); | ||
if (xf == null) { | ||
@@ -62,15 +73,2 @@ return value; | ||
function lookup(_obj, _path) { | ||
var obj = _obj; | ||
var path = _path; | ||
if (!/^\d+$/.test(path[0])) { | ||
path = ['0'].concat(path); | ||
} | ||
for (var idx = 0; idx < path.length; idx += 1) { | ||
var key = path[idx]; | ||
obj = typeof obj[key] === 'function' ? obj[key]() : obj[key]; | ||
} | ||
return obj; | ||
} | ||
// format :: String,*... -> String | ||
@@ -77,0 +75,0 @@ var format = create({}); |
{ | ||
"name": "string-format", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "String formatting inspired by Python's str.format()", | ||
@@ -14,3 +14,3 @@ "author": "David Chambers <dc@davidchambers.me>", | ||
"bugs": "https://github.com/davidchambers/string-format/issues", | ||
"license": "(WTFPL OR MIT)", | ||
"license": "WTFPL OR MIT", | ||
"repository": { | ||
@@ -28,3 +28,3 @@ "type": "git", | ||
"devDependencies": { | ||
"sanctuary-scripts": "1.2.x" | ||
"sanctuary-scripts": "1.6.x" | ||
}, | ||
@@ -31,0 +31,0 @@ "scripts": { |
@@ -146,7 +146,7 @@ # string-format | ||
const sheldon = { | ||
firstName: 'Sheldon', | ||
lastName: 'Cooper', | ||
dob: new Date('1970-01-01'), | ||
fullName: function() { return this.firstName + ' ' + this.lastName }, | ||
quip: function() { return 'Bazinga!' }, | ||
firstName: 'Sheldon', | ||
lastName: 'Cooper', | ||
dob: new Date('1970-01-01'), | ||
fullName: function() { return this.firstName + ' ' + this.lastName }, | ||
quip: function() { return 'Bazinga!' }, | ||
} | ||
@@ -170,3 +170,3 @@ | ||
escape: s => s.replace(/[&<>"'`]/g, c => '&#' + c.charCodeAt(0) + ';'), | ||
upper: s => s.toUpperCase(), | ||
upper: s => s.toUpperCase(), | ||
}) | ||
@@ -193,3 +193,3 @@ | ||
escape: s => s.replace(/[&<>"'`]/g, c => '&#' + c.charCodeAt(0) + ';'), | ||
upper: s => s.toUpperCase(), | ||
upper: s => s.toUpperCase(), | ||
}) | ||
@@ -196,0 +196,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
9925
3.94%0
-100%85
-1.16%