0.1.2 • Published 1 year ago

traverse-x v0.1.2

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

traverse-x

A library for traversing objects and arrays

Installation

npm install traverse-x

Usage findByKey

import { findByKey } from 'traverse-dk';

interface ExampleObject {
    name: string;
    details: {
        age: number;
        address: {
            city: string;
            country: string;
        };
    };
    hobbies: string[];
}

const example: ExampleObject = {
    name: 'John',
    details: {
        age: 30,
        address: {
            city: 'New York',
            country: 'USA'
        }
    },
    hobbies: ['reading', 'traveling']
};

const keyResults = findByKey(example, 'city');
console.log('Results by key:', keyResults);
// Output: [{ path: ['details', 'address', 'city'], value: 'New York' }]

Usage findByValue

import { findByValue } from 'traverse-dk';

interface ExampleObject {
    name: string;
    details: {
        age: number;
        address: {
            city: string;
            country: string;
        };
    };
    hobbies: string[];
}

const example: ExampleObject = {
    name: 'John',
    details: {
        age: 30,
        address: {
            city: 'New York',
            country: 'USA'
        }
    },
    hobbies: ['reading', 'traveling']
};

const valueResults = findByValue(example, 'USA');
console.log('Results by value:', valueResults);
// Output: [{ path: ['details', 'address', 'country'], key: 'country' }]

Usage traverse

import { traverse } from 'traverse-dk';

interface ExampleObject {
  name: string;
  details: {
    age: number;
    address: {
      city: string;
      country: string;
    };
  };
  hobbies: string[];
}

const example: ExampleObject = {
  name: 'John',
  details: {
    age: 30,
    address: {
      city: 'New York',
      country: 'USA'
    }
  },
  hobbies: ['reading', 'traveling']
};

traverse(example, (key, value, path) => {
  console.log(`Key: ${key}, Path: ${path.join('.')}`);
  console.log('Value -', value)
});

Usage watchObject

import { watchObject } from 'traverse-dk';

interface ExampleObject {
  name: string;
  details: {
    age: number;
    address: {
      city: string;
      country: string;
    };
  };
  hobbies: string[];
}

const initialExample: ExampleObject = {
    name: 'John',
    details: {
        age: 30,
        address: {
            city: 'New York',
            country: 'USA',
        },
    },
    hobbies: ['reading', 'traveling'],
};

const [example, setExample] = useState(initialExample);

const [watchedExample, setWatchedExample] = watchObject(example, (updatedState) => {
    console.log(`Object changed:`, updatedState);

    const keyResults = findByKey(updatedState, 'city');
    console.log('Updated results by key:', keyResults);

    const valueResults = findByValue(updatedState, 'USA');
    console.log('Updated results by value:', valueResults);

    // Trigger a state update to force the component to re-render
    setExample({ ...updatedState });
});

const updateCity = (newCity: string) => {
    setWatchedExample((draft) => {
        draft.details.address.city = newCity;
    });
};
0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago