UNPKG

awesome-string

Version:

The ultimate JavaScript string library

160 lines (147 loc) 3.78 kB
import functions from 'functions'; /** * The chain wrapper constructor. * * @ignore * @param {string} subject The string to be wrapped. * @param {boolean} [explicitChain=false] A boolean that indicates if the chain sequence is explicit or implicit. * @return {ChainWrapper} Returns a new instance of `ChainWrapper` * @constructor */ function ChainWrapper(subject, explicitChain) { this._wrappedValue = subject; this._explicitChain = explicitChain; } /** * Unwraps the chain sequence wrapped value. * * @memberof Chain * @since 1.0.0 * @function __proto__value * @return {*} Returns the unwrapped value. * @example * v * .chain('Hello world') * .replace('Hello', 'Hi') * .lowerCase() * .slugify() * .value() * // => 'hi-world' * * v(' Space travel ') * .trim() * .truncate(8) * .value() * // => 'Space...' */ ChainWrapper.prototype.value = function() { return this._wrappedValue; }; /** * Override the default object valueOf(). * * @ignore * @return {*} Returns the wrapped value. */ ChainWrapper.prototype.valueOf = function() { return this.value(); }; /** * Returns the wrapped value to be used in JSON.stringify(). * * @ignore * @return {*} Returns the wrapped value. */ ChainWrapper.prototype.toJSON = function() { return this.value(); }; /** * Returns the string representation of the wrapped value. * * @ignore * @return {string} Returns the string representation. */ ChainWrapper.prototype.toString = function() { return String(this.value()); }; /** * Creates a new chain object that enables <i>explicit</i> chain sequences. * Use `as.prototype.value()` to unwrap the result. <br/> * Does not modify the wrapped value. * * @memberof Chain * @since 1.0.0 * @function __proto__chain * @return {Object} Returns the wrapper in <i>explicit</i> mode. * @example * v('Back to School') * .chain() * .lowerCase() * .words() * .value() * // => ['back', 'to', 'school'] * * v(" Back to School ") * .chain() * .trim() * .truncate(7) * .value() * // => 'Back...' */ ChainWrapper.prototype.chain = function() { return new ChainWrapper(this._wrappedValue, true); }; /** * Modifies the wrapped value with the invocation result of `changer` function. The current wrapped value is the * argument of `changer` invocation. * * @memberof Chain * @since 1.0.0 * @function __proto__thru * @param {Function} changer The function to invoke. * @return {Object} Returns the new wrapper that wraps the invocation result of `changer`. * @example * v * .chain('sun is shining') * .words() * .thru(function(words) { * return words[0]; * }) * .value() * // => 'sun' * */ ChainWrapper.prototype.thru = function(changer) { if (typeof changer === 'function') { return new ChainWrapper(changer(this._wrappedValue), this._explicitChain); } return this; }; /** * A boolean that indicates if the chain sequence is explicit or implicit. * @ignore * @type {boolean} * @private */ ChainWrapper.prototype._explicitChain = true; /** * Make a AwesomeString function chainable. * * @ignore * @param {Function} functionInstance The function to make chainable * @return {Function} Returns the chainable function */ function makeFunctionChainable(functionInstance) { return function(...args) { const result = functionInstance(this._wrappedValue, ...args); if (this._explicitChain || typeof result === 'string') { return new ChainWrapper(result, this._explicitChain); } else { return result; } }; } Object.keys(functions).forEach(function(name) { ChainWrapper.prototype[name] = makeFunctionChainable(functions[name]); }); export default ChainWrapper;