├── _config.yml ├── index.js ├── demo.js ├── .gitignore ├── package.json ├── LICENSE ├── test ├── khongdau.v2.test.js └── khongdau.v1.test.js ├── lib └── khongdau.js └── README.md /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-minimal -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./lib/khongdau.js'); 2 | -------------------------------------------------------------------------------- /demo.js: -------------------------------------------------------------------------------- 1 | var KhongDau = require('./index.js'); 2 | 3 | var str = 'Hoàng Sa - Trường Sa là của Việt Nam'; 4 | var alphabet = 'abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZáàảãạăắằẳẵặâấầẩẫậAÀẢÃẠĂẮẰẲẴẶÂẤẦẨẪẬđĐéèẻẽẹêếềểễệÉÈẺẼẸÊẾỀỂỄỆíìỉĩịÍÌỈĨỊóòỏõọôốồổỗộơớờởỡợÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢúùủũụưứừửữựÚÙỦŨỤƯỨỪỬỮỰýỳỷỹỵÝỲỶỸỴ'; 5 | 6 | console.log('\n*** Bo dau ***\n'); 7 | console.log(KhongDau(str)); 8 | console.log(KhongDau(alphabet)); 9 | 10 | console.log('\n*** URL sau khi Bo Dau***\n'); 11 | console.log(KhongDau(str, ["chuyen", "url"])); 12 | console.log(KhongDau(alphabet, ["chuyen", "url"])); 13 | 14 | console.log('\n*** FILE sau khi Bo Dau ***\n'); 15 | console.log(KhongDau(str, ["chuyen", "file"])); 16 | console.log(KhongDau(alphabet, ["chuyen", "file"])); 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | package-lock.json -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "khong-dau", 3 | "version": "2.0.0", 4 | "description": "Convert Vietnamese characters to Latin characters", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha", 8 | "coverage": "nyc npm run test" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/vuanhhaogk/khong-dau.git" 13 | }, 14 | "keywords": [ 15 | "Vietnamese", 16 | "Language", 17 | "NodeJS", 18 | "Javascript", 19 | "Server", 20 | "Client", 21 | "Converter" 22 | ], 23 | "author": "Vu Anh Hao (http://xn--vanhhoblog-l4a35n.vn)", 24 | "license": "MIT", 25 | "bugs": { 26 | "url": "https://github.com/vuanhhaogk/khong-dau/issues" 27 | }, 28 | "homepage": "https://github.com/vuanhhaogk/khong-dau#readme", 29 | "dependencies": { 30 | "chai": "^4.2.0", 31 | "mocha": "^8.2.1", 32 | "nyc": "^15.1.0", 33 | "sanitize-filename": "^1.6.1" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Vu Anh Hao 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, 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, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /test/khongdau.v2.test.js: -------------------------------------------------------------------------------- 1 | const { expect } = require('chai'); 2 | const KhongDau = require('../lib/khongdau'); 3 | 4 | describe('Traditional khong dau v2', () => { 5 | it('Basic convert', async () => { 6 | const src = 'Hoàng Sa, Trường Sa là của Việt Nam'; 7 | const dst = KhongDau(src); 8 | const dst1 = KhongDau(src, ["chuyen", "url"]); 9 | const dst2 = KhongDau(src, ["chuyen", "file"]); 10 | 11 | expect(dst).to.be.equal('Hoang Sa, Truong Sa la cua Viet Nam'); 12 | expect(dst1).to.be.equal('Hoang-Sa-Truong-Sa-la-cua-Viet-Nam'); 13 | expect(dst2).to.be.equal('Hoang Sa, Truong Sa la cua Viet Nam'); 14 | }); 15 | 16 | it('Alphabet convert', async () => { 17 | const src = 'abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZáàảãạăắằẳẵặâấầẩẫậAÀẢÃẠĂẮẰẲẴẶÂẤẦẨẪẬđĐéèẻẽẹêếềểễệÉÈẺẼẸÊẾỀỂỄỆíìỉĩịÍÌỈĨỊóòỏõọôốồổỗộơớờởỡợÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢúùủũụưứừửữựÚÙỦŨỤƯỨỪỬỮỰýỳỷỹỵÝỲỶỸỴ'; 18 | const rel = 'abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZaaaaaaaaaaaaaaaaaAAAAAAAAAAAAAAAAAdDeeeeeeeeeeeEEEEEEEEEEEiiiiiIIIIIoooooooooooooooooOOOOOOOOOOOOOOOOOuuuuuuuuuuuUUUUUUUUUUUyyyyyYYYYY'; 19 | const dst = KhongDau(src); 20 | const dst1 = KhongDau(src, ["chuyen", "url"]); 21 | const dst2 = KhongDau(src, ["chuyen", "file"]); 22 | 23 | expect(dst).to.be.equal(rel); 24 | expect(dst1).to.be.equal(rel); 25 | expect(dst2).to.be.equal(rel); 26 | }); 27 | }); -------------------------------------------------------------------------------- /test/khongdau.v1.test.js: -------------------------------------------------------------------------------- 1 | const { expect } = require('chai'); 2 | const KhongDau = require('../lib/khongdau'); 3 | 4 | describe('Traditional khong dau v1', () => { 5 | it('Basic convert', async () => { 6 | const src = 'Hoàng Sa, Trường Sa là của Việt Nam'; 7 | const dst = KhongDau.c(src); 8 | const dst1 = KhongDau.cLowerCase(src); 9 | const dst2 = KhongDau.cUpperCase(src); 10 | const dst3 = KhongDau.cFriendlyURI(src); 11 | 12 | expect(dst).to.be.equal('Hoang Sa, Truong Sa la cua Viet Nam'); 13 | expect(dst1).to.be.equal('hoang sa, truong sa la cua viet nam'); 14 | expect(dst2).to.be.equal('HOANG SA, TRUONG SA LA CUA VIET NAM'); 15 | expect(dst3).to.be.equal('hoang-sa-truong-sa-la-cua-viet-nam'); 16 | }); 17 | 18 | it('Alphabet convert', async () => { 19 | const src = 'abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZáàảãạăắằẳẵặâấầẩẫậAÀẢÃẠĂẮẰẲẴẶÂẤẦẨẪẬđĐéèẻẽẹêếềểễệÉÈẺẼẸÊẾỀỂỄỆíìỉĩịÍÌỈĨỊóòỏõọôốồổỗộơớờởỡợÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢúùủũụưứừửữựÚÙỦŨỤƯỨỪỬỮỰýỳỷỹỵÝỲỶỸỴ'; 20 | const rel = 'abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZaaaaaaaaaaaaaaaaaAAAAAAAAAAAAAAAAAdDeeeeeeeeeeeEEEEEEEEEEEiiiiiIIIIIoooooooooooooooooOOOOOOOOOOOOOOOOOuuuuuuuuuuuUUUUUUUUUUUyyyyyYYYYY'; 21 | const dst = KhongDau.c(src); 22 | const dst1 = KhongDau.cLowerCase(src); 23 | const dst2 = KhongDau.cUpperCase(src); 24 | 25 | expect(dst).to.be.equal(rel); 26 | expect(dst1).to.be.equal(rel.toLowerCase()); 27 | expect(dst2).to.be.equal(rel.toUpperCase()); 28 | }); 29 | }); -------------------------------------------------------------------------------- /lib/khongdau.js: -------------------------------------------------------------------------------- 1 | const sanitize = require('sanitize-filename'); 2 | 3 | function KhongDau(str, rules){ 4 | rules = rules || ["chuyen"] 5 | 6 | for (var i = 0; i < rules.length; i++) 7 | switch (rules[i]){ 8 | case "chuyen": 9 | str = KhongDau.c(str); 10 | break; 11 | case "url": 12 | str = KhongDau.cURI(str); 13 | break; 14 | case "file": 15 | str = sanitize(str); 16 | break; 17 | } 18 | 19 | return str; 20 | } 21 | 22 | KhongDau.c = function(str){ 23 | str = str.replace(/(á|à|ả|ã|ạ|ă|ắ|ằ|ẳ|ẵ|ặ|â|ấ|ầ|ẩ|ẫ|ậ)/g, 'a'); 24 | str = str.replace(/(A|Á|À|Ả|Ã|Ạ|Ă|Ắ|Ằ|Ẳ|Ẵ|Ặ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ)/g, 'A'); 25 | str = str.replace(/đ/g, 'd'); 26 | str = str.replace(/Đ/g, 'D'); 27 | str = str.replace(/(é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ)/g, 'e'); 28 | str = str.replace(/(É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ)/g, 'E'); 29 | str = str.replace(/(í|ì|ỉ|ĩ|ị)/g, 'i'); 30 | str = str.replace(/(Í|Ì|Ỉ|Ĩ|Ị)/g, 'I'); 31 | str = str.replace(/(ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ)/g, 'o'); 32 | str = str.replace(/(Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ)/g, 'O'); 33 | str = str.replace(/(ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự)/g, 'u'); 34 | str = str.replace(/(Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự)/g, 'U'); 35 | str = str.replace(/(ý|ỳ|ỷ|ỹ|ỵ)/g, 'y'); 36 | str = str.replace(/(Ý|Ỳ|Ỷ|Ỹ|Ỵ)/g, 'Y'); 37 | 38 | return str; 39 | }; 40 | 41 | KhongDau.cLowerCase = function(str){ 42 | return this.c(str).toLowerCase(); 43 | }; 44 | 45 | KhongDau.cUpperCase = function(str){ 46 | return this.c(str).toUpperCase(); 47 | }; 48 | 49 | KhongDau.cURI = function(str){ 50 | str = str.replace(/[^a-zA-Z0-9_-]/g, '-'); 51 | 52 | while (str.length > 0 && (/--/g).test(str)){ 53 | str = str.replace(/--/g, '-'); 54 | } 55 | 56 | return str; 57 | } 58 | 59 | KhongDau.cFriendlyURI = function(str){ 60 | str = this.cLowerCase(str); 61 | 62 | str = this.cURI(str).toLowerCase(); 63 | 64 | return str; 65 | } 66 | 67 | // Export if using node 68 | try {module.exports = KhongDau; } catch(e){}; 69 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Tiếng Việt không dấu 2 | 3 | _Node Module dành cho người Việt_ 4 | 5 | [![Tieng Viet khong dau](https://img.shields.io/badge/node-%5E0.10.40-brightgreen.svg)](//www.npmjs.com/package/khong-dau) [![Tieng Viet khong dau](https://img.shields.io/badge/npm-%5E1.4.28-brightgreen.svg)](//www.npmjs.com/package/khong-dau) 6 | 7 | ## Cài đặt 8 | 9 | ```shell 10 | $ npm install --save khong-dau 11 | ``` 12 | 13 | ## Sử dụng 14 | 15 | ### Đối với NodeJS 16 | 17 | ```javascript 18 | var KhongDau = require('khong-dau'); 19 | 20 | console.log(KhongDau('Hoàng Sa, Trường Sa là của Việt Nam')); 21 | 22 | // Kết quả: Hoang Sa, Truong Sa la cua Viet Nam 23 | ``` 24 | 25 | ### Đối với Javascript phía Font-end 26 | 27 | ```html 28 | 29 | ``` 30 | 31 | ```js 32 | console.log(KhongDau('Hoàng Sa, Trường Sa là của Việt Nam')); 33 | // Kết quả: Hoang Sa, Truong Sa la cua Viet Nam 34 | ``` 35 | 36 | ## API 37 | 38 | > Lưu ý: Bạn vẫn có thể sử dụng cú pháp của phiên bản trước **1.0.0** 39 | 40 | **KhongDau(str [, rules])** 41 | 42 | Trong đó `rules` là một mảng quy định cách chuyển đổi, gồm: 43 | 44 | + "chuyen" (Chuyển đổi từ Tiếng Việt sang Latin Alphabet) 45 | + "url" (Chuẩn hóa chuỗi để sử dụng cho các liên kết) 46 | + "file" (Chuẩn hóa chuỗi để sử dụng làm tên file) 47 | 48 | Ví dụ: 49 | 50 | ```js 51 | 52 | var str = 'Hoàng Sa - Trường Sa là của Việt Nam'; 53 | 54 | console.log('\n*** Bo dau ***\n'); 55 | console.log(KhongDau(str)); 56 | // Hoang Sa - Truong Sa la cua Viet Nam 57 | 58 | console.log('\n*** URL sau khi Bo Dau***\n'); 59 | console.log(KhongDau(str, ["chuyen", "url"])); 60 | // Hoang-Sa-Truong-Sa-la-cua-Viet-Nam 61 | 62 | console.log('\n*** FILE sau khi Bo Dau ***\n'); 63 | console.log(KhongDau(str, ["chuyen", "file"])); 64 | // Hoang Sa - Truong Sa la cua Viet Nam 65 | 66 | ``` 67 | 68 | ## Demo 69 | 70 | [Xem thêm ví dụ](./demo.js) 71 | 72 | ## Thay thế 73 | 74 | Bạn có thể sử dụng thư viện [transliteration](https://www.npmjs.com/package/transliteration) để thay thế. Thư viện này không chỉ chuyển đổi tiếng Việt mà còn nhiều ngôn ngữ khác. 75 | 76 | ## Giấy phép 77 | 78 | MIT 79 | --------------------------------------------------------------------------------