Last active
August 29, 2015 14:11
-
-
Save shanelau/cd1b77ee42f0d8044b80 to your computer and use it in GitHub Desktop.
不重复 url 生成
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (c) 2014 Meizu bigertech, All rights reserved. | |
* http://www.bigertech.com/ | |
* @author liuxing | |
* @date 15/3/19 | |
* @description | |
* | |
*/ | |
var unidecode = require('unidecode'); | |
var safeString = function (string) { | |
string = string.trim(); | |
// Remove non ascii characters | |
string = unidecode(string)+''; | |
// Remove URL reserved chars: `:/?#[]@!$&'()*+,;=` as well as `\%<>|^~£"` | |
string = string | |
.replace(/[:\/\?#\[\]@!$&'()*+,;=\\%<>\|\^~£"]/g, '') | |
// Replace dots and spaces with a dash | |
.replace(/(\s|\.)/g, '-') | |
// Convert 2 or more dashes into a single dash | |
.replace(/-+/g, '-') | |
// Make the whole thing lowercase | |
.toLowerCase(); | |
return string; | |
} | |
/** | |
* 更具模型生成域名, | |
* @param Model 模型 | |
* @param title 标题或者name | |
*/ | |
var generateSlug = function (Model, title) { | |
var slug, | |
slugTryCount = 1, | |
// Look for a matching slug, append an incrementing number if so | |
checkIfSlugExists, longSlug; | |
checkIfSlugExists = function (slugToFind) { | |
var args = {domain: slugToFind}; | |
slugToFind; | |
return Model.findOne() | |
.where(args) | |
.then(function (found) { | |
var trimSpace; | |
if (!found) { | |
return slugToFind; | |
} | |
slugTryCount += 1; | |
// If we shortened, go back to the full version and try again | |
if (slugTryCount === 2 && longSlug) { | |
slugToFind = longSlug; | |
longSlug = null; | |
slugTryCount = 1; | |
return checkIfSlugExists(slugToFind); | |
} | |
// If this is the first time through, add the hyphen | |
if (slugTryCount === 2) { | |
slugToFind += '-'; | |
} else { | |
// Otherwise, trim the number off the end | |
trimSpace = -(String(slugTryCount - 1).length); | |
slugToFind = slugToFind.slice(0, trimSpace); | |
} | |
slugToFind += slugTryCount; | |
return checkIfSlugExists(slugToFind); | |
}); | |
}; | |
slug = safeString(title); | |
// Remove trailing hyphen | |
slug = slug.charAt(slug.length - 1) === '-' ? slug.substr(0, slug.length - 1) : slug; | |
// Test for duplicate slugs. | |
return checkIfSlugExists(slug); | |
} | |
module.exports = { | |
generateSlug: generateSlug | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
safeString: function (string) { | |
string = string.trim(); | |
// Remove non ascii characters | |
string = unidecode(string); | |
// Remove URL reserved chars: `:/?#[]@!$&'()*+,;=` as well as `\%<>|^~£"` | |
string = string.replace(/[:\/\?#\[\]@!$&'()*+,;=\\%<>\|\^~£"]/g, '') | |
// Replace dots and spaces with a dash | |
.replace(/(\s|\.)/g, '-') | |
// Convert 2 or more dashes into a single dash | |
.replace(/-+/g, '-') | |
// Make the whole thing lowercase | |
.toLowerCase(); | |
return string; | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment