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.

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');
fork icon14
star icon70
watch icon0

+ 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);
fork icon0
star icon7
watch icon0

+ 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;
fork icon7
star icon6
watch icon0

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;
fork icon7
star icon6
watch icon0

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.
fork icon6
star icon21
watch icon0

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);
fork icon3
star icon18
watch icon0

+ 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;
fork icon0
star icon6
watch icon0

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) {
fork icon1
star icon4
watch icon0

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));
fork icon2
star icon2
watch icon0

+ 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.
fork icon448
star icon0
watch icon97

+ 3 other calls in file

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;
},
fork icon10
star icon116
watch icon10

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
fork icon1
star icon0
watch icon0

+ 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) {
fork icon0
star icon0
watch icon1

+ 5 other calls in file

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,
fork icon0
star icon0
watch icon2

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()
fork icon0
star icon0
watch icon1

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;
fork icon0
star icon0
watch icon0