<|start_filename|>demo.js<|end_filename|>
var KhongDau = require('./index.js');

var str = 'Hoàng Sa - Trường Sa là của Việt Nam';
var alphabet = 'abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZáàảãạăắằẳẵặâấầẩẫậAÀẢÃẠĂẮẰẲẴẶÂẤẦẨẪẬđĐéèẻẽẹêếềểễệÉÈẺẼẸÊẾỀỂỄỆíìỉĩịÍÌỈĨỊóòỏõọôốồổỗộơớờởỡợÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢúùủũụưứừửữựÚÙỦŨỤƯỨỪỬỮỰýỳỷỹỵÝỲỶỸỴ';

console.log('\n*** Bo dau ***\n');
console.log(KhongDau(str));
console.log(KhongDau(alphabet));

console.log('\n*** URL sau khi Bo Dau***\n');
console.log(KhongDau(str, ["chuyen", "url"]));
console.log(KhongDau(alphabet, ["chuyen", "url"]));

console.log('\n*** FILE sau khi Bo Dau ***\n');
console.log(KhongDau(str, ["chuyen", "file"]));
console.log(KhongDau(alphabet, ["chuyen", "file"]));


<|start_filename|>test/khongdau.v2.test.js<|end_filename|>
const { expect } = require('chai');
const KhongDau = require('../lib/khongdau');

describe('Traditional khong dau v2', () => {
  it('Basic convert', async () => {
    const src = 'Hoàng Sa, Trường Sa là của Việt Nam';
    const dst = KhongDau(src);
    const dst1 = KhongDau(src, ["chuyen", "url"]);
    const dst2 = KhongDau(src, ["chuyen", "file"]);

    expect(dst).to.be.equal('Hoang Sa, Truong Sa la cua Viet Nam');
    expect(dst1).to.be.equal('Hoang-Sa-Truong-Sa-la-cua-Viet-Nam');
    expect(dst2).to.be.equal('Hoang Sa, Truong Sa la cua Viet Nam');
  });

  it('Alphabet convert', async () => {
    const src = 'abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZáàảãạăắằẳẵặâấầẩẫậAÀẢÃẠĂẮẰẲẴẶÂẤẦẨẪẬđĐéèẻẽẹêếềểễệÉÈẺẼẸÊẾỀỂỄỆíìỉĩịÍÌỈĨỊóòỏõọôốồổỗộơớờởỡợÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢúùủũụưứừửữựÚÙỦŨỤƯỨỪỬỮỰýỳỷỹỵÝỲỶỸỴ';
    const rel = 'abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZaaaaaaaaaaaaaaaaaAAAAAAAAAAAAAAAAAdDeeeeeeeeeeeEEEEEEEEEEEiiiiiIIIIIoooooooooooooooooOOOOOOOOOOOOOOOOOuuuuuuuuuuuUUUUUUUUUUUyyyyyYYYYY';
    const dst = KhongDau(src);
    const dst1 = KhongDau(src, ["chuyen", "url"]);
    const dst2 = KhongDau(src, ["chuyen", "file"]);

    expect(dst).to.be.equal(rel);
    expect(dst1).to.be.equal(rel);
    expect(dst2).to.be.equal(rel);
  });
});

<|start_filename|>test/khongdau.v1.test.js<|end_filename|>
const { expect } = require('chai');
const KhongDau = require('../lib/khongdau');

describe('Traditional khong dau v1', () => {
  it('Basic convert', async () => {
    const src = 'Hoàng Sa, Trường Sa là của Việt Nam';
    const dst = KhongDau.c(src);
    const dst1 = KhongDau.cLowerCase(src);
    const dst2 = KhongDau.cUpperCase(src);
    const dst3 = KhongDau.cFriendlyURI(src);

    expect(dst).to.be.equal('Hoang Sa, Truong Sa la cua Viet Nam');
    expect(dst1).to.be.equal('hoang sa, truong sa la cua viet nam');
    expect(dst2).to.be.equal('HOANG SA, TRUONG SA LA CUA VIET NAM');
    expect(dst3).to.be.equal('hoang-sa-truong-sa-la-cua-viet-nam');
  });

  it('Alphabet convert', async () => {
    const src = 'abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZáàảãạăắằẳẵặâấầẩẫậAÀẢÃẠĂẮẰẲẴẶÂẤẦẨẪẬđĐéèẻẽẹêếềểễệÉÈẺẼẸÊẾỀỂỄỆíìỉĩịÍÌỈĨỊóòỏõọôốồổỗộơớờởỡợÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢúùủũụưứừửữựÚÙỦŨỤƯỨỪỬỮỰýỳỷỹỵÝỲỶỸỴ';
    const rel = 'abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZaaaaaaaaaaaaaaaaaAAAAAAAAAAAAAAAAAdDeeeeeeeeeeeEEEEEEEEEEEiiiiiIIIIIoooooooooooooooooOOOOOOOOOOOOOOOOOuuuuuuuuuuuUUUUUUUUUUUyyyyyYYYYY';
    const dst = KhongDau.c(src);
    const dst1 = KhongDau.cLowerCase(src);
    const dst2 = KhongDau.cUpperCase(src);

    expect(dst).to.be.equal(rel);
    expect(dst1).to.be.equal(rel.toLowerCase());
    expect(dst2).to.be.equal(rel.toUpperCase());
  });
});

<|start_filename|>package.json<|end_filename|>
{
  "name": "khong-dau",
  "version": "2.0.0",
  "description": "Convert Vietnamese characters to Latin characters",
  "main": "index.js",
  "scripts": {
    "test": "mocha",
    "coverage": "nyc npm run test"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/vuanhhaogk/khong-dau.git"
  },
  "keywords": [
    "Vietnamese",
    "Language",
    "NodeJS",
    "Javascript",
    "Server",
    "Client",
    "Converter"
  ],
  "author": "<NAME> <<EMAIL>> (http://xn--vanhhoblog-l4a35n.vn)",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/vuanhhaogk/khong-dau/issues"
  },
  "homepage": "https://github.com/vuanhhaogk/khong-dau#readme",
  "dependencies": {
    "chai": "^4.2.0",
    "mocha": "^8.2.1",
    "nyc": "^15.1.0",
    "sanitize-filename": "^1.6.1"
  }
}


<|start_filename|>index.js<|end_filename|>
module.exports = require('./lib/khongdau.js');


<|start_filename|>lib/khongdau.js<|end_filename|>
const sanitize = require('sanitize-filename');

function KhongDau(str, rules){
	rules = rules || ["chuyen"]

	for (var i = 0; i < rules.length; i++)
	switch (rules[i]){
		case "chuyen":
			str = KhongDau.c(str);
			break;
		case "url":
			str = KhongDau.cURI(str);
			break;
		case "file":
			str = sanitize(str);
			break;
	}

	return str;
}

KhongDau.c = function(str){
	str = str.replace(/(á|à|ả|ã|ạ|ă|ắ|ằ|ẳ|ẵ|ặ|â|ấ|ầ|ẩ|ẫ|ậ)/g, 'a');
	str = str.replace(/(A|Á|À|Ả|Ã|Ạ|Ă|Ắ|Ằ|Ẳ|Ẵ|Ặ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ)/g, 'A');
	str = str.replace(/đ/g, 'd');
	str = str.replace(/Đ/g, 'D');
	str = str.replace(/(é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ)/g, 'e');
	str = str.replace(/(É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ)/g, 'E');
	str = str.replace(/(í|ì|ỉ|ĩ|ị)/g, 'i');
	str = str.replace(/(Í|Ì|Ỉ|Ĩ|Ị)/g, 'I');
	str = str.replace(/(ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ)/g, 'o');
	str = str.replace(/(Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ)/g, 'O');
	str = str.replace(/(ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự)/g, 'u');
	str = str.replace(/(Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự)/g, 'U');
	str = str.replace(/(ý|ỳ|ỷ|ỹ|ỵ)/g, 'y');
	str = str.replace(/(Ý|Ỳ|Ỷ|Ỹ|Ỵ)/g, 'Y');

	return str;
};

KhongDau.cLowerCase = function(str){
	return this.c(str).toLowerCase();
};

KhongDau.cUpperCase = function(str){
	return this.c(str).toUpperCase();
};

KhongDau.cURI = function(str){
	str = str.replace(/[^a-zA-Z0-9_-]/g, '-');

	while (str.length > 0 && (/--/g).test(str)){
		str = str.replace(/--/g, '-');
	}

	return str;
}

KhongDau.cFriendlyURI = function(str){
	str = this.cLowerCase(str);

	str = this.cURI(str).toLowerCase();

	return str;
}

// Export if using node
try {module.exports = KhongDau; } catch(e){};
