Overview of @vn-utils/text library
@vn-utils/text is a free, open-source library designed to help developers work more efficiently with the Vietnamese language. It is available on NPM and GitHub.
website·Indexed 29 days ago
https://vn-text.vercel.app/A library for processing Vietnamese text, providing utilities to remove or check for diacritics and tone marks, and tools for creating Vietnamese-specific regular expressions.
createRegex function from @vn-utils/text can be used directly in Mongoose queries if the environment supports JavaScript RegExp objects. If the specific MongoDB driver version does not support passing the RegExp object directly, use the .source and .flags properties of the generated regex within the $regex and $options operators.import { createRegex, CreateRegexOptions } from '@vn-utils/text';
const options: CreateRegexOptions = { /* your options */ };
const keywordRegex = createRegex('từ khoá tìm kiếm', options);
// Option 1: Direct RegExp object usage
const foundData = await model.find({ $or: [{ name: keywordRegex }] });
// Option 2: Using .source and .flags for environments that require explicit $regex operators
const foundDataAlt = await model.find({
$or: [{ name: { $regex: keywordRegex.source, $options: keywordRegex.flags } }],
});To use @vn-utils/text, ensure your environment meets the following requirements:
@vn-utils/text library using your preferred package manager via the command line.# Using NPM
npm install @vn-utils/text
# Using Yarn
yarn add @vn-utils/text
# Using PNPM
pnpm install @vn-utils/text<VERSION> with the desired version number.<!-- Latest version -->
<script src="https://cdn.jsdelivr.net/npm/@vn-utils/text@latest/lib/bundle.js"></script>
<!-- Specific version -->
<script src="https://cdn.jsdelivr.net/npm/@vn-utils/text@<VERSION>/lib/bundle.js"></script>// Search for the exact word 'học'
học
// Search for all variations of 'hoc' with different tone marks (o, ó, ọ, ỏ, õ)
h[oóọỏõ]c// ES Module
import * as vnUtilsText from '@vn-utils/text';
// CommonJS
const vnUtilsText = require('@vn-utils/text');createRegex with removeDiacritics and removeTone. This ensures that a search term like 'Có dấu' matches variations such as 'Có dau day du' or 'Co dấu đầy du'.import { createRegex, removeDiacritics, removeTone } from '@vn-utils/text';
const data = ['Có dấu đầy đủ', 'Có dau day du', 'Co dấu đầy du', 'Có dáu day'];
// Strict match: only matches exact diacritics
data.filter((item) => createRegex('Có dấu').test(item)); // 1 result
// Broad match: removes both diacritics and tones to match all variations
data.filter((item) => createRegex(removeTone(removeDiacritics('Có dấu'))).test(item)); // 4 resultsvnUtilsText variable. You can use this variable directly or assign it to a custom variable.<script>
// Direct usage
vnUtilsText.hasTone('các dấu sắc huyền ngã hỏi nặng có tồn tại');
// Assigning to a custom variable
const customVariable = vnUtilsText;
customVariable.hasTone('các dấu sắc huyền ngã hỏi nặng có tồn tại');
</script>createRegex function from @vn-utils/text generates a regular expression suitable for searching Vietnamese text. By default, it allows for flexible matching (e.g., matching 'Ha Noi' against 'Hà Nội').import { createRegex } from '@vn-utils/text';
const regex = createRegex('Tiếng Việt');
const data = ['Hà Nội', 'Hải Phòng'];
// Default behavior: accent-insensitive matching
data.filter((item) => createRegex('Ha Noi').test(item)); // [ 'Hà Nội' ]Vietnamese uses the following tone marks to modify the meaning and pronunciation of words:
Vietnamese uses several diacritical marks (dấu phụ) to modify the sound of letters. These include: