{"id":250,"date":"2023-07-12T19:27:01","date_gmt":"2023-07-12T21:27:01","guid":{"rendered":"https:\/\/blog.iordanov.info\/?p=250"},"modified":"2023-07-13T10:30:46","modified_gmt":"2023-07-13T12:30:46","slug":"pipes-utilization-to-display-friendly-labels-for-enums-in-angular","status":"publish","type":"post","link":"https:\/\/blog.iordanov.info\/?p=250","title":{"rendered":"Pipes utilization to display friendly labels for Enums in Angular"},"content":{"rendered":"\n<div class=\"wp-block-group alignwide\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<div class=\"wp-block-media-text alignwide is-stacked-on-mobile is-vertically-aligned-top\" style=\"grid-template-columns:30% auto\"><figure class=\"wp-block-media-text__media\"><img loading=\"lazy\" decoding=\"async\" width=\"234\" height=\"159\" src=\"https:\/\/blog.iordanov.info\/wp-content\/uploads\/2023\/07\/vlog-img-2.png\" alt=\"\" class=\"wp-image-258 size-full\"\/><\/figure><div class=\"wp-block-media-text__content\">\n<p class=\"has-text-align-justify wp-block-paragraph\">In this article, I will&nbsp;<em>show<\/em>&nbsp;a simple solution&nbsp;to display a user-friendly labels for enumerations in angular using Pipes. The solution is an example of <a rel=\"noreferrer noopener\" href=\"https:\/\/levelup.gitconnected.com\/software-engineering-best-practices-for-reusing-in-software-development-6006f9d8d364\" target=\"_blank\">reuse-based software engineering strategy<\/a>.&nbsp; <\/p>\n<\/div><\/div>\n<\/div><\/div>\n\n\n\n<p class=\"has-text-align-justify wp-block-paragraph\">It define a design pattern (template) that can be repeatedly applied to a commonly recurring situation in software design. The patterns ensure a good practice environment, quality assurance, and compatible test results. The development process involves implementing the design pattern in a reusable way, allowing for easier maintenance and updates in the future. This approach promotes efficiency, reduces redundancy, and enhances the overall software development lifecycle.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For this purpose an common class EnumViewModel width following definition is used. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export class EnumViewModel {\n    id: number;\n    name: string;\n\n    constructor(){\n        this.id = -1;\n        this.name = 'Default';\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">An example enumeration:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export enum AircraftStatus {\n    New = 1,\n    Operational = 2,\n    Maintenance = 3,\n    ForLeaseOrSale = 4,\n    Retired = 5\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"> For each enumeration an option array of type EnumViewModel is defined<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export const AircraftStatusOpt: EnumViewModel&#91;] = &#91;\n    {\n        id: 1,\n        name: 'New'\n    },\n    {\n        id: 2,\n        name: 'Operational'\n    },\n    {\n        id: 3,\n        name: 'Maintenance'\n    },\n    {\n        id: 4,\n        name: 'For Lease Or Sale'\n    },\n    {\n        id: 5,\n        name: 'Retired'\n    },\n];<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Next is a pipe definition:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Pipe({ name: 'acstatus' })\nexport class AcStatusDisplayPipe implements PipeTransform {\n    transform(value: number, args?: string&#91;]): string {\n        let rv: string = AircraftStatus&#91;value];\n        const data: EnumViewModel = AircraftStatusOpt.find(x =&gt; x.id === value);\n        if (data) {\n            rv = data.name;\n        }\n        return rv;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Entire code of the solution can be found in <a rel=\"noreferrer noopener\" href=\"https:\/\/stackblitz.com\/edit\/stackblitz-starters-ngxh8p?file=src%2Fmain.ts\" target=\"_blank\">this stackbiz<\/a> project.<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<div class=\"wp-block-media-text alignwide is-stacked-on-mobile is-vertically-aligned-top is-image-fill\"><figure class=\"wp-block-media-text__media\" style=\"background-image:url(https:\/\/blog.iordanov.info\/wp-content\/uploads\/2023\/07\/pipe-utilization-design-patern.png);background-position:100% 100%\"><img loading=\"lazy\" decoding=\"async\" width=\"517\" height=\"527\" src=\"https:\/\/blog.iordanov.info\/wp-content\/uploads\/2023\/07\/pipe-utilization-design-patern.png\" alt=\"\" class=\"wp-image-272 size-full\" srcset=\"https:\/\/blog.iordanov.info\/wp-content\/uploads\/2023\/07\/pipe-utilization-design-patern.png 517w, https:\/\/blog.iordanov.info\/wp-content\/uploads\/2023\/07\/pipe-utilization-design-patern-294x300.png 294w\" sizes=\"auto, (max-width: 517px) 100vw, 517px\" \/><\/figure><div class=\"wp-block-media-text__content\">\n<p class=\"has-text-align-justify wp-block-paragraph\">This is an screenshot of my <a rel=\"noreferrer noopener\" href=\"https:\/\/stackblitz.com\/edit\/stackblitz-starters-ngxh8p?file=src%2Fmain.ts\" target=\"_blank\">stackblitz <\/a>example where you can see utilization of this structural design-pattern in a table, select box and text. Described in this post pattern falls into <strong>Structural<\/strong> Design Pattern category.<\/p>\n<\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">Structural Design Pattern<\/mark><\/strong><\/p>\n\n\n\n<p class=\"has-text-align-justify wp-block-paragraph\">A design pattern is a description or template that can be repeatedly applied to a commonly recurring problem in software design. There are three pattern types which are creational, structural, and behavioral patterns. Described in this post pattern falls into <strong>Structural<\/strong> category, that focuses more on how classes and objects are composed using the different structural techniques, and to form structures with more or altered flexibility.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this article, I will&nbsp;show&nbsp;a simple solution&nbsp;to display a user-friendly labels for enumerations in angular using Pipes. The solution is an example of reuse-based software engineering strategy.&nbsp; It define a design pattern (template) that can be repeatedly applied to a &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/blog.iordanov.info\/?p=250\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":253,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[41,31],"tags":[38,40,39,46,45,42,44],"class_list":["post-250","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular","category-javascript","tag-angular","tag-enums","tag-pipes","tag-stacjblitz-example","tag-stackblitz","tag-structural-design-pattern","tag-typescript"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p3jsZJ-42","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_featured_media_url":"https:\/\/blog.iordanov.info\/wp-content\/uploads\/2023\/07\/vlog-img.png","_links":{"self":[{"href":"https:\/\/blog.iordanov.info\/index.php?rest_route=\/wp\/v2\/posts\/250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.iordanov.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.iordanov.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.iordanov.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.iordanov.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=250"}],"version-history":[{"count":24,"href":"https:\/\/blog.iordanov.info\/index.php?rest_route=\/wp\/v2\/posts\/250\/revisions"}],"predecessor-version":[{"id":282,"href":"https:\/\/blog.iordanov.info\/index.php?rest_route=\/wp\/v2\/posts\/250\/revisions\/282"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.iordanov.info\/index.php?rest_route=\/wp\/v2\/media\/253"}],"wp:attachment":[{"href":"https:\/\/blog.iordanov.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iordanov.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iordanov.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}