How to use the trim function from underscore.string
Find comprehensive JavaScript underscore.string.trim code examples handpicked from public code repositorys.
In the Underscore.string library for JavaScript, the _.trim function is used to remove whitespace from the beginning and end of a string.
GitHub: PastVu/pastvu
344 345 346 347 348 349 350 351 352 353
} return function (txt) { let result = txt; result = _s.trim(result); //Обрезаем концы //Заменяем ссылку на фото на диез-ссылку #xxx //Например, http://domain.com/p/123456 -> #123456 result = result.replace(new RegExp(`(\\b)(?:https?://)?(?:www.)?${host}/p/(\\d{1,8})/?(?=[${trailingChars}]|$)`, 'gi'), '$1#$2');
+ 6 other calls in file
75 76 77 78 79 80 81 82 83
var start = new Date(); var tags = []; var cleanMethod = _s.strLeftBack(this.url, '?'); cleanMethod = _s.replaceAll(cleanMethod, '/', '_'); cleanMethod = _s.trim(cleanMethod, '_'); cleanMethod = this.method + '.' + cleanMethod; cleanMethod = cleanMethod.toLowerCase(); tags.push('endpoint:' + cleanMethod);
+ 2 other calls in file
How does underscore.string.trim work?
_.trim
is a function in the Underscore.string library for JavaScript that removes whitespace from the beginning and end of a string.
When _.trim
is called with a string as input, it performs the following operations:
- It removes any whitespace characters (spaces, tabs, newlines, etc.) from the beginning of the input string.
- It removes any whitespace characters from the end of the input string.
- It returns the resulting string with the whitespace removed.
By using _.trim
, developers can easily remove leading and trailing whitespace from strings, which can be useful for validating user input or preparing data for display. Note that _.trim
only removes whitespace from the beginning and end of strings, so it may not be suitable for all types of string manipulation.
71 72 73 74 75 76 77 78 79 80 81
*/ function parseMedicationDetails(medicationStr){ if(medicationStr && medicationStr.length > 0){ var split = medicationStr.split('^'); return {ien: split[0].substring(1, split[0].length), name: _str.trim(split[1])}; } return null; } exports.parseMedicationDetails = parseMedicationDetails;
6 7 8 9 10 11 12 13 14 15
if(index == 0){ user.duz = currentValue; }else if(index == 1){ user.name = currentValue; }else if(index == 2){ user.role = _str.trim(_str.ltrim(currentValue, '-')); } } }); return user;
Ai Example
1 2 3 4 5 6 7
const _ = require("underscore.string"); // Removing whitespace from the beginning and end of a string const string = " Hello, world! "; const trimmedString = _.trim(string); console.log(trimmedString); // Outputs: "Hello, world!"
In this example, we're using _.trim to remove leading and trailing whitespace from the string " Hello, world! ". The resulting string, "Hello, world!", has the same content as the original string but with the whitespace removed. Note that in this example, we're passing the string directly to _.trim, which removes the leading and trailing whitespace without requiring any additional code.
609 610 611 612 613 614 615 616 617 618
* * @param {String} str * @return {String} */ function classnameize(str) { return _s.trim(str).replace(/\s+/g, '-'); } /** * Return path of CSS file.
GitHub: maboiteaspam/ssh2-utils
277 278 279 280 281 282 283 284 285
stream.stderr.on('data', function(data){ debug('sudoExec stderr %s', _s.trim(''+data)) }); stream.on('data', function(data){ debug('sudoExec stdout %s', _s.trim(''+data)) }); if(done) done(null, stream);
+ 2 other calls in file
975 976 977 978 979 980 981 982 983 984 985
s.escapeRegExp = require('./helper/escapeRegExp'); s.wrap = require('./wrap'); s.map = require('./map'); // Aliases s.strip = s.trim; s.lstrip = s.ltrim; s.rstrip = s.rtrim; s.center = s.lrpad; s.rjust = s.lpad;
GitHub: dnnsmnstrr/workflows
5 6 7 8 9 10 11 12 13 14 15
const Fuse = require('fuse.js'); const Promise = require('bluebird'); const fs = Promise.promisifyAll(require('fs')); const moment = require('moment'); const srcFolder = process.env.NOTES_PATH; const query = s.trim(_.last(process.argv)); const log = require('./log'); const grep = require('./grep'); if (!srcFolder) {
65 66 67 68 69 70 71 72 73 74 75 76
exports.humanize = function(value, options, callback){ callback(null, str.humanize(String(value))); }; exports.trim = function(value, options, callback){ callback(null, str.trim(String(value), typeof options[0] === 'string' ? options[0] : undefined)); }; exports.ltrim = function(value, options, callback){ callback(null, str.ltrim(String(value), typeof options[0] === 'string' ? options[0] : undefined));
+ 2 other calls in file
23 24 25 26 27 28 29 30 31 32 33
const u = use_underscore || false; str = str.replace(/\//g, ' ').trim(); if (u) { return _s.underscored(str); } else { return _s.trim(_s.dasherize(str), '-'); } } // Clean object strings.
+ 3 other calls in file
GitHub: restberry/restberry
16 17 18 19 20 21 22 23 24 25
if (this.name) { var firstName = this.name.first; if (!firstName) firstName = ''; var lastName = this.name.last; if (!lastName) lastName = ''; name = _s.trim(firstName + ' ' + lastName); } if (!name) name = _.first(this.email.split('@')); return name; },
GitHub: khsmaxim/sef-1
104 105 106 107 108 109 110 111 112 113 114 115
var headerAndValue = _splitIntoTwoParts(line, ':'); if (!headerAndValue) { throw new InvalidRequestError('Host line must have format: [Host]: [Value]', line); } return _s.trim(headerAndValue[1]); } function _parseHeadersLines(lines) { // TODO: add check for duplicate headers
+ 27 other calls in file
200 201 202 203 204 205 206 207 208 209 210 211
if (u) { return _s.underscored(field); } else { return _s.trim(_s.dasherize(field), '-'); } } function markdown_extract_metadata_fields(obj) {
+ 5 other calls in file
GitHub: hokkoo/dianli
220 221 222 223 224 225 226 227 228 229
offset: 10, count: 1, pages: 1, } }; search.query = _.trim(search.query); if (!search.query || search.query == 'undefined' || search.query == 'null') search.query = ''; if (!search.page) search.page = { cur: 1, offset: 10,
11 12 13 14 15 16 17 18 19 20 21
const path = require('path'); // Function fo generate object path function generateObjectPath(filePath) { return _string.camelize( _string.trim( filePath.replace( path.extname(filePath), '') .replace(/([^a-zA-Z0-9//])/g, "-") .toLowerCase()
GitHub: simhyr/ircbot
197 198 199 200 201 202 203 204 205 206 207 208
socket.end(); }); }; function parseIRCMessage(message) { message = _str.trim(message); // :<botname>!<botname@botaddress> <command> <parameterlist>:<message> //(?:) => does not form a capture group to not include ' ' into group const regex = /:(.+)!(\S*)[ ]([a-z]+)[ ](?:(\S+)[ ]){0,1}:(.*)/i;
underscore.string.slugify is the most popular function in underscore.string (323 examples)