Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSHint to project, and fix errors. #56

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"bitwise": true,
"curly": true,
"eqeqeq": true,
"indent": 4,
"newcap": true,
"noarg": true,
"undef": true,
"unused": true,
"globals": {
"define": true,
"module": true,
"require": true
}
}
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
before_install: "npm install npm -g"
before_script: "npm install --dev"
script: "npm test"
script:
- "npm run lint"
- "npm test"
language: node_js
node_js:
- 0.8
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
, "engines": {"node": ">= 0.4.x"}
, "keywords": ["slugify", "slug", "string", "utf8", "utf-8", "unicode", "url"]
, "scripts": {
"lint": "./node_modules/.bin/jshint slug.js",
"test": "./node_modules/.bin/mocha ./test/*.test.* --require should --reporter spec --colors --compilers coffee:coffee-script/register"}
, "dependencies": {
"unicode": ">= 0.3.1"}
, "devDependencies": {
"jshint": "~2.8.0",
"mocha": "~1.17.1",
"should": "~3.1.2",
"coffee-script": "~1.7.1"}
Expand Down
99 changes: 52 additions & 47 deletions slug.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,51 @@
// lazy require symbols table
var _symbols, removelist;
function symbols(code) {
if (_symbols) return _symbols[code];
if (_symbols) {
return _symbols[code];
}
_symbols = require('unicode/category/So');
removelist = ['sign','cross','of','symbol','staff','hand','black','white']
.map(function (word) {return new RegExp(word, 'gi')});
.map(function (word) {return new RegExp(word, 'gi');});
return _symbols[code];
}

function multicharat(index, string, multicharmap) {
for (var property in multicharmap) {
if (!multicharmap.hasOwnProperty(property)) {
continue;
}
if (string.indexOf(property) === index) {
return property;
}
}
}

function slug(string, opts) {
string = string.toString();
if ('string' === typeof opts)
if ('string' === typeof opts) {
opts = {replacement:opts};
}
opts = opts || {};
opts.mode = opts.mode || slug.defaults.mode;
var defaults = slug.defaults.modes[opts.mode];
var keys = ['replacement','multicharmap','charmap','remove','lower'];
for (var key, i = 0, l = keys.length; i < l; i++) { key = keys[i];
keys.forEach(function (key) {
opts[key] = (key in opts) ? opts[key] : defaults[key];
}
if ('undefined' === typeof opts.symbols)
});
if ('undefined' === typeof opts.symbols) {
opts.symbols = defaults.symbols;

var lengths = [];
for (var key in opts.multicharmap) {
if (!opts.multicharmap.hasOwnProperty(key))
continue;

var len = key.length;
if (lengths.indexOf(len) === -1)
lengths.push(len);
}

var code, unicode, result = "";
for (var char, i = 0, l = string.length; i < l; i++) { char = string[i];
if (!lengths.some(function (len) {
var str = string.substr(i, len);
if (opts.multicharmap[str]) {
i += len - 1;
char = opts.multicharmap[str];
return true;
} else return false;
})) {
var code, unicode, result = '';
for (var char, i = 0, l = string.length; i < l; i++) {
char = string[i];
var multichar = multicharat(0, string.substring(i), opts.multicharmap);

if (multichar) {
i += multichar.length - 1;
char = opts.multicharmap[multichar];
} else {
if (opts.charmap[char]) {
char = opts.charmap[char];
code = char.charCodeAt(0);
Expand All @@ -58,16 +62,16 @@ function slug(string, opts) {
}
}
char = char.replace(/[^\w\s\-\.\_~]/g, ''); // allowed
if (opts.remove) char = char.replace(opts.remove, ''); // add flavour
if (opts.remove) {
char = char.replace(opts.remove, ''); // add flavour
}
result += char;
}
result = result.replace(/^\s+|\s+$/g, ''); // trim leading/trailing spaces
result = result.replace(/[-\s]+/g, opts.replacement); // convert spaces
result = result.replace(opts.replacement+"$",''); // remove trailing separator
if (opts.lower)
result = result.toLowerCase();
return result;
};
return opts.lower ? result.toLowerCase() : result;
}

slug.defaults = {
mode: 'pretty',
Expand Down Expand Up @@ -186,27 +190,28 @@ slug.defaults.modes = {

// Be compatible with different module systems

if (typeof define !== 'undefined' && define.amd) { // AMD
// dont load symbols table in the browser
for (var key in slug.defaults.modes) {
if (!slug.defaults.modes.hasOwnProperty(key))
continue;

slug.defaults.modes[key].symbols = false;
}
define([], function () {return slug});
} else if (typeof module !== 'undefined' && module.exports) { // CommonJS
// CommonJS
if (typeof module !== 'undefined' && module.exports) {
symbols(); // preload symbols table
module.exports = slug;
} else { // Script tag
// dont load symbols table in the browser
for (var key in slug.defaults.modes) {
if (!slug.defaults.modes.hasOwnProperty(key))
continue;
return;
}

slug.defaults.modes[key].symbols = false;
// dont load symbols table in the browser
for (var key in slug.defaults.modes) {
if (!slug.defaults.modes.hasOwnProperty(key)) {
continue;
}
root.slug = slug;
slug.defaults.modes[key].symbols = false;
}

// AMD
if (typeof define !== 'undefined' && define.amd) {
define([], function () {return slug;});
return;
}

// Script tag
root.slug = slug;

}(this));