UNPKG

@rashedmakkouk/dev-utils

Version:
31 lines (30 loc) 1.02 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /** Utilities */ const isNumber_1 = __importDefault(require("lodash/isNumber")); const isString_1 = __importDefault(require("lodash/isString")); /** * Removes leading and trailing spaces and replaces multiple whitespace, tabs * and newlines with one space. * * @returns Trimmed text. */ function trimWhitespace(text) { if ((0, isNumber_1.default)(text)) { return text; } else if (!text || !(0, isString_1.default)(text)) { return ''; } text = text /** Removes leading and trailing spaces. */ .trim() /** Replaces multiple 'whitespace', 'tab' and 'newline' with one space. */ .replace(/[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, ' ') .trim(); return !text.replace(/\s/g, '').length ? '' : text; } exports.default = trimWhitespace;