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

just-omit only handles root level omissions #518

Open
cycle4passion opened this issue Nov 27, 2022 · 4 comments
Open

just-omit only handles root level omissions #518

cycle4passion opened this issue Nov 27, 2022 · 4 comments
Labels

Comments

@cycle4passion
Copy link

cycle4passion commented Nov 27, 2022

easily could change to use code like just-safe-set, then it will handle nested objects/arrays. Working from the just-safe-set code

Changes

  • new function name/export name: omit and dropped the value param
  • obj[lastProp] = value becomes delete obj[lastProp]
  • if used on array, delete obj[lastProp] puts in null, so check for array and handle with obj.splice(parseInt(lastProp), 1)
module.exports = omit;

/*
var obj = {a: 3, b: {c:5, d:7}, e:[1,2,3]};
omit(obj, 'a'); // {b: {c:5, d:7}, e:[1,2,3]};
omit(obj, 'b.c'); // {a:3, b: {d:7}, e:[1,2,3]};
omit(obj, 'e.1'); // {a: 3, b: {c:5, d:7}, e:[1,3]};
*/

function omit(obj, propsArg) {
  var props, lastProp;
  if (Array.isArray(propsArg)) {
    props = propsArg.slice(0);
  }
  if (typeof propsArg == 'string') {
    props = propsArg.split('.');
  }
  if (typeof propsArg == 'symbol') {
    props = [propsArg];
  }
  if (!Array.isArray(props)) {
    throw new Error('props arg must be an array, a string or a symbol');
  }
  lastProp = props.pop();
  if (!lastProp) {
    return false;
  }
  prototypeCheck(lastProp);
  var thisProp;
  while ((thisProp = props.shift())) {
    prototypeCheck(thisProp);
    if (typeof obj[thisProp] == 'undefined') {
      obj[thisProp] = {};
    }
    obj = obj[thisProp];
    if (!obj || typeof obj != 'object') {
      return false;
    }
  }
  if (Array.isArray(obj)) {
    obj.splice(parseInt(lastProp), 1);
  } else { 
    delete obj[lastProp];
  }
  return true;
}

function prototypeCheck(prop) {
  // coercion is intentional to catch prop values like `['__proto__']`
  if (prop == '__proto__' || prop == 'constructor' || prop == 'prototype') {
    throw new Error('setting of prototype values not supported');
  }
}
@angus-c
Copy link
Owner

angus-c commented Dec 29, 2022

Thanks, this is nice. Feel free to put up a PR including new tests

@cycle4passion
Copy link
Author

cycle4passion commented Dec 30, 2022

I will take a stab at it. Will be my first PR. Should it be a new function renamed as "just-safe-omit" to match up with just-safe-[get/set]? As you know both safe functions dig deep in nesting. Seems like the other functions (like just-pick) are all aimed at root level object and array methods? Happy to leave it name just-omit if you think thats better.

@angus-c
Copy link
Owner

angus-c commented Dec 30, 2022

A new function sounds good. How about just-deep-omit?

@cycle4passion
Copy link
Author

Awesome

@angus-c angus-c added hasPR and removed accepted labels Feb 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants