Skip to content
/ find-and Public

Find nested objects and: appendProps / replaceObject / changeProps / removeObject / returnFound / insertObjectBefore / insertObjectAfter. TypeScript friendly. Plug & play.

License

Notifications You must be signed in to change notification settings

arfeo/find-and

Repository files navigation

find-and

Build Status

Find nested objects and:

  • appendProps: append props to the found object.
  • replaceObject: replace all props of the found object.
  • changeProps: replace some existing props of the found object.
  • removeObject: remove the found object.
  • returnFound: get the found object, or an object array if there's more than one object found.
  • insertObjectBefore: insert an object before the found object if the found object's parent is array.
  • insertObjectAfter: insert an object after the found object if the found object's parent is array.

Installation

$ npm i find-and

Examples

Say, we have an object array like:

const data = [
  {
    id: 1,
    name: 'One',
    children: [
      {
        id: 2,
        name: 'Two',
        children: [
          {
            id: 3,
            name: 'Three',
          },
          {
            id: 4,
            name: 'Four',
          },
        ],
      },
    ],
  },
  {
    id: 5,
    name: 'Five',
  },
];
  1. The result of appendProps(data, { id: 5 }, { description: 'Blah' }):

    [
      {
        id: 1,
        name: 'One',
        children: [
          {
            id: 2,
            name: 'Two',
            children: [
              {
                id: 3,
                name: 'Three',
              },
              {
                id: 4,
                name: 'Four',
              },
            ],
          },
        ],
      },
      {
        id: 5,
        name: 'Five',
        description: 'Blah',
      },
    ]
    
  2. The result of replaceObject(data, { id: 3 }, { id: 30 }):

    [
      {
        id: 1,
        name: 'One',
        children: [
          {
            id: 2,
            name: 'Two',
            children: [
              {
                id: 30,
              },
              {
                id: 4,
                name: 'Four',
              },
            ],
          },
        ],
      },
      {
        id: 5,
        name: 'Five',
      },
    ]
    
  3. The result of changeProps(data, { id: 2 }, { name: 'Foo' }):

    [
      {
        id: 1,
        name: 'One',
        children: [
          {
            id: 2,
            name: 'Foo',
            children: [
              {
                id: 3,
                name: 'Three',
              },
              {
                id: 4,
                name: 'Four',
              },
            ],
          },
        ],
      },
      {
        id: 5,
        name: 'Five',
      },
    ]
    
  4. The result of removeObject(data, { id: 3 }):

    [
      {
        id: 1,
        name: 'One',
        children: [
          {
            id: 2,
            name: 'Two',
            children: [
              {
                id: 4,
                name: 'Four',
              },
            ],
          },
        ],
      },
      {
        id: 5,
        name: 'Five',
      },
    ]
    

The behavior is quite the same for an object.

const data = {
  name: 'One',
  description: 'Description',
  children: [
    {
      id: 1,
      name: 'Two',
    },
    {
      id: 2,
      name: 'Three',
    },
  ],
};

In this case, the result of changeProps(data, { name: 'One' }, { name: 'Foo' }):

{
  name: 'Foo',
  description: 'Description',
  children: [
    {
      id: 1,
      name: 'Two',
    },
    {
      id: 2,
      name: 'Three',
    },
  ],
}

About

Find nested objects and: appendProps / replaceObject / changeProps / removeObject / returnFound / insertObjectBefore / insertObjectAfter. TypeScript friendly. Plug & play.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published