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

No example for _.set #395

Open
a-roberts opened this issue Mar 8, 2024 · 2 comments
Open

No example for _.set #395

a-roberts opened this issue Mar 8, 2024 · 2 comments

Comments

@a-roberts
Copy link

a-roberts commented Mar 8, 2024

Hi, I run a Code Transformation Guild on the side where I work and, frankly, I love the readme for this repository - it's fantastic. I'm intending to use this as repo as an example as we move developers off Lodash in favour of some of the suggestions here.

Only one problem...we can't see a ._set example.

Is this because it's super easy to do, or so general it'd be a pain to do effectively?

I figured I'd be able to see an existing issue for this but...I don't.

Cheers

@binury
Copy link

binury commented Mar 9, 2024

Yeah if you look at what set is doing, you'll see it is a bit heavy handed 😥 With all the "safety checks" and flexibility and edge cases, it's probably ~150 LoC across half a dozen files.

A mvp bare set, as a partner to get (basically), would be something like…
`

/**
 * @param {Object} obj
 * We'll make assumptions about path in order to avoid reimplementing
 * https://github.com/lodash/lodash/blob/main/src/.internal/stringToPath.ts
 * @param {Array|string} path
 * @param {*} value
 * @returns {Object} Returns obj
 */
function set(obj, path, value) {
  path = Array.isArray(path) ? path.join('.') : path;
  const paths = path
    .split('.')
    .map((p) => (isNaN(parseInt(p, 10)) ? p : parseInt(p, 10)));
  const lastIndex = paths.length - 1;

  let nested = obj;
  for (const [i, p] of paths.entries()) {
    if (i === lastIndex) {
      nested[p] = value;
    } else {
      nested =
        nested[p] || (nested[p] = typeof paths[i + 1] === 'number' ? [] : {});
    }
  }
  return obj;
}
const person = {
  birth: {
    dateTime: '',
    location: {
      state: 'CA',
      city: 'Los Angeles',
      hospital: {
        name: '',
      },
    },
  },
};

set(person, ['birth', 'location', 'hospital', 'name'], 'Hollywood Pediatrics');
set(person, 'vehicle.manufacturer', 'Toyota');
set(person, 'parents.0.name', 'John Doe');

console.log(person);
/*

{
  birth: {
    dateTime: '',
    location: { state: 'CA', city: 'Los Angeles', hospital: [Object] }
  },
  vehicle: { manufacturer: 'Toyota' },
  parents: [ { name: 'John Doe' } ]
}

*/

@a-roberts
Copy link
Author

@binury - thank you very much, that works perfectly!

Are you thinking of creating a PR to the main readme? No worries if not as folks could find this as a useful reference. Appreciate your help

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