Skip to content

Commit

Permalink
Allow optional use of Pinyin tone numbers lovell#4
Browse files Browse the repository at this point in the history
  • Loading branch information
lovell committed May 28, 2015
1 parent d698031 commit 08da49c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"strict": true,
"node": true,
"maxparams": 2,
"maxcomplexity": 6
"maxcomplexity": 7
}
28 changes: 15 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Currently supports, but not limited to, the following scripts:

* Latin: e.g. English, français, Deutsch, español, português
* Cyrillic: e.g. Русский язык, български език, українська мова
* Chinese: e.g. 官话, 吴语 (converts to Latin script using tone number Pinyin)
* Chinese: e.g. 官话, 吴语 (converts to Latin script using Pinyin with optional tone number)
* Japanese: e.g. ひらがな, カタカナ (converts to Romaji using Hepburn)

If you already use either the
Expand All @@ -32,37 +32,39 @@ Oh, and `limax` is the Latin word for slug.
## Usage

```javascript
var slug = require("limax");
var slug = require('limax');
```

### slug(text)

```javascript
var latin = slug("i ♥ latin"); // i-love-latin
var cyrillic = slug("Я люблю русский"); // ya-lyublyu-russkij
var pinyin = slug("我爱官话"); // wo3-ai4-guan1-hua4
var romaji = slug("私は ひらがな が大好き"); // ha-hiragana-gaki
var latin = slug('i ♥ latin'); // i-love-latin
var cyrillic = slug('Я люблю русский'); // ya-lyublyu-russkij
var pinyin = slug('我爱官话'); // wo3-ai4-guan1-hua4
var romaji = slug('私は ひらがな が大好き'); // ha-hiragana-gaki
```

### slug(text, options)

options:
* replacement: char to replace whitespace with, defaults to `-` (provides API compatibility with the `slug` module)
* separator: equivalent to `replacement` (provides API compatibility with the `speakingurl` module)
* lang: ISO 639-1 two-letter language code, defaults to auto-detected language
* `replacement`: String to replace whitespace with, defaults to `-` (provides API compatibility with the `slug` module)
* `separator`: String, equivalent to `replacement` (provides API compatibility with the `speakingurl` module)
* `lang`: String, ISO 639-1 two-letter language code, defaults to auto-detected language
* `tone`: Boolean, add tone numbers to Pinyin transliteration of Chinese, defaults to `true`

```javascript
var strich = slug("Ich ♥ Deutsch", {lang: "de"}); // ich-liebe-deutsch
var unterstreichen1 = slug("Ich ♥ Deutsch", {lang: "de", replacement: "_"}); // i_liebe_deutsch
var unterstreichen2 = slug("Ich ♥ Deutsch", {lang: "de", separator: "_"}); // i_liebe_deutsch
var strich = slug('Ich ♥ Deutsch', {lang: 'de'}); // ich-liebe-deutsch
var unterstreichen1 = slug('Ich ♥ Deutsch', {lang: 'de', replacement: '_'}); // i_liebe_deutsch
var unterstreichen2 = slug('Ich ♥ Deutsch', {lang: 'de', separator: '_'}); // i_liebe_deutsch
var wuYin = slug('弄堂里的菜品赤醬', {tone: false}); // nong-tang-li-di-cai-pin-chi-jiang
```

### slug(text, replacement)

Provided to support backwards-compatibility with the `slug` module.

```javascript
var underscore = slug("i ♥ unicode", "_"); // i_love_unicode
var underscore = slug('i ♥ unicode', '_'); // i_love_unicode
```

## Test [![Build Status](https://travis-ci.org/lovell/limax.png?branch=master)](https://travis-ci.org/lovell/limax)
Expand Down
11 changes: 8 additions & 3 deletions lib/limax.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = function(text, opt) {
// Default to English
deferredLang.resolve('en');
} else {
deferredLang.resolve(data.languages[0].code);
deferredLang.resolve(data.languages[0].code.substr(0, 2));
}
});
lang = deferredLang.promise;
Expand All @@ -32,8 +32,10 @@ module.exports = function(text, opt) {
var mergeDigitSuffixes = false;
// Convert Mandarin Chinese to Pinyin with numeric tones
if (lang == 'zh') {
// Should we use tone numbers? (default is true)
var tone = (typeof options.tone === 'boolean') ? options.tone : true;
text = pinyin(text, {
'style': pinyin.STYLE_TONE2
'style': tone ? pinyin.STYLE_TONE2 : pinyin.STYLE_NORMAL
}).join(' ');
// Remove punctuation symbols
text = text.replace(/([^0-9A-Za-z ]+)/g, '');
Expand All @@ -50,7 +52,10 @@ module.exports = function(text, opt) {
lang = 'en';
}
// Convert to slug using speakingurl
var separator = options.replacement || options.separator || '-';
var separator = options.replacement || options.separator;
if (typeof separator !== 'string') {
separator = '-';
}
var slug = speakingurl(text, {
lang: lang,
separator: separator
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "limax",
"version": "1.0.2",
"version": "1.1.0",
"main": "./lib/limax",
"description": "Node.js module to generate URL slugs. Another one? This one cares about i18n and transliterates non-Latin scripts to conform to the RFC3986 standard. Mostly API-compatible with similar modules.",
"repository": {
Expand Down
8 changes: 7 additions & 1 deletion test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ var tests = {
// https://github.com/keystonejs/keystone-utils/issues/12
'one2three': 'one-2-three',
'The User\'s Guide': 'the-users-guide',
'The User’s Guide': 'the-users-guide'
'The User’s Guide': 'the-users-guide',
// https://github.com/lovell/limax/issues/4
'弄堂里的菜品赤醬': 'nong4-tang2-li3-di2-cai4-pin3-chi4-jiang4'
};

Object.keys(tests).forEach(function(test) {
Expand Down Expand Up @@ -39,3 +41,7 @@ Object.keys(tests).forEach(function(test) {
});

assert.strictEqual(slug('Ich ♥ Deutsch', {lang: 'de'}), 'ich-liebe-deutsch');

// https://github.com/lovell/limax/issues/4
assert.strictEqual(slug('弄堂里的菜品赤醬', {tone: true}), 'nong4-tang2-li3-di2-cai4-pin3-chi4-jiang4');
assert.strictEqual(slug('弄堂里的菜品赤醬', {tone: false}), 'nong-tang-li-di-cai-pin-chi-jiang');

0 comments on commit 08da49c

Please sign in to comment.