Skip to content

Commit 96f8581

Browse files
committed
Flattened functions for more possible renaming by minifier.
Simpler array elements insertion. Simplified redundant `window` binding.
1 parent bb694cf commit 96f8581

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

src/sprintf.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@
2222
}
2323

2424
function sprintf(key) {
25-
var cache = sprintf.cache
26-
if (!(cache[key])) {
27-
cache[key] = sprintf.parse(key)
28-
}
29-
return sprintf.format.call(null, cache[key], arguments)
25+
// `arguments` is not an array, but should be fine for this call
26+
return sprintf_format(sprintf_parse(key), arguments)
27+
}
28+
29+
function vsprintf(fmt, argv) {
30+
return sprintf.apply(null, [fmt].concat(argv || []))
3031
}
3132

32-
sprintf.format = function(parse_tree, argv) {
33+
function sprintf_format(parse_tree, argv) {
3334
var cursor = 1, tree_length = parse_tree.length, arg, output = '', i, k, match, pad, pad_character, pad_length, is_positive, sign
3435
for (i = 0; i < tree_length; i++) {
3536
if (typeof parse_tree[i] === 'string') {
@@ -138,29 +139,33 @@
138139
return output
139140
}
140141

141-
sprintf.cache = Object.create(null)
142+
var sprintf_cache = Object.create(null)
143+
144+
function sprintf_parse(fmt) {
145+
if (sprintf_cache[fmt]) {
146+
return sprintf_cache[fmt]
147+
}
142148

143-
sprintf.parse = function(fmt) {
144149
var _fmt = fmt, match, parse_tree = [], arg_names = 0
145150
while (_fmt) {
146151
if ((match = re.text.exec(_fmt)) !== null) {
147-
parse_tree[parse_tree.length] = match[0]
152+
parse_tree.push(match[0])
148153
}
149154
else if ((match = re.modulo.exec(_fmt)) !== null) {
150-
parse_tree[parse_tree.length] = '%'
155+
parse_tree.push('%')
151156
}
152157
else if ((match = re.placeholder.exec(_fmt)) !== null) {
153158
if (match[2]) {
154159
arg_names |= 1
155160
var field_list = [], replacement_field = match[2], field_match = []
156161
if ((field_match = re.key.exec(replacement_field)) !== null) {
157-
field_list[field_list.length] = field_match[1]
162+
field_list.push(field_match[1])
158163
while ((replacement_field = replacement_field.substring(field_match[0].length)) !== '') {
159164
if ((field_match = re.key_access.exec(replacement_field)) !== null) {
160-
field_list[field_list.length] = field_match[1]
165+
field_list.push(field_match[1])
161166
}
162167
else if ((field_match = re.index_access.exec(replacement_field)) !== null) {
163-
field_list[field_list.length] = field_match[1]
168+
field_list.push(field_match[1])
164169
}
165170
else {
166171
throw new SyntaxError("[sprintf] failed to parse named argument key")
@@ -178,19 +183,14 @@
178183
if (arg_names === 3) {
179184
throw new Error("[sprintf] mixing positional and named placeholders is not (yet) supported")
180185
}
181-
parse_tree[parse_tree.length] = match
186+
parse_tree.push(match)
182187
}
183188
else {
184189
throw new SyntaxError("[sprintf] unexpected placeholder")
185190
}
186191
_fmt = _fmt.substring(match[0].length)
187192
}
188-
return parse_tree
189-
}
190-
191-
var vsprintf = function(fmt, argv, _argv) {
192-
_argv = [fmt].concat(argv || [])
193-
return sprintf.apply(null, _argv)
193+
return sprintf_cache[fmt] = parse_tree
194194
}
195195

196196
/**
@@ -213,4 +213,4 @@
213213
})
214214
}
215215
}
216-
})(typeof window === 'undefined' ? this : window);
216+
})(this);

0 commit comments

Comments
 (0)