Skip to content

Commit

Permalink
Use arrow functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Dec 13, 2022
1 parent b282553 commit 64748d6
Show file tree
Hide file tree
Showing 343 changed files with 1,838 additions and 2,773 deletions.
22 changes: 11 additions & 11 deletions benchmark/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const veryPrecise = async () => {
}

// Very imprecise task
export const imprecise = function () {
export const imprecise = () => {
randomInt(2)
}

Expand All @@ -42,50 +42,50 @@ let fileContent

// CPU-bound task
export const cpu = {
async beforeAll() {
beforeAll: async () => {
// eslint-disable-next-line fp/no-mutation
fileContent = await readFile(CURRENT_URL, 'utf8')
},
beforeEach({ context }) {
beforeEach: ({ context }) => {
// eslint-disable-next-line no-param-reassign, fp/no-mutation
context.hash = createHash('sha256')
},
main({ context: { hash } }) {
main: ({ context: { hash } }) => {
hash.update(fileContent)
hash.digest('hex')
},
}

// IO-bound read task
export const read = async function () {
export const read = async () => {
await readFile(CURRENT_URL)
}

// IO-bound write task
export const write = {
async beforeAll() {
beforeAll: async () => {
// eslint-disable-next-line fp/no-mutation
fileContent = await readFile(CURRENT_URL, 'utf8')
},
async beforeEach({ context }) {
beforeEach: async ({ context }) => {
// eslint-disable-next-line fp/no-mutation, no-param-reassign
context.tmpPath = await tmpName()
},
async main({ context: { tmpPath } }) {
main: async ({ context: { tmpPath } }) => {
await writeFile(tmpPath, fileContent)
},
async afterEach({ context: { tmpPath } }) {
afterEach: async ({ context: { tmpPath } }) => {
await unlink(tmpPath)
},
}

// Process-spawning-bound task
export const spawn = async function () {
export const spawn = async () => {
await pExecFile('node', ['--version'])
}

// Network-bound task
export const network = async function () {
export const network = async () => {
await got('https://example.com')
}

Expand Down
4 changes: 2 additions & 2 deletions src/bin/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { defineCli } from './top.js'
// - The return value for most methods is a promise which resolves to the same
// `result` that would be passed to reporters
// - Any exception should be handled
const runCli = async function () {
const runCli = async () => {
try {
await checkUpdate()

Expand All @@ -43,7 +43,7 @@ const runCli = async function () {
}

// TODO: use static JSON imports once those are possible
const checkUpdate = async function () {
const checkUpdate = async () => {
const cwd = dirname(fileURLToPath(import.meta.url))
const { packageJson } = await readPackageUp({ cwd, normalize: false })
updateNotifier({ pkg: packageJson }).notify()
Expand Down
12 changes: 5 additions & 7 deletions src/bin/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { mapValues } from '../utils/map.js'
// - `boolean`: this enables boolean flags to be followed by a command or
// another flag, while still allowing passing "true|false" to them
// - `string`: this enables values to be "true", "false" or "1"
export const parseCliFlags = function (yargs) {
export const parseCliFlags = (yargs) => {
const {
_: [command = DEFAULT_COMMAND],
...configFlags
Expand All @@ -28,9 +28,8 @@ const DEFAULT_COMMAND = 'run'
// Remove `yargs`-specific properties and shortcuts.
// We do not remove dasherized properties because user-defined identifiers can
// use dashes and we disable yargs' `camel-case-expansion`.
const isInternalProp = function (key, value) {
return value === undefined || INTERNAL_KEYS.has(key) || key.length === 1
}
const isInternalProp = (key, value) =>
value === undefined || INTERNAL_KEYS.has(key) || key.length === 1

const INTERNAL_KEYS = new Set(['help', 'version', '_', '$0'])

Expand All @@ -40,9 +39,8 @@ const INTERNAL_KEYS = new Set(['help', 'version', '_', '$0'])
// with the `config`, `reporter`, `select` and `limit` configuration
// properties.
// - This requires not using `requiresArg: true`
const handleEmptyArr = function (value, key) {
return value === '' && ARRAY_PROPERTIES.has(key) ? [] : value
}
const handleEmptyArr = (value, key) =>
value === '' && ARRAY_PROPERTIES.has(key) ? [] : value

const ARRAY_PROPERTIES = new Set([
'reporter',
Expand Down
33 changes: 14 additions & 19 deletions src/bin/top.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,18 @@ import { hideBin } from 'yargs/helpers'
import { COMMANDS } from './commands/main.js'

// Define all CLI commands and flags
export const defineCli = function () {
return (
yargs(hideBin(argv))
.command(COMMANDS.map(getCommand))
.usage('')
// Disable the default conversion made by yargs of dasherized CLI flags to
// camelCase because user-defined identifiers can use dashes
.parserConfiguration({ 'camel-case-expansion': false })
.strict()
)
}
export const defineCli = () =>
yargs(hideBin(argv))
.command(COMMANDS.map(getCommand))
.usage('')
// Disable the default conversion made by yargs of dasherized CLI flags to
// camelCase because user-defined identifiers can use dashes
.parserConfiguration({ 'camel-case-expansion': false })
.strict()

const getCommand = function ({ command, describe, config, usage, examples }) {
return {
command,
describe,
builder: (commandYargs) =>
commandYargs.options(config).usage(usage).example(examples),
}
}
const getCommand = ({ command, describe, config, usage, examples }) => ({
command,
describe,
builder: (commandYargs) =>
commandYargs.options(config).usage(usage).example(examples),
})
41 changes: 15 additions & 26 deletions src/combination/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,26 @@ import { removePrefix } from './prefix.js'
// - Comparable in the history
// - Reported nicely
// - Selectable with `select`
export const addDefaultIds = function (results, combinations) {
export const addDefaultIds = (results, combinations) => {
const defaultDimensions = getDefaultDimensions(combinations)
return results.map((result) => addResultDefaultIds(result, defaultDimensions))
}

const getDefaultDimensions = function (combinations) {
const getDefaultDimensions = (combinations) => {
const targetDimensions = getCombsDimensions(combinations)
return Object.fromEntries(
targetDimensions.filter(hasDefaultId).map(getDefaultDimension),
)
}

const hasDefaultId = function ({ defaultIdPrefix }) {
return defaultIdPrefix !== undefined
}
const hasDefaultId = ({ defaultIdPrefix }) => defaultIdPrefix !== undefined

const getDefaultDimension = function (dimension) {
const getDefaultDimension = (dimension) => {
const id = getDefaultId(dimension.propName, dimension)
return [dimension.propName, { id }]
}

const addResultDefaultIds = function (result, defaultDimensions) {
const addResultDefaultIds = (result, defaultDimensions) => {
const combinations = result.combinations.map((combination) => ({
...combination,
dimensions: { ...defaultDimensions, ...combination.dimensions },
Expand All @@ -53,29 +51,20 @@ const addResultDefaultIds = function (result, defaultDimensions) {
}

// Find whether an id matches the default id pattern of a dimension.
export const isInvalidDefaultId = function (id, { propName }) {
return DIMENSIONS.some((dimension) =>
isInvalidDimensionId(id, propName, dimension),
)
}
export const isInvalidDefaultId = (id, { propName }) =>
DIMENSIONS.some((dimension) => isInvalidDimensionId(id, propName, dimension))

const isInvalidDimensionId = function (id, propName, dimension) {
return (
dimension.defaultIdPrefix !== undefined &&
id.startsWith(dimension.defaultIdPrefix) &&
!isDimensionDefaultId(id, propName, dimension)
)
}
const isInvalidDimensionId = (id, propName, dimension) =>
dimension.defaultIdPrefix !== undefined &&
id.startsWith(dimension.defaultIdPrefix) &&
!isDimensionDefaultId(id, propName, dimension)

const isDimensionDefaultId = function (id, propName, dimension) {
return (
isDimension(propName, dimension.propName, dimension.prefixName) &&
id === getDefaultId(propName, dimension)
)
}
const isDimensionDefaultId = (id, propName, dimension) =>
isDimension(propName, dimension.propName, dimension.prefixName) &&
id === getDefaultId(propName, dimension)

// Retrieve the default id of a dimension
const getDefaultId = function (propName, { prefixName, defaultIdPrefix }) {
const getDefaultId = (propName, { prefixName, defaultIdPrefix }) => {
const propNameA = removePrefix(propName, prefixName)
const propNameB = propNameA.replace(DOT_REGEXP, '_')
return `${defaultIdPrefix}${propNameB}`
Expand Down
44 changes: 17 additions & 27 deletions src/combination/dimensions.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,37 @@
import { hasPrefix, removePrefix } from './prefix.js'

// Find a dimension's order
export const findDimensionIndex = function (propName) {
return DIMENSIONS.findIndex((dimension) =>
export const findDimensionIndex = (propName) =>
DIMENSIONS.findIndex((dimension) =>
isDimension(propName, dimension.propName, dimension.prefixName),
)
}

// Retrieve several combinations' dimensions.
// Array order follows same order as `getCombDimensions()`
export const getCombsDimensions = function (combinations) {
return combinations.flatMap(getCombDimensions).filter(isUniqueCombDimension)
}
export const getCombsDimensions = (combinations) =>
combinations.flatMap(getCombDimensions).filter(isUniqueCombDimension)

const isUniqueCombDimension = function (combDimensionA, index, combDimensions) {
return combDimensions
const isUniqueCombDimension = (combDimensionA, index, combDimensions) =>
combDimensions
.slice(index + 1)
.every((combDimensionB) => !isSameDimension(combDimensionA, combDimensionB))
}

const isSameDimension = function (dimensionA, dimensionB) {
return dimensionA.propName === dimensionB.propName
}
const isSameDimension = (dimensionA, dimensionB) =>
dimensionA.propName === dimensionB.propName

// Retrieve one combination's dimensions.
export const getCombDimensions = function (combination) {
return DIMENSIONS.flatMap((dimension) => getDimension(combination, dimension))
}
export const getCombDimensions = (combination) =>
DIMENSIONS.flatMap((dimension) => getDimension(combination, dimension))

// We dissociate:
// - `propName`: used internally and when saving
// - `messageName`: used in output and error messages
const getDimension = function (
const getDimension = (
{ dimensions },
{ propName: dimensionPropName, prefixName, defaultIdPrefix, createdByUser },
) {
) =>
// eslint-disable-next-line fp/no-mutating-methods
return Object.keys(dimensions)
Object.keys(dimensions)
.filter((propName) => isDimension(propName, dimensionPropName, prefixName))
.sort()
.map((propName) => ({
Expand All @@ -46,19 +41,14 @@ const getDimension = function (
defaultIdPrefix,
createdByUser,
}))
}

export const isDimension = function (propName, dimensionPropName, prefixName) {
return prefixName === undefined
export const isDimension = (propName, dimensionPropName, prefixName) =>
prefixName === undefined
? propName === dimensionPropName
: hasPrefix(propName, prefixName)
}

const getMessageName = function (propName, prefixName) {
return prefixName === undefined
? propName
: removePrefix(propName, prefixName)
}
const getMessageName = (propName, prefixName) =>
prefixName === undefined ? propName : removePrefix(propName, prefixName)

// All dimensions. They:
// - create dimensions in runs using a cartesian product
Expand Down
14 changes: 6 additions & 8 deletions src/combination/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,22 @@ import { excludeKeys } from 'filter-obj'
// - We do filter `noDimensions` inside the `result` passed to reporters though
// - So they do not have to
// - To ensure they do not report them
export const getNoDimensions = function (combinations) {
export const getNoDimensions = (combinations) => {
const dimensionsArray = combinations.map(getCombinationDimensions)
return getCombsNoDimensions(dimensionsArray)
}

const getCombinationDimensions = function ({ dimensions }) {
return dimensions
}
const getCombinationDimensions = ({ dimensions }) => dimensions

// Same with already computed dimensions
export const getCombsNoDimensions = function (dimensionsArray) {
export const getCombsNoDimensions = (dimensionsArray) => {
const dimensionNames = [...new Set(dimensionsArray.flatMap(Object.keys))]
return dimensionNames.filter((dimensionName) =>
isNoDimensions(dimensionsArray, dimensionName),
)
}

const isNoDimensions = function (dimensionsArray, dimensionName) {
const isNoDimensions = (dimensionsArray, dimensionName) => {
const isSparseDimension = dimensionsArray.some(
(dimensions) => dimensions[dimensionName] === undefined,
)
Expand All @@ -49,15 +47,15 @@ const isNoDimensions = function (dimensionsArray, dimensionName) {
// - Retrieving `noDimensions` can be performed once per run
// - But applying `noDimensions` might be repeated on many results or many
// previews
export const omitNoDimensions = function (result, noDimensions) {
export const omitNoDimensions = (result, noDimensions) => {
const combinations = result.combinations.map((combination) =>
omitCombNoDimensions(combination, noDimensions),
)
return { ...result, combinations }
}

// Same for a single combination
export const omitCombNoDimensions = function (combination, noDimensions) {
export const omitCombNoDimensions = (combination, noDimensions) => {
const dimensions = excludeKeys(combination.dimensions, noDimensions)
return { ...combination, dimensions }
}
Loading

0 comments on commit 64748d6

Please sign in to comment.