How to use the slugify function from underscore.string

Find comprehensive JavaScript underscore.string.slugify code examples handpicked from public code repositorys.

underscore.string.slugify is a function in the Underscore.string library that converts a string to a URL-friendly slug by replacing spaces and special characters with dashes.

23
24
25
26
27
28
29
30
31
32
	filter: x => normalizeUrl(x)
}]);

const template = {
	appName: props.appName,
	slugifiedAppName: _s.slugify(props.appName),
	classifiedAppName: _s.classify(props.appName),
	githubUsername: props.githubUsername,
	repoUrl: `https://github.com/${props.githubUsername}/${props.slugifiedAppName}`,
	name: this.user.git.name(),
fork icon65
star icon715
watch icon0

57
58
59
60
61
62
63
64
65
66
    if (this.appName && Path.basename(oldRoot) !== this.appName) {
        this.destinationRoot(Path.join(oldRoot, this.appName));
        //Reset the defaults
        Util.setDefaults(this);
    }
    this.slugAppName = _.slugify(this.appName);
},
writing: {
    app: function () {
        var self = this;
fork icon34
star icon70
watch icon12

How does underscore.string.slugify work?

The underscore.string.slugify function is used to convert a string into a valid slug by removing special characters, replacing spaces with hyphens, and converting all characters to lower case.

38
39
40
41
42
43
44
45
46
	);
},

bower: function() {
	var bowerJson = {
		name: _s.slugify(this.appname),
		private: true,
		dependencies: {}
	};
fork icon2
star icon5
watch icon0

125
126
127
128
129
130
131
132
133
134
cli.prompt([
  {
    type: 'input',
    name: 'id',
    message: 'Choose an ID for this configuration',
    default: _s.slugify(answers.issuerName),
    value: flags['id'] || flags.i
  }
], function (issId) {
  request({
fork icon11
star icon3
watch icon14

Ai Example

1
2
3
4
5
6
7
const _ = require("underscore.string");

const originalString = "This is a sample string!";
const slugifiedString = _.slugify(originalString);

console.log(slugifiedString);
// Output: this-is-a-sample-string

124
125
126
127
128
129
130
131
132
133
cli.prompt([
  {
    type: 'input',
    name: 'id',
    message: 'Choose an ID for this configuration',
    default: _s.slugify(answers.name),
    value: flags['id'] || flags.i,
    filter: function (input) {
      return input && _s.slugify(input)
    },
fork icon11
star icon3
watch icon14

183
184
185
186
187
188
189
190
191
192
}

if (type == null) {
  type = 'meetup'
}
if (post.type === 'academy') post.slug = _s.slugify(post.what.split(':')[0])
else post.slug = _s.slugify(post.what)
if (!event_id) {
  const slugs = await Events.query((qb) =>
    qb.where('slug', 'LIKE', `${post.slug}%`)
fork icon5
star icon3
watch icon8

+ 121 other calls in file

54
55
56
57
58
59
60
61
62
63
	this.deployDirectory = this.answers.deployDirectory;

} else {

	this.appname = this.options.name;
	this.appslug = _s.slugify(this.options.name);
	this.stash = '';
	this.deployDirectory = this.options.deployDir;

}
fork icon6
star icon2
watch icon7

19
20
21
22
23
24
25
26
27
28
29
30
  // If appName is not given, use the current directory
  if (appName === undefined) {
    appName = getBaseDir()
  }


  return _.slugify(_.humanize(appName))
}


/**
 * kintone API へのリクエストメソッドをラップして以下の挙動を追加する
fork icon0
star icon2
watch icon3

+ 94 other calls in file

22
23
24
25
26
27
28
29
30
31
init() {
	return this.prompt([{
		name: 'repoName',
		message: 'What do you want to name your module?',
		default: this.appname.replace(/\s/g, '-'),
		filter: x => _s.slugify(x)
	}, {
		name: 'moduleDescription',
		message: 'What is your module description?',
		default: `My ${superb()} module`
fork icon0
star icon2
watch icon1

+ 8 other calls in file

10
11
12
13
14
15
16
17
18
19
20
module.exports = {
   getDefaultAppName: (appName) => {
      if (appName == undefined) {
         appName = path.basename(process.cwd());
      }
      return _.slugify(_.humanize(appName));
   },


   updatePackageConfig: (options, next) => {
      const fileName = path.resolve(options.appName, './package.json');
fork icon0
star icon1
watch icon1

+ 33 other calls in file

31
32
33
34
35
36
37
38
39
40
}
init() {
	return this.prompt([{
		name: 'moduleName',
		message: 'What do you want to name your module?',
		default: _s.slugify(this.appname),
		filter: x => utils.slugifyPackageName(x)
	}, {
		name: 'moduleDescription',
		message: 'What is your module description?',
fork icon0
star icon1
watch icon0

+ 4 other calls in file

188
189
190
191
192
193
194
195
196
197
        this.templatePath('gitattributes'),
        this.destinationPath('.gitattributes'));
},
bower: function() {
    var bowerJson = {
        name: _s.slugify(this.appName),
        version: this.appVersion,
        license: this.appLicense,
        private: true,
        dependencies: {}
fork icon0
star icon1
watch icon2

+ 2 other calls in file

200
201
202
203
204
205
206
207
208
209
210
 * @param  {String} path
 * @return {String}
 */
let getComponentStyleName = (path) => {
  let fileName = path.split('/').pop().toLowerCase();
  return _.slugify(_.humanize(fileName)) + '-component';
};


/**
 * Get a js friendly application name
fork icon0
star icon1
watch icon0

30
31
32
33
34
35
36
37
38
39
40
    message: 'Name to deploy as:',
    default: this.appname
  }];


  this.prompt(prompts, function (props) {
    this.deployedName = s.slugify(props.deployedName).split('-').join('');
    done();
  }.bind(this));
};

fork icon0
star icon0
watch icon1

80
81
82
83
84
85
86
87
88
89
    default: false
}];

this.prompt(prompts, function(props) {
    this.componentNameRaw = props.componentName.trim();
    this.componentName = str.slugify(this.componentNameRaw);
    this.componentClassName = str.classify(this.componentNameRaw);
    this.description = props.description.trim();
    this.license = props.license;
    this.contrib = props.contrib;
fork icon0
star icon0
watch icon0

12
13
14
15
16
17
18
19
20
21
22
  try {
    this.appname = require(path.join(process.cwd(), 'bower.json')).name;
  } catch (e) {
    this.appname = path.basename(process.cwd());
  }
  this.appname = s.slugify(this.appname);
  this.filters = this.config.get('filters') || {};
};


util.inherits(Generator, yeoman.generators.NamedBase);
fork icon0
star icon0
watch icon0