Skip to content

Commit d97f338

Browse files
committed
chore: replace util functions values and contains and usages of indexOf with using native JS functions values and contains (see #3194)
1 parent e37bacd commit d97f338

File tree

20 files changed

+39
-61
lines changed

20 files changed

+39
-61
lines changed

bin/cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ function completer (text) {
113113
const ignore = ['expr', 'type']
114114
for (const func in math.expression.mathWithTransform) {
115115
if (hasOwnProperty(math.expression.mathWithTransform, func)) {
116-
if (func.indexOf(keyword) === 0 && ignore.indexOf(func) === -1) {
116+
if (func.indexOf(keyword) === 0 && !ignore.includes(func)) {
117117
matches.push(func)
118118
}
119119
}

src/core/function/config.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,24 +87,14 @@ export function configFactory (config, emit) {
8787
return _config
8888
}
8989

90-
/**
91-
* Test whether an Array contains a specific item.
92-
* @param {Array.<string>} array
93-
* @param {string} item
94-
* @return {boolean}
95-
*/
96-
function contains (array, item) {
97-
return array.indexOf(item) !== -1
98-
}
99-
10090
/**
10191
* Validate an option
10292
* @param {Object} options Object with options
10393
* @param {string} name Name of the option to validate
10494
* @param {Array.<string>} values Array with valid values for this option
10595
*/
10696
function validateOption (options, name, values) {
107-
if (options[name] !== undefined && !contains(values, options[name])) {
97+
if (options[name] !== undefined && !values.includes(options[name])) {
10898
// unknown value
10999
console.warn('Warning: Unknown value "' + options[name] + '" for configuration option "' + name + '". ' +
110100
'Available options: ' + values.map(value => JSON.stringify(value)).join(', ') + '.')

src/core/function/import.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { isBigNumber, isComplex, isFraction, isMatrix, isUnit } from '../../utils/is.js'
22
import { isFactory, stripOptionalNotation } from '../../utils/factory.js'
33
import { hasOwnProperty, lazy } from '../../utils/object.js'
4-
import { contains } from '../../utils/array.js'
54
import { ArgumentsError } from '../../error/ArgumentsError.js'
65

76
export function importFactory (typed, load, math, importedFactories) {
@@ -235,7 +234,7 @@ export function importFactory (typed, load, math, importedFactories) {
235234
* @private
236235
*/
237236
function _importFactory (factory, options, name = factory.fn) {
238-
if (contains(name, '.')) {
237+
if (name.includes('.')) {
239238
throw new Error('Factory name should not contain a nested path. ' +
240239
'Name: ' + JSON.stringify(name))
241240
}
@@ -253,7 +252,7 @@ export function importFactory (typed, load, math, importedFactories) {
253252
factory.dependencies
254253
.map(stripOptionalNotation)
255254
.forEach(dependency => {
256-
if (contains(dependency, '.')) {
255+
if (dependency.includes('.')) {
257256
throw new Error('Factory dependency should not contain a nested path. ' +
258257
'Name: ' + JSON.stringify(dependency))
259258
}
@@ -353,7 +352,7 @@ export function importFactory (typed, load, math, importedFactories) {
353352
}
354353

355354
function factoryAllowedInExpressions (factory) {
356-
return factory.fn.indexOf('.') === -1 && // FIXME: make checking on path redundant, check on meta data instead
355+
return !factory.fn.includes('.') && // FIXME: make checking on path redundant, check on meta data instead
357356
!hasOwnProperty(unsafe, factory.fn) &&
358357
(!factory.meta || !factory.meta.isClass)
359358
}

src/expression/parse.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,7 +1341,7 @@ export const createParse = /* #__PURE__ */ factory(name, dependencies, ({
13411341

13421342
if (hasOwnProperty(CONSTANTS, name)) { // true, false, null, ...
13431343
node = new ConstantNode(CONSTANTS[name])
1344-
} else if (NUMERIC_CONSTANTS.indexOf(name) !== -1) { // NaN, Infinity
1344+
} else if (NUMERIC_CONSTANTS.includes(name)) { // NaN, Infinity
13451345
node = new ConstantNode(numeric(name, 'number'))
13461346
} else {
13471347
node = new SymbolNode(name)
@@ -1373,7 +1373,7 @@ export const createParse = /* #__PURE__ */ factory(name, dependencies, ({
13731373
let params
13741374

13751375
while ((state.token === '(' || state.token === '[' || state.token === '.') &&
1376-
(!types || types.indexOf(state.token) !== -1)) { // eslint-disable-line no-unmodified-loop-condition
1376+
(!types || types.includes(state.token))) { // eslint-disable-line no-unmodified-loop-condition
13771377
params = []
13781378

13791379
if (state.token === '(') {

src/function/algebra/derivative.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ export const createDerivative = /* #__PURE__ */ factory(name, dependencies, ({
172172
},
173173

174174
'Object, FunctionAssignmentNode, string': function (constNodes, node, varName) {
175-
if (node.params.indexOf(varName) === -1) {
175+
if (!node.params.includes(varName)) {
176176
constNodes[node] = true
177177
return true
178178
}

src/function/algebra/rationalize.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ export const createRationalize = /* #__PURE__ */ factory(name, dependencies, ({
252252
recPoly(node.args[0])
253253
}
254254
} else {
255-
if (oper.indexOf(node.op) === -1) {
255+
if (!oper.includes(node.op)) {
256256
throw new Error('Operator ' + node.op + ' invalid in polynomial expression')
257257
}
258258
for (let i = 0; i < node.args.length; i++) {
@@ -536,7 +536,7 @@ export const createRationalize = /* #__PURE__ */ factory(name, dependencies, ({
536536
throw new Error('There is an unsolved function call')
537537
} else if (tp === 'OperatorNode') {
538538
// ***** OperatorName *****
539-
if ('+-*^'.indexOf(node.op) === -1) throw new Error('Operator ' + node.op + ' invalid')
539+
if (!'+-*^'.includes(node.op)) throw new Error('Operator ' + node.op + ' invalid')
540540

541541
if (noPai !== null) {
542542
// -(unary),^ : children of *,+,-

src/function/algebra/simplifyConstant.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ export const createSimplifyConstant = /* #__PURE__ */ factory(name, dependencies
355355
{
356356
// Process operators as OperatorNode
357357
const operatorFunctions = ['add', 'multiply']
358-
if (operatorFunctions.indexOf(node.name) === -1) {
358+
if (!operatorFunctions.includes(node.name)) {
359359
const args = node.args.map(arg => foldFraction(arg, options))
360360

361361
// If all args are numbers

src/function/statistics/mad.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export const createMad = /* #__PURE__ */ factory(name, dependencies, ({ typed, a
5353
return abs(subtract(value, med))
5454
}))
5555
} catch (err) {
56-
if (err instanceof TypeError && err.message.indexOf('median') !== -1) {
56+
if (err instanceof TypeError && err.message.includes('median')) {
5757
throw new TypeError(err.message.replace('median', 'mad'))
5858
} else {
5959
throw improveErrorMessage(err, 'mad')

src/function/statistics/std.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export const createStd = /* #__PURE__ */ factory(name, dependencies, ({ typed, m
8888
return sqrt(v)
8989
}
9090
} catch (err) {
91-
if (err instanceof TypeError && err.message.indexOf(' variance') !== -1) {
91+
if (err instanceof TypeError && err.message.includes(' variance')) {
9292
throw new TypeError(err.message.replace(' variance', ' std'))
9393
} else {
9494
throw err

src/function/statistics/utils/improveErrorMessage.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ export function improveErrorMessage (err, fnName, value) {
1414
// TODO: add information with the index (also needs transform in expression parser)
1515
let details
1616

17-
if (String(err).indexOf('Unexpected type') !== -1) {
17+
if (String(err).includes('Unexpected type')) {
1818
details = arguments.length > 2
1919
? ' (type: ' + typeOf(value) + ', value: ' + JSON.stringify(value) + ')'
2020
: ' (type: ' + err.data.actual + ')'
2121

2222
return new TypeError('Cannot calculate ' + fnName + ', unexpected type of argument' + details)
2323
}
2424

25-
if (String(err).indexOf('complex numbers') !== -1) {
25+
if (String(err).includes('complex numbers')) {
2626
details = arguments.length > 2
2727
? ' (type: ' + typeOf(value) + ', value: ' + JSON.stringify(value) + ')'
2828
: ''

0 commit comments

Comments
 (0)