Skip to content
This repository has been archived by the owner on Feb 23, 2023. It is now read-only.

Commit

Permalink
Merge e422143 into 2615b1b
Browse files Browse the repository at this point in the history
  • Loading branch information
cpilson committed Oct 29, 2018
2 parents 2615b1b + e422143 commit d062478
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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)

Expand Down
19 changes: 19 additions & 0 deletions docs/string.md
Expand Up @@ -382,6 +382,25 @@ import { WrapPipe } from 'angular-pipes';
<p>{{ 'foo' | wrap: '{{': '}}' }}</p> <!--result: {{foo}} -->
```

#### 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}} <!-- result: 'an egg' -->
{{'peanut' | a-or-an}} <!-- result: 'a peanut' -->
```

#### with

With pipe check string has start and/or ends
Expand Down
54 changes: 54 additions & 0 deletions 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);
});
});
});
40 changes: 40 additions & 0 deletions 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}`;
}
}
}
}
3 changes: 3 additions & 0 deletions src/string/string.module.ts
Expand Up @@ -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";

Expand Down Expand Up @@ -50,6 +51,7 @@ import {ReverseStrPipe} from "./reverse-str.pipe";
StripTagsPipe,
LatinizePipe,
WrapPipe,
AorAnPipe,
WithPipe,
ReverseStrPipe
],
Expand All @@ -76,6 +78,7 @@ import {ReverseStrPipe} from "./reverse-str.pipe";
StripTagsPipe,
LatinizePipe,
WrapPipe,
AorAnPipe,
WithPipe,
ReverseStrPipe
]
Expand Down
5 changes: 5 additions & 0 deletions src/utils/utils.ts
Expand Up @@ -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';
Expand Down

0 comments on commit d062478

Please sign in to comment.