type-fest icon indicating copy to clipboard operation
type-fest copied to clipboard

Use `number` for array indexes in `ToPath`

Open NicolasCwy opened this issue 1 year ago • 6 comments

Would it be possible to add an option to make paths in array format instead of dot format Some form item libraries e.g Antd use arrays of strings/ number to denote the path, essentially splitting them by the "." that separates them.

type Obj = {
  a: {
    b: {
      e: string;
    };
    c: {
      d: ['foo'];
    };
  };
};

// Current feature
a.c.d[0]
a.c.d.0

// New Feature - array notation
["a", "c", "d", 0] 

NicolasCwy avatar Nov 08 '24 10:11 NicolasCwy

Seems like what is needed is in the Get source file, with some slight tweaks

// Current behaviour
ToPath<'foo[0].bar.baz'>
//=> ['foo', '0', 'bar', 'baz']

// Expected behaviour
ToPath<'foo[0].bar.baz'>
//=> ['foo', 0, 'bar', 'baz'] // 0 should be a number since it is an array index

NicolasCwy avatar Nov 12 '24 01:11 NicolasCwy

Seems like what is needed is in the Get

Can this whole request be summarized into: Use number for array indexes in ToPath?

fregante avatar Nov 12 '24 17:11 fregante

Yes and also ToPath has to be exported as well. Let me change the title and description

NicolasCwy avatar Nov 12 '24 23:11 NicolasCwy

Question to other maintainers: At what point are the path related helpers so large and complex that they merit extraction into a standalone project that focuses on them and just them?

voxpelli avatar Nov 13 '24 00:11 voxpelli

Size isn't necessarily a problem but continuous requests may be. There are 10 open issues tagged https://github.com/sindresorhus/type-fest/labels/component%3Apaths and every related file has already received a lot of tweaks.

fregante avatar Nov 13 '24 02:11 fregante

Cool I'm interested in working on exposing this and implementing this

NicolasCwy avatar Nov 13 '24 03:11 NicolasCwy