Skip to content

Commit

Permalink
v2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
skip405 committed Apr 5, 2023
1 parent 0cd7c09 commit 758d2d7
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 260 deletions.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Changelog

## v2.0.0 - 2023-04-05

### Changed

- **Breaking:** `geographical` option changed into `geo-2000`
- Bump `jest` to v29.5.0
- Internal data structure

### Added

- `geo-2023` style option

### Removed

- **Breaking:** `slugify` style option and @sindresorhus/slugify dependency
- **Breaking:** `geographical` style option. Use `geo-2000` instead

## v1.0.0 - 2022-12-30

Initial version.
19 changes: 5 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# belLat

Convert cyrillic Belarusian characters to Latin characters using transliteration. Can transliterate in accordance with the ["Instruction on transliteration of geographical names"](https://en.wikipedia.org/wiki/Instruction_on_transliteration_of_Belarusian_geographical_names_with_letters_of_Latin_script) or in accordance with the rules of the [Belarusian Latin alphabet (Łacinka)](https://en.wikipedia.org/wiki/Belarusian_Latin_alphabet). Can slugify the resulted transliterated string.
Convert cyrillic Belarusian characters to Latin characters using transliteration. Can transliterate in accordance with the ["Instruction on transliteration of geographical names"](https://en.wikipedia.org/wiki/Instruction_on_transliteration_of_Belarusian_geographical_names_with_letters_of_Latin_script) (2000 and 2023) or in accordance with the rules of the [Belarusian Latin alphabet (Łacinka)](https://en.wikipedia.org/wiki/Belarusian_Latin_alphabet).

## Installation

Expand Down Expand Up @@ -28,22 +28,13 @@ belLat('Лацінка', { style: 'lacinka' }); // Łacinka

## Instruction for geographical names

You can specify conversion in accordance with the instruction for geographical names, e.g.
You can specify conversion in accordance with the instructions for geographical names (2000 and 2023), e.g.

```javascript
import belLat from '@skip405/bel-lat';

belLat('Лацінка', { style: 'geographical' }); // Lacinka
```

## Creating slugs

The package uses the wonderful [@sindresorhus/slugify](https://github.com/sindresorhus/slugify) package to create slugified strings after conversion.

```javascript
import belLat from '@skip405/bel-lat';

belLat("прывітанне, сусвет", { style: 'slugify' }); // pryvitannie-susviet
belLat('Шчучыншчына', { style: 'geo-2000' }); // Ščučynščyna
belLat('Шчучыншчына', { style: 'geo-2023' }); // Shchuchynshchyna
```

## Basic replacements
Expand All @@ -54,7 +45,7 @@ The package allows to specify own replacement symbols.
import belLat from '@skip405/bel-lat';

belLat("", {
customReplacements: [ ['', { regular: '#' }] ]
customReplacements: [ ['', ['#']] ]
}); // #
```

Expand Down
206 changes: 62 additions & 144 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
import slugify from '@sindresorhus/slugify';

const processSpecialCase = (char, nextChar, prevChar, style, replacement) => {
let variant = 'regular';
let index = 0;

switch( char.toLowerCase() ) {
case 'ў' :
variant = 'with-breve';
index = 1;

if( 'slugify' === style ) {
variant = 'regular';
if( 'geo-2023' === style ) {
index = 2;
}

break;
case 'л' :
if( 'geographic' === style ) {
if( 'geo-2000' === style ) {
if( ['ь'].includes(nextChar.toLowerCase()) ) {
variant = 'with-acute';
index = 2;
} else {
variant = 'regular';
index = 0;
}
} else if( 'lacinka' === style ) {
if( ['е', 'ё', 'ю', 'я', 'ь'].includes(nextChar.toLowerCase()) ) {
variant = 'regular';
index = 0;
} else {
variant = 'with-stroke';
index = 1;
}
}

Expand All @@ -32,10 +30,8 @@ const processSpecialCase = (char, nextChar, prevChar, style, replacement) => {
case 'ё' :
case 'ю' :
case 'я' :
variant = 'with-i';

if( ['а', 'і', 'о', 'у', 'ы', 'ў', 'э', 'ь', '’', '‘'].includes(prevChar.toLowerCase()) || '' === prevChar ) {
variant = 'with-j';
index = 1;
}

break;
Expand All @@ -44,26 +40,26 @@ const processSpecialCase = (char, nextChar, prevChar, style, replacement) => {
case 'с' :
case 'ц' :
if( ['ь'].includes(nextChar.toLowerCase()) ) {
variant = 'with-acute';
index = 1;
}
if( 'slugify' === style ) {
variant = 'regular';
if( 'geo-2023' === style ) {
index = 0;
}

break;
case 'ж' :
case 'ч' :
case 'ш' :
variant = 'with-caron';
index = 1;

if( 'slugify' === style ) {
variant = 'regular';
if( 'geo-2023' === style ) {
index = 2;
}

break;
}

return processReturnValue(replacement[variant], char, nextChar, style);
return processReturnValue(replacement[index], char, nextChar, style);
}

const processChar = (char, nextChar, prevChar, style, replacement) => {
Expand All @@ -77,20 +73,22 @@ const processChar = (char, nextChar, prevChar, style, replacement) => {
return processSpecialCase(char, nextChar, prevChar, style, replacement);
}

return processReturnValue(replacement.regular, char, nextChar, style);
let replacementValue = replacement[0];

if( 'geo-2023' === style && replacement[1] ) {
replacementValue = replacement[1];
}

return processReturnValue(replacementValue, char, nextChar, style);
}

const processReturnValue = (value, char, nextChar, style) => {
if( 'slugify' === style ) {
return value.toLowerCase();
} else {
if( char === char.toUpperCase() && char !== char.toLowerCase() ) {
if( '' !== nextChar && nextChar === nextChar.toUpperCase() ) {
return value.toUpperCase();
}

return `${value[0].toUpperCase()}${value.slice(1)}`;
if( char === char.toUpperCase() && char !== char.toLowerCase() ) {
if( '' !== nextChar && nextChar === nextChar.toUpperCase() ) {
return value.toUpperCase();
}

return `${value[0].toUpperCase()}${value.slice(1)}`;
}

return value;
Expand All @@ -108,123 +106,45 @@ export default function belLat(string, options) {
};

const replacements = new Map([
['а', {
'regular': 'a',
}],
['б', {
'regular': 'b',
}],
['в', {
'regular': 'v',
}],
['г', {
'regular': 'h',
}],
['ґ', {
'regular': 'g',
}],
['д', {
'regular': 'd',
}],
['е', {
'with-i': 'ie',
'with-j': 'je',
}],
['ё', {
'with-i': 'io',
'with-j': 'jo',
}],
['ж', {
'regular': 'z',
'with-caron': 'ž',
}],
['з', {
'regular': 'z',
'with-acute': 'ź',
}],
['і', {
'regular': 'i',
}],
['й', {
'regular': 'j',
}],
['к', {
'regular': 'k',
}],
['л', {
'regular': 'l',
'with-stroke': 'ł',
'with-acute': 'ĺ',
}],
['м', {
'regular': 'm',
}],
['н', {
'regular': 'n',
'with-acute': 'ń',
}],
['о', {
'regular': 'o',
}],
['п', {
'regular': 'p',
}],
['р', {
'regular': 'r',
}],
['с', {
'regular': 's',
'with-acute': 'ś',
}],
['т', {
'regular': 't',
}],
['у', {
'regular': 'u',
}],
['ў', {
'regular': 'u',
'with-breve': 'ŭ',
}],
['ф', {
'regular': 'f',
}],
['х', {
'regular': 'ch',
}],
['ц', {
'regular': 'c',
'with-acute': 'ć',
}],
['ч', {
'regular': 'c',
'with-caron': 'č',
}],
['ш', {
'regular': 's',
'with-caron': 'š',
}],
['а', ['a']],
['б', ['b']],
['в', ['v']],
['г', ['h', 'g']],
['ґ', ['g']],
['д', ['d']],
['е', ['ie', 'je']],
['ё', ['io', 'jo']],
['ж', ['z', 'ž', 'zh']],
['з', ['z', 'ź']],
['і', ['i']],
['й', ['j']],
['к', ['k']],
['л', ['l', 'ł', 'ĺ']],
['м', ['m']],
['н', ['n', 'ń']],
['о', ['o']],
['п', ['p']],
['р', ['r']],
['с', ['s', 'ś']],
['т', ['t']],
['у', ['u']],
['ў', ['u', 'ŭ', 'w']],
['ф', ['f']],
['х', ['ch', 'h']],
['ц', ['c', 'ć']],
['ч', ['c', 'č', 'ch']],
['ш', ['s', 'š', 'sh']],
['ь', '_omitted'],
['ы', {
'regular': 'y',
}],
['э', {
'regular': 'e',
}],
['ю', {
'with-i': 'iu',
'with-j': 'ju',
}],
['я', {
'with-i': 'ia',
'with-j': 'ja'
}],
['ы', ['y']],
['э', ['e']],
['ю', ['iu', 'ju']],
['я', ['ia', 'ja']],
['’', '_omitted'],
['‘', '_omitted'],
...options.customReplacements
]);

const transformedString = string.split(' ').map( word => {
return string.split(' ').map( word => {
if( '' === word ) {
return '';
}
Expand All @@ -248,6 +168,4 @@ export default function belLat(string, options) {

return resultForWord.join('');
} ).join(' ');

return 'slugify' === options.style ? slugify( transformedString ) : transformedString;
}
13 changes: 5 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "@skip405/bel-lat",
"version": "1.0.2",
"version": "2.0.0",
"repository": {
"type": "git",
"url": "https://github.com/skip405/bel-lat.git"
},
"description": "Convert cyrillic Belarusian characters to Latin characters using transliteration. Can transliterate in accordance with the \"Instruction on transliteration of geographical names\" or in accordance with the rules of the Belarusian Latin alphabet (Łacinka). Can slugify the resulted transliterated string.",
"description": "Convert cyrillic Belarusian characters to Latin characters using transliteration. Can transliterate in accordance with the \"Instruction on transliteration of geographical names\" or in accordance with the rules of the Belarusian Latin alphabet (Łacinka).",
"exports": "./index.js",
"main": "index.js",
"scripts": {
Expand All @@ -15,8 +15,8 @@
"node": ">=12"
},
"bugs": {
"url" : "https://github.com/skip405/bel-lat/issues",
"email" : "[email protected]"
"url": "https://github.com/skip405/bel-lat/issues",
"email": "[email protected]"
},
"files": [
"LICENSE.md",
Expand Down Expand Up @@ -48,10 +48,7 @@
"transform": {}
},
"license": "MIT",
"dependencies": {
"@sindresorhus/slugify": "^2.1.1"
},
"devDependencies": {
"jest": "^29.3.1"
"jest": "^29.5.0"
}
}
Loading

0 comments on commit 758d2d7

Please sign in to comment.