Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Nov 2, 2022
1 parent f791286 commit 47dbf38
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 53 deletions.
12 changes: 6 additions & 6 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import {URL} from 'node:url'
import {syllable} from './index.js'

/** @type {Object.<string, unknown>} */
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())
Expand All @@ -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)
Expand All @@ -46,8 +46,8 @@ function getSyllables(value) {
* @param {Array.<string>} values
*/
function syllables(values) {
var sum = 0
var index = -1
let sum = 0
let index = -1

while (++index < values.length) {
sum += syllable(values[index])
Expand Down
48 changes: 19 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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|$)',
Expand Down Expand Up @@ -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]',
Expand All @@ -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',
Expand All @@ -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]',
Expand All @@ -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',
Expand All @@ -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(
[
'^(?:' +
[
Expand Down Expand Up @@ -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(
[
'^' +
'(?:' +
Expand Down Expand Up @@ -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).
/**
Expand All @@ -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.
Expand All @@ -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.<string>} */
var parts
/** @type {ReturnType.<returnFactory>} */
var addOne
/** @type {ReturnType.<returnFactory>} */
var subtractOne
let count = 0

if (value.length === 0) {
return count
Expand All @@ -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
Expand All @@ -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] !== '') {
Expand Down
7 changes: 1 addition & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
2 changes: 1 addition & 1 deletion problematic.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export var problematic = {
export const problematic = {
abalone: 4,
abare: 3,
abbruzzese: 4,
Expand Down
20 changes: 9 additions & 11 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.<string, unknown>} */
var pack = JSON.parse(
const pack = JSON.parse(
String(fs.readFileSync(new URL('../package.json', import.meta.url)))
)

/** @type {Object.<string, number>} */
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)')
Expand Down Expand Up @@ -249,7 +249,7 @@ test('api', function (t) {
})

test('cli', function (t) {
var input = new PassThrough()
const input = new PassThrough()

t.plan(8)

Expand All @@ -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')
})

Expand Down Expand Up @@ -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: <https://github.com/words/syllable/issues/22>,
// Barbed is one syllable as well:
// <https://www.howmanysyllables.com/words/barbed>
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)
}
}
Expand Down

0 comments on commit 47dbf38

Please sign in to comment.