From a41ddb23131bf926e9748bfb7f579ce4b92c5630 Mon Sep 17 00:00:00 2001 From: Belema Gancarz Date: Mon, 21 Dec 2015 16:00:16 +0000 Subject: [PATCH 1/2] Add JSHint to project, and fix errors. --- .jshintrc | 15 ++++++++ .travis.yml | 4 ++- package.json | 2 ++ slug.js | 99 +++++++++++++++++++++++++++------------------------- 4 files changed, 72 insertions(+), 48 deletions(-) create mode 100644 .jshintrc diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..27ea22a --- /dev/null +++ b/.jshintrc @@ -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 + } +} diff --git a/.travis.yml b/.travis.yml index ea8987f..09ad107 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,7 @@ before_script: "npm install --dev" -script: "npm test" +script: + - "npm run lint" + - "npm test" language: node_js node_js: - 0.8 diff --git a/package.json b/package.json index 1794401..b8890a3 100644 --- a/package.json +++ b/package.json @@ -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"} diff --git a/slug.js b/slug.js index 010c89f..caa93f8 100644 --- a/slug.js +++ b/slug.js @@ -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); @@ -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', @@ -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)); From db6a4039f946304c81b7f52d162b2462d21cf8ba Mon Sep 17 00:00:00 2001 From: Belema Gancarz Date: Mon, 21 Dec 2015 17:11:51 +0000 Subject: [PATCH 2/2] Update travis config. --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 09ad107..8c7f6d6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,4 @@ +before_install: "npm install npm -g" before_script: "npm install --dev" script: - "npm run lint"