Support toggling the removal of stop words in input preset handling (#3320)

Add `data-input-preset-remove-stop-words="false"` to an element being handled with the input preset JS to disable the removal of stop words from slug generation. Credit to @tim0991.
This commit is contained in:
tim0991 2017-12-26 07:56:58 +08:00 committed by Luke Towers
parent 0d0be9d0e5
commit 06780f5123

View File

@ -12,6 +12,7 @@
* url, file, slug, camel. * url, file, slug, camel.
* - data-input-preset-prefix-input: optional, prefixes the converted value with the value found * - data-input-preset-prefix-input: optional, prefixes the converted value with the value found
* in the supplied input element using a CSS selector. * in the supplied input element using a CSS selector.
* - data-input-preset-remove-words: optional, use removeList to filter stop words of source string.
* *
* Example: <input type="text" id="name" value=""/> * Example: <input type="text" id="name" value=""/>
* <input type="text" * <input type="text"
@ -190,41 +191,7 @@
} }
} }
function toCamel(slug, numChars) {
Downcoder.Initialize()
slug = slug.replace(Downcoder.regex, function(m) {
return Downcoder.map[m]
})
var regex = new RegExp('\\b(' + removeList.join('|') + ')\\b', 'gi')
slug = slug.replace(regex, '')
slug = slug.toLowerCase()
slug = slug.replace(/(\b|-)\w/g, function(m) {
return m.toUpperCase();
});
slug = slug.replace(/[^-\w\s]/g, '')
slug = slug.replace(/^\s+|\s+$/g, '')
slug = slug.replace(/[-\s]+/g, '')
slug = slug.substr(0, 1).toLowerCase() + slug.substr(1);
return slug.substring(0, numChars)
}
function slugify(slug, numChars) {
Downcoder.Initialize()
slug = slug.replace(Downcoder.regex, function(m) {
return Downcoder.map[m]
})
var regex = new RegExp('\\b(' + removeList.join('|') + ')\\b', 'gi')
slug = slug.replace(regex, '')
slug = slug.replace(/[^-\w\s]/g, '')
slug = slug.replace(/^\s+|\s+$/g, '')
slug = slug.replace(/[-\s]+/g, '-')
slug = slug.toLowerCase()
return slug.substring(0, numChars)
}
var InputPreset = function (element, options) { var InputPreset = function (element, options) {
var $el = this.$el = $(element) var $el = this.$el = $(element)
@ -277,7 +244,7 @@
} }
InputPreset.prototype.formatNamespace = function() { InputPreset.prototype.formatNamespace = function() {
var value = toCamel(this.$src.val()) var value = this.toCamel(this.$src.val())
return value.substr(0, 1).toUpperCase() + value.substr(1) return value.substr(0, 1).toUpperCase() + value.substr(1)
} }
@ -291,10 +258,10 @@
} }
if (this.options.inputPresetType == 'camel') { if (this.options.inputPresetType == 'camel') {
var value = toCamel(this.$src.val()) var value = this.toCamel(this.$src.val())
} }
else { else {
var value = slugify(this.$src.val()) var value = this.slugify(this.$src.val())
} }
if (this.options.inputPresetType == 'url') { if (this.options.inputPresetType == 'url') {
@ -304,11 +271,55 @@
return value.replace(/\s/gi, "-") return value.replace(/\s/gi, "-")
} }
InputPreset.prototype.toCamel = function(slug, numChars) {
Downcoder.Initialize()
slug = slug.replace(Downcoder.regex, function(m) {
return Downcoder.map[m]
})
slug = this.removeStopWords(slug);
slug = slug.toLowerCase()
slug = slug.replace(/(\b|-)\w/g, function(m) {
return m.toUpperCase();
});
slug = slug.replace(/[^-\w\s]/g, '')
slug = slug.replace(/^\s+|\s+$/g, '')
slug = slug.replace(/[-\s]+/g, '')
slug = slug.substr(0, 1).toLowerCase() + slug.substr(1);
return slug.substring(0, numChars)
}
InputPreset.prototype.slugify = function(slug, numChars) {
Downcoder.Initialize()
slug = slug.replace(Downcoder.regex, function(m) {
return Downcoder.map[m]
})
slug = this.removeStopWords(slug);
slug = slug.replace(/[^-\w\s]/g, '')
slug = slug.replace(/^\s+|\s+$/g, '')
slug = slug.replace(/[-\s]+/g, '-')
slug = slug.toLowerCase()
return slug.substring(0, numChars)
}
InputPreset.prototype.removeStopWords = function(str) {
if (this.options.inputPresetRemoveWords) {
var regex = new RegExp('\\b(' + removeList.join('|') + ')\\b', 'gi')
str = str.replace(regex, '')
}
return str;
}
InputPreset.DEFAULTS = { InputPreset.DEFAULTS = {
inputPreset: '', inputPreset: '',
inputPresetType: 'slug', inputPresetType: 'slug',
inputPresetClosestParent: undefined, inputPresetClosestParent: undefined,
inputPresetPrefixInput: undefined inputPresetPrefixInput: undefined,
inputPresetRemoveWords: true
} }
// INPUT CONVERTER PLUGIN DEFINITION // INPUT CONVERTER PLUGIN DEFINITION