This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}`; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters