Comparing version 1.5.0 to 1.6.0
@@ -7,3 +7,8 @@ 'use strict'; | ||
module.exports = function(text, opt) { | ||
function customCharsAsArray (custom) { | ||
custom = custom || []; | ||
return Array.isArray(custom) ? custom : Object.keys(custom); | ||
} | ||
module.exports = function (text, opt) { | ||
const options = typeof opt === 'string' | ||
@@ -25,22 +30,21 @@ ? { separator: opt } | ||
// Language-specific behaviour | ||
const lang = options.lang; | ||
if (typeof lang === 'undefined') { | ||
if (hepburn.containsKana(text)) { | ||
// Convert from Japanese Kana using Hepburn romanisation | ||
text = hepburn.fromKana(text); | ||
// Remove any remaining non-Kana, e.g. Kanji | ||
text = text.replace(/([^A-Za-z0-9\- ]+)/g, ''); | ||
} else if (/[\u4e00-\u9fa5]+/.test(text)) { | ||
// Convert Mandarin Chinese to Pinyin with numeric tones | ||
mergeDigitSuffixes = true; | ||
// Should we use tone numbers? (default is true) | ||
const tone = (typeof options.tone === 'boolean') ? options.tone : true; | ||
text = pinyin(text, { | ||
'style': tone ? pinyin.STYLE_TONE2 : pinyin.STYLE_NORMAL | ||
}).join(' '); | ||
// Remove punctuation symbols | ||
text = text.replace(/([^0-9A-Za-z ]+)/g, ''); | ||
// Remove space around single character words, caused by non-Mandarin symbols in otherwise Mandarin text | ||
text = text.replace(/([^1-4]) ([A-Za-z]) /g, '$1$2'); | ||
} | ||
const lang = options.lang || ''; | ||
if (lang.toLowerCase().startsWith('ja') || hepburn.containsKana(text)) { | ||
// Convert from Japanese Kana using Hepburn romanisation | ||
text = hepburn.fromKana(text); | ||
// Remove any remaining non-Kana, e.g. Kanji | ||
text = text.replace(/([^A-Za-z0-9\- ]+)/g, ''); | ||
} else if (lang.toLowerCase().startsWith('zh') || /[\u4e00-\u9fa5]+/.test(text)) { | ||
// Should we use tone numbers? (default is true) | ||
const tone = (typeof options.tone === 'boolean') ? options.tone : true; | ||
mergeDigitSuffixes = tone; | ||
text = pinyin(text, { | ||
'style': tone ? pinyin.STYLE_TONE2 : pinyin.STYLE_NORMAL | ||
}).join(' '); | ||
// Remove punctuation symbols | ||
const customNonPunctuation = customCharsAsArray(options.custom).map(function (c) { return `\\${c}`; }).join(''); | ||
const nonPunctuationMatcher = new RegExp(`([^0-9A-Za-z ${customNonPunctuation}]+)`, 'g'); | ||
text = text.replace(nonPunctuationMatcher, ''); | ||
// Remove space around single character words, caused by non-Mandarin symbols in otherwise Mandarin text | ||
text = text.replace(/([^1-4]) ([A-Za-z]) /g, '$1$2'); | ||
} | ||
@@ -47,0 +51,0 @@ // Convert to slug using speakingurl |
{ | ||
"name": "limax", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"main": "./lib/limax", | ||
@@ -14,3 +14,3 @@ "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.", | ||
"scripts": { | ||
"test": "node test/unit" | ||
"test": "semistandard && nyc --reporter=html ava" | ||
}, | ||
@@ -43,5 +43,10 @@ "keywords": [ | ||
"hepburn": "^1.0.0", | ||
"pinyin": "^2.8.2", | ||
"speakingurl": "^13.0.0" | ||
"pinyin": "^2.8.3", | ||
"speakingurl": "^14.0.1" | ||
}, | ||
"devDependencies": { | ||
"ava": "^0.24.0", | ||
"nyc": "^11.4.1", | ||
"semistandard": "^11.0.0" | ||
} | ||
} |
@@ -8,3 +8,6 @@ # limax | ||
This module includes Romanisation of non-Latin scripts. | ||
This module extends the fantastic | ||
[speakingurl](https://www.npmjs.com/package/speakingurl) | ||
module to include Romanisation of Chinese and Japanese scripts. | ||
Give it a string of text in pretty much any major world language | ||
@@ -58,4 +61,4 @@ and it will convert it to valid characters, | ||
* `custom`: | ||
* Object, custom map for translation, overwrites all i.e. { '&': '#', '*': ' star ' } | ||
* Array, add chars to allowed charMap | ||
- Object, custom map for translation, overwrites all i.e. `{ '&': '#', '*': ' star ' }` | ||
- Array, add chars to allowed charMap | ||
@@ -101,3 +104,3 @@ ```javascript | ||
Copyright 2013, 2014, 2015, 2016 Lovell Fuller and contributors. | ||
Copyright 2013, 2014, 2015, 2016, 2017, 2018 Lovell Fuller and contributors. | ||
@@ -104,0 +107,0 @@ Licensed under the Apache License, Version 2.0 (the "License"); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8141
54
113
3
+ Addedspeakingurl@14.0.1(transitive)
- Removedspeakingurl@13.0.0(transitive)
Updatedpinyin@^2.8.3
Updatedspeakingurl@^14.0.1