'use strict';

var endsWith  = require('es5-ext/string/#/ends-with')
  , value     = require('es5-ext/object/valid-value')
  , unidecode = require('unidecode')

  , nonAsciiRe = /[\0-\/:-@\[-`{-\uffff]/g
  , dashRe = /-{2,}/g;

module.exports = function (name) {
	name = unidecode(String(value(name))).replace(nonAsciiRe, '-').replace(dashRe, '-').toLowerCase();
	if (endsWith.call(name, '-')) name = name.slice(0, -1);
	if (!name) name = 'x';
	return name;
};
