@newdash/newdash
Version:
javascript/typescript utility library
45 lines (44 loc) • 1.7 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.trim = void 0;
const baseToString_1 = __importDefault(require("./.internal/baseToString"));
const castSlice_1 = __importDefault(require("./.internal/castSlice"));
const charsEndIndex_1 = __importDefault(require("./.internal/charsEndIndex"));
const charsStartIndex_1 = __importDefault(require("./.internal/charsStartIndex"));
const stringToArray_1 = __importDefault(require("./.internal/stringToArray"));
const toString_1 = __importDefault(require("./toString"));
/**
* Removes leading and trailing whitespace or specified characters from `string`.
*
* @since 5.6.0
* @category String
* @param str The string to trim.
* @param chars The characters to trim.
* @returns Returns the trimmed string.
* @see [[trimEnd]],[[trimStart]]
* @example
*
* ```js
* trim(' abc ')
* // => 'abc'
*
* trim('-_-abc-_-', '_-')
* // => 'abc'
* ```
*/
function trim(str, chars, guard) {
const string = (0, toString_1.default)(str);
if (string && (guard || chars === undefined)) {
return string.trim();
}
if (!string || !(chars = (0, baseToString_1.default)(chars))) {
return string;
}
const strSymbols = (0, stringToArray_1.default)(string), chrSymbols = (0, stringToArray_1.default)(chars), start = (0, charsStartIndex_1.default)(strSymbols, chrSymbols), end = (0, charsEndIndex_1.default)(strSymbols, chrSymbols) + 1;
return (0, castSlice_1.default)(strSymbols, start, end).join("");
}
exports.trim = trim;
exports.default = trim;