swagger-js icon indicating copy to clipboard operation
swagger-js copied to clipboard

Try to remove lodash?

Open jimmywarting opened this issue 4 years ago • 12 comments

https://github.com/swagger-api/swagger-js/blob/3937607d80879c4627a780e7b3a94c1c13ebed48/src/http/index.js#L4-L5

// Native pick
const { a, c } = object;
const result = { a, c };

// native is fn
typeof x === 'function'

https://github.com/swagger-api/swagger-js/blob/22da4ad9bbe9ea742ad8a20b15f7c10160bb3043/src/execute/oas3/build-request.js#L3-L4

Object.assign(...)
const securityDef = spec?.components?.securitySchemes || {}

jimmywarting avatar Aug 12 '21 15:08 jimmywarting

Hi,

Using lodash over native JS and vice versa is highly debated topic lately. Some of the articles I read lately on the topic includes:

  • https://dev.to/jrdev_/the-battle-of-the-array-titans-lodash-vs-vanilla-an-experiment-b6g
  • https://areknawo.com/lodash-and-usefulness-of-utility-libraries/
  • https://thejs.dev/jmitchell/its-time-to-let-go-of-lodash-nqc
  • https://codeburst.io/why-you-shouldnt-use-lodash-anymore-and-use-pure-javascript-instead-c397df51a66
  • https://blog.bjoern-ruberg.de/2018/10/06/why-you-should-still-embrace-lodash/

My take on this topic is:

It depends on the context...If there is a native JS alternative in this code-base we should probably use it. Code in this code-base is highly imperative anyway, so converting a declarative approach of using utility function from lodash into imperative native JS counterparts wouldn't do any harm. Feel free to issue PRs.

Note on predicates:

I would still prefer the use of predicate function, such as isFunction, isString, isNumber and so on. They either come from lodash or are defined locally in utility module. The reason I prefer them is that they are

  • self-contained
  • already predefined, tested and cover corner cases
  • low risk

By using typeof x === 'function' there is too much going on here IMHO, and too much things can go wrong here as well. We can make a simple typo in function string very easily and get an unexpected program results.

typeof x === 'function' vs isFunction(x)

char0n avatar Aug 13 '21 07:08 char0n

@jimmywarting would you like to own this issue and implement the proposed changes?

char0n avatar Sep 10 '21 07:09 char0n

sure thing

jimmywarting avatar Sep 13 '21 07:09 jimmywarting

As part of this issue, pick has been replaced with object destructuring. @jimmywarting nice job!

char0n avatar Sep 13 '21 11:09 char0n

as part of trying to remove lodash/find i notice this IE11 comment

  nextPlugin() {
    // Array.prototype.find doesn't work in IE 11 :(
    return find(this.wrappedPlugins, (plugin) => {

do you still support IE at all? or do you use something lite corejs that polyfills missing features or something like that?

Object.assign got merged but it's not supported in ie11 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

jimmywarting avatar Sep 13 '21 13:09 jimmywarting

We define that very precisely here: https://github.com/swagger-api/swagger-js#runtime. IE is not part of that list.

Theoretically your change still might work in IE if IE 11 currently falls within this browserlist:

> 1%
last 2 versions
Firefox ESR
not dead

We do apply polyfills given above browserlist using babel-env and @babel/plugin-transform-runtime

char0n avatar Sep 13 '21 15:09 char0n

left:

  • [x] isObject
  • [x] isPlainObject
  • [ ] get
  • [x] isEmpty
  • [ ] cloneDeep

The rest is kind of annoying...

jimmywarting avatar Sep 15 '21 21:09 jimmywarting

get could be replaced with "optional chaining" unless they are a dynamic array or use weird features that get has

(depends on the context)

jimmywarting avatar Nov 02 '21 09:11 jimmywarting

I found this package https://www.npmjs.com/package/is-empty, do you think we can replace lodash/isEmpty with it?

gabrielsimongianotti avatar Nov 10 '21 13:11 gabrielsimongianotti

@gabrielsimongianotti we have only one case of using the isEmpty function here: https://github.com/swagger-api/swagger-js/blob/e5788a8d1233b6cbbc0821d43392393c5a6ec607/src/specmap/lib/all-of.js#L38

We're specifically asserting if the plain object is not an empty one. IMHO we can replace that isEmpty functiona with Object.keys(originalDefinitionObj).length === 0 in that particular case.

char0n avatar Nov 10 '21 16:11 char0n

Anybody would want to take care of get? Just not to loose the momentum with this issue.

char0n avatar Dec 14 '21 10:12 char0n

There is a regression in introduced in https://github.com/swagger-api/swagger-js/commit/067229e8f7d6b55a68cb62278a662fc2a97c9f0a described in https://github.com/swagger-api/swagger-ui/issues/7771 that manifests in SwaggerUI

char0n avatar Jan 19 '22 11:01 char0n

Is there a branch I can use, where this is implemented. I am running into the issue described here: https://github.com/swagger-api/swagger-js/issues/2439

transfluxus avatar May 15 '23 09:05 transfluxus

@transfluxus nope there is not. We've working on this progressively. If you want to help, you can work directly on master.

char0n avatar May 15 '23 10:05 char0n

Lodash removed in https://github.com/swagger-api/swagger-js/pull/3137

char0n avatar Sep 12 '23 09:09 char0n

:tada: This issue has been resolved in version 3.21.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

swagger-bot avatar Sep 12 '23 10:09 swagger-bot