Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dotobject.Object function #28

Closed
v1d3rm3 opened this issue Sep 26, 2017 · 3 comments
Closed

dotobject.Object function #28

v1d3rm3 opened this issue Sep 26, 2017 · 3 comments

Comments

@v1d3rm3
Copy link

v1d3rm3 commented Sep 26, 2017

Object.keys(obj).forEach(function (k) {
         ^
TypeError: Cannot convert undefined or null to object

I think it'll be good if there was a filter to undefined and null objects in this function, i looked at the code, but there wasn't a filter.

@rhalff
Copy link
Owner

rhalff commented Sep 26, 2017

If you would like type checking like that you could consider using typescript.

Somebody made a definition available at: https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/dot-object

Although I do notice the types there should be updated, because the argument type is currently defined there as any which really should be an object.

Perhaps I'll do a pull request there later, however I'm not really using typescript at the moment.

@v1d3rm3
Copy link
Author

v1d3rm3 commented Sep 26, 2017

Hi, @rhalff. But, i think is not a problem with the Typescript. I'm using the type definition. Like i said, it's just a definition of a behavior to dotobject.object(obj), when obj is null or undefined. Actually, the library is giving an error of nullor undefined. I think you just need to define a standard behavior, example:

DotObject.prototype.object = function (obj, mods) {
  var self = this

  // HERE COMES THE FILTER EXAMPLE
  // I think if it's null or undefined, just return undefined
  if (!obj) { // or, more readable, if (obj === null || obj === undefined)
    return undefined;
  }

  Object.keys(obj).forEach(function (k) {
    var mod = mods === undefined ? null : mods[k]
    // normalize array notation.
    var ok = parsePath(k, self.seperator).join(self.seperator)

    if (ok.indexOf(self.seperator) !== -1) {
      self._fill(ok.split(self.seperator), obj, obj[k], mod)
      delete obj[k]
    } else if (self.override) {
      obj[k] = _process(obj[k], mod)
    }
  })

  return obj
}

@rhalff
Copy link
Owner

rhalff commented Sep 27, 2017

I think updating the type definition would be enough else every single function should always test the type of each and every argument during runtime.

If in your code obj could be undefined or null you should test it before making a call to .object

@rhalff rhalff closed this as completed Nov 2, 2019
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants