Skip to content
This repository has been archived by the owner on Sep 23, 2021. It is now read-only.

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremys committed Feb 1, 2012
0 parents commit 39fd289
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
node_modules/
.DS_Store
19 changes: 19 additions & 0 deletions LICENSE
@@ -0,0 +1,19 @@
Copyright (c) 2012 Jeremy Selier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
44 changes: 44 additions & 0 deletions README.md
@@ -0,0 +1,44 @@
# Uslug.js

Permissive slug generator that works with unicode.
We keep only characters from the categories Letter, Number and Separator (see [Unicode Categories](http://www.unicode.org/versions/Unicode6.0.0/ch04.pdf))
and the common [CJK Unified Ideographs](http://www.unicode.org/versions/Unicode6.0.0/ch12.pdf) as defined in the version 6.0.0 of the Unicode specification.

Inspired by [unicode-slugify](https://github.com/mozilla/unicode-slugify).
Note that this slug generator is different from [node-slug](https://github.com/dodo/node-slug) which focus on translating unicode characters to english or latin equivalent.


## Quick Examples

uslug('Быстрее и лучше!') // 'быстрее-и-лучше'
uslug('汉语/漢語') // '汉语漢語'

uslug('Y U NO', { lower: false })) // 'Y-U-NO'
uslug('Y U NO', { spaces: true })) // 'y u no'
uslug('Y-U|NO', { allowedChars: '|' })) // 'yu|no'


## Installation

npm install uslug


## Options

### uslug(string, options)

Generate a slug for the string passed.

__Arguments__

* string - The string you want to slugify.
* options - An optional object that can contain:
allowedChars: a String of chars that you want to be whitelisted.
Default: '-_~'.
lower: a Boolean to force to lower case the slug. Default: true.
spaces: a Boolean to allow spaces. Default: false.


## License

This project is distributed under the MIT License. See LICENSE file for more information.
1 change: 1 addition & 0 deletions index.js
@@ -0,0 +1 @@
module.exports = require('./lib/slugify');
15 changes: 15 additions & 0 deletions lib/L.js

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions lib/N.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions lib/Z.js
@@ -0,0 +1,12 @@
/*
* List of Unicode code that are flagged as separator.
*
* Contains Unicode code of:
* - Zs = Separator, space
* - Zl = Separator, line
* - Zp = Separator, paragraph
*
* This list has been computed from http://unicode.org/Public/UNIDATA/UnicodeData.txt
*
*/
exports.Z = [32, 160, 5760, 6158, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288];
40 changes: 40 additions & 0 deletions lib/uslug.js
@@ -0,0 +1,40 @@
(function() {
var L = require('./L').L,
N = require('./N').N,
Z = require('./Z').Z,
unorm = require('unorm');

var _unicodeCategory = function(code) {
if (~L.indexOf(code)) return 'L';
if (~N.indexOf(code)) return 'N';
if (~Z.indexOf(code)) return 'Z';
return undefined;
};

module.exports = function(string, options) {
string = string || '';
options = options || {};
var allowedChars = options.allowedChars || '-_~';
var lower = typeof options.lower === 'boolean' ? options.lower : true;
var spaces = typeof options.spaces === 'boolean' ? options.spaces : false;
var rv = [];
var chars = unorm.nfkc(string);
for(var i = 0; i < chars.length; i++) {
var c = chars[i];
var code = c.charCodeAt(0);
// Allow Common CJK Unified Ideographs
// See: http://www.unicode.org/versions/Unicode6.0.0/ch12.pdf - Table 12-2
if (0x4E00 <= code && code <= 0x9FFF) rv.push(c);
if (allowedChars.indexOf(c) != -1) rv.push(c);
var val = _unicodeCategory(code);
if (val) {
if ('LN'.indexOf(val) != -1) rv.push(c);
if ('Z'.indexOf(val) != -1) rv.push(' ');
}
}
slug = rv.join('').replace(/^\s+|\s+$/g, '').replace(/\s+/g,' ');
if (!spaces) slug = slug.replace(/\s+/g,'-').replace(/-+/g,'-');
if (lower) slug = slug.toLowerCase();
return slug;
};
}());
25 changes: 25 additions & 0 deletions package.json
@@ -0,0 +1,25 @@
{
"name" : "uslug",
"version" : "1.0.0",
"description" : "A permissive slug generator that works with unicode.",
"author": "Jeremy Selier <jerem.selier@gmail.com>",
"dependencies": {
"unorm": ">= 1.0.0"
},
"devDependencies": {
"should": ">= 0.2.1"
},
"repository" : {
"type" : "git",
"url" : "http://github.com/jeremys/uslug.git"
},
"main": "./index",
"engines" : {
"node" : ">= 0.4.0"
},
"bugs" : { "url" : "http://github.com/jeremys/uslug/issues" },
"licenses" :[{
"type" : "MIT",
"url" : "http://github.com/jeremys/uslug/raw/master/LICENSE"
}]
}
33 changes: 33 additions & 0 deletions test/test.js
@@ -0,0 +1,33 @@
var should = require('should'),
uslug = require('../lib/uslug');


var word0 = 'Ελληνικά';
var word1 = [word0, word0].join('-');
var word2 = [word0, word0].join(' - ');

var tests = [
['The \u212B symbol invented by A. J. \u00C5ngstr\u00F6m (1814, L\u00F6gd\u00F6, \u2013 1874) denotes the length 10\u207B\u00B9\u2070 m.', 'the-å-symbol-invented-by-a-j-ångström-1814-lögdö-1874-denotes-the-length-1010-m'],
['Быстрее и лучше!', 'быстрее-и-лучше'],
['xx x - "#$@ x', 'xx-x-x'],
['Bän...g (bang)', 'bäng-bang'],
[word0, word0.toLowerCase()],
[word1, word1.toLowerCase()],
[word2, word1.toLowerCase()],
[' a ', 'a'],
['tags/', 'tags'],
['y_u_no', 'y_u_no'],
['el-ni\xf1o', 'el-ni\xf1o'],
['x荿', 'x荿'],
['ϧ΃蒬蓣', '\u03e7蒬蓣'],
['¿x', 'x'],
['汉语/漢語', '汉语漢語'],
['فار,سي', 'فارسي'],
['เแโ|ใไ', 'เแโใไ'],
['日本語ドキュメンテ(ーション)', '日本語ドキュメンテーション']
];

for (var t in tests) {
var test = tests[t];
uslug(test[0]).should.equal(test[1]);
}

0 comments on commit 39fd289

Please sign in to comment.