UNPKG

flooent

Version:

Fluent interface to provide an expressive syntax for common manipulations.

197 lines (196 loc) 5.74 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const Str = __importStar(require("../string")); const override = [ "replace", "replaceAll", "trim", "trimEnd", "trimStart", "substr", "substring", "concat", "repeat", "slice", "toLocaleLowerCase", "toLocaleUpperCase", "toLowerCase", "toUpperCase", "charAt", ]; class Stringable extends String { constructor(value) { super(value); override.forEach((name) => { this[name] = (...args) => { return new this.constructor(super[name](...args)); }; }); } /** * Returns the remaining text after the first occurrence of the given value. * If the value does not exist in the string, the entire string is returned unchanged. */ after(part) { return Str.after(this, part); } /** * Returns the remaining text after the last occurrence of the given value. * If the value does not exist in the string, the entire string is returned unchanged. */ afterLast(part) { return Str.afterLast(this, part); } /** * Returns the text before the first occurrence of the given value. * If the value does not exist in the string, the entire string is returned unchanged. */ before(part) { return Str.before(this, part); } /** * Returns the text before the last occurrence of the given value. * If the value does not exist in the string, the entire string is returned unchanged. */ beforeLast(part) { return Str.beforeLast(this, part); } /** * Executes the callback if first given value evaluates to true. Result will get transformed back into a flooent string if it is a raw string. */ when(comparison, then) { const isBoolean = typeof comparison === "boolean"; if (isBoolean && !comparison) { return this; } if (!isBoolean && !comparison(this)) { return this; } return this.pipe(then); } /** * Executes the callback if string is empty. Result will get transformed back into a flooent string if it is a raw string. */ whenEmpty(then) { return this.when(this.valueOf() === "", then); } pipe(callback) { const result = callback(this); if (result instanceof Stringable || typeof result !== 'string') return result; return new this.constructor(result); } /** * Tap into the chain without modifying the string. */ tap(fn) { fn(this); return this; } /** * Wraps a string with the given value. */ wrap(start, end = start) { return new this.constructor(Str.wrap(this.valueOf(), start, end)); } /** * Unwraps a string with the given value. */ unwrap(start, end = start) { return Str.unwrap(this, start, end); } /** * Alias for `concat`. Appends the given value to string. */ append(part) { return Str.append(this, part); } /** * Prepends the given value to string. */ prepend(part) { return new this.constructor(Str.prepend(this.valueOf(), part)); } /** * Appends the given value only if string doesn't already end with it. */ endWith(part) { return Str.endWith(this, part); } /** * Prepends the given value only if string doesn't already start with it. */ startWith(part) { return new this.constructor(Str.startWith(this, part)); } /** * Truncates text to given length and appends second argument if string got truncated. */ limit(n, append = "...") { return Str.limit(this, n, append); } /** * Turns the string into title case. */ title() { return new this.constructor(Str.title(this)); } /** * Turns the string into kebab case. */ kebab() { return this.snake('-'); } /** * Turns the string into snake case. */ snake(replacement = '_') { const words = Str.snake(this, replacement); return new this.constructor(words); } /** * Turns the string into studly case. */ studly() { const words = Str.studly(this); return new this.constructor(words); } /** * Turns the string into camel case. */ camel() { return new this.constructor(Str.camel(this)); } /** * Capitalizes the first character. */ capitalize() { return new this.constructor(Str.capitalize(this)); } /** * Turns the string into URI conform slug. */ slug(replacement = "-") { return new this.constructor(Str.slug(this, replacement)); } } exports.default = Stringable;