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

Pass value's path for checkFn on findAndReplaceIf #14

Open
soknifedev opened this issue May 3, 2022 · 0 comments
Open

Pass value's path for checkFn on findAndReplaceIf #14

soknifedev opened this issue May 3, 2022 · 0 comments

Comments

@soknifedev
Copy link

soknifedev commented May 3, 2022

Currently, checkFn receives two things: target and propKey. Which is useful for low complexity objects.
Implementing a propKey's target path will be useful for more complex objects.

For example

      const result = findAndReplaceIf({
        caption: {
          text: 'Github example',
          thumbnail: 'https://soknife.dev/avatar_thumb.jpg'
        },
        items: [{
          taken_at: 1645437935,
          thumbnails: {
            thumbnail_width: 100,
            thumbnail_height: 126,
            sprites: [ "https://soknife.dev/avatar.jpg" ]    
          },
          images: {
            candidates: [{
              width: 720,
              height: 900,
              url: "https://soknife.dev/avatar_f8huefh8dfusih.jpg",
            }, {
              width: 200,
              height: 300,
              url: "https://soknife.dev/avatar_12313213.jpg",
            }, {
              width: 50,
              height: 50,
              url: "https://soknife.dev/avatar_asdsadsaas.jpg",
            }]
          }
        }]
      }, downloadIfSmallest, { checkArrayValues: true });

What if I need to download the smallest image only?

I won't know where "val" came from when downloadIfSmallest is called because it only knows two things: val, propKey, so, implementing a "propPath" may be useful for such task.

this propPath should look like .images.candidates.1.url which is the 50x50 image.

with that path, I should be able to look into .images.candidates.1 of the main object to know if it is the value I am looking for or not (using width and height properties.)

What do you think?

I did it by myself if anyone is interested:

function _findAndReplaceIf(target, checkFn, propKey, propPath, config = { onlyPlainObjects: true, checkArrayValues: false }) {
    const _target = checkFn(target, propKey, propPath);
    if (config.checkArrayValues && isWhat.isArray(_target) && !isWhat.isAnyObject(_target)) {
        return _target.map((value, index) => {
            const ppath = [propPath, index].join('.');
            return _findAndReplaceIf(value, checkFn, undefined, ppath, config);
        });
    }
    if (!isWhat.isPlainObject(_target)) {
        return _target;
    }
    return Object.entries(_target).reduce((carry, [key, val]) => {
        const ppath = [propPath, key].join('.');
        carry[key] = _findAndReplaceIf(val, checkFn, key, ppath, config);
        return carry;
    }, {});
}
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

1 participant