Skip to content

Instantly share code, notes, and snippets.

@evilive3000
Last active October 5, 2015 11:17
Show Gist options
  • Save evilive3000/964e826d073c24705fc7 to your computer and use it in GitHub Desktop.
Save evilive3000/964e826d073c24705fc7 to your computer and use it in GitHub Desktop.
var _ = require('lodash');
var rW = "0-9a-zа-я",
rSpace = /[\s_]+/g,
rFeat = rFeatSoft = new RegExp('[^'+rW+']((?:featuring|feat|ft|&|vs|при уч|п\.у\.)[^'+rW+']+)', 'g'),
rU = /[ù-üийі]/g,
rE = /[è-ëёеє]/g,
rI = /[ì-ï]/g,
rA = /[à-æ]/g,
rO = /[ò-ö]/g,
rHyphen = /[\u2010-\u2014\u2043\uFE63]/g,
rDots = /\.+/g,
trimsym = '[-.ツ•٠·۰˙°*۩†๑٩۶ღ~!?#\'\"\\s]',
rTrim = new RegExp("^"+trimsym+"+|"+trimsym+"+$", 'g'),
rCapDot = /([A-ZА-Я])\./g,// надо подумать над п.р.о.п.и.с.н.ы.м.и. буквами
braces = [
'\\(+([^\\)]*)\\)+',
'\\[+([^\\]]*)\\]+',
'\\{+([^\\}]*)\\}+',
],
rBraces = new RegExp("(" +braces.join("|") + ")", 'g'),
rBraceOnly = new RegExp('^(?:'+braces.join('|')+')$'),
clearRanges = [
'[\u0080-\u00BF]',
'[\u0300-\u036F]',
'[\u2100-\u2BFF]',
'\uD83C[\uDF00-\uDFFF]', // U+1F300 to U+1F3FF
'\uD83D[\uDC00-\uDFFF]', // U+1F400 to U+1F7FF
],
rClear = new RegExp(clearRanges.join('|'), 'g'),
rBreak = new RegExp('[-:;#&!?,.\\s]+'),
rSplit = /[-&\s;,.]+/,
rThe = /(\bthe\s|\sthe\b)/g,
rJoinedComma = /([а-яa-z0-9]),([a-zа-я])/g,
rPunc = /[‘’′‵'`´;]|,(\s)/g;
function _simpChars(str){
return str
.replace(rU, 'u')
.replace(rE, 'e')
.replace(rI, 'i')
.replace(rA, 'a')
.replace(rO, 'o')
.replace(rHyphen, '-')
}
function stripAlbum(str){
return _simpChars(str.replace(rCapDot, '$1').toLowerCase())
.replace(rBraceOnly, '$1$2$3')
//.replace(rBraces, ' ')
.replace(rJoinedComma, '$1 $2')
.replace(rPunc, '$1')
.replace(/\s?\((.+)\)/g, function(match, group){
return group.split(rBreak)
.map(function(w){ return w.substr(0,4) })
.join(" ").toUpperCase();
})
.split(rBreak).join(' ')
.replace(rClear, ' ')
.replace(rSpace, ' ')
.replace(rDots, '.')
.replace(rTrim, '');
}
function stripTitle(str){
return _simpChars(str.replace(rCapDot, '$1').toLowerCase())
.replace(rBraceOnly, '$1$2$3')
//.replace(rBraces, ' ')
.replace(rJoinedComma, '$1 $2')
.replace(rPunc, '$1')
.replace(/\s?\((.+)\)/g, function(match, group){
return group.split(rBreak)
.map(function(w){ return w.substr(0,1) })
.join("").toUpperCase();
})
.split(rBreak).join(' ')
.replace(rClear, ' ')
.replace(rSpace, ' ')
.replace(rDots, '.')
.replace(rTrim, '');
}
module.exports = {
stripTitle: stripTitle,
stripAlbum: stripAlbum
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment