Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 33 additions & 34 deletions f2.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function F2() {
*
* @returns {String}
* */
this.format = this.__f2();
this.format = getFormat(this);
}

/**
Expand Down Expand Up @@ -103,7 +103,7 @@ F2.prototype.type = function (name, formatter) {
* @returns {String}
* */
F2.prototype.applyArgs = function (args, offsetLeft, offsetRight) {
return this.__applyArgs(args, offsetLeft >> 0, offsetRight >> 0);
return applyArgs(this, args, offsetLeft >> 0, offsetRight >> 0);
};

/**
Expand All @@ -119,7 +119,7 @@ F2.prototype.applyArgs = function (args, offsetLeft, offsetRight) {
* @returns {String}
* */
F2.prototype.applyArgsTo = function (f, args, offsetLeft, offsetRight) {
return this.__applyArgsTo(f, args, offsetLeft >> 0, offsetRight >> 0);
return applyArgsTo(this, f, args, offsetLeft >> 0, offsetRight >> 0);
};

/**
Expand All @@ -134,7 +134,7 @@ F2.prototype.applyArgsTo = function (f, args, offsetLeft, offsetRight) {
* @returns {Boolean}
* */
F2.prototype.isPattern = function (f) {
var tmpl = this.__pickTmpl(f);
var tmpl = pickTmpl(this, f);
return (tmpl.containsKwargs | tmpl.restArgsIndex) > 0;
};

Expand All @@ -149,34 +149,35 @@ F2.prototype._inspect = function (v) {
return util.inspect(v);
};

F2.prototype.__applyArgs = function (args, offsetLeft, offsetRight) {
function applyArgs(f2, args, offsetLeft, offsetRight) {
if (typeof args[offsetLeft] === 'string') {
return this.__applyArgsTo(args[offsetLeft], args, offsetLeft + 1, offsetRight);
return applyArgsTo(f2, args[offsetLeft], args, offsetLeft + 1, offsetRight);
}

return this.__appendRestArgs([], args, offsetLeft, offsetRight);
};
return appendRestArgs(f2, [], args, offsetLeft, offsetRight);
}

function applyArgsTo(f2, f, args, offsetLeft, offsetRight) {
var tmpl = pickTmpl(f2, f);

F2.prototype.__applyArgsTo = function (f, args, offsetLeft, offsetRight) {
var tmpl = this.__pickTmpl(f);
offsetRight += Number(tmpl.containsKwargs);

return this.__appendRestArgs([this.__substituteTmplItems(tmpl.items, args, offsetLeft, offsetRight)],
return appendRestArgs(f2, [substituteTmplItems(f2, tmpl.items, args, offsetLeft, offsetRight)],
args, offsetLeft + tmpl.restArgsIndex, offsetRight);
};
}

F2.prototype.__pickTmpl = function (f) {
var tmpl = this.__cache.get(f);
function pickTmpl(f2, f) {
var tmpl = f2.__cache.get(f);

if (!tmpl) {
tmpl = this.__parseF(f);
this.__cache.set(f, tmpl);
tmpl = parseF(f2, f);
f2.__cache.set(f, tmpl);
}

return tmpl;
};
}

F2.prototype.__parseF = function (f) {
function parseF(f2, f) {
/*eslint complexity: 0*/
var autoIndex = 0;
var containsKwargs = false;
Expand All @@ -190,7 +191,7 @@ F2.prototype.__parseF = function (f) {
/*eslint no-cond-assign: 0*/
while (m = LEX.exec(f)) {

if (!m[7] || typeof this.__types[m[7]] !== 'function') {
if (!m[7] || typeof f2.__types[m[7]] !== 'function') {
// text node (no type match, or no type formatter)
m = [m[8] || m[9] || m[0], undefined, undefined, undefined, undefined, undefined, undefined, undefined];

Expand Down Expand Up @@ -235,23 +236,23 @@ F2.prototype.__parseF = function (f) {
items: tmplItems,
restArgsIndex: restArgsIndex
};
};
}

F2.prototype.__appendRestArgs = function (parts, args, offsetLeft, offsetRight) {
function appendRestArgs(f2, parts, args, offsetLeft, offsetRight) {
var i = offsetLeft;
var l = args.length - offsetRight;
var j = parts.length;

while (i < l) {
parts[j] = this._inspect(args[i]);
parts[j] = f2._inspect(args[i]);
j += 1;
i += 1;
}

return parts.join(' ');
};
}

F2.prototype.__substituteTmplItems = function (tmplItems, args, offsetLeft, offsetRight) {
function substituteTmplItems(f2, tmplItems, args, offsetLeft, offsetRight) {
var argc = args.length - offsetRight;
var tmplItem;
var tmplItemsCount = tmplItems.length;
Expand Down Expand Up @@ -279,23 +280,21 @@ F2.prototype.__substituteTmplItems = function (tmplItems, args, offsetLeft, offs
subst = undefined;
}

chunks[tmplItemsCount] = this.__formatPlaceholder(tmplItem, subst);
chunks[tmplItemsCount] = formatPlaceholder(f2, tmplItem, subst);
}

return chunks.join('');
};
}

F2.prototype.__formatPlaceholder = function (ph, subst) {
function formatPlaceholder(f2, ph, subst) {
if (typeof subst === 'function') {
subst = subst();
}

return this.__types[ph.subType](subst, ph.sign, ph.fill, ph.width, ph.precision);
};

F2.prototype.__f2 = function () {
var self = this;
return f2.__types[ph.subType](subst, ph.sign, ph.fill, ph.width, ph.precision);
}

function getFormat(f2) {
function format() {
// clone arguments to allow V8 optimize the function
var argc = arguments.length;
Expand All @@ -306,10 +305,10 @@ F2.prototype.__f2 = function () {
args[argc] = arguments[argc];
}

return self.__applyArgs(args, 0, 0);
return applyArgs(f2, args, 0, 0);
}

return format;
};
}

module.exports = F2;