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

better splitString #30

Closed
wants to merge 1 commit into from

Conversation

dkebler
Copy link
Contributor

@dkebler dkebler commented May 2, 2021

Ok so worked up some changes to splitString function, plus wrote some corresponding tests, and updated the readme

  • allow multiple separator delimiters
  • split string elements in an array path if requested

I see that I could have just wrote my own string split but now these features are incorporated. Nothing has changed per former usage (single delimiter works, don't split string elements by default).

in my use case I needed to split any elements that may contain delimiters when passing a path as an array and use more than one delimeter so I wrote this "pre format path" function. Now it is not necessary. :)

 formatPath(path) {
    if (path==null) return path
    if (Array.isArray(path)) {
      path = path.filter(Boolean).join('.')
    } else path = path.replace(/\/:/g, '.')
    return path
  }

Along the way I came up with another way using regex look behind to find \\ and not split those, then remove them. Avoids that loop with slices and such :).

There is only one thing that bothers me and that is the caching. Some time the fn() is not being called cause it thinks the value is in the cache

const val = setValue.cache.get(key) || fn();

in my test for splitting elements

    it('should split any string elements of an array path if requested', () => {
      const o = {};
      const key1 = Symbol('key-1');
      set(o, ['a.b', key1, 'c'], 'd');
      assert.equal(o['a.b'][key1].c, 'd');

      const o2 = {};
      set(o2, ['z.b', key1, 'c'], 'd', { elSplit: true });
      assert.equal(o2.z.b[key1].c, 'd');
    });

note the 'z.b' if you change that to 'a.b' the test will throw error because fn is not called.
not exactly grokking how this cache is working (it is even a different object so different cache?)
It seems like a testing issue as the example works fine

gone for a few days but able to make updates later in the week per your review.

split string elements in an array path if requested
@jonschlinkert
Copy link
Owner

Hi @dkebler sorry for taking so long to reply. Currently, set-value supports a custom function for splitting the string. Can you accomplish what you want by using that with your function?

FWIW, on a related note, I also needed similar functionality recently, but for getting vs setting. I created expand-value for that use case.

@dkebler
Copy link
Contributor Author

dkebler commented Sep 14, 2021

All is good my end.

I had been using my forked version but given latest node has native esm and inspired by you I've written a one package esm replacement for get,set,del-value that also exports related path/keys utility functions. I call it obj-nested-prop.

expand-value may be useful to me, thx. Closing this

@dkebler dkebler closed this Sep 14, 2021
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

Successfully merging this pull request may close these issues.

3 participants