indefinite
Advanced tools
Comparing version 2.0.3 to 2.0.4
@@ -64,3 +64,3 @@ window["indefinite"] = | ||
/******/ // Load entry module and return exports | ||
/******/ return __webpack_require__(__webpack_require__.s = 0); | ||
/******/ return __webpack_require__(__webpack_require__.s = 1); | ||
/******/ }) | ||
@@ -70,59 +70,2 @@ /************************************************************************/ | ||
/* 0 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
var irregulars = __webpack_require__(1); | ||
var ACRONYM = /^U[A-Z]+$/; | ||
var STARTS_WITH_VOWEL = /^[aeiou]/; | ||
var EXTRAS = /[\s'-]/; | ||
var xor = function xor(a, b) { | ||
return (a || b) && !(a && b); | ||
}; | ||
var checkForAcronyms = function checkForAcronyms(word, caseInsensitive) { | ||
return caseInsensitive ? false : ACRONYM.test(word.split(' ')[0]); | ||
}; | ||
var cap = function cap(str) { | ||
return '' + str.charAt(0).toUpperCase() + str.slice(1); | ||
}; | ||
// Try some variations on the word to determine whether it's irregular. | ||
// Specifically, try trimming s, then es, then ed because those are common | ||
// forms of plurals and past tense verbs (which can be used like adjectives). | ||
var checkForIrregulars = function checkForIrregulars(part) { | ||
return [null, 's', 'es', 'ed'].reduce(function (memo, ending) { | ||
return memo || irregulars.check(part, ending); | ||
}, false); | ||
}; | ||
var indefinite = function indefinite(word) { | ||
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var isAcronymWithU = checkForAcronyms(word, opts.caseInsensitive); | ||
var startsWithVowel = STARTS_WITH_VOWEL.test(word.toLowerCase()); | ||
// Only check the first word. Also, if it's hyphenated, only | ||
// check the first part. Finally, if it's possessive, ignore | ||
// the possessive part. | ||
var part = word.split(EXTRAS)[0].toLowerCase(); | ||
var isIrregular = checkForIrregulars(part); | ||
/** | ||
* If it's an acronym that starts with "u," the article should be "a" | ||
* If it starts with a vowel and isn't irregular, it should be "an" | ||
* If it starts with a vowel and IS irregular, it should be "a" | ||
* If it starts with a consonant and isn't irregular, it should be "a" | ||
* If it starts with a consonant and IS irregular, it should be "an" | ||
*/ | ||
var article = !isAcronymWithU && xor(startsWithVowel, isIrregular) ? 'an' : 'a'; | ||
return opts.capitalize ? cap(article) + ' ' + word : article + ' ' + word; | ||
}; | ||
indefinite.irregularWords = irregulars.list; | ||
module.exports = indefinite; | ||
/***/ }), | ||
/* 1 */ | ||
/***/ (function(module, exports) { | ||
@@ -200,3 +143,127 @@ | ||
/***/ }), | ||
/* 1 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
var irregulars = __webpack_require__(0); | ||
var _require = __webpack_require__(2), | ||
startsWithVowel = _require.startsWithVowel, | ||
xor = _require.xor, | ||
bothOrNeither = _require.bothOrNeither, | ||
isAcronym = _require.isAcronym, | ||
isIrregularAcronym = _require.isIrregularAcronym, | ||
checkForIrregulars = _require.checkForIrregulars, | ||
capitalize = _require.capitalize, | ||
getFirst = _require.getFirst; | ||
var indefinite = function indefinite(word) { | ||
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var hasInitialVowel = startsWithVowel(word); | ||
// If it's an acronym, make different checks. | ||
if (isAcronym(word, opts.caseInsensitive)) { | ||
var isIrregular = isIrregularAcronym(word); | ||
/* | ||
* If it starts with U: "a" | ||
* If it starts with any other vowel: "an" | ||
* If it starts with F, H, L, M, N, R, S, or X: "an" | ||
* If it starts with any other consonant: "a" | ||
*/ | ||
var article = bothOrNeither(hasInitialVowel, isIrregular) ? 'a' : 'an'; | ||
return capitalize(article, word, opts); | ||
} else { | ||
// Only check the first word. Also, if it's hyphenated, only | ||
// check the first part. Finally, if it's possessive, ignore | ||
// the possessive part. | ||
var first = getFirst(word); | ||
var _isIrregular = checkForIrregulars(first); | ||
/** | ||
* If it starts with a vowel and isn't irregular: "an" | ||
* If it starts with a vowel and IS irregular: "a" | ||
* If it starts with a consonant and isn't irregular: "a" | ||
* If it starts with a consonant and IS irregular: "an" | ||
*/ | ||
var _article = xor(hasInitialVowel, _isIrregular) ? 'an' : 'a'; | ||
return capitalize(_article, word, opts); | ||
} | ||
}; | ||
indefinite.irregularWords = irregulars.list; | ||
module.exports = indefinite; | ||
/***/ }), | ||
/* 2 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
var irregulars = __webpack_require__(0); | ||
var ACRONYM = /^[A-Z]+$/; | ||
var IRREGULAR_ACRONYM = /^[UFHLMNRSX]/; | ||
var STARTS_WITH_VOWEL = /^[aeiouAEIOU]/; | ||
var EXTRAS = /[\s'-]/; | ||
/** | ||
* Array#indexOf is faster IF the word starts with "a" (for example), | ||
* but negligibly faster when you have to .toLowerCase() the word, and | ||
* slower if the word happens to start with (e.g.) "u." | ||
*/ | ||
exports.startsWithVowel = function (word) { | ||
return STARTS_WITH_VOWEL.test(word); | ||
}; | ||
exports.xor = function (a, b) { | ||
return (a || b) && !(a && b); | ||
}; | ||
/** | ||
* Both = a && b | ||
* Neither = !a && !b | ||
* In the case of Booleans, this means | ||
* either both true or both false, so | ||
* we can just compare the equality of | ||
* a and b. | ||
*/ | ||
exports.bothOrNeither = function (a, b) { | ||
return a === b; | ||
}; | ||
/** | ||
* If the entirety of the first word is capital letters | ||
* and case insensitivity is off, it's an acronym. | ||
*/ | ||
exports.isAcronym = function (word, caseInsensitive) { | ||
return caseInsensitive ? false : ACRONYM.test(word.split(' ')[0]); | ||
}; | ||
exports.isIrregularAcronym = function (word) { | ||
return IRREGULAR_ACRONYM.test(word.charAt(0)); | ||
}; | ||
/** | ||
* Try some variations on the word to determine whether it's irregular. | ||
* Specifically, try trimming s, then es, then ed because those are common | ||
* forms of plurals and past tense verbs (which can be used like adjectives). | ||
*/ | ||
exports.checkForIrregulars = function (part) { | ||
return [null, 's', 'es', 'ed'].reduce(function (memo, ending) { | ||
return memo || irregulars.check(part, ending); | ||
}, false); | ||
}; | ||
exports.capitalize = function (article, word, opts) { | ||
if (opts.capitalize) { | ||
article = '' + article.charAt(0).toUpperCase() + article.slice(1); | ||
} | ||
return article + ' ' + word; | ||
}; | ||
exports.getFirst = function (word) { | ||
return word.split(EXTRAS)[0].toLowerCase(); | ||
}; | ||
/***/ }) | ||
/******/ ]); |
@@ -1,1 +0,1 @@ | ||
window.indefinite=function(u){function e(n){if(r[n])return r[n].exports;var i=r[n]={i:n,l:!1,exports:{}};return u[n].call(i.exports,i,i.exports,e),i.l=!0,i.exports}var r={};return e.m=u,e.c=r,e.d=function(u,r,n){e.o(u,r)||Object.defineProperty(u,r,{configurable:!1,enumerable:!0,get:n})},e.n=function(u){var r=u&&u.__esModule?function(){return u.default}:function(){return u};return e.d(r,"a",r),r},e.o=function(u,e){return Object.prototype.hasOwnProperty.call(u,e)},e.p="",e(e.s=0)}([function(u,e,r){var n=r(1),i=/^U[A-Z]+$/,t=/^[aeiou]/,o=/[\s'-]/,s=function(u,e){return(u||e)&&!(u&&e)},l=function(u,e){return!e&&i.test(u.split(" ")[0])},a=function(u){return""+u.charAt(0).toUpperCase()+u.slice(1)},c=function(u){return[null,"s","es","ed"].reduce(function(e,r){return e||n.check(u,r)},!1)},y=function(u){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=l(u,e.caseInsensitive),n=t.test(u.toLowerCase()),i=u.split(o)[0].toLowerCase(),y=c(i),p=!r&&s(n,y)?"an":"a";return e.capitalize?a(p)+" "+u:p+" "+u};y.irregularWords=n.list,u.exports=y},function(u,e){e.check=function(u,r){if(r){var n=new RegExp(r+"$");u=u.replace(n,"")}return e.list.indexOf(u)>-1},e.list=["eunuch","eucalyptus","eugenics","eulogy","euphemism","euphony","euphoria","eureka","european","euphemistic","euphonic","euphoric","euphemistically","euphonically","euphorically","heir","heiress","herb","homage","honesty","honor","honour","hour","honest","honorous","honestly","hourly","one","ouija","once","ubiquity","udometer","ufo","uke","ukelele","ululate","unicorn","unicycle","uniform","unify","union","unison","unit","unity","universe","university","upas","ural","uranium","urea","ureter","urethra","urine","urology","urus","usage","use","usual","usurp","usury","utensil","uterus","utility","utopia","utricle","uvarovite","uvea","uvula","ubiquitous","ugandan","ukrainian","unanimous","unicameral","unified","unique","unisex","universal","urinal","urological","useful","useless","usurious","usurped","utilitarian","utopic","ubiquitously","unanimously","unicamerally","uniquely","universally","urologically","usefully","uselessly","usuriously","yttria","yggdrasil","ylem","yperite","ytterbia","ytterbium","yttrium","ytterbous","ytterbic","yttric"]}]); | ||
window.indefinite=function(u){function e(t){if(r[t])return r[t].exports;var n=r[t]={i:t,l:!1,exports:{}};return u[t].call(n.exports,n,n.exports,e),n.l=!0,n.exports}var r={};return e.m=u,e.c=r,e.d=function(u,r,t){e.o(u,r)||Object.defineProperty(u,r,{configurable:!1,enumerable:!0,get:t})},e.n=function(u){var r=u&&u.__esModule?function(){return u.default}:function(){return u};return e.d(r,"a",r),r},e.o=function(u,e){return Object.prototype.hasOwnProperty.call(u,e)},e.p="",e(e.s=1)}([function(u,e){e.check=function(u,r){if(r){var t=new RegExp(r+"$");u=u.replace(t,"")}return e.list.indexOf(u)>-1},e.list=["eunuch","eucalyptus","eugenics","eulogy","euphemism","euphony","euphoria","eureka","european","euphemistic","euphonic","euphoric","euphemistically","euphonically","euphorically","heir","heiress","herb","homage","honesty","honor","honour","hour","honest","honorous","honestly","hourly","one","ouija","once","ubiquity","udometer","ufo","uke","ukelele","ululate","unicorn","unicycle","uniform","unify","union","unison","unit","unity","universe","university","upas","ural","uranium","urea","ureter","urethra","urine","urology","urus","usage","use","usual","usurp","usury","utensil","uterus","utility","utopia","utricle","uvarovite","uvea","uvula","ubiquitous","ugandan","ukrainian","unanimous","unicameral","unified","unique","unisex","universal","urinal","urological","useful","useless","usurious","usurped","utilitarian","utopic","ubiquitously","unanimously","unicamerally","uniquely","universally","urologically","usefully","uselessly","usuriously","yttria","yggdrasil","ylem","yperite","ytterbia","ytterbium","yttrium","ytterbous","ytterbic","yttric"]},function(u,e,r){var t=r(0),n=r(2),i=n.startsWithVowel,o=n.xor,s=n.bothOrNeither,a=n.isAcronym,l=n.isIrregularAcronym,c=n.checkForIrregulars,y=n.capitalize,h=n.getFirst,p=function(u){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=i(u);if(a(u,e.caseInsensitive)){var t=l(u),n=s(r,t)?"a":"an";return y(n,u,e)}var p=h(u),f=c(p),g=o(r,f)?"an":"a";return y(g,u,e)};p.irregularWords=t.list,u.exports=p},function(u,e,r){var t=r(0),n=/^[A-Z]+$/,i=/^[UFHLMNRSX]/,o=/^[aeiouAEIOU]/,s=/[\s'-]/;e.startsWithVowel=function(u){return o.test(u)},e.xor=function(u,e){return(u||e)&&!(u&&e)},e.bothOrNeither=function(u,e){return u===e},e.isAcronym=function(u,e){return!e&&n.test(u.split(" ")[0])},e.isIrregularAcronym=function(u){return i.test(u.charAt(0))},e.checkForIrregulars=function(u){return[null,"s","es","ed"].reduce(function(e,r){return e||t.check(u,r)},!1)},e.capitalize=function(u,e,r){return r.capitalize&&(u=""+u.charAt(0).toUpperCase()+u.slice(1)),u+" "+e},e.getFirst=function(u){return u.split(s)[0].toLowerCase()}}]); |
const gulp = require('gulp'); | ||
require('file-manifest').generate('./gulp', { match: '*.js' }); | ||
gulp.task('build', gulp.series('clean:dist', 'spawn:webpack')); | ||
gulp.task('cover', gulp.series('clean:coverage', 'spawn:nyc')); | ||
gulp.task('ci', gulp.series(gulp.parallel('lint', 'cover', 'phantom'), 'codeclimate')); | ||
gulp.task('test', gulp.series('cover', 'browser')); | ||
gulp.task('test', gulp.series('cover', 'browser', 'build')); | ||
gulp.task('unit', gulp.series('spawn:nyc')); | ||
gulp.task('default', gulp.series('lint', 'test')); | ||
gulp.task('build', gulp.series('clean:dist', 'spawn:webpack')); |
@@ -10,3 +10,3 @@ // Karma configuration | ||
reporters: ['dots'], | ||
browsers: ['Chrome', 'PhantomJS'], | ||
browsers: ['Chrome', 'PhantomJS', 'Firefox', 'Safari'], | ||
preprocessors: { | ||
@@ -13,0 +13,0 @@ 'test/**/*.js': ['webpack'] |
const irregulars = require('./irregular-words'); | ||
const ACRONYM = /^U[A-Z]+$/; | ||
const STARTS_WITH_VOWEL = /^[aeiou]/; | ||
const EXTRAS = /[\s'-]/; | ||
const { | ||
startsWithVowel, | ||
xor, | ||
bothOrNeither, | ||
isAcronym, | ||
isIrregularAcronym, | ||
checkForIrregulars, | ||
capitalize, | ||
getFirst | ||
} = require('./helpers'); | ||
const xor = (a, b) => (a || b) && !(a && b); | ||
const checkForAcronyms = (word, caseInsensitive) => caseInsensitive ? false : ACRONYM.test(word.split(' ')[0]); | ||
const cap = str => `${str.charAt(0).toUpperCase()}${str.slice(1)}`; | ||
// Try some variations on the word to determine whether it's irregular. | ||
// Specifically, try trimming s, then es, then ed because those are common | ||
// forms of plurals and past tense verbs (which can be used like adjectives). | ||
const checkForIrregulars = part => [ null, 's', 'es', 'ed' ].reduce((memo, ending) => memo || irregulars.check(part, ending), false); | ||
const indefinite = (word, opts = {}) => { | ||
let isAcronymWithU = checkForAcronyms(word, opts.caseInsensitive); | ||
let startsWithVowel = STARTS_WITH_VOWEL.test(word.toLowerCase()); | ||
let hasInitialVowel = startsWithVowel(word); | ||
// Only check the first word. Also, if it's hyphenated, only | ||
// check the first part. Finally, if it's possessive, ignore | ||
// the possessive part. | ||
let part = word.split(EXTRAS)[0].toLowerCase(); | ||
// If it's an acronym, make different checks. | ||
if (isAcronym(word, opts.caseInsensitive)) { | ||
let isIrregular = isIrregularAcronym(word); | ||
/* | ||
* If it starts with U: "a" | ||
* If it starts with any other vowel: "an" | ||
* If it starts with F, H, L, M, N, R, S, or X: "an" | ||
* If it starts with any other consonant: "a" | ||
*/ | ||
let article = bothOrNeither(hasInitialVowel, isIrregular) ? 'a' : 'an'; | ||
return capitalize(article, word, opts); | ||
} else { | ||
// Only check the first word. Also, if it's hyphenated, only | ||
// check the first part. Finally, if it's possessive, ignore | ||
// the possessive part. | ||
let first = getFirst(word); | ||
let isIrregular = checkForIrregulars(first); | ||
let isIrregular = checkForIrregulars(part); | ||
/** | ||
* If it's an acronym that starts with "u," the article should be "a" | ||
* If it starts with a vowel and isn't irregular, it should be "an" | ||
* If it starts with a vowel and IS irregular, it should be "a" | ||
* If it starts with a consonant and isn't irregular, it should be "a" | ||
* If it starts with a consonant and IS irregular, it should be "an" | ||
*/ | ||
let article = !isAcronymWithU && xor(startsWithVowel, isIrregular) ? 'an' : 'a'; | ||
return opts.capitalize ? `${cap(article)} ${word}` : `${article} ${word}`; | ||
/** | ||
* If it starts with a vowel and isn't irregular: "an" | ||
* If it starts with a vowel and IS irregular: "a" | ||
* If it starts with a consonant and isn't irregular: "a" | ||
* If it starts with a consonant and IS irregular: "an" | ||
*/ | ||
let article = xor(hasInitialVowel, isIrregular) ? 'an' : 'a'; | ||
return capitalize(article, word, opts); | ||
} | ||
}; | ||
@@ -37,0 +44,0 @@ |
{ | ||
"name": "indefinite", | ||
"description": "Prefix a noun with an indefinite article - a or an - based on whether it begins with a vowel", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"main": "lib/indefinite.js", | ||
@@ -45,4 +45,6 @@ "scripts": { | ||
"karma-chrome-launcher": "^2.2.0", | ||
"karma-firefox-launcher": "^1.1.0", | ||
"karma-mocha": "^1.3.0", | ||
"karma-phantomjs-launcher": "^1.0.4", | ||
"karma-safari-launcher": "^1.0.0", | ||
"karma-webpack": "^2.0.9", | ||
@@ -49,0 +51,0 @@ "mocha": "^4.0.1", |
@@ -30,2 +30,8 @@ require('should'); | ||
context('a word that starts with a capital that would be a irregular if it were an acronym', () => { | ||
it('should be prefixed with the regular article', () => { | ||
indefinite('Umbrella').should.equal('an Umbrella'); | ||
}) | ||
}) | ||
context('a word that starts with a vowel when capitalize is passed in', () => { | ||
@@ -43,3 +49,3 @@ it('should capitalize the article', () => { | ||
context('an acronym that starts with A, E, I, or O', () => { | ||
context('an acronym that starts with a regular vowel', () => { | ||
it('should be prefixed with an', () => { | ||
@@ -50,3 +56,3 @@ indefinite('IOU').should.equal('an IOU'); | ||
context('an acronym that starts with U', () => { | ||
context('an acronym that starts with an irregular vowel', () => { | ||
it('should be prefixed with a', () => { | ||
@@ -63,2 +69,8 @@ indefinite('UFO').should.equal('a UFO'); | ||
context('an acronym that starts with an irregular consonant', () => { | ||
it('should be prefixed with an', () => { | ||
indefinite('FFA prodigy').should.equal('an FFA prodigy'); | ||
}) | ||
}) | ||
context('an acronym that starts with U but with caseInsensitive passed in', () => { | ||
@@ -65,0 +77,0 @@ it('should be prefixed with an', () => { |
38817
24
735
22