diff --git a/cli.js b/cli.js index 5a792ab..e08690e 100755 --- a/cli.js +++ b/cli.js @@ -5,13 +5,13 @@ import {URL} from 'node:url' import {syllable} from './index.js' /** @type {Object.} */ -var pack = JSON.parse( +const pack = JSON.parse( String(fs.readFileSync(new URL('package.json', import.meta.url))) ) -var argv = process.argv.slice(2) +const argv = process.argv.slice(2) -var command = pack.name +const command = pack.name if (argv.includes('--help') || argv.includes('-h')) { console.log(help()) @@ -29,7 +29,7 @@ if (argv.includes('--help') || argv.includes('-h')) { * @param {string} value */ function getSyllables(value) { - var values = value + const values = value .split(/\s+/g) .map((/** @type {string} */ d) => d.trim()) .filter(Boolean) @@ -46,8 +46,8 @@ function getSyllables(value) { * @param {Array.} values */ function syllables(values) { - var sum = 0 - var index = -1 + let sum = 0 + let index = -1 while (++index < values.length) { sum += syllable(values[index]) diff --git a/index.js b/index.js index 581d367..e934a37 100644 --- a/index.js +++ b/index.js @@ -3,11 +3,11 @@ import pluralize from 'pluralize' import normalize from 'normalize-strings' import {problematic} from './problematic.js' -var own = {}.hasOwnProperty +const own = {}.hasOwnProperty // Two expressions of occurrences which normally would be counted as two // syllables, but should be counted as one. -var EXPRESSION_MONOSYLLABIC_ONE = new RegExp( +const EXPRESSION_MONOSYLLABIC_ONE = new RegExp( [ 'awe($|d|so)', 'cia(?:l|$)', @@ -63,7 +63,7 @@ var EXPRESSION_MONOSYLLABIC_ONE = new RegExp( 'g' ) -var EXPRESSION_MONOSYLLABIC_TWO = new RegExp( +const EXPRESSION_MONOSYLLABIC_TWO = new RegExp( '[aeiouy](?:' + [ '[bcdfgklmnprstvyz]', @@ -84,7 +84,7 @@ var EXPRESSION_MONOSYLLABIC_TWO = new RegExp( // Four expression of occurrences which normally would be counted as one // syllable, but should be counted as two. -var EXPRESSION_DOUBLE_SYLLABIC_ONE = new RegExp( +const EXPRESSION_DOUBLE_SYLLABIC_ONE = new RegExp( '(?:' + [ '([^aeiouy])\\1l', @@ -111,7 +111,7 @@ var EXPRESSION_DOUBLE_SYLLABIC_ONE = new RegExp( 'g' ) -var EXPRESSION_DOUBLE_SYLLABIC_TWO = new RegExp( +const EXPRESSION_DOUBLE_SYLLABIC_TWO = new RegExp( [ 'creat(?!u)', '[^gq]ua[^auieo]', @@ -123,7 +123,7 @@ var EXPRESSION_DOUBLE_SYLLABIC_TWO = new RegExp( 'g' ) -var EXPRESSION_DOUBLE_SYLLABIC_THREE = new RegExp( +const EXPRESSION_DOUBLE_SYLLABIC_THREE = new RegExp( [ '[^aeiou]y[ae]', '[^l]lien', @@ -142,10 +142,10 @@ var EXPRESSION_DOUBLE_SYLLABIC_THREE = new RegExp( 'g' ) -var EXPRESSION_DOUBLE_SYLLABIC_FOUR = /[^s]ia/ +const EXPRESSION_DOUBLE_SYLLABIC_FOUR = /[^s]ia/ // Expression to match single syllable pre- and suffixes. -var EXPRESSION_SINGLE = new RegExp( +const EXPRESSION_SINGLE = new RegExp( [ '^(?:' + [ @@ -187,7 +187,7 @@ var EXPRESSION_SINGLE = new RegExp( ) // Expression to match double syllable pre- and suffixes. -var EXPRESSION_DOUBLE = new RegExp( +const EXPRESSION_DOUBLE = new RegExp( [ '^' + '(?:' + @@ -223,7 +223,7 @@ var EXPRESSION_DOUBLE = new RegExp( ) // Expression to match triple syllable suffixes. -var EXPRESSION_TRIPLE = /(creations?|ology|ologist|onomy|onomist)$/g +const EXPRESSION_TRIPLE = /(creations?|ology|ologist|onomy|onomist)$/g // Wrapper to support multiple word-parts (GH-11). /** @@ -233,14 +233,14 @@ var EXPRESSION_TRIPLE = /(creations?|ology|ologist|onomy|onomist)$/g * @returns {number} */ export function syllable(value) { - var values = normalize(String(value)) + const values = normalize(String(value)) .toLowerCase() // Remove apostrophes. .replace(/['’]/g, '') // Split on word boundaries. .split(/\b/g) - var index = -1 - var sum = 0 + let index = -1 + let sum = 0 while (++index < values.length) { // Remove non-alphabetic characters from a given value. @@ -257,17 +257,7 @@ export function syllable(value) { * @returns {number} */ function one(value) { - var count = 0 - /** @type {number} */ - var index - /** @type {string} */ - var singular - /** @type {Array.} */ - var parts - /** @type {ReturnType.} */ - var addOne - /** @type {ReturnType.} */ - var subtractOne + let count = 0 if (value.length === 0) { return count @@ -284,14 +274,14 @@ function one(value) { } // Additionally, the singular word might be in `problematic`. - singular = pluralize(value, 1) + const singular = pluralize(value, 1) if (own.call(problematic, singular)) { return problematic[singular] } - addOne = returnFactory(1) - subtractOne = returnFactory(-1) + const addOne = returnFactory(1) + const subtractOne = returnFactory(-1) // Count some prefixes and suffixes, and remove their matched ranges. value = value @@ -300,8 +290,8 @@ function one(value) { .replace(EXPRESSION_SINGLE, countFactory(1)) // Count multiple consonants. - parts = value.split(/[^aeiouy]+/) - index = -1 + const parts = value.split(/[^aeiouy]+/) + let index = -1 while (++index < parts.length) { if (parts[index] !== '') { diff --git a/package.json b/package.json index 4cb51c6..bf521ac 100644 --- a/package.json +++ b/package.json @@ -67,12 +67,7 @@ "trailingComma": "none" }, "xo": { - "prettier": true, - "rules": { - "import/no-mutable-exports": "off", - "no-var": "off", - "prefer-arrow-callback": "off" - } + "prettier": true }, "remarkConfig": { "plugins": [ diff --git a/problematic.js b/problematic.js index f723b18..d5b0c84 100644 --- a/problematic.js +++ b/problematic.js @@ -1,4 +1,4 @@ -export var problematic = { +export const problematic = { abalone: 4, abare: 3, abbruzzese: 4, diff --git a/test/index.js b/test/index.js index 9fcb11f..55a7b2d 100644 --- a/test/index.js +++ b/test/index.js @@ -5,20 +5,20 @@ import {PassThrough} from 'node:stream' import test from 'tape' import {syllable} from '../index.js' -var own = {}.hasOwnProperty +const own = {}.hasOwnProperty /** @type {Object.} */ -var pack = JSON.parse( +const pack = JSON.parse( String(fs.readFileSync(new URL('../package.json', import.meta.url))) ) /** @type {Object.} */ -var fixtures = JSON.parse( +const fixtures = JSON.parse( String(fs.readFileSync(new URL('fixture.json', import.meta.url))) ) test('api', function (t) { - var result = syllable('syllables') + const result = syllable('syllables') t.equal(syllable('SYLLABLES'), result, 'should be case insensitive (1)') t.equal(syllable('SyLlABlEs'), result, 'should be case insensitive (2)') @@ -249,7 +249,7 @@ test('api', function (t) { }) test('cli', function (t) { - var input = new PassThrough() + const input = new PassThrough() t.plan(8) @@ -269,7 +269,7 @@ test('cli', function (t) { ) }) - var subprocess = exec('./cli.js', function (error, stdout, stderr) { + const subprocess = exec('./cli.js', function (error, stdout, stderr) { t.deepEqual([error, stdout, stderr], [null, '6\n', ''], 'stdin') }) @@ -322,20 +322,18 @@ test('cli', function (t) { // This library focusses on the required Text-Statistics tests (the library // provides both optional and required tests). test('fixtures', function (t) { - var overwrite = { + const overwrite = { // GH-22: , // Barbed is one syllable as well: // arbed: 1 } /** @type {string} */ - var key - /** @type {number} */ - var expected + let key for (key in fixtures) { if (own.call(fixtures, key)) { - expected = (key in overwrite ? overwrite : fixtures)[key] + const expected = (key in overwrite ? overwrite : fixtures)[key] t.equal(syllable(key), expected, key) } }