Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core,common,admin-ui): Implement normalizeString function using slug library #2721

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class CollectionDetailComponent
const currentTranslation = this.entity ? findTranslation(this.entity, this.languageCode) : undefined;
const currentSlugIsEmpty = !currentTranslation || !currentTranslation.slug;
if (slugControl && slugControl.pristine && currentSlugIsEmpty) {
slugControl.setValue(normalizeString(`${nameValue}`, '-'));
slugControl.setValue(normalizeString(`${nameValue}`, '-', this.languageCode));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class CreateProductOptionGroupDialogComponent implements Dialog<CreatePro
const nameControl = this.form.get('name');
const codeControl = this.form.get('code');
if (nameControl && codeControl && codeControl.pristine) {
codeControl.setValue(normalizeString(`${nameControl.value}`, '-'));
codeControl.setValue(normalizeString(`${nameControl.value}`, '-', this.languageCode));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class FacetDetailComponent
if (!currentCode) {
const codeControl = this.detailForm.get(['facet', 'code']);
if (codeControl && codeControl.pristine) {
codeControl.setValue(normalizeString(nameValue, '-'));
codeControl.setValue(normalizeString(nameValue, '-', this.languageCode));
}
}
}
Expand All @@ -129,7 +129,7 @@ export class FacetDetailComponent
if (!currentCode) {
const codeControl = this.detailForm.get(['values', valueId, 'code']);
if (codeControl && codeControl.pristine) {
codeControl.setValue(normalizeString(nameValue, '-'));
codeControl.setValue(normalizeString(nameValue, '-', this.languageCode));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export class ProductDetailComponent
const currentTranslation = this.entity ? findTranslation(this.entity, this.languageCode) : undefined;
const currentSlugIsEmpty = !currentTranslation || !currentTranslation.slug;
if (slugControl && slugControl.pristine && currentSlugIsEmpty) {
slugControl.setValue(normalizeString(`${nameValue}`, '-'));
slugControl.setValue(normalizeString(`${nameValue}`, '-', this.languageCode));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export class ProductVariantsEditorComponent implements OnInit, DeactivateAware {
this.dataService.product
.addOptionToGroup({
productOptionGroupId: group.id,
code: normalizeString(optionName, '-'),
code: normalizeString(optionName, '-', this.languageCode),
translations: [{ name: optionName, languageCode: this.languageCode }],
})
.subscribe(({ createProductOption }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class UpdateProductOptionDialogComponent

updateCode(nameValue: string) {
if (!this.codeInputTouched && !this.productOption.code) {
this.code = normalizeString(nameValue, '-');
this.code = normalizeString(nameValue, '-', this.activeLanguage);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ export class ProductDetailService {
groups.map(c =>
this.dataService.product
.createProductOptionGroups({
code: normalizeString(c.name, '-'),
code: normalizeString(c.name, '-', languageCode),
translations: [{ languageCode, name: c.name }],
options: c.values.map(v => ({
code: normalizeString(v, '-'),
code: normalizeString(v, '-', languageCode),
translations: [{ languageCode, name: v }],
})),
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class PaymentMethodDetailComponent
if (!currentCode) {
const codeControl = this.detailForm.get('code');
if (codeControl && codeControl.pristine) {
codeControl.setValue(normalizeString(nameValue, '-'));
codeControl.setValue(normalizeString(nameValue, '-', this.languageCode));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class RoleDetailComponent
updateCode(nameValue: string) {
const codeControl = this.detailForm.get(['code']);
if (codeControl && codeControl.pristine) {
codeControl.setValue(normalizeString(nameValue, '-'));
codeControl.setValue(normalizeString(nameValue, '-', this.languageCode));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export class ShippingMethodDetailComponent
if (!currentCode) {
const codeControl = this.detailForm.get(['code']);
if (codeControl && codeControl.pristine) {
codeControl.setValue(normalizeString(nameValue, '-'));
codeControl.setValue(normalizeString(nameValue, '-', this.languageCode));
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
"files": [
"lib/**/*"
],
"dependencies": {
"slug": "^8.2.3"
},
"devDependencies": {
"@types/slug": "^5.0.8",
"rimraf": "^3.0.2",
"typescript": "4.9.5"
}
Expand Down
12 changes: 10 additions & 2 deletions packages/common/src/normalize-string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,16 @@ describe('normalizeString()', () => {
expect(normalizeString('KONGREẞ im Straßennamen')).toBe('kongress im strassennamen');
});

// works for German language, might not work for e.g. Finnish language
// works for German language
it('replaces combining diaeresis with e', () => {
expect(normalizeString('Ja quäkt Schwyz Pöbel vor Gmünd')).toBe('ja quaekt schwyz poebel vor gmuend');
expect(normalizeString('Ja quäkt Schwyz Pöbel vor Gmünd', ' ', 'de')).toBe('ja quaekt schwyz poebel vor gmuend');
});

it('removes empty space', () => {
expect(normalizeString(' ')).toBe('');
});

it('transliterate cyrillic text', () => {
expect(normalizeString('українська паляниця', ' ', 'uk')).toBe('ukrayinska palyanytsya');
});
});
20 changes: 9 additions & 11 deletions packages/common/src/normalize-string.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import slug from 'slug';

slug.charmap['-'] = '-';
slug.charmap['.'] = '.';
slug.charmap._ = '_';

/**
* Normalizes a string to replace non-alphanumeric and diacritical marks with
* plain equivalents.
* Based on https://stackoverflow.com/a/37511463/772859
* Utilizes https://www.npmjs.com/package/slug
*/
export function normalizeString(input: string, spaceReplacer = ' '): string {
return (input || '')
.normalize('NFD')
.replace(/[\u00df]/g, 'ss')
.replace(/[\u1e9e]/g, 'SS')
.replace(/[\u0308]/g, 'e')
.replace(/[\u0300-\u036f]/g, '')
.toLowerCase()
.replace(/[!"£$%^&*()+[\]{};:@#~?\\/,|><`¬'=‘’©®™]/g, '')
.replace(/\s+/g, spaceReplacer);
export function normalizeString(input: string = '', spaceReplacer = ' ', languageCode?: string ): string {
return slug(input, { replacement: spaceReplacer, fallback: false, locale: languageCode });
}
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ export class ImportParser {
slug = parseString(r.slug);
}
if (slug.length === 0) {
slug = normalizeString(name, '-');
slug = normalizeString(name, '-', languageCode);
}
return {
languageCode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class SlugValidator {
if (input.translations) {
for (const t of input.translations) {
if (t.slug) {
t.slug = normalizeString(t.slug, '-');
t.slug = normalizeString(t.slug, '-', t.languageCode);
let match: E | null;
let suffix = 1;
const seen: ID[] = [];
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5518,6 +5518,11 @@
dependencies:
"@types/node" "*"

"@types/slug@^5.0.8":
version "5.0.8"
resolved "https://registry.yarnpkg.com/@types/slug/-/slug-5.0.8.tgz#76d40f033a8a99793f3dcbfb3f187ab9014f9d47"
integrity sha512-mblTWR1OST257k1gZ3QvqG+ERSr8Ea6dyM1FH6Jtm4jeXi0/r0/95VNctofuiywPxCVQuE8AuFoqmvJ4iVUlXQ==

"@types/sockjs@^0.3.33":
version "0.3.33"
resolved "https://registry.yarnpkg.com/@types/sockjs/-/sockjs-0.3.33.tgz#570d3a0b99ac995360e3136fd6045113b1bd236f"
Expand Down Expand Up @@ -16904,6 +16909,11 @@ slick@^1.12.2:
resolved "https://registry.yarnpkg.com/slick/-/slick-1.12.2.tgz#bd048ddb74de7d1ca6915faa4a57570b3550c2d7"
integrity sha512-4qdtOGcBjral6YIBCWJ0ljFSKNLz9KkhbWtuGvUyRowl1kxfuE1x/Z/aJcaiilpb3do9bl5K7/1h9XC5wWpY/A==

slug@^8.2.3:
version "8.2.3"
resolved "https://registry.yarnpkg.com/slug/-/slug-8.2.3.tgz#9aa33e532ae9ccddaf55a42abd9bf6a89d5e44d2"
integrity sha512-fXjhAZszNecz855GUNIwW0+sFPi9WV4bMiEKDOCA4wcq1ts1UnUVNy/F78B0Aat7/W3rA+se//33ILKNMrbeYQ==

smart-buffer@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/smart-buffer/-/smart-buffer-4.2.0.tgz#6e1d71fa4f18c05f7d0ff216dd16a481d0e8d9ae"
Expand Down
Loading