Skip to content

Commit

Permalink
refactor: replace lodash with own implementation when possible (#593)
Browse files Browse the repository at this point in the history
  • Loading branch information
re-taro authored Apr 1, 2024
1 parent a93c0c4 commit bd82f64
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"chalk": "^3.0.0",
"css.escape": "^1.5.1",
"dom-accessibility-api": "^0.6.3",
"lodash": "^4.17.15",
"lodash": "^4.17.21",
"redent": "^3.0.0"
},
"devDependencies": {
Expand Down
7 changes: 3 additions & 4 deletions src/to-have-form-values.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import isEqualWith from 'lodash/isEqualWith.js'
import uniq from 'lodash/uniq.js'
import isEqualWith from 'lodash/isEqualWith'
import escape from 'css.escape'
import {
checkHtmlElement,
compareArraysAsSet,
getSingleElementValue,
compareArraysAsSet,
} from './utils'

// Returns the combined value of several elements that have the same name
// e.g. radio buttons or groups of checkboxes
function getMultiElementValue(elements) {
const types = uniq(elements.map(element => element.type))
const types = [...new Set(elements.map(element => element.type))]
if (types.length !== 1) {
throw new Error(
'Multiple form elements with the same name must be of the same type',
Expand Down
4 changes: 2 additions & 2 deletions src/to-have-value.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import isEqualWith from 'lodash/isEqualWith.js'
import isEqualWith from 'lodash/isEqualWith'
import {
checkHtmlElement,
compareArraysAsSet,
getMessage,
getSingleElementValue,
compareArraysAsSet,
} from './utils'

export function toHaveValue(htmlElement, expectedValue) {
Expand Down
17 changes: 8 additions & 9 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import redent from 'redent'
import isEqual from 'lodash/isEqual.js'
import {parse} from '@adobe/css-tools'

class GenericTypeError extends Error {
Expand Down Expand Up @@ -212,13 +211,6 @@ function getSingleElementValue(element) {
}
}

function compareArraysAsSet(a, b) {
if (Array.isArray(a) && Array.isArray(b)) {
return isEqual(new Set(a), new Set(b))
}
return undefined
}

function toSentence(
array,
{wordConnector = ', ', lastWordConnector = ' and '} = {},
Expand All @@ -228,6 +220,13 @@ function toSentence(
)
}

function compareArraysAsSet(arr1, arr2) {
if (Array.isArray(arr1) && Array.isArray(arr2)) {
return [...new Set(arr1)].every(v => new Set(arr2).has(v))
}
return undefined
}

export {
HtmlElementTypeError,
NodeTypeError,
Expand All @@ -240,6 +239,6 @@ export {
normalize,
getTag,
getSingleElementValue,
compareArraysAsSet,
toSentence,
compareArraysAsSet,
}

0 comments on commit bd82f64

Please sign in to comment.