├── .editorconfig ├── .gitignore ├── .jshintignore ├── .jshintrc ├── changelog.markdown ├── license ├── package.json ├── readme.markdown ├── sluggish.js └── test └── sluggish.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /.jshintignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | bower_components 3 | dist 4 | example 5 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "curly": true, 3 | "eqeqeq": true, 4 | "newcap": true, 5 | "noarg": true, 6 | "noempty": true, 7 | "nonew": true, 8 | "sub": true, 9 | "undef": true, 10 | "unused": true, 11 | "trailing": true, 12 | "boss": true, 13 | "eqnull": true, 14 | "strict": true, 15 | "immed": true, 16 | "expr": true, 17 | "latedef": "nofunc", 18 | "quotmark": "single", 19 | "validthis": true, 20 | "indent": 2, 21 | "node": true, 22 | "browser": true 23 | } 24 | -------------------------------------------------------------------------------- /changelog.markdown: -------------------------------------------------------------------------------- 1 | # 1.0.1 Unusify 2 | 3 | - Removed unused variables 4 | 5 | # 1.0.0 IPO 6 | 7 | - Initial Public Release 8 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright © 2015 Nicolas Bevacqua 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sluggish", 3 | "version": "1.0.1", 4 | "description": "Sluggish slug generator that works universally", 5 | "repository": { 6 | "type": "git", 7 | "url": "https://github.com/bevacqua/sluggish.git" 8 | }, 9 | "author": "Nicolas Bevacqua (http://bevacqua.io/)", 10 | "license": "MIT", 11 | "bugs": { 12 | "url": "https://github.com/bevacqua/sluggish/issues" 13 | }, 14 | "homepage": "https://github.com/bevacqua/sluggish", 15 | "main": "sluggish.js", 16 | "scripts": { 17 | "test": "tape test/**/*.js" 18 | }, 19 | "dependencies": {}, 20 | "devDependencies": { 21 | "tape": "3.5.0" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /readme.markdown: -------------------------------------------------------------------------------- 1 | # sluggish 2 | 3 | > Sluggish slug generator that works universally 4 | 5 | Works well in browsers, as its footprint size is very small. 6 | 7 | # Install 8 | 9 | ```shell 10 | npm install sluggish --save 11 | ``` 12 | 13 | # Usage 14 | 15 | ```js 16 | sluggish('Hola mundo soy -Awésome-') 17 | <- 'hola-mundo-soy-awesome' 18 | ``` 19 | 20 | # API 21 | 22 | Using `sluggish(text)` yields a hyphenated slug that's meant to be used for a URL or an `id` attribute. 23 | 24 | # License 25 | 26 | MIT 27 | -------------------------------------------------------------------------------- /sluggish.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var spaces = /\s+/g; 4 | var dashes = /[-_]+/g; 5 | var dashesLeadTrail = /^-|-$/g; 6 | var invalid = /[^\x20\x2D0-9A-Z\x5Fa-z\xC0-\xD6\xD8-\xF6\xF8-\xFF]/g; 7 | var accentCodePoints = /[\xC0-\xFF]/g; 8 | var accents = [ 9 | [/[\xC0-\xC5]/g, 'A'], 10 | [/[\xC6]/g, 'AE'], 11 | [/[\xC7]/g, 'C'], 12 | [/[\xC8-\xCB]/g, 'E'], 13 | [/[\xCC-\xCF]/g, 'I'], 14 | [/[\xD0]/g, 'D'], 15 | [/[\xD1]/g, 'N'], 16 | [/[\xD2-\xD6\xD8]/g, 'O'], 17 | [/[\xD9-\xDC]/g, 'U'], 18 | [/[\xDD]/g, 'Y'], 19 | [/[\xDE]/g, 'P'], 20 | [/[\xE0-\xE5]/g, 'a'], 21 | [/[\xE6]/g, 'ae'], 22 | [/[\xE7]/g, 'c'], 23 | [/[\xE8-\xEB]/g, 'e'], 24 | [/[\xEC-\xEF]/g, 'i'], 25 | [/[\xF1]/g, 'n'], 26 | [/[\xF2-\xF6\xF8]/g, 'o'], 27 | [/[\xF9-\xFC]/g, 'u'], 28 | [/[\xFE]/g, 'p'], 29 | [/[\xFD\xFF]/g, 'y'] 30 | ]; 31 | var replacements = [[ 32 | /&/g, ' and ' 33 | ], [ 34 | /\./g, '' 35 | ]]; 36 | 37 | function slugify (text) { 38 | var partial = translate(text, replacements); 39 | if (partial.search(accentCodePoints) === -1) { 40 | return partial; 41 | } 42 | return translate(partial, accents); 43 | } 44 | 45 | function translate (text, translations) { 46 | return translations.reduce(function (text, pair) { 47 | return text.replace(pair[0], pair[1]); 48 | }, text); 49 | } 50 | 51 | function parse (input) { 52 | return input === null || input === void 0 ? '' : input.toString(); 53 | } 54 | 55 | function slug (text) { 56 | return slugify(parse(text)) 57 | .replace(invalid, '-') // remove invalid chars 58 | .replace(spaces, '-') // collapse whitespace and replace by '-' 59 | .replace(dashes, '-') // collapse dashes 60 | .replace(dashesLeadTrail, '') // remove leading or trailing dashes 61 | .trim() 62 | .toLowerCase(); 63 | } 64 | 65 | module.exports = slug; 66 | -------------------------------------------------------------------------------- /test/sluggish.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var test = require('tape'); 4 | var sluggish = require('..'); 5 | 6 | test('deals with titles accurately', function (t) { 7 | t.equal(sluggish('slow-moving or inactive.\n"a sluggish stream"'), 'slow-moving-or-inactive-a-sluggish-stream'); 8 | t.equal(sluggish('so many äccénts ï Ö'), 'so-many-accents-i-o'); 9 | t.equal(sluggish('--on either END-_--_?¡¿'), 'on-either-end'); 10 | t.end(); 11 | }); 12 | --------------------------------------------------------------------------------