From e422143c2bfb6abf4ef60cbe512af78788398da8 Mon Sep 17 00:00:00 2001 From: Chris Pilson Date: Sun, 28 Oct 2018 21:30:59 -0700 Subject: [PATCH] Adding a-or-an pipe. --- README.md | 1 + docs/string.md | 19 ++++++++++++ src/string/a-or-an.pipe.spec.ts | 54 +++++++++++++++++++++++++++++++++ src/string/a-or-an.pipe.ts | 40 ++++++++++++++++++++++++ src/string/string.module.ts | 3 ++ src/utils/utils.ts | 5 +++ 6 files changed, 122 insertions(+) create mode 100644 src/string/a-or-an.pipe.spec.ts create mode 100644 src/string/a-or-an.pipe.ts diff --git a/README.md b/README.md index 1fe69b1..b3a8cc9 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,7 @@ You can find the documentations in the [`docs`](./docs) folder or on [`GitBook`] * [`stripTags`](./docs/string.md#striptags) * [`latinize`](./docs/string.md#latinize) * [`wrap`](./docs/string.md#wrap) +* [`a-or-an`](./docs/string.md#a-or-an) * [`with`](./docs/string.md#with) * [`reverseStr`](./docs/string.md#reversestr) diff --git a/docs/string.md b/docs/string.md index fb9d015..b8d2962 100644 --- a/docs/string.md +++ b/docs/string.md @@ -382,6 +382,25 @@ import { WrapPipe } from 'angular-pipes';

{{ 'foo' | wrap: '{{': '}}' }}

``` +#### a-or-an + +a-or-an pipe check if string needs to be prepended with `a` or with `an` + +Arguments: \( string \) + +##### File + +```typescript +import { AorAnPipe } from 'angular-pipes'; +``` + +##### Usage + +```html +{{'egg' | a-or-an}} +{{'peanut' | a-or-an}} +``` + #### with With pipe check string has start and/or ends diff --git a/src/string/a-or-an.pipe.spec.ts b/src/string/a-or-an.pipe.spec.ts new file mode 100644 index 0000000..5c70f6e --- /dev/null +++ b/src/string/a-or-an.pipe.spec.ts @@ -0,0 +1,54 @@ +import { AorAnPipe } from "./a-or-an.pipe"; + +describe("AorAnPipe Tests", () => { + let pipe: AorAnPipe; + beforeEach(() => { + pipe = new AorAnPipe(); + }); + describe("Misc. Inputs", () => { + it('should return "" if passed null', () => { + expect(pipe.transform(null)).toEqual("" as string); + }); + it('should return "" if passed undefined', () => { + expect(pipe.transform(undefined)).toEqual("" as string); + }); + it('should return "" if passed ""', () => { + expect(pipe.transform("")).toEqual("" as string); + }); + }); + describe("Words Prefixed With 'A'", () => { + it('should return "a cat" when passed "cat"', () => { + expect(pipe.transform("cat")).toEqual("a cat" as string); + }); + }); + describe("Words Prefixed With 'An'", () => { + it('should return "an egg" if passed "egg"', () => { + expect(pipe.transform("egg")).toEqual("an egg" as string); + }); + }); + describe("Multiple Words", () => { + it('should return "an aubergine from the store" if passed "aubergine from the store"', () => { + expect(pipe.transform("aubergine from the store")).toEqual("an aubergine from the store" as string); + }); + it('should return "a cat of many colors" if passed "cat of many colors"', () => { + expect(pipe.transform("cat of many colors")).toEqual("a cat of many colors" as string); + }); + it('should return "an M.Sc. from a large public Midwestern university" if passed "M.Sc. from a large public Midwestern university"', () => { + expect(pipe.transform("M.Sc. from a large public Midwestern university")).toEqual("an M.Sc. from a large public Midwestern university" as string); + }); + }); + describe("Words from the Irregular Group", () => { + it('should return "an herb" if passed "herb"', () => { + expect(pipe.transform("herb")).toEqual("an herb" as string); + }); + it('should return "an honor" if passed "honor"', () => { + expect(pipe.transform("honor")).toEqual("an honor" as string); + }); + it('should return "an M.Sc." if passed "M.Sc."', () => { + expect(pipe.transform("M.Sc.")).toEqual("an M.Sc." as string); + }); + it('should return "a unicorn" if passed "unicorn"', () => { + expect(pipe.transform("unicorn")).toEqual("a unicorn" as string); + }); + }); +}); diff --git a/src/string/a-or-an.pipe.ts b/src/string/a-or-an.pipe.ts new file mode 100644 index 0000000..6c620a4 --- /dev/null +++ b/src/string/a-or-an.pipe.ts @@ -0,0 +1,40 @@ +import { Pipe, PipeTransform } from "@angular/core"; +import { isVowel } from "../utils/utils"; +@Pipe({ + name: "a-or-an", +}) +/** + * Takes a string and returns the string prepended by 'a' or 'an'. + * Uses both naive and holdout-list approaches. + * @constructor + * @param {string} stringEntity - Entity to prepend 'a' or 'an' to. + */ +export class AorAnPipe implements PipeTransform { + private irregularMap: any = { + aubergine: 'an', + herb: 'an', + honor: 'an', + honorable: 'an', + hour: 'an', + mba: 'an', + msc: 'an', + 'm.sc.': 'an', + umbrella: 'an', + unicorn: 'a', + }; + transform(stringEntity: string): string { + if (!stringEntity || stringEntity === "") { + return ""; + } + else { + const firstWord = stringEntity.trim().split(" ")[0]; + if (this.irregularMap[firstWord.toLocaleLowerCase()]) { + return `${this.irregularMap[firstWord.toLocaleLowerCase()]} ${stringEntity}`; + } else { + return (isVowel(stringEntity[0])) + ? `an ${stringEntity}` + : `a ${stringEntity}`; + } + } + } +} \ No newline at end of file diff --git a/src/string/string.module.ts b/src/string/string.module.ts index ba515f6..10a6ea0 100644 --- a/src/string/string.module.ts +++ b/src/string/string.module.ts @@ -22,6 +22,7 @@ import {SlugifyPipe} from './slugify.pipe'; import {StripTagsPipe} from "./strip-tags.pipe"; import {LatinizePipe} from "./latinize.pipe"; import {WrapPipe} from "./wrap.pipe"; +import {AorAnPipe} from "./a-or-an.pipe"; import {WithPipe} from "./with.pipe"; import {ReverseStrPipe} from "./reverse-str.pipe"; @@ -50,6 +51,7 @@ import {ReverseStrPipe} from "./reverse-str.pipe"; StripTagsPipe, LatinizePipe, WrapPipe, + AorAnPipe, WithPipe, ReverseStrPipe ], @@ -76,6 +78,7 @@ import {ReverseStrPipe} from "./reverse-str.pipe"; StripTagsPipe, LatinizePipe, WrapPipe, + AorAnPipe, WithPipe, ReverseStrPipe ] diff --git a/src/utils/utils.ts b/src/utils/utils.ts index c4f3c9a..e72ebf2 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -42,6 +42,11 @@ export function isString (value: any): value is string { return typeof value === 'string'; } +export function isVowel(letter: string): boolean { + const vowels = ["a", "e", "i", "o", "u"]; + return vowels.indexOf(letter) !== -1; +} + export function isObject (value: any): boolean { return value !== null && typeof value === 'object';