Skip to content

Commit

Permalink
remove null objects from extents
Browse files Browse the repository at this point in the history
  • Loading branch information
mhkeller committed Jun 19, 2020
1 parent 65156dc commit a43c463
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/LayerCake.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { writable, derived } from 'svelte/store';
import makeAccessor from './utils/makeAccessor.js';
import filterObject from './utils/filterObject.js';
import calcExtents from './lib/calcExtents.js';
import calcDomain from './helpers/calcDomain.js';
import createScale from './helpers/createScale.js';
Expand Down Expand Up @@ -158,7 +159,7 @@
$: _zRange.set(zRange);
$: _rRange.set(rRange);
$: _padding.set(padding);
$: _extents.set(extents);
$: _extents.set(filterObject(extents));
$: _flatData.set(flatData || data);
/* --------------------------------------------
Expand Down
35 changes: 35 additions & 0 deletions src/utils/filterObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* --------------------------------------------
*
* Remove undefined fields from an object
*
* --------------------------------------------
*/
export default function filterObject (obj) {
return fromEntries(Object.entries(obj).filter(([key, value]) => {
return value !== undefined;
}));
}

function fromEntries(iter) {
const obj = {};

for (const pair of iter) {
if (Object(pair) !== pair) {
throw new TypeError('iterable for fromEntries should yield objects');
}

// Consistency with Map: contract is that entry has "0" and "1" keys, not
// that it is an array or iterable.

const { '0': key, '1': val } = pair;

Object.defineProperty(obj, key, {
configurable: true,
enumerable: true,
writable: true,
value: val,
});
}

return obj;
}
23 changes: 23 additions & 0 deletions test/filterObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* globals describe it */
import * as assert from 'assert';
import filterObject from '../src/utils/filterObject.js';

const name = 'filterObject';

const tests = [
{ args: [{}], expected: {} },
{ args: [{x: null, y: undefined}], expected: { x: null } },
{ args: [{x: 'a', y: undefined, z: undefined}], expected: { x: 'a' } },
{ args: [{x: 'a', y: 'b', z: undefined}], expected: { x: 'a', y: 'b' } },
];

describe(name, () => {
tests.forEach(test => {
describe(JSON.stringify(test.args), () => {
it(`should equal ${JSON.stringify(test.expected)}`, () => {
const actual = filterObject(...test.args);
assert.deepStrictEqual(actual, test.expected);
});
});
});
});

0 comments on commit a43c463

Please sign in to comment.