react-mapfilter
Version:
These components are designed for viewing data in Mapeo. They share a common interface:
72 lines (64 loc) • 2 kB
JavaScript
import "core-js/modules/es.array.iterator";
import "core-js/modules/web.dom-collections.iterator";
import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat";
import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys";
import _mapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/map";
import _someInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/some";
import _Array$isArray from "@babel/runtime-corejs3/core-js-stable/array/is-array";
// @flow
import { isObj } from './helpers';
function isArrayOfPrimitives(value
/*: any*/
)
/*: boolean*/
{
return _Array$isArray(value) && value.length && !_someInstanceProperty(value).call(value, isObj);
}
/*:: type Primitive = string | boolean | null | void | number*/
/**
* Similar to Object.entries, but flattens an object and keys are arrays. Arrays
* of primitives are *not* flattened.
*
* @example
* flatObjectEntries({
* foo: { bar: 'qux' },
* hello: 'world',
* arr: ['biz', 'baz'],
* other: [{ foo: 'bar'}]
* })
* // [
* // [['foo', 'bar'], 'qux'],
* // [['hello'], 'world'],
* // [['arr'], ['biz', 'baz']],
* // [['other', 0, 'foo'], 'bar']
* // ]
*/
export function flatObjectEntries(object
/*: {}*/
)
/*: Array<
[Array<string | number>, Primitive | Primitive[]]
>*/
{
var entries = [];
(function step(object
/*: any*/
, prev
/*: Array<string | number>*/
= [], currentDepth
/*: number*/
= 1) {
const keys = _Array$isArray(object) ? _mapInstanceProperty(object).call(object, (_, i) => i) : _Object$keys(object);
for (const key of keys) {
const value = object[key];
const newKey = _concatInstanceProperty(prev).call(prev, [key]);
if (isObj(value) && !isArrayOfPrimitives(value)) {
step(value, newKey, currentDepth + 1);
} else {
entries.push([newKey, value]);
}
}
})(object);
return entries;
}
//# sourceMappingURL=flat_object_entries.js.map