Use lodash individual libraries instead and make consistent use of them.
Declare one-offs directly where needed.
The tradeoff of bringing more external libraries vs having these utilities in our own "utils" package seems worth it in this case. Since this change makes everything consistent, in HSDS4 we can easily switch back to custom functions if advantageous.
This PR doesn't remove the is.js library or change it in any meaningful way, the changes are HSDS-only
isString replace with lodash.isstring, no changes needed
isNumber replace with lodash.isnumber, no changes needed
isFunction replace with lodash.isfunction, no changes needed
isObject replace with lodash.isplainobject
is.js provided 2 methods: isObject and isPlainObject, isPlainObject was not used at all and the definition of isObject was incorrect, basically was the same as isPlainObject (check for an object that is not a function nor an array). So we use lodash.isplainobject instead, I checked the usage when replacing and indeed every usage was looking for plain objects.
isDefined replace with lodash.isnil
isNil is the opposite from isDefined but makes the same check, when replaced I made sure to flip the check.
Included here, I replaced all checks of null and undefined made with something == null or something != null with isNil for consistency, readability and because not every js dev knows that that checks for both null and undefined.
Uh, I think I was looking at an outdated mdn or something, Array.isArrayissupported on IE so I that's what we'll use
isArray replace with ~lodash.isarray~ Array.isArray
~Replaced the usage of Array.isArray, this made me sad but it is not supported on internet explorer and we still have to support it for Beacon, so might as well be consistent and safe~.
isDOMTypeElement only used in ChoiceGroup so made the declaration there instead
allPropsDefined only used in image.utils, used a simpler check with a for loop instead.
Each change is self contained in a commit for easier revision
https://helpscout.atlassian.net/browse/HSDS4-31
Continuing the work to make a transition to a monorepo easier, we start processing the utilities hsds provides, in this PR we deal with
is.js
Problem
These are very simple utilities that:
isNumber
isFunction
isString
isFunction
a few timesSolution
isString
replace withlodash.isstring
, no changes neededisNumber
replace withlodash.isnumber
, no changes neededisFunction
replace withlodash.isfunction
, no changes neededisObject
replace withlodash.isplainobject
is.js
provided 2 methods:isObject
andisPlainObject
,isPlainObject
was not used at all and the definition ofisObject
was incorrect, basically was the same asisPlainObject
(check for an object that is not a function nor an array). So we uselodash.isplainobject
instead, I checked the usage when replacing and indeed every usage was looking for plain objects.isDefined
replace withlodash.isnil
isNil
is the opposite fromisDefined
but makes the same check, when replaced I made sure to flip the check.null
andundefined
made withsomething == null
orsomething != null
withisNil
for consistency, readability and because not every js dev knows that that checks for bothnull
andundefined
.isArray
replace with ~lodash.isarray
~Array.isArray
Array.isArray
, this made me sad but it is not supported on internet explorer and we still have to support it for Beacon, so might as well be consistent and safe~.isDOMTypeElement
only used inChoiceGroup
so made the declaration there insteadallPropsDefined
only used inimage.utils
, used a simpler check with afor
loop instead.