Skip to content

Instantly share code, notes, and snippets.

@saeedseyfi
Last active May 24, 2022 22:48
Show Gist options
  • Save saeedseyfi/a4c8474e684507df9e9a9b7be6cb61f0 to your computer and use it in GitHub Desktop.
Save saeedseyfi/a4c8474e684507df9e9a9b7be6cb61f0 to your computer and use it in GitHub Desktop.
Closest Available Value - JS Object Proxy Challenge

Find Closest Available Parent - JavaScript Object Proxy Challenge

Problem

We have an object, containing nested locations and their coordinates:

const data = {
  name: 'World',
  coords: '0,0',
  sweden: {
    name: 'Sweden',
    coords: '61.7426757,8.4501191',
    stockholm: {
      name: 'Stockholm',
      coords: '59.3261232,17.9120139',
      sodermalm: {
        name: 'Södermalm',
        coords: '59.3142931,18.0266148',
      },
    },
  },
};

given the data object, we read different locations coordinates by:

console.log({
  world: data,
  sweden: data.sweden,
  stockholm: data.sweden.stockholm
});

but not all of the locations are defined in our data source, so when the data is queried, we need to fallback to the parent location if the location is not found:

console.log({
  iran: data.iran, // should return world
  taby: data.sweden.stockholm.taby, // should return stockholm
  nasbyPark: data.sweden.stockholm.taby.nasbyPark, // should return stockholm since it's the closest available parent
});

Solution

Click to expand!
  1. We need to create a proxy to the source of the data.
const handler = {
  get(target, prop) {
    const value = target[prop] || target;

    return value;
  },
};

const proxy = new Proxy(data, handler);

now proxy.iran is available, which is "deepEqual" to data. First part of the problem is now solved. 2. Now we need to cover the cases where the value is nested. This requires to have new proxy when the returned value is an object as well.

const handler = {
  get(target, prop) {
    const value = target[prop] || target;

    if (value && typeof value === 'object') {
      return new Proxy(value, handler);
    }

    return value;
  },
};

const proxy = new Proxy(data, handler);
// node test.js
const assert = require('assert');
// the source data should not be mutated
const data = {
name: 'World',
coords: '0,0',
sweden: {
name: 'Sweden',
coords: '61.7426757,8.4501191',
stockholm: {
name: 'Stockholm',
coords: '59.3261232,17.9120139',
sodermalm: {
name: 'Södermalm',
coords: '59.3142931,18.0266148',
},
},
},
};
// your code here
// const proxy = ...
test(proxy);
function test(world) => {
const it = (message, testFn) => {
assert.doesNotThrow(testFn, message);
};
it.skip = () => () => {};
it('should return world coordinates, if country not found', () => {
assert.deepEqual(world, world.iran);
});
it('should return country coordinates, if county not found', () => {
assert.deepEqual(world.sweden, world.sweden.gothenburg);
});
it('should return county coordinates, if city not found', () => {
assert.deepEqual(world.sweden.stockholm, world.sweden.stockholm.taby);
});
it('should return closest available parent', () => {
assert.deepEqual(
world.sweden.stockholm,
world.sweden.stockholm.taby.nasbyPark
);
});
console.log('All passed 🍺');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment