forked from Supositware/Haha-Yes
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
172 lines
18 KiB
JavaScript
172 lines
18 KiB
JavaScript
6 years ago
|
'use strict';
|
||
|
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
|
||
|
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
||
|
|
||
|
var _templateObject = _taggedTemplateLiteral(['', ''], ['', '']);
|
||
|
|
||
|
function _taggedTemplateLiteral(strings, raw) { return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
|
||
|
|
||
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||
|
|
||
|
/**
|
||
|
* @class TemplateTag
|
||
|
* @classdesc Consumes a pipeline of composable transformer plugins and produces a template tag.
|
||
|
*/
|
||
|
var TemplateTag = function () {
|
||
|
/**
|
||
|
* constructs a template tag
|
||
|
* @constructs TemplateTag
|
||
|
* @param {...Object} [...transformers] - an array or arguments list of transformers
|
||
|
* @return {Function} - a template tag
|
||
|
*/
|
||
|
function TemplateTag() {
|
||
|
var _this = this;
|
||
|
|
||
|
for (var _len = arguments.length, transformers = Array(_len), _key = 0; _key < _len; _key++) {
|
||
|
transformers[_key] = arguments[_key];
|
||
|
}
|
||
|
|
||
|
_classCallCheck(this, TemplateTag);
|
||
|
|
||
|
this.tag = function (strings) {
|
||
|
for (var _len2 = arguments.length, expressions = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
|
||
|
expressions[_key2 - 1] = arguments[_key2];
|
||
|
}
|
||
|
|
||
|
if (typeof strings === 'function') {
|
||
|
// if the first argument passed is a function, assume it is a template tag and return
|
||
|
// an intermediary tag that processes the template using the aforementioned tag, passing the
|
||
|
// result to our tag
|
||
|
return _this.interimTag.bind(_this, strings);
|
||
|
}
|
||
|
|
||
|
if (typeof strings === 'string') {
|
||
|
// if the first argument passed is a string, just transform it
|
||
|
return _this.transformEndResult(strings);
|
||
|
}
|
||
|
|
||
|
// else, return a transformed end result of processing the template with our tag
|
||
|
strings = strings.map(_this.transformString.bind(_this));
|
||
|
return _this.transformEndResult(strings.reduce(_this.processSubstitutions.bind(_this, expressions)));
|
||
|
};
|
||
|
|
||
|
// if first argument is an array, extrude it as a list of transformers
|
||
|
if (transformers.length > 0 && Array.isArray(transformers[0])) {
|
||
|
transformers = transformers[0];
|
||
|
}
|
||
|
|
||
|
// if any transformers are functions, this means they are not initiated - automatically initiate them
|
||
|
this.transformers = transformers.map(function (transformer) {
|
||
|
return typeof transformer === 'function' ? transformer() : transformer;
|
||
|
});
|
||
|
|
||
|
// return an ES2015 template tag
|
||
|
return this.tag;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Applies all transformers to a template literal tagged with this method.
|
||
|
* If a function is passed as the first argument, assumes the function is a template tag
|
||
|
* and applies it to the template, returning a template tag.
|
||
|
* @param {(Function|String|Array<String>)} strings - Either a template tag or an array containing template strings separated by identifier
|
||
|
* @param {...*} ...expressions - Optional list of substitution values.
|
||
|
* @return {(String|Function)} - Either an intermediary tag function or the results of processing the template.
|
||
|
*/
|
||
|
|
||
|
|
||
|
_createClass(TemplateTag, [{
|
||
|
key: 'interimTag',
|
||
|
|
||
|
|
||
|
/**
|
||
|
* An intermediary template tag that receives a template tag and passes the result of calling the template with the received
|
||
|
* template tag to our own template tag.
|
||
|
* @param {Function} nextTag - the received template tag
|
||
|
* @param {Array<String>} template - the template to process
|
||
|
* @param {...*} ...substitutions - `substitutions` is an array of all substitutions in the template
|
||
|
* @return {*} - the final processed value
|
||
|
*/
|
||
|
value: function interimTag(previousTag, template) {
|
||
|
for (var _len3 = arguments.length, substitutions = Array(_len3 > 2 ? _len3 - 2 : 0), _key3 = 2; _key3 < _len3; _key3++) {
|
||
|
substitutions[_key3 - 2] = arguments[_key3];
|
||
|
}
|
||
|
|
||
|
return this.tag(_templateObject, previousTag.apply(undefined, [template].concat(substitutions)));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Performs bulk processing on the tagged template, transforming each substitution and then
|
||
|
* concatenating the resulting values into a string.
|
||
|
* @param {Array<*>} substitutions - an array of all remaining substitutions present in this template
|
||
|
* @param {String} resultSoFar - this iteration's result string so far
|
||
|
* @param {String} remainingPart - the template chunk after the current substitution
|
||
|
* @return {String} - the result of joining this iteration's processed substitution with the result
|
||
|
*/
|
||
|
|
||
|
}, {
|
||
|
key: 'processSubstitutions',
|
||
|
value: function processSubstitutions(substitutions, resultSoFar, remainingPart) {
|
||
|
var substitution = this.transformSubstitution(substitutions.shift(), resultSoFar);
|
||
|
return ''.concat(resultSoFar, substitution, remainingPart);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Iterate through each transformer, applying the transformer's `onString` method to the template
|
||
|
* strings before all substitutions are processed.
|
||
|
* @param {String} str - The input string
|
||
|
* @return {String} - The final results of processing each transformer
|
||
|
*/
|
||
|
|
||
|
}, {
|
||
|
key: 'transformString',
|
||
|
value: function transformString(str) {
|
||
|
var cb = function cb(res, transform) {
|
||
|
return transform.onString ? transform.onString(res) : res;
|
||
|
};
|
||
|
return this.transformers.reduce(cb, str);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* When a substitution is encountered, iterates through each transformer and applies the transformer's
|
||
|
* `onSubstitution` method to the substitution.
|
||
|
* @param {*} substitution - The current substitution
|
||
|
* @param {String} resultSoFar - The result up to and excluding this substitution.
|
||
|
* @return {*} - The final result of applying all substitution transformations.
|
||
|
*/
|
||
|
|
||
|
}, {
|
||
|
key: 'transformSubstitution',
|
||
|
value: function transformSubstitution(substitution, resultSoFar) {
|
||
|
var cb = function cb(res, transform) {
|
||
|
return transform.onSubstitution ? transform.onSubstitution(res, resultSoFar) : res;
|
||
|
};
|
||
|
return this.transformers.reduce(cb, substitution);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Iterates through each transformer, applying the transformer's `onEndResult` method to the
|
||
|
* template literal after all substitutions have finished processing.
|
||
|
* @param {String} endResult - The processed template, just before it is returned from the tag
|
||
|
* @return {String} - The final results of processing each transformer
|
||
|
*/
|
||
|
|
||
|
}, {
|
||
|
key: 'transformEndResult',
|
||
|
value: function transformEndResult(endResult) {
|
||
|
var cb = function cb(res, transform) {
|
||
|
return transform.onEndResult ? transform.onEndResult(res) : res;
|
||
|
};
|
||
|
return this.transformers.reduce(cb, endResult);
|
||
|
}
|
||
|
}]);
|
||
|
|
||
|
return TemplateTag;
|
||
|
}();
|
||
|
|
||
|
exports.default = TemplateTag;
|
||
|
module.exports = exports['default'];
|
||
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9UZW1wbGF0ZVRhZy9UZW1wbGF0ZVRhZy5qcyJdLCJuYW1lcyI6WyJUZW1wbGF0ZVRhZyIsInRyYW5zZm9ybWVycyIsInRhZyIsInN0cmluZ3MiLCJleHByZXNzaW9ucyIsImludGVyaW1UYWciLCJiaW5kIiwidHJhbnNmb3JtRW5kUmVzdWx0IiwibWFwIiwidHJhbnNmb3JtU3RyaW5nIiwicmVkdWNlIiwicHJvY2Vzc1N1YnN0aXR1dGlvbnMiLCJsZW5ndGgiLCJBcnJheSIsImlzQXJyYXkiLCJ0cmFuc2Zvcm1lciIsInByZXZpb3VzVGFnIiwidGVtcGxhdGUiLCJzdWJzdGl0dXRpb25zIiwicmVzdWx0U29GYXIiLCJyZW1haW5pbmdQYXJ0Iiwic3Vic3RpdHV0aW9uIiwidHJhbnNmb3JtU3Vic3RpdHV0aW9uIiwic2h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